xref: /original-bsd/contrib/sort/sort.c (revision 4286235d)
1*4286235dSbostic /*-
2*4286235dSbostic  * Copyright (c) 1993 The Regents of the University of California.
3*4286235dSbostic  * All rights reserved.
4*4286235dSbostic  *
5*4286235dSbostic  * This code is derived from software contributed to Berkeley by
6*4286235dSbostic  * Peter McIlroy.
7*4286235dSbostic  *
8*4286235dSbostic  * %sccs.include.redist.c%
9*4286235dSbostic  */
10*4286235dSbostic 
11*4286235dSbostic #ifndef lint
12*4286235dSbostic char copyright[] =
13*4286235dSbostic "@(#) Copyright (c) 1993 The Regents of the University of California.\n\
14*4286235dSbostic  All rights reserved.\n";
15*4286235dSbostic #endif /* not lint */
16*4286235dSbostic 
17*4286235dSbostic #ifndef lint
18*4286235dSbostic static char sccsid[] = "@(#)sort.c	5.1 (Berkeley) 06/01/93";
19*4286235dSbostic #endif /* not lint */
20*4286235dSbostic 
21*4286235dSbostic /* Sort sorts a file using an optional user-defined key.
22*4286235dSbostic  * Sort uses radix sort for internal sorting, and allows
23*4286235dSbostic  * a choice of merge sort and radix sort for external sorting.
24*4286235dSbostic  */
25*4286235dSbostic 
26*4286235dSbostic #include "sort.h"
27*4286235dSbostic #include "fsort.h"
28*4286235dSbostic #include "pathnames.h"
29*4286235dSbostic 
30*4286235dSbostic #include <paths.h>
31*4286235dSbostic #include <signal.h>
32*4286235dSbostic #include <stdlib.h>
33*4286235dSbostic #include <string.h>
34*4286235dSbostic #include <unistd.h>
35*4286235dSbostic 
36*4286235dSbostic int REC_D = '\n';
37*4286235dSbostic u_char d_mask[NBINS];		/* flags for rec_d, field_d, <blank> */
38*4286235dSbostic /*
39*4286235dSbostic  * weight tables.  Gweights is one of ascii, Rascii..
40*4286235dSbostic  * modified to weight rec_d = 0 (or 255)
41*4286235dSbostic  */
42*4286235dSbostic extern u_char gweights[NBINS];
43*4286235dSbostic u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
44*4286235dSbostic /*
45*4286235dSbostic  * masks of ignored characters.  Alltable is 256 ones
46*4286235dSbostic  */
47*4286235dSbostic u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
48*4286235dSbostic int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
49*4286235dSbostic struct coldesc clist[(ND+1)*2];
50*4286235dSbostic int ncols = 0;
51*4286235dSbostic extern struct coldesc clist[(ND+1)*2];
52*4286235dSbostic extern int ncols;
53*4286235dSbostic 
54*4286235dSbostic char devstdin[] = _PATH_STDIN;
55*4286235dSbostic char toutpath[_POSIX_PATH_MAX];
56*4286235dSbostic 
57*4286235dSbostic static void cleanup __P((void));
58*4286235dSbostic static void onsig __P((int));
59*4286235dSbostic static void usage __P((char *));
60*4286235dSbostic 
61*4286235dSbostic int
62*4286235dSbostic main(argc, argv)
63*4286235dSbostic 	int argc;
64*4286235dSbostic 	char *argv[];
65*4286235dSbostic {
66*4286235dSbostic 	extern int optind;
67*4286235dSbostic 	extern char *optarg;
68*4286235dSbostic 	int (*get)();
69*4286235dSbostic 	int ch, i, stdinflag = 0, tmp = 0;
70*4286235dSbostic 	char cflag = 0, mflag = 0, nflag = 0;
71*4286235dSbostic 	char *outfile, *outpath = 0;
72*4286235dSbostic 	struct field fldtab[ND+2], *ftpos;
73*4286235dSbostic 	union f_handle filelist;
74*4286235dSbostic 	FILE *outfd;
75*4286235dSbostic 	memset(fldtab, 0, (ND+2)*sizeof(struct field));
76*4286235dSbostic 	memset(d_mask, 0, NBINS);
77*4286235dSbostic 	d_mask[REC_D = '\n'] = REC_D_F;
78*4286235dSbostic 	SINGL_FLD = SEP_FLAG = 0;
79*4286235dSbostic 	d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
80*4286235dSbostic 	ftpos = fldtab;
81*4286235dSbostic 	fixit(&argc, argv);
82*4286235dSbostic 	while ((ch = getopt(argc, argv, "bcdfik:mHno:rt:T:ux")) != EOF) {
83*4286235dSbostic 	switch (ch) {
84*4286235dSbostic 		case 'b': fldtab->flags |= BI | BT;
85*4286235dSbostic 			break;
86*4286235dSbostic 		case 'd':
87*4286235dSbostic 		case 'i':
88*4286235dSbostic 		case 'f':
89*4286235dSbostic 		case 'r': tmp |= optval(ch, 0);
90*4286235dSbostic 			if (tmp & R && tmp & F)
91*4286235dSbostic 				fldtab->weights = RFtable;
92*4286235dSbostic 			else if (tmp & F)
93*4286235dSbostic 				fldtab->weights = Ftable;
94*4286235dSbostic 			else if(tmp & R)
95*4286235dSbostic 				fldtab->weights = Rascii;
96*4286235dSbostic 			fldtab->flags |= tmp;
97*4286235dSbostic 			break;
98*4286235dSbostic 		case 'o':
99*4286235dSbostic 			outpath = optarg;
100*4286235dSbostic 			break;
101*4286235dSbostic 		case 'n':
102*4286235dSbostic 			nflag = 1;
103*4286235dSbostic 			setfield("1n", ++ftpos, fldtab->flags&(~R));
104*4286235dSbostic 			break;
105*4286235dSbostic 		case 'k':
106*4286235dSbostic 			 setfield(optarg, ++ftpos, fldtab->flags);
107*4286235dSbostic 			break;
108*4286235dSbostic 		case 't':
109*4286235dSbostic 			if (SEP_FLAG)
110*4286235dSbostic 				usage("multiple field delimiters");
111*4286235dSbostic 			SEP_FLAG = 1;
112*4286235dSbostic 			d_mask[' '] &= ~FLD_D;
113*4286235dSbostic 			d_mask['\t'] &= ~FLD_D;
114*4286235dSbostic 			d_mask[*optarg] |= FLD_D;
115*4286235dSbostic 			if (d_mask[*optarg] & REC_D_F)
116*4286235dSbostic 				err(2, "record/field delimiter clash");
117*4286235dSbostic 			break;
118*4286235dSbostic 		case 'T':
119*4286235dSbostic 			if (REC_D != '\n')
120*4286235dSbostic 				usage("multiple record delimiters");
121*4286235dSbostic 			if ('\n' == (REC_D = *optarg))
122*4286235dSbostic 				break;
123*4286235dSbostic 			d_mask['\n'] = d_mask[' '];
124*4286235dSbostic 			d_mask[REC_D] = REC_D_F;
125*4286235dSbostic 			break;
126*4286235dSbostic 		case 'u':
127*4286235dSbostic 			UNIQUE = 1;
128*4286235dSbostic 			break;
129*4286235dSbostic 		case 'c':
130*4286235dSbostic 			cflag = 1;
131*4286235dSbostic 			break;
132*4286235dSbostic 		case 'm':
133*4286235dSbostic 			mflag = 1;
134*4286235dSbostic 			break;
135*4286235dSbostic 		case 'H':
136*4286235dSbostic 			PANIC = 0;
137*4286235dSbostic 			break;
138*4286235dSbostic 		case '?':
139*4286235dSbostic 		default: usage("");
140*4286235dSbostic 		}
141*4286235dSbostic 	}
142*4286235dSbostic 	if (cflag && argc > optind+1)
143*4286235dSbostic 		errx(2, "too many input files for -c option");
144*4286235dSbostic 	if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
145*4286235dSbostic 		outpath = argv[argc-1];
146*4286235dSbostic 		argc -= 2;
147*4286235dSbostic 	}
148*4286235dSbostic 	if (mflag && argc - optind > (MAXFCT - (16+1))*16)
149*4286235dSbostic 		errx(2, "too many input files for -m option");
150*4286235dSbostic 	for (i = optind; i < argc; i++) {
151*4286235dSbostic 		/* allow one occurrence of /dev/stdin */
152*4286235dSbostic 		if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
153*4286235dSbostic 			if (stdinflag)
154*4286235dSbostic 				warnx("ignoring extra \"%s\" in file list",
155*4286235dSbostic 				    argv[i]);
156*4286235dSbostic 			else {
157*4286235dSbostic 				stdinflag = 1;
158*4286235dSbostic 				argv[i] = devstdin;
159*4286235dSbostic 			}
160*4286235dSbostic 		} else if (ch = access(argv[i], R_OK))
161*4286235dSbostic 			err(2, "%s", argv[i]);
162*4286235dSbostic 	}
163*4286235dSbostic 	if (!(fldtab->flags & (I|D) || fldtab[1].icol.num)) {
164*4286235dSbostic 		SINGL_FLD = 1;
165*4286235dSbostic 		fldtab[0].icol.num = 1;
166*4286235dSbostic 	} else {
167*4286235dSbostic 		if (!fldtab[1].icol.num) {
168*4286235dSbostic 			fldtab[0].flags &= ~(BI|BT);
169*4286235dSbostic 			setfield("1", ++ftpos, fldtab->flags);
170*4286235dSbostic 		}
171*4286235dSbostic 		if (nflag)
172*4286235dSbostic 			fldtab[1].flags |= fldtab->flags;
173*4286235dSbostic 		fldreset(fldtab);
174*4286235dSbostic 		fldtab[0].flags &= ~F;
175*4286235dSbostic 	}
176*4286235dSbostic 	settables(fldtab[0].flags);
177*4286235dSbostic 	num_init();
178*4286235dSbostic 	fldtab->weights = gweights;
179*4286235dSbostic 	if (optind == argc)
180*4286235dSbostic 		argv[--optind] = devstdin;
181*4286235dSbostic 	filelist.names = argv+optind;
182*4286235dSbostic 	if (SINGL_FLD)
183*4286235dSbostic 		get = makeline;
184*4286235dSbostic 	else
185*4286235dSbostic 		get = makekey;
186*4286235dSbostic 	if (cflag) {
187*4286235dSbostic 		order(filelist, get, fldtab);
188*4286235dSbostic 		/* NOT REACHED */
189*4286235dSbostic 	}
190*4286235dSbostic 	if (!outpath) {
191*4286235dSbostic 		(void)snprintf(toutpath,
192*4286235dSbostic 		    sizeof(toutpath), "%sstdout", _PATH_DEV);
193*4286235dSbostic 		outfile = outpath = toutpath;
194*4286235dSbostic 	} else if (!(ch = access(outpath, 0)) &&
195*4286235dSbostic 	    strncmp(_PATH_DEV, outpath, 5)) {
196*4286235dSbostic 		struct sigaction act = {0, SIG_BLOCK, 6};
197*4286235dSbostic 		int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
198*4286235dSbostic 		    SIGVTALRM, SIGPROF, 0};
199*4286235dSbostic 		errno = 0;
200*4286235dSbostic 		if (access(outpath, W_OK))
201*4286235dSbostic 			err(2, "%s", outpath);
202*4286235dSbostic 		act.sa_handler = cleanup;
203*4286235dSbostic 		(void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
204*4286235dSbostic 		outfile = mktemp(toutpath);
205*4286235dSbostic 		if (!outfile)
206*4286235dSbostic 			err(2, "%s", toutpath);
207*4286235dSbostic 		(void)atexit(cleanup);
208*4286235dSbostic 		for (i = 0; sigtable[i]; ++i)	/* always unlink toutpath */
209*4286235dSbostic 			sigaction(sigtable[i], &act, 0);
210*4286235dSbostic 	} else outfile = outpath;
211*4286235dSbostic 	if (!(outfd = fopen(outfile, "w")))
212*4286235dSbostic 		err(2, "%s", outfile);
213*4286235dSbostic 	if (mflag)
214*4286235dSbostic 		fmerge(-1, filelist, argc-optind, get, outfd, putline, fldtab);
215*4286235dSbostic 	else
216*4286235dSbostic 		fsort(-1, 0, filelist, argc-optind, outfd, fldtab);
217*4286235dSbostic 	if (outfile != outpath) {
218*4286235dSbostic 		if (access(outfile, 0))
219*4286235dSbostic 			err(2, "%s", outfile);
220*4286235dSbostic 		(void)unlink(outpath);
221*4286235dSbostic 		if (link(outfile, outpath))
222*4286235dSbostic 			err(2, "cannot link %s: output left in %s",
223*4286235dSbostic 			    outpath, outfile);
224*4286235dSbostic 		(void)unlink(outfile);
225*4286235dSbostic 	}
226*4286235dSbostic 	exit(0);
227*4286235dSbostic }
228*4286235dSbostic 
229*4286235dSbostic static void
230*4286235dSbostic onsig(s)
231*4286235dSbostic 	int s;
232*4286235dSbostic {
233*4286235dSbostic 	cleanup();
234*4286235dSbostic 	exit(2);			/* return 2 on error/interrupt */
235*4286235dSbostic }
236*4286235dSbostic 
237*4286235dSbostic static void
238*4286235dSbostic cleanup()
239*4286235dSbostic {
240*4286235dSbostic 	if (toutpath[0])
241*4286235dSbostic 		(void)unlink(toutpath);
242*4286235dSbostic }
243*4286235dSbostic 
244*4286235dSbostic static void
245*4286235dSbostic usage(msg)
246*4286235dSbostic 	char *msg;
247*4286235dSbostic {
248*4286235dSbostic 	if (msg)
249*4286235dSbostic 		(void)fprintf(stderr, "sort: %s\n", msg);
250*4286235dSbostic 	(void)fprintf(stderr, "usage: [-o output] [-cmubdfinr] [-t char] ");
251*4286235dSbostic 	(void)fprintf(stderr, "[-T char] [-k keydef] ... [files]\n");
252*4286235dSbostic 	exit(2);
253*4286235dSbostic }
254