Annotation of src/usr.bin/sort/sort.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: sort.c,v 1.3 1997/01/26 00:02:25 deraadt Exp $ */
1.1 millert 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: #ifndef lint
40: static char copyright[] =
41: "@(#) Copyright (c) 1993\n\
42: The Regents of the University of California. All rights reserved.\n";
43: #endif /* not lint */
44:
45: #ifndef lint
46: #if 0
47: static char sccsid[] = "@(#)sort.c 8.1 (Berkeley) 6/6/93";
48: #else
1.4 ! millert 49: static char rcsid[] = "$OpenBSD: sort.c,v 1.3 1997/01/26 00:02:25 deraadt Exp $";
1.1 millert 50: #endif
51: #endif /* not lint */
52:
1.4 ! millert 53: /*
! 54: * Sort sorts a file using an optional user-defined key.
1.1 millert 55: * Sort uses radix sort for internal sorting, and allows
56: * a choice of merge sort and radix sort for external sorting.
57: */
58:
59: #include "sort.h"
60: #include "fsort.h"
61: #include "pathnames.h"
62:
63: #include <paths.h>
64: #include <signal.h>
65: #include <stdlib.h>
66: #include <string.h>
67: #include <unistd.h>
68:
69: int REC_D = '\n';
70: u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */
1.4 ! millert 71:
1.1 millert 72: /*
73: * weight tables. Gweights is one of ascii, Rascii..
74: * modified to weight rec_d = 0 (or 255)
75: */
76: extern u_char gweights[NBINS];
77: u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
1.4 ! millert 78:
1.1 millert 79: /*
80: * masks of ignored characters. Alltable is 256 ones
81: */
82: u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
83: int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
84: struct coldesc clist[(ND+1)*2];
85: int ncols = 0;
86: extern struct coldesc clist[(ND+1)*2];
87: extern int ncols;
88:
89: char devstdin[] = _PATH_STDIN;
90: char toutpath[_POSIX_PATH_MAX];
91: char *tmpdir = _PATH_VARTMP;
92:
93: static void cleanup __P((void));
94: static void onsig __P((int));
95: static void usage __P((char *));
96:
97: int
98: main(argc, argv)
99: int argc;
100: char *argv[];
101: {
102: int (*get)();
103: int ch, i, stdinflag = 0, tmp = 0;
104: char cflag = 0, mflag = 0, nflag = 0;
105: char *outfile, *outpath = 0;
106: struct field fldtab[ND+2], *ftpos;
107: union f_handle filelist;
108: FILE *outfp = NULL;
1.4 ! millert 109:
1.1 millert 110: memset(fldtab, 0, (ND+2)*sizeof(struct field));
111: memset(d_mask, 0, NBINS);
112: d_mask[REC_D = '\n'] = REC_D_F;
113: SINGL_FLD = SEP_FLAG = 0;
114: d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
115: ftpos = fldtab;
116: fixit(&argc, argv);
117: if (!issetugid() && (outfile = getenv("TMPDIR")))
118: tmpdir = outfile;
119: while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:")) != -1) {
1.4 ! millert 120: switch (ch) {
1.1 millert 121: case 'b': fldtab->flags |= BI | BT;
122: break;
123: case 'd':
1.4 ! millert 124: case 'f':
1.1 millert 125: case 'i':
126: case 'r': tmp |= optval(ch, 0);
127: if (tmp & R && tmp & F)
128: fldtab->weights = RFtable;
129: else if (tmp & F)
130: fldtab->weights = Ftable;
1.4 ! millert 131: else if (tmp & R)
1.1 millert 132: fldtab->weights = Rascii;
133: fldtab->flags |= tmp;
134: break;
135: case 'o':
136: outpath = optarg;
137: break;
138: case 'n':
1.4 ! millert 139: /* XXX - this does not deal with -n in with -k */
1.1 millert 140: nflag = 1;
141: setfield("1n", ++ftpos, fldtab->flags&(~R));
142: break;
143: case 'k':
144: setfield(optarg, ++ftpos, fldtab->flags);
145: break;
146: case 't':
147: if (SEP_FLAG)
148: usage("multiple field delimiters");
149: SEP_FLAG = 1;
150: d_mask[' '] &= ~FLD_D;
151: d_mask['\t'] &= ~FLD_D;
152: d_mask[(int)*optarg] |= FLD_D;
153: if (d_mask[(int)*optarg] & REC_D_F)
154: err(2, "record/field delimiter clash");
155: break;
156: case 'R':
157: if (REC_D != '\n')
158: usage("multiple record delimiters");
159: if ('\n' == (REC_D = *optarg))
160: break;
161: d_mask['\n'] = d_mask[' '];
162: d_mask[REC_D] = REC_D_F;
163: break;
164: case 'T':
165: tmpdir = optarg;
166: break;
167: case 'u':
168: UNIQUE = 1;
169: break;
170: case 'c':
171: cflag = 1;
172: break;
173: case 'm':
174: mflag = 1;
175: break;
176: case 'H':
177: PANIC = 0;
178: break;
179: case 'y':
180: /* accept -y for backwards compat. */
181: break;
182: case '?':
183: default: usage("");
184: }
185: }
1.4 ! millert 186:
1.1 millert 187: if (cflag && argc > optind+1)
188: errx(2, "too many input files for -c option");
1.4 ! millert 189:
1.1 millert 190: if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
191: outpath = argv[argc-1];
192: argc -= 2;
193: }
1.4 ! millert 194:
1.1 millert 195: if (mflag && argc - optind > (MAXFCT - (16+1))*16)
196: errx(2, "too many input files for -m option");
1.4 ! millert 197:
1.1 millert 198: for (i = optind; i < argc; i++) {
199: /* allow one occurrence of /dev/stdin */
200: if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
201: if (stdinflag)
202: warnx("ignoring extra \"%s\" in file list",
203: argv[i]);
204: else {
205: stdinflag = 1;
206: argv[i] = devstdin;
207: }
208: } else if ((ch = access(argv[i], R_OK)))
1.2 millert 209: err(2, argv[i]);
1.1 millert 210: }
1.4 ! millert 211:
1.1 millert 212: if (!(fldtab->flags & (I|D) || fldtab[1].icol.num)) {
213: SINGL_FLD = 1;
214: fldtab[0].icol.num = 1;
215: } else {
216: if (!fldtab[1].icol.num) {
217: fldtab[0].flags &= ~(BI|BT);
218: setfield("1", ++ftpos, fldtab->flags);
219: }
220: if (nflag)
221: fldtab[1].flags |= fldtab->flags;
222: fldreset(fldtab);
223: fldtab[0].flags &= ~F;
224: }
225: settables(fldtab[0].flags);
226: num_init();
227: fldtab->weights = gweights;
1.4 ! millert 228:
1.3 deraadt 229: if (optind == argc) {
230: static char *names[2];
231:
232: names[0] = devstdin;
233: names[1] = NULL;
234: filelist.names = names;
235: optind--;
236: } else
237: filelist.names = argv+optind;
1.4 ! millert 238:
1.1 millert 239: if (SINGL_FLD)
240: get = makeline;
241: else
242: get = makekey;
1.4 ! millert 243:
1.1 millert 244: if (cflag) {
245: order(filelist, get, fldtab);
246: /* NOT REACHED */
247: }
1.4 ! millert 248:
1.1 millert 249: if (!outpath) {
250: (void)snprintf(toutpath,
251: sizeof(toutpath), "%sstdout", _PATH_DEV);
252: outfile = outpath = toutpath;
253: } else if (!(ch = access(outpath, 0)) &&
254: strncmp(_PATH_DEV, outpath, 5)) {
255: struct sigaction act = {0, SIG_BLOCK, 6};
256: int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
257: SIGVTALRM, SIGPROF, 0};
258: int outfd;
1.4 ! millert 259:
1.1 millert 260: errno = 0;
1.4 ! millert 261:
1.1 millert 262: if (access(outpath, W_OK))
1.2 millert 263: err(2, outpath);
1.1 millert 264: act.sa_handler = onsig;
1.4 ! millert 265: (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXXXXXX",
! 266: outpath);
1.1 millert 267: if ((outfd = mkstemp(toutpath)) < 0 ||
268: (outfp = fdopen(outfd, "w")) == 0)
269: err(2, toutpath);
270: outfile = toutpath;
1.4 ! millert 271:
1.1 millert 272: (void)atexit(cleanup);
273: for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
274: sigaction(sigtable[i], &act, 0);
275: } else
276: outfile = outpath;
277: if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
278: err(2, outfile);
279: if (mflag)
280: fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
281: else
282: fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
283: if (outfile != outpath) {
284: if (access(outfile, 0))
1.2 millert 285: err(2, outfile);
1.1 millert 286: (void)unlink(outpath);
287: if (link(outfile, outpath))
288: err(2, "cannot link %s: output left in %s",
289: outpath, outfile);
290: (void)unlink(outfile);
291: }
292: exit(0);
293: }
294:
295: static void
296: onsig(s)
297: int s;
298: {
1.4 ! millert 299:
1.1 millert 300: cleanup();
301: exit(2); /* return 2 on error/interrupt */
302: }
303:
304: static void
305: cleanup()
306: {
1.4 ! millert 307:
1.1 millert 308: if (toutpath[0])
309: (void)unlink(toutpath);
310: }
311:
312: static void
313: usage(msg)
314: char *msg;
315: {
1.4 ! millert 316:
1.1 millert 317: if (msg)
318: (void)fprintf(stderr, "sort: %s\n", msg);
1.2 millert 319: (void)fprintf(stderr, "usage: [-T dir] [-o output] [-cmubdfinr] ");
320: (void)fprintf(stderr, "[-t char] [-R char] [-k keydef] ... [files]\n");
1.1 millert 321: exit(2);
322: }