sort.c revision 1.9 1 /* $NetBSD: sort.c,v 1.9 2001/01/08 18:00:31 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Peter McIlroy.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 /* Sort sorts a file using an optional user-defined key.
40 * Sort uses radix sort for internal sorting, and allows
41 * a choice of merge sort and radix sort for external sorting.
42 */
43
44 #include "sort.h"
45 #include "fsort.h"
46 #include "pathnames.h"
47
48 #ifndef lint
49 __COPYRIGHT("@(#) Copyright (c) 1993\n\
50 The Regents of the University of California. All rights reserved.\n");
51 #endif /* not lint */
52
53 #ifndef lint
54 __RCSID("$NetBSD: sort.c,v 1.9 2001/01/08 18:00:31 jdolecek Exp $");
55 __SCCSID("@(#)sort.c 8.1 (Berkeley) 6/6/93");
56 #endif /* not lint */
57
58 #include <paths.h>
59 #include <signal.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 int REC_D = '\n';
65 u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */
66 /*
67 * weight tables. Gweights is one of ascii, Rascii..
68 * modified to weight rec_d = 0 (or 255)
69 */
70 extern u_char gweights[NBINS];
71 u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
72 /*
73 * masks of ignored characters. Alltable is 256 ones
74 */
75 u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
76 int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
77 struct coldesc clist[(ND+1)*2];
78 int ncols = 0;
79 extern struct coldesc clist[(ND+1)*2];
80 extern int ncols;
81
82 /*
83 * Default to stable sort.
84 */
85 int stable_sort = 1;
86
87 char devstdin[] = _PATH_STDIN;
88 char toutpath[_POSIX_PATH_MAX];
89 const char *tmpdir = _PATH_TMP;
90
91 static void cleanup __P((void));
92 static void onsignal __P((int));
93 static void usage __P((const char *));
94
95 int main __P((int argc, char **argv));
96
97 int
98 main(argc, argv)
99 int argc;
100 char *argv[];
101 {
102 extern int optind;
103 extern char *optarg;
104 int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
105 struct field *));
106 int ch, i, stdinflag = 0, tmp = 0;
107 char cflag = 0, mflag = 0;
108 char *outfile, *outpath = 0;
109 struct field fldtab[ND+2], *ftpos;
110 union f_handle filelist;
111 FILE *outfp = NULL;
112 memset(fldtab, 0, (ND+2)*sizeof(struct field));
113 memset(d_mask, 0, NBINS);
114 d_mask[REC_D = '\n'] = REC_D_F;
115 SINGL_FLD = SEP_FLAG = 0;
116 d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
117 ftpos = fldtab;
118 fixit(&argc, argv);
119 while ((ch = getopt(argc, argv, "bcdfik:mHno:rsSt:T:ux")) != EOF) {
120 if ((outfile = getenv("TMPDIR")))
121 tmpdir = outfile;
122 switch (ch) {
123 case 'b': fldtab->flags |= BI | BT;
124 break;
125 case 'd':
126 case 'i':
127 case 'f':
128 case 'n':
129 case 'r': tmp |= optval(ch, 0);
130 if (tmp & R && tmp & F)
131 fldtab->weights = RFtable;
132 else if (tmp & F)
133 fldtab->weights = Ftable;
134 else if(tmp & R)
135 fldtab->weights = Rascii;
136 fldtab->flags |= tmp;
137 break;
138 case 'o':
139 outpath = optarg;
140 break;
141 case 'k':
142 setfield(optarg, ++ftpos, fldtab->flags);
143 break;
144 case 's':
145 /* for GNU sort compatibility (this is our default) */
146 stable_sort = 1;
147 break;
148 case 'S':
149 stable_sort = 0;
150 break;
151 case 't':
152 if (SEP_FLAG)
153 usage("multiple field delimiters");
154 SEP_FLAG = 1;
155 d_mask[' '] &= ~FLD_D;
156 d_mask['\t'] &= ~FLD_D;
157 d_mask[(u_char)*optarg] |= FLD_D;
158 if (d_mask[(u_char)*optarg] & REC_D_F)
159 err(2, "record/field delimiter clash");
160 break;
161 case 'T':
162 if (REC_D != '\n')
163 usage("multiple record delimiters");
164 if ('\n' == (REC_D = *optarg))
165 break;
166 d_mask['\n'] = d_mask[' '];
167 d_mask[REC_D] = REC_D_F;
168 break;
169 case 'u':
170 UNIQUE = 1;
171 break;
172 case 'c':
173 cflag = 1;
174 break;
175 case 'm':
176 mflag = 1;
177 break;
178 case 'H':
179 PANIC = 0;
180 break;
181 case '?':
182 default: usage("");
183 }
184 }
185 if (cflag && argc > optind+1)
186 errx(2, "too many input files for -c option");
187 if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
188 outpath = argv[argc-1];
189 argc -= 2;
190 }
191 if (mflag && argc - optind > (MAXFCT - (16+1))*16)
192 errx(2, "too many input files for -m option");
193 for (i = optind; i < argc; i++) {
194 /* allow one occurrence of /dev/stdin */
195 if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
196 if (stdinflag)
197 warnx("ignoring extra \"%s\" in file list",
198 argv[i]);
199 else {
200 stdinflag = 1;
201 argv[i] = devstdin;
202 }
203 } else if ((ch = access(argv[i], R_OK)))
204 err(2, "%s", argv[i]);
205 }
206 if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
207 SINGL_FLD = 1;
208 fldtab[0].icol.num = 1;
209 } else {
210 if (!fldtab[1].icol.num) {
211 fldtab[0].flags &= ~(BI|BT);
212 setfield("1", ++ftpos, fldtab->flags);
213 }
214 fldreset(fldtab);
215 fldtab[0].flags &= ~F;
216 }
217 settables(fldtab[0].flags);
218 num_init();
219 fldtab->weights = gweights;
220 if (optind == argc) {
221 static char *names[2];
222
223 names[0] = devstdin;
224 names[1] = NULL;
225 filelist.names = names;
226 optind--;
227 } else
228 filelist.names = argv+optind;
229 if (SINGL_FLD)
230 get = makeline;
231 else
232 get = makekey;
233 if (cflag) {
234 order(filelist, get, fldtab);
235 /* NOT REACHED */
236 }
237 if (!outpath) {
238 (void)snprintf(toutpath,
239 sizeof(toutpath), "%sstdout", _PATH_DEV);
240 outfile = outpath = toutpath;
241 } else if (!(ch = access(outpath, 0)) &&
242 strncmp(_PATH_DEV, outpath, 5)) {
243 struct sigaction act = {0};
244 int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
245 SIGVTALRM, SIGPROF, 0};
246 int outfd;
247 errno = 0;
248 if (access(outpath, W_OK))
249 err(2, "%s", outpath);
250 act.sa_handler = onsignal;
251 act.sa_flags = SA_RESTART | SA_RESETHAND;
252 (void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
253 if ((outfd = mkstemp(toutpath)) < 0 ||
254 (outfp = fdopen(outfd, "w")) == 0)
255 err(2, "temporary file %s", toutpath);
256 outfile = toutpath;
257 (void)atexit(cleanup);
258 for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
259 sigaction(sigtable[i], &act, 0);
260 } else
261 outfile = outpath;
262 if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
263 err(2, "output file %s", outfile);
264 if (mflag)
265 fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
266 else
267 fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
268 if (outfile != outpath) {
269 if (access(outfile, 0))
270 err(2, "%s", outfile);
271 (void)unlink(outpath);
272 if (link(outfile, outpath))
273 err(2, "cannot link %s: output left in %s",
274 outpath, outfile);
275 (void)unlink(outfile);
276 }
277 exit(0);
278 }
279
280 static void
281 onsignal(sig)
282 int sig;
283 {
284 cleanup();
285 }
286
287 static void
288 cleanup()
289 {
290 if (toutpath[0])
291 (void)unlink(toutpath);
292 }
293
294 static void
295 usage(msg)
296 const char *msg;
297 {
298 if (msg)
299 (void)fprintf(stderr, "sort: %s\n", msg);
300 (void)fprintf(stderr, "usage: [-o output] [-cmubdfinr] [-t char] ");
301 (void)fprintf(stderr, "[-T char] [-k keydef] ... [files]\n");
302 exit(2);
303 }
304