Annotation of src/usr.bin/sort/sort.c, Revision 1.25
1.25 ! moritz 1: /* $OpenBSD: sort.c,v 1.24 2004/09/14 22:57:21 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.
1.20 millert 18: * 3. Neither the name of the University nor the names of its contributors
1.1 millert 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
35: #ifndef lint
36: static char copyright[] =
37: "@(#) Copyright (c) 1993\n\
38: The Regents of the University of California. All rights reserved.\n";
39: #endif /* not lint */
40:
41: #ifndef lint
42: #if 0
43: static char sccsid[] = "@(#)sort.c 8.1 (Berkeley) 6/6/93";
44: #else
1.25 ! moritz 45: static char rcsid[] = "$OpenBSD: sort.c,v 1.24 2004/09/14 22:57:21 deraadt Exp $";
1.1 millert 46: #endif
47: #endif /* not lint */
48:
1.4 millert 49: /*
50: * Sort sorts a file using an optional user-defined key.
1.1 millert 51: * Sort uses radix sort for internal sorting, and allows
52: * a choice of merge sort and radix sort for external sorting.
53: */
54:
55: #include "sort.h"
56: #include "fsort.h"
57: #include "pathnames.h"
58:
1.13 espie 59: #include <sys/types.h>
60: #include <sys/stat.h>
1.16 ericj 61: #include <locale.h>
1.1 millert 62: #include <paths.h>
63: #include <signal.h>
64: #include <stdlib.h>
65: #include <string.h>
66: #include <unistd.h>
1.10 mickey 67: #include <err.h>
1.1 millert 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;
1.12 millert 84: struct coldesc *clist;
1.1 millert 85: int ncols = 0;
1.12 millert 86: int ND = 10; /* limit on number of -k options. */
1.1 millert 87:
1.19 millert 88: char *devstdin = _PATH_STDIN;
1.1 millert 89: char *tmpdir = _PATH_VARTMP;
1.19 millert 90: char toutpath[PATH_MAX];
1.1 millert 91:
1.18 millert 92: static void cleanup(void);
93: static void onsig(int);
94: static void usage(char *);
1.1 millert 95:
1.12 millert 96: #define CHECK_NFIELDS \
97: if (++nfields == ND) { \
98: ND += 10; \
99: if ((p = realloc(fldtab, ND)) == NULL) \
100: errx(2, "cannot allocate memory"); \
101: ftpos = p + (ftpos - fldtab); \
102: fldtab = p; \
103: }
104:
1.1 millert 105: int
1.21 deraadt 106: main(int argc, char *argv[])
1.1 millert 107: {
1.22 deraadt 108: int (*get)(int, union f_handle, int, RECHEADER *, u_char *, struct field *);
1.1 millert 109: int ch, i, stdinflag = 0, tmp = 0;
1.12 millert 110: char nfields = 0, cflag = 0, mflag = 0;
1.1 millert 111: char *outfile, *outpath = 0;
1.12 millert 112: struct field *fldtab, *ftpos;
1.1 millert 113: union f_handle filelist;
114: FILE *outfp = NULL;
1.12 millert 115: void *p;
1.4 millert 116:
1.16 ericj 117: setlocale(LC_ALL, "");
118:
1.12 millert 119: if ((clist = calloc((ND+1)*2, sizeof(struct coldesc))) == NULL ||
120: (ftpos = fldtab = calloc(ND+2, sizeof(struct field))) == NULL)
121: errx(2, "cannot allocate memory");
1.1 millert 122: memset(d_mask, 0, NBINS);
123: d_mask[REC_D = '\n'] = REC_D_F;
124: d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
125: fixit(&argc, argv);
126: if (!issetugid() && (outfile = getenv("TMPDIR")))
127: tmpdir = outfile;
128: while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:")) != -1) {
1.4 millert 129: switch (ch) {
1.1 millert 130: case 'b': fldtab->flags |= BI | BT;
131: break;
132: case 'd':
1.4 millert 133: case 'f':
1.1 millert 134: case 'i':
1.5 millert 135: case 'n':
1.1 millert 136: case 'r': tmp |= optval(ch, 0);
137: if (tmp & R && tmp & F)
138: fldtab->weights = RFtable;
139: else if (tmp & F)
140: fldtab->weights = Ftable;
1.4 millert 141: else if (tmp & R)
1.1 millert 142: fldtab->weights = Rascii;
143: fldtab->flags |= tmp;
144: break;
145: case 'o':
146: outpath = optarg;
147: break;
148: case 'k':
1.12 millert 149: CHECK_NFIELDS;
1.5 millert 150: setfield(optarg, ++ftpos, fldtab->flags);
1.1 millert 151: break;
152: case 't':
153: if (SEP_FLAG)
154: usage("multiple field delimiters");
155: SEP_FLAG = 1;
156: d_mask[' '] &= ~FLD_D;
157: d_mask['\t'] &= ~FLD_D;
158: d_mask[(int)*optarg] |= FLD_D;
159: if (d_mask[(int)*optarg] & REC_D_F)
160: err(2, "record/field delimiter clash");
161: break;
162: case 'R':
163: if (REC_D != '\n')
164: usage("multiple record delimiters");
165: if ('\n' == (REC_D = *optarg))
166: break;
167: d_mask['\n'] = d_mask[' '];
168: d_mask[REC_D] = REC_D_F;
169: break;
170: case 'T':
171: tmpdir = optarg;
172: break;
173: case 'u':
174: UNIQUE = 1;
175: break;
176: case 'c':
177: cflag = 1;
178: break;
179: case 'm':
180: mflag = 1;
181: break;
182: case 'H':
183: PANIC = 0;
184: break;
185: case 'y':
186: /* accept -y for backwards compat. */
187: break;
188: case '?':
1.8 deraadt 189: default:
190: usage(NULL);
1.1 millert 191: }
192: }
1.4 millert 193:
1.1 millert 194: if (cflag && argc > optind+1)
195: errx(2, "too many input files for -c option");
1.4 millert 196:
1.1 millert 197: if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
198: outpath = argv[argc-1];
199: argc -= 2;
200: }
1.4 millert 201:
1.1 millert 202: if (mflag && argc - optind > (MAXFCT - (16+1))*16)
203: errx(2, "too many input files for -m option");
1.4 millert 204:
1.1 millert 205: for (i = optind; i < argc; i++) {
206: /* allow one occurrence of /dev/stdin */
207: if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
208: if (stdinflag)
209: warnx("ignoring extra \"%s\" in file list",
210: argv[i]);
211: else {
212: stdinflag = 1;
213: argv[i] = devstdin;
214: }
215: } else if ((ch = access(argv[i], R_OK)))
1.14 millert 216: err(2, "%s", argv[i]);
1.1 millert 217: }
1.4 millert 218:
1.5 millert 219: if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
1.1 millert 220: SINGL_FLD = 1;
221: fldtab[0].icol.num = 1;
222: } else {
223: if (!fldtab[1].icol.num) {
1.12 millert 224: CHECK_NFIELDS;
1.1 millert 225: fldtab[0].flags &= ~(BI|BT);
226: setfield("1", ++ftpos, fldtab->flags);
227: }
228: fldreset(fldtab);
229: fldtab[0].flags &= ~F;
230: }
231: settables(fldtab[0].flags);
232: num_init();
233: fldtab->weights = gweights;
1.4 millert 234:
1.3 deraadt 235: if (optind == argc) {
236: static char *names[2];
237:
238: names[0] = devstdin;
239: names[1] = NULL;
240: filelist.names = names;
241: optind--;
242: } else
243: filelist.names = argv+optind;
1.4 millert 244:
1.1 millert 245: if (SINGL_FLD)
246: get = makeline;
247: else
248: get = makekey;
1.4 millert 249:
1.1 millert 250: if (cflag) {
251: order(filelist, get, fldtab);
252: /* NOT REACHED */
253: }
1.4 millert 254:
1.1 millert 255: if (!outpath) {
256: (void)snprintf(toutpath,
257: sizeof(toutpath), "%sstdout", _PATH_DEV);
258: outfile = outpath = toutpath;
259: } else if (!(ch = access(outpath, 0)) &&
260: strncmp(_PATH_DEV, outpath, 5)) {
1.17 millert 261: struct sigaction act;
1.1 millert 262: int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
263: SIGVTALRM, SIGPROF, 0};
264: int outfd;
1.13 espie 265: mode_t um;
1.4 millert 266:
1.1 millert 267: errno = 0;
1.4 millert 268:
1.1 millert 269: if (access(outpath, W_OK))
1.14 millert 270: err(2, "%s", outpath);
1.4 millert 271: (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXXXXXX",
272: outpath);
1.13 espie 273: um = umask(S_IWGRP|S_IWOTH);
1.25 ! moritz 274: (void)umask(um);
1.13 espie 275: if ((outfd = mkstemp(toutpath)) == -1 ||
276: fchmod(outfd, DEFFILEMODE & ~um) == -1 ||
1.1 millert 277: (outfp = fdopen(outfd, "w")) == 0)
1.14 millert 278: err(2, "%s", toutpath);
1.1 millert 279: outfile = toutpath;
1.4 millert 280:
1.1 millert 281: (void)atexit(cleanup);
1.17 millert 282: sigfillset(&act.sa_mask);
283: act.sa_flags = SA_RESTART;
284: act.sa_handler = onsig;
1.1 millert 285: for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
286: sigaction(sigtable[i], &act, 0);
287: } else
288: outfile = outpath;
289: if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
1.14 millert 290: err(2, "%s", outfile);
1.1 millert 291: if (mflag)
292: fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
293: else
294: fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
295: if (outfile != outpath) {
296: if (access(outfile, 0))
1.14 millert 297: err(2, "%s", outfile);
1.1 millert 298: (void)unlink(outpath);
299: if (link(outfile, outpath))
300: err(2, "cannot link %s: output left in %s",
301: outpath, outfile);
302: (void)unlink(outfile);
303: }
304: exit(0);
305: }
306:
1.24 deraadt 307: /* ARGSUSED */
1.1 millert 308: static void
1.21 deraadt 309: onsig(int signo)
1.1 millert 310: {
1.4 millert 311:
1.1 millert 312: cleanup();
1.15 deraadt 313: _exit(2); /* return 2 on error/interrupt */
1.1 millert 314: }
315:
316: static void
1.21 deraadt 317: cleanup(void)
1.1 millert 318: {
1.4 millert 319:
1.1 millert 320: if (toutpath[0])
321: (void)unlink(toutpath);
322: }
323:
324: static void
1.21 deraadt 325: usage(char *msg)
1.1 millert 326: {
1.8 deraadt 327: extern char *__progname;
1.4 millert 328:
1.16 ericj 329: if (msg != NULL)
1.14 millert 330: warnx("%s", msg);
1.11 deraadt 331: (void)fprintf(stderr, "usage: %s [-T dir] [-o output] [-cmubdfinrH] "
1.9 deraadt 332: "[-t char] [-R char] [-k keydef] ... [files]\n", __progname);
1.1 millert 333: exit(2);
334: }