xref: /original-bsd/contrib/sort/sort.c (revision 6c633850)
14286235dSbostic /*-
2*6c633850Sbostic  * Copyright (c) 1993
3*6c633850Sbostic  *	The Regents of the University of California.  All rights reserved.
44286235dSbostic  *
54286235dSbostic  * This code is derived from software contributed to Berkeley by
64286235dSbostic  * Peter McIlroy.
74286235dSbostic  *
84286235dSbostic  * %sccs.include.redist.c%
94286235dSbostic  */
104286235dSbostic 
114286235dSbostic #ifndef lint
12*6c633850Sbostic static char copyright[] =
13*6c633850Sbostic "@(#) Copyright (c) 1993\n\
14*6c633850Sbostic 	The Regents of the University of California.  All rights reserved.\n";
154286235dSbostic #endif /* not lint */
164286235dSbostic 
174286235dSbostic #ifndef lint
18*6c633850Sbostic static char sccsid[] = "@(#)sort.c	8.1 (Berkeley) 06/06/93";
194286235dSbostic #endif /* not lint */
204286235dSbostic 
214286235dSbostic /* Sort sorts a file using an optional user-defined key.
224286235dSbostic  * Sort uses radix sort for internal sorting, and allows
234286235dSbostic  * a choice of merge sort and radix sort for external sorting.
244286235dSbostic  */
254286235dSbostic 
264286235dSbostic #include "sort.h"
274286235dSbostic #include "fsort.h"
284286235dSbostic #include "pathnames.h"
294286235dSbostic 
304286235dSbostic #include <paths.h>
314286235dSbostic #include <signal.h>
324286235dSbostic #include <stdlib.h>
334286235dSbostic #include <string.h>
344286235dSbostic #include <unistd.h>
354286235dSbostic 
364286235dSbostic int REC_D = '\n';
374286235dSbostic u_char d_mask[NBINS];		/* flags for rec_d, field_d, <blank> */
384286235dSbostic /*
394286235dSbostic  * weight tables.  Gweights is one of ascii, Rascii..
404286235dSbostic  * modified to weight rec_d = 0 (or 255)
414286235dSbostic  */
424286235dSbostic extern u_char gweights[NBINS];
434286235dSbostic u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
444286235dSbostic /*
454286235dSbostic  * masks of ignored characters.  Alltable is 256 ones
464286235dSbostic  */
474286235dSbostic u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
484286235dSbostic int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
494286235dSbostic struct coldesc clist[(ND+1)*2];
504286235dSbostic int ncols = 0;
514286235dSbostic extern struct coldesc clist[(ND+1)*2];
524286235dSbostic extern int ncols;
534286235dSbostic 
544286235dSbostic char devstdin[] = _PATH_STDIN;
554286235dSbostic char toutpath[_POSIX_PATH_MAX];
564286235dSbostic 
574286235dSbostic static void cleanup __P((void));
584286235dSbostic static void onsig __P((int));
594286235dSbostic static void usage __P((char *));
604286235dSbostic 
614286235dSbostic int
main(argc,argv)624286235dSbostic main(argc, argv)
634286235dSbostic 	int argc;
644286235dSbostic 	char *argv[];
654286235dSbostic {
664286235dSbostic 	extern int optind;
674286235dSbostic 	extern char *optarg;
684286235dSbostic 	int (*get)();
694286235dSbostic 	int ch, i, stdinflag = 0, tmp = 0;
704286235dSbostic 	char cflag = 0, mflag = 0, nflag = 0;
714286235dSbostic 	char *outfile, *outpath = 0;
724286235dSbostic 	struct field fldtab[ND+2], *ftpos;
734286235dSbostic 	union f_handle filelist;
744286235dSbostic 	FILE *outfd;
754286235dSbostic 	memset(fldtab, 0, (ND+2)*sizeof(struct field));
764286235dSbostic 	memset(d_mask, 0, NBINS);
774286235dSbostic 	d_mask[REC_D = '\n'] = REC_D_F;
784286235dSbostic 	SINGL_FLD = SEP_FLAG = 0;
794286235dSbostic 	d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
804286235dSbostic 	ftpos = fldtab;
814286235dSbostic 	fixit(&argc, argv);
824286235dSbostic 	while ((ch = getopt(argc, argv, "bcdfik:mHno:rt:T:ux")) != EOF) {
834286235dSbostic 	switch (ch) {
844286235dSbostic 		case 'b': fldtab->flags |= BI | BT;
854286235dSbostic 			break;
864286235dSbostic 		case 'd':
874286235dSbostic 		case 'i':
884286235dSbostic 		case 'f':
894286235dSbostic 		case 'r': tmp |= optval(ch, 0);
904286235dSbostic 			if (tmp & R && tmp & F)
914286235dSbostic 				fldtab->weights = RFtable;
924286235dSbostic 			else if (tmp & F)
934286235dSbostic 				fldtab->weights = Ftable;
944286235dSbostic 			else if(tmp & R)
954286235dSbostic 				fldtab->weights = Rascii;
964286235dSbostic 			fldtab->flags |= tmp;
974286235dSbostic 			break;
984286235dSbostic 		case 'o':
994286235dSbostic 			outpath = optarg;
1004286235dSbostic 			break;
1014286235dSbostic 		case 'n':
1024286235dSbostic 			nflag = 1;
1034286235dSbostic 			setfield("1n", ++ftpos, fldtab->flags&(~R));
1044286235dSbostic 			break;
1054286235dSbostic 		case 'k':
1064286235dSbostic 			 setfield(optarg, ++ftpos, fldtab->flags);
1074286235dSbostic 			break;
1084286235dSbostic 		case 't':
1094286235dSbostic 			if (SEP_FLAG)
1104286235dSbostic 				usage("multiple field delimiters");
1114286235dSbostic 			SEP_FLAG = 1;
1124286235dSbostic 			d_mask[' '] &= ~FLD_D;
1134286235dSbostic 			d_mask['\t'] &= ~FLD_D;
1144286235dSbostic 			d_mask[*optarg] |= FLD_D;
1154286235dSbostic 			if (d_mask[*optarg] & REC_D_F)
1164286235dSbostic 				err(2, "record/field delimiter clash");
1174286235dSbostic 			break;
1184286235dSbostic 		case 'T':
1194286235dSbostic 			if (REC_D != '\n')
1204286235dSbostic 				usage("multiple record delimiters");
1214286235dSbostic 			if ('\n' == (REC_D = *optarg))
1224286235dSbostic 				break;
1234286235dSbostic 			d_mask['\n'] = d_mask[' '];
1244286235dSbostic 			d_mask[REC_D] = REC_D_F;
1254286235dSbostic 			break;
1264286235dSbostic 		case 'u':
1274286235dSbostic 			UNIQUE = 1;
1284286235dSbostic 			break;
1294286235dSbostic 		case 'c':
1304286235dSbostic 			cflag = 1;
1314286235dSbostic 			break;
1324286235dSbostic 		case 'm':
1334286235dSbostic 			mflag = 1;
1344286235dSbostic 			break;
1354286235dSbostic 		case 'H':
1364286235dSbostic 			PANIC = 0;
1374286235dSbostic 			break;
1384286235dSbostic 		case '?':
1394286235dSbostic 		default: usage("");
1404286235dSbostic 		}
1414286235dSbostic 	}
1424286235dSbostic 	if (cflag && argc > optind+1)
1434286235dSbostic 		errx(2, "too many input files for -c option");
1444286235dSbostic 	if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
1454286235dSbostic 		outpath = argv[argc-1];
1464286235dSbostic 		argc -= 2;
1474286235dSbostic 	}
1484286235dSbostic 	if (mflag && argc - optind > (MAXFCT - (16+1))*16)
1494286235dSbostic 		errx(2, "too many input files for -m option");
1504286235dSbostic 	for (i = optind; i < argc; i++) {
1514286235dSbostic 		/* allow one occurrence of /dev/stdin */
1524286235dSbostic 		if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
1534286235dSbostic 			if (stdinflag)
1544286235dSbostic 				warnx("ignoring extra \"%s\" in file list",
1554286235dSbostic 				    argv[i]);
1564286235dSbostic 			else {
1574286235dSbostic 				stdinflag = 1;
1584286235dSbostic 				argv[i] = devstdin;
1594286235dSbostic 			}
1604286235dSbostic 		} else if (ch = access(argv[i], R_OK))
1614286235dSbostic 			err(2, "%s", argv[i]);
1624286235dSbostic 	}
1634286235dSbostic 	if (!(fldtab->flags & (I|D) || fldtab[1].icol.num)) {
1644286235dSbostic 		SINGL_FLD = 1;
1654286235dSbostic 		fldtab[0].icol.num = 1;
1664286235dSbostic 	} else {
1674286235dSbostic 		if (!fldtab[1].icol.num) {
1684286235dSbostic 			fldtab[0].flags &= ~(BI|BT);
1694286235dSbostic 			setfield("1", ++ftpos, fldtab->flags);
1704286235dSbostic 		}
1714286235dSbostic 		if (nflag)
1724286235dSbostic 			fldtab[1].flags |= fldtab->flags;
1734286235dSbostic 		fldreset(fldtab);
1744286235dSbostic 		fldtab[0].flags &= ~F;
1754286235dSbostic 	}
1764286235dSbostic 	settables(fldtab[0].flags);
1774286235dSbostic 	num_init();
1784286235dSbostic 	fldtab->weights = gweights;
1794286235dSbostic 	if (optind == argc)
1804286235dSbostic 		argv[--optind] = devstdin;
1814286235dSbostic 	filelist.names = argv+optind;
1824286235dSbostic 	if (SINGL_FLD)
1834286235dSbostic 		get = makeline;
1844286235dSbostic 	else
1854286235dSbostic 		get = makekey;
1864286235dSbostic 	if (cflag) {
1874286235dSbostic 		order(filelist, get, fldtab);
1884286235dSbostic 		/* NOT REACHED */
1894286235dSbostic 	}
1904286235dSbostic 	if (!outpath) {
1914286235dSbostic 		(void)snprintf(toutpath,
1924286235dSbostic 		    sizeof(toutpath), "%sstdout", _PATH_DEV);
1934286235dSbostic 		outfile = outpath = toutpath;
1944286235dSbostic 	} else if (!(ch = access(outpath, 0)) &&
1954286235dSbostic 	    strncmp(_PATH_DEV, outpath, 5)) {
1964286235dSbostic 		struct sigaction act = {0, SIG_BLOCK, 6};
1974286235dSbostic 		int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
1984286235dSbostic 		    SIGVTALRM, SIGPROF, 0};
1994286235dSbostic 		errno = 0;
2004286235dSbostic 		if (access(outpath, W_OK))
2014286235dSbostic 			err(2, "%s", outpath);
2024286235dSbostic 		act.sa_handler = cleanup;
2034286235dSbostic 		(void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
2044286235dSbostic 		outfile = mktemp(toutpath);
2054286235dSbostic 		if (!outfile)
2064286235dSbostic 			err(2, "%s", toutpath);
2074286235dSbostic 		(void)atexit(cleanup);
2084286235dSbostic 		for (i = 0; sigtable[i]; ++i)	/* always unlink toutpath */
2094286235dSbostic 			sigaction(sigtable[i], &act, 0);
2104286235dSbostic 	} else outfile = outpath;
2114286235dSbostic 	if (!(outfd = fopen(outfile, "w")))
2124286235dSbostic 		err(2, "%s", outfile);
2134286235dSbostic 	if (mflag)
2144286235dSbostic 		fmerge(-1, filelist, argc-optind, get, outfd, putline, fldtab);
2154286235dSbostic 	else
2164286235dSbostic 		fsort(-1, 0, filelist, argc-optind, outfd, fldtab);
2174286235dSbostic 	if (outfile != outpath) {
2184286235dSbostic 		if (access(outfile, 0))
2194286235dSbostic 			err(2, "%s", outfile);
2204286235dSbostic 		(void)unlink(outpath);
2214286235dSbostic 		if (link(outfile, outpath))
2224286235dSbostic 			err(2, "cannot link %s: output left in %s",
2234286235dSbostic 			    outpath, outfile);
2244286235dSbostic 		(void)unlink(outfile);
2254286235dSbostic 	}
2264286235dSbostic 	exit(0);
2274286235dSbostic }
2284286235dSbostic 
2294286235dSbostic static void
onsig(s)2304286235dSbostic onsig(s)
2314286235dSbostic 	int s;
2324286235dSbostic {
2334286235dSbostic 	cleanup();
2344286235dSbostic 	exit(2);			/* return 2 on error/interrupt */
2354286235dSbostic }
2364286235dSbostic 
2374286235dSbostic static void
cleanup()2384286235dSbostic cleanup()
2394286235dSbostic {
2404286235dSbostic 	if (toutpath[0])
2414286235dSbostic 		(void)unlink(toutpath);
2424286235dSbostic }
2434286235dSbostic 
2444286235dSbostic static void
usage(msg)2454286235dSbostic usage(msg)
2464286235dSbostic 	char *msg;
2474286235dSbostic {
2484286235dSbostic 	if (msg)
2494286235dSbostic 		(void)fprintf(stderr, "sort: %s\n", msg);
2504286235dSbostic 	(void)fprintf(stderr, "usage: [-o output] [-cmubdfinr] [-t char] ");
2514286235dSbostic 	(void)fprintf(stderr, "[-T char] [-k keydef] ... [files]\n");
2524286235dSbostic 	exit(2);
2534286235dSbostic }
254