Annotation of src/usr.bin/sort/sort.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: sort.c,v 1.13 1999/11/30 16:41:41 espie 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.14 ! millert 49: static char rcsid[] = "$OpenBSD: sort.c,v 1.13 1999/11/30 16:41:41 espie 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:
1.13 espie 63: #include <sys/types.h>
64: #include <sys/stat.h>
1.1 millert 65: #include <paths.h>
66: #include <signal.h>
67: #include <stdlib.h>
68: #include <string.h>
69: #include <unistd.h>
1.10 mickey 70: #include <err.h>
1.1 millert 71:
72: int REC_D = '\n';
73: u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */
1.4 millert 74:
1.1 millert 75: /*
76: * weight tables. Gweights is one of ascii, Rascii..
77: * modified to weight rec_d = 0 (or 255)
78: */
79: extern u_char gweights[NBINS];
80: u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
1.4 millert 81:
1.1 millert 82: /*
83: * masks of ignored characters. Alltable is 256 ones
84: */
85: u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
86: int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
1.12 millert 87: struct coldesc *clist;
1.1 millert 88: int ncols = 0;
1.12 millert 89: int ND = 10; /* limit on number of -k options. */
1.1 millert 90:
91: char devstdin[] = _PATH_STDIN;
92: char toutpath[_POSIX_PATH_MAX];
93: char *tmpdir = _PATH_VARTMP;
94:
95: static void cleanup __P((void));
96: static void onsig __P((int));
97: static void usage __P((char *));
98:
1.12 millert 99: #define CHECK_NFIELDS \
100: if (++nfields == ND) { \
101: ND += 10; \
102: if ((p = realloc(fldtab, ND)) == NULL) \
103: errx(2, "cannot allocate memory"); \
104: ftpos = p + (ftpos - fldtab); \
105: fldtab = p; \
106: }
107:
1.1 millert 108: int
109: main(argc, argv)
110: int argc;
111: char *argv[];
112: {
113: int (*get)();
114: int ch, i, stdinflag = 0, tmp = 0;
1.12 millert 115: char nfields = 0, cflag = 0, mflag = 0;
1.1 millert 116: char *outfile, *outpath = 0;
1.12 millert 117: struct field *fldtab, *ftpos;
1.1 millert 118: union f_handle filelist;
119: FILE *outfp = NULL;
1.12 millert 120: void *p;
1.4 millert 121:
1.12 millert 122: if ((clist = calloc((ND+1)*2, sizeof(struct coldesc))) == NULL ||
123: (ftpos = fldtab = calloc(ND+2, sizeof(struct field))) == NULL)
124: errx(2, "cannot allocate memory");
1.1 millert 125: memset(d_mask, 0, NBINS);
126: d_mask[REC_D = '\n'] = REC_D_F;
127: SINGL_FLD = SEP_FLAG = 0;
128: d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
129: fixit(&argc, argv);
130: if (!issetugid() && (outfile = getenv("TMPDIR")))
131: tmpdir = outfile;
132: while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:")) != -1) {
1.4 millert 133: switch (ch) {
1.1 millert 134: case 'b': fldtab->flags |= BI | BT;
135: break;
136: case 'd':
1.4 millert 137: case 'f':
1.1 millert 138: case 'i':
1.5 millert 139: case 'n':
1.1 millert 140: case 'r': tmp |= optval(ch, 0);
141: if (tmp & R && tmp & F)
142: fldtab->weights = RFtable;
143: else if (tmp & F)
144: fldtab->weights = Ftable;
1.4 millert 145: else if (tmp & R)
1.1 millert 146: fldtab->weights = Rascii;
147: fldtab->flags |= tmp;
148: break;
149: case 'o':
150: outpath = optarg;
151: break;
152: case 'k':
1.12 millert 153: CHECK_NFIELDS;
1.5 millert 154: setfield(optarg, ++ftpos, fldtab->flags);
1.1 millert 155: break;
156: case 't':
157: if (SEP_FLAG)
158: usage("multiple field delimiters");
159: SEP_FLAG = 1;
160: d_mask[' '] &= ~FLD_D;
161: d_mask['\t'] &= ~FLD_D;
162: d_mask[(int)*optarg] |= FLD_D;
163: if (d_mask[(int)*optarg] & REC_D_F)
164: err(2, "record/field delimiter clash");
165: break;
166: case 'R':
167: if (REC_D != '\n')
168: usage("multiple record delimiters");
169: if ('\n' == (REC_D = *optarg))
170: break;
171: d_mask['\n'] = d_mask[' '];
172: d_mask[REC_D] = REC_D_F;
173: break;
174: case 'T':
175: tmpdir = optarg;
176: break;
177: case 'u':
178: UNIQUE = 1;
179: break;
180: case 'c':
181: cflag = 1;
182: break;
183: case 'm':
184: mflag = 1;
185: break;
186: case 'H':
187: PANIC = 0;
188: break;
189: case 'y':
190: /* accept -y for backwards compat. */
191: break;
192: case '?':
1.8 deraadt 193: default:
194: usage(NULL);
1.1 millert 195: }
196: }
1.4 millert 197:
1.1 millert 198: if (cflag && argc > optind+1)
199: errx(2, "too many input files for -c option");
1.4 millert 200:
1.1 millert 201: if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
202: outpath = argv[argc-1];
203: argc -= 2;
204: }
1.4 millert 205:
1.1 millert 206: if (mflag && argc - optind > (MAXFCT - (16+1))*16)
207: errx(2, "too many input files for -m option");
1.4 millert 208:
1.1 millert 209: for (i = optind; i < argc; i++) {
210: /* allow one occurrence of /dev/stdin */
211: if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
212: if (stdinflag)
213: warnx("ignoring extra \"%s\" in file list",
214: argv[i]);
215: else {
216: stdinflag = 1;
217: argv[i] = devstdin;
218: }
219: } else if ((ch = access(argv[i], R_OK)))
1.14 ! millert 220: err(2, "%s", argv[i]);
1.1 millert 221: }
1.4 millert 222:
1.5 millert 223: if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
1.1 millert 224: SINGL_FLD = 1;
225: fldtab[0].icol.num = 1;
226: } else {
227: if (!fldtab[1].icol.num) {
1.12 millert 228: CHECK_NFIELDS;
1.1 millert 229: fldtab[0].flags &= ~(BI|BT);
230: setfield("1", ++ftpos, fldtab->flags);
231: }
232: fldreset(fldtab);
233: fldtab[0].flags &= ~F;
234: }
235: settables(fldtab[0].flags);
236: num_init();
237: fldtab->weights = gweights;
1.4 millert 238:
1.3 deraadt 239: if (optind == argc) {
240: static char *names[2];
241:
242: names[0] = devstdin;
243: names[1] = NULL;
244: filelist.names = names;
245: optind--;
246: } else
247: filelist.names = argv+optind;
1.4 millert 248:
1.1 millert 249: if (SINGL_FLD)
250: get = makeline;
251: else
252: get = makekey;
1.4 millert 253:
1.1 millert 254: if (cflag) {
255: order(filelist, get, fldtab);
256: /* NOT REACHED */
257: }
1.4 millert 258:
1.1 millert 259: if (!outpath) {
260: (void)snprintf(toutpath,
261: sizeof(toutpath), "%sstdout", _PATH_DEV);
262: outfile = outpath = toutpath;
263: } else if (!(ch = access(outpath, 0)) &&
264: strncmp(_PATH_DEV, outpath, 5)) {
265: struct sigaction act = {0, SIG_BLOCK, 6};
266: int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
267: SIGVTALRM, SIGPROF, 0};
268: int outfd;
1.13 espie 269: mode_t um;
1.4 millert 270:
1.1 millert 271: errno = 0;
1.4 millert 272:
1.1 millert 273: if (access(outpath, W_OK))
1.14 ! millert 274: err(2, "%s", outpath);
1.1 millert 275: act.sa_handler = onsig;
1.4 millert 276: (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXXXXXX",
277: outpath);
1.13 espie 278: /* use default umask to try and avoid one syscall */
279: um = umask(S_IWGRP|S_IWOTH);
280: if (um != S_IWGRP|S_IWOTH)
281: (void)umask(um);
282: if ((outfd = mkstemp(toutpath)) == -1 ||
283: fchmod(outfd, DEFFILEMODE & ~um) == -1 ||
1.1 millert 284: (outfp = fdopen(outfd, "w")) == 0)
1.14 ! millert 285: err(2, "%s", toutpath);
1.1 millert 286: outfile = toutpath;
1.4 millert 287:
1.1 millert 288: (void)atexit(cleanup);
289: for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
290: sigaction(sigtable[i], &act, 0);
291: } else
292: outfile = outpath;
293: if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
1.14 ! millert 294: err(2, "%s", outfile);
1.1 millert 295: if (mflag)
296: fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
297: else
298: fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
299: if (outfile != outpath) {
300: if (access(outfile, 0))
1.14 ! millert 301: err(2, "%s", outfile);
1.1 millert 302: (void)unlink(outpath);
303: if (link(outfile, outpath))
304: err(2, "cannot link %s: output left in %s",
305: outpath, outfile);
306: (void)unlink(outfile);
307: }
308: exit(0);
309: }
310:
311: static void
312: onsig(s)
313: int s;
314: {
1.4 millert 315:
1.1 millert 316: cleanup();
317: exit(2); /* return 2 on error/interrupt */
318: }
319:
320: static void
321: cleanup()
322: {
1.4 millert 323:
1.1 millert 324: if (toutpath[0])
325: (void)unlink(toutpath);
326: }
327:
328: static void
329: usage(msg)
330: char *msg;
331: {
1.8 deraadt 332: extern char *__progname;
1.4 millert 333:
1.1 millert 334: if (msg)
1.14 ! millert 335: warnx("%s", msg);
1.11 deraadt 336: (void)fprintf(stderr, "usage: %s [-T dir] [-o output] [-cmubdfinrH] "
1.9 deraadt 337: "[-t char] [-R char] [-k keydef] ... [files]\n", __progname);
1.1 millert 338: exit(2);
339: }