Home | History | Annotate | Line # | Download | only in sort
sort.c revision 1.5
      1  1.5  bjh21 /*	$NetBSD: sort.c,v 1.5 2000/10/07 21:13:56 bjh21 Exp $	*/
      2  1.2  bjh21 
      3  1.1  bjh21 /*-
      4  1.1  bjh21  * Copyright (c) 1993
      5  1.1  bjh21  *	The Regents of the University of California.  All rights reserved.
      6  1.1  bjh21  *
      7  1.1  bjh21  * This code is derived from software contributed to Berkeley by
      8  1.1  bjh21  * Peter McIlroy.
      9  1.1  bjh21  *
     10  1.1  bjh21  * Redistribution and use in source and binary forms, with or without
     11  1.1  bjh21  * modification, are permitted provided that the following conditions
     12  1.1  bjh21  * are met:
     13  1.1  bjh21  * 1. Redistributions of source code must retain the above copyright
     14  1.1  bjh21  *    notice, this list of conditions and the following disclaimer.
     15  1.1  bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  bjh21  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  bjh21  *    documentation and/or other materials provided with the distribution.
     18  1.1  bjh21  * 3. All advertising materials mentioning features or use of this software
     19  1.1  bjh21  *    must display the following acknowledgement:
     20  1.1  bjh21  *	This product includes software developed by the University of
     21  1.1  bjh21  *	California, Berkeley and its contributors.
     22  1.1  bjh21  * 4. Neither the name of the University nor the names of its contributors
     23  1.1  bjh21  *    may be used to endorse or promote products derived from this software
     24  1.1  bjh21  *    without specific prior written permission.
     25  1.1  bjh21  *
     26  1.1  bjh21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1  bjh21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1  bjh21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1  bjh21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1  bjh21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1  bjh21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1  bjh21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1  bjh21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1  bjh21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1  bjh21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1  bjh21  * SUCH DAMAGE.
     37  1.1  bjh21  */
     38  1.1  bjh21 
     39  1.1  bjh21 /* Sort sorts a file using an optional user-defined key.
     40  1.1  bjh21  * Sort uses radix sort for internal sorting, and allows
     41  1.1  bjh21  * a choice of merge sort and radix sort for external sorting.
     42  1.1  bjh21  */
     43  1.1  bjh21 
     44  1.1  bjh21 #include "sort.h"
     45  1.1  bjh21 #include "fsort.h"
     46  1.1  bjh21 #include "pathnames.h"
     47  1.1  bjh21 
     48  1.2  bjh21 #ifndef lint
     49  1.2  bjh21 __COPYRIGHT("@(#) Copyright (c) 1993\n\
     50  1.2  bjh21 	The Regents of the University of California.  All rights reserved.\n");
     51  1.2  bjh21 #endif /* not lint */
     52  1.2  bjh21 
     53  1.2  bjh21 #ifndef lint
     54  1.5  bjh21 __RCSID("$NetBSD: sort.c,v 1.5 2000/10/07 21:13:56 bjh21 Exp $");
     55  1.2  bjh21 __SCCSID("@(#)sort.c	8.1 (Berkeley) 6/6/93");
     56  1.2  bjh21 #endif /* not lint */
     57  1.2  bjh21 
     58  1.1  bjh21 #include <paths.h>
     59  1.1  bjh21 #include <signal.h>
     60  1.1  bjh21 #include <stdlib.h>
     61  1.1  bjh21 #include <string.h>
     62  1.1  bjh21 #include <unistd.h>
     63  1.1  bjh21 
     64  1.1  bjh21 int REC_D = '\n';
     65  1.1  bjh21 u_char d_mask[NBINS];		/* flags for rec_d, field_d, <blank> */
     66  1.1  bjh21 /*
     67  1.1  bjh21  * weight tables.  Gweights is one of ascii, Rascii..
     68  1.1  bjh21  * modified to weight rec_d = 0 (or 255)
     69  1.1  bjh21  */
     70  1.1  bjh21 extern u_char gweights[NBINS];
     71  1.1  bjh21 u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
     72  1.1  bjh21 /*
     73  1.1  bjh21  * masks of ignored characters.  Alltable is 256 ones
     74  1.1  bjh21  */
     75  1.1  bjh21 u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
     76  1.1  bjh21 int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
     77  1.1  bjh21 struct coldesc clist[(ND+1)*2];
     78  1.1  bjh21 int ncols = 0;
     79  1.1  bjh21 extern struct coldesc clist[(ND+1)*2];
     80  1.1  bjh21 extern int ncols;
     81  1.1  bjh21 
     82  1.1  bjh21 char devstdin[] = _PATH_STDIN;
     83  1.1  bjh21 char toutpath[_POSIX_PATH_MAX];
     84  1.3  bjh21 char *tmpdir = _PATH_VARTMP;
     85  1.1  bjh21 
     86  1.1  bjh21 static void cleanup __P((void));
     87  1.2  bjh21 static void onsignal __P((int));
     88  1.1  bjh21 static void usage __P((char *));
     89  1.1  bjh21 
     90  1.2  bjh21 int main __P((int argc, char **argv));
     91  1.2  bjh21 
     92  1.1  bjh21 int
     93  1.1  bjh21 main(argc, argv)
     94  1.1  bjh21 	int argc;
     95  1.1  bjh21 	char *argv[];
     96  1.1  bjh21 {
     97  1.1  bjh21 	extern int optind;
     98  1.1  bjh21 	extern char *optarg;
     99  1.2  bjh21 	int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
    100  1.2  bjh21 	    struct field *));
    101  1.1  bjh21 	int ch, i, stdinflag = 0, tmp = 0;
    102  1.1  bjh21 	char cflag = 0, mflag = 0, nflag = 0;
    103  1.1  bjh21 	char *outfile, *outpath = 0;
    104  1.1  bjh21 	struct field fldtab[ND+2], *ftpos;
    105  1.1  bjh21 	union f_handle filelist;
    106  1.3  bjh21 	FILE *outfp = NULL;
    107  1.1  bjh21 	memset(fldtab, 0, (ND+2)*sizeof(struct field));
    108  1.1  bjh21 	memset(d_mask, 0, NBINS);
    109  1.1  bjh21 	d_mask[REC_D = '\n'] = REC_D_F;
    110  1.1  bjh21 	SINGL_FLD = SEP_FLAG = 0;
    111  1.1  bjh21 	d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
    112  1.1  bjh21 	ftpos = fldtab;
    113  1.1  bjh21 	fixit(&argc, argv);
    114  1.1  bjh21 	while ((ch = getopt(argc, argv, "bcdfik:mHno:rt:T:ux")) != EOF) {
    115  1.3  bjh21 	if ((outfile = getenv("TMPDIR")))
    116  1.3  bjh21 		tmpdir = outfile;
    117  1.1  bjh21 	switch (ch) {
    118  1.1  bjh21 		case 'b': fldtab->flags |= BI | BT;
    119  1.1  bjh21 			break;
    120  1.1  bjh21 		case 'd':
    121  1.1  bjh21 		case 'i':
    122  1.1  bjh21 		case 'f':
    123  1.1  bjh21 		case 'r': tmp |= optval(ch, 0);
    124  1.1  bjh21 			if (tmp & R && tmp & F)
    125  1.1  bjh21 				fldtab->weights = RFtable;
    126  1.1  bjh21 			else if (tmp & F)
    127  1.1  bjh21 				fldtab->weights = Ftable;
    128  1.1  bjh21 			else if(tmp & R)
    129  1.1  bjh21 				fldtab->weights = Rascii;
    130  1.1  bjh21 			fldtab->flags |= tmp;
    131  1.1  bjh21 			break;
    132  1.1  bjh21 		case 'o':
    133  1.1  bjh21 			outpath = optarg;
    134  1.1  bjh21 			break;
    135  1.1  bjh21 		case 'n':
    136  1.1  bjh21 			nflag = 1;
    137  1.1  bjh21 			setfield("1n", ++ftpos, fldtab->flags&(~R));
    138  1.1  bjh21 			break;
    139  1.1  bjh21 		case 'k':
    140  1.1  bjh21 			 setfield(optarg, ++ftpos, fldtab->flags);
    141  1.1  bjh21 			break;
    142  1.1  bjh21 		case 't':
    143  1.1  bjh21 			if (SEP_FLAG)
    144  1.1  bjh21 				usage("multiple field delimiters");
    145  1.1  bjh21 			SEP_FLAG = 1;
    146  1.1  bjh21 			d_mask[' '] &= ~FLD_D;
    147  1.1  bjh21 			d_mask['\t'] &= ~FLD_D;
    148  1.2  bjh21 			d_mask[(u_char)*optarg] |= FLD_D;
    149  1.2  bjh21 			if (d_mask[(u_char)*optarg] & REC_D_F)
    150  1.1  bjh21 				err(2, "record/field delimiter clash");
    151  1.1  bjh21 			break;
    152  1.1  bjh21 		case 'T':
    153  1.1  bjh21 			if (REC_D != '\n')
    154  1.1  bjh21 				usage("multiple record delimiters");
    155  1.1  bjh21 			if ('\n' == (REC_D = *optarg))
    156  1.1  bjh21 				break;
    157  1.1  bjh21 			d_mask['\n'] = d_mask[' '];
    158  1.1  bjh21 			d_mask[REC_D] = REC_D_F;
    159  1.1  bjh21 			break;
    160  1.1  bjh21 		case 'u':
    161  1.1  bjh21 			UNIQUE = 1;
    162  1.1  bjh21 			break;
    163  1.1  bjh21 		case 'c':
    164  1.1  bjh21 			cflag = 1;
    165  1.1  bjh21 			break;
    166  1.1  bjh21 		case 'm':
    167  1.1  bjh21 			mflag = 1;
    168  1.1  bjh21 			break;
    169  1.1  bjh21 		case 'H':
    170  1.1  bjh21 			PANIC = 0;
    171  1.1  bjh21 			break;
    172  1.1  bjh21 		case '?':
    173  1.1  bjh21 		default: usage("");
    174  1.1  bjh21 		}
    175  1.1  bjh21 	}
    176  1.1  bjh21 	if (cflag && argc > optind+1)
    177  1.1  bjh21 		errx(2, "too many input files for -c option");
    178  1.1  bjh21 	if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
    179  1.1  bjh21 		outpath = argv[argc-1];
    180  1.1  bjh21 		argc -= 2;
    181  1.1  bjh21 	}
    182  1.1  bjh21 	if (mflag && argc - optind > (MAXFCT - (16+1))*16)
    183  1.1  bjh21 		errx(2, "too many input files for -m option");
    184  1.1  bjh21 	for (i = optind; i < argc; i++) {
    185  1.1  bjh21 		/* allow one occurrence of /dev/stdin */
    186  1.1  bjh21 		if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
    187  1.1  bjh21 			if (stdinflag)
    188  1.1  bjh21 				warnx("ignoring extra \"%s\" in file list",
    189  1.1  bjh21 				    argv[i]);
    190  1.1  bjh21 			else {
    191  1.1  bjh21 				stdinflag = 1;
    192  1.1  bjh21 				argv[i] = devstdin;
    193  1.1  bjh21 			}
    194  1.2  bjh21 		} else if ((ch = access(argv[i], R_OK)))
    195  1.4  bjh21 			err(2, argv[i]);
    196  1.1  bjh21 	}
    197  1.1  bjh21 	if (!(fldtab->flags & (I|D) || fldtab[1].icol.num)) {
    198  1.1  bjh21 		SINGL_FLD = 1;
    199  1.1  bjh21 		fldtab[0].icol.num = 1;
    200  1.1  bjh21 	} else {
    201  1.1  bjh21 		if (!fldtab[1].icol.num) {
    202  1.1  bjh21 			fldtab[0].flags &= ~(BI|BT);
    203  1.1  bjh21 			setfield("1", ++ftpos, fldtab->flags);
    204  1.1  bjh21 		}
    205  1.1  bjh21 		if (nflag)
    206  1.1  bjh21 			fldtab[1].flags |= fldtab->flags;
    207  1.1  bjh21 		fldreset(fldtab);
    208  1.1  bjh21 		fldtab[0].flags &= ~F;
    209  1.1  bjh21 	}
    210  1.1  bjh21 	settables(fldtab[0].flags);
    211  1.1  bjh21 	num_init();
    212  1.1  bjh21 	fldtab->weights = gweights;
    213  1.5  bjh21 	if (optind == argc) {
    214  1.5  bjh21 		static char *names[2];
    215  1.5  bjh21 
    216  1.5  bjh21 		names[0] = devstdin;
    217  1.5  bjh21 		names[1] = NULL;
    218  1.5  bjh21 		filelist.names = names;
    219  1.5  bjh21 		optind--;
    220  1.5  bjh21 	} else
    221  1.5  bjh21 		filelist.names = argv+optind;
    222  1.1  bjh21 	if (SINGL_FLD)
    223  1.1  bjh21 		get = makeline;
    224  1.1  bjh21 	else
    225  1.1  bjh21 		get = makekey;
    226  1.1  bjh21 	if (cflag) {
    227  1.1  bjh21 		order(filelist, get, fldtab);
    228  1.1  bjh21 		/* NOT REACHED */
    229  1.1  bjh21 	}
    230  1.1  bjh21 	if (!outpath) {
    231  1.1  bjh21 		(void)snprintf(toutpath,
    232  1.1  bjh21 		    sizeof(toutpath), "%sstdout", _PATH_DEV);
    233  1.1  bjh21 		outfile = outpath = toutpath;
    234  1.1  bjh21 	} else if (!(ch = access(outpath, 0)) &&
    235  1.1  bjh21 	    strncmp(_PATH_DEV, outpath, 5)) {
    236  1.2  bjh21 		struct sigaction act = {0};
    237  1.1  bjh21 		int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
    238  1.1  bjh21 		    SIGVTALRM, SIGPROF, 0};
    239  1.3  bjh21 		int outfd;
    240  1.1  bjh21 		errno = 0;
    241  1.1  bjh21 		if (access(outpath, W_OK))
    242  1.1  bjh21 			err(2, "%s", outpath);
    243  1.2  bjh21 		act.sa_handler = onsignal;
    244  1.2  bjh21 		act.sa_flags = SA_RESTART | SA_RESETHAND;
    245  1.1  bjh21 		(void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
    246  1.3  bjh21 		if ((outfd = mkstemp(toutpath)) < 0 ||
    247  1.3  bjh21 		    (outfp = fdopen(outfd, "w")) == 0)
    248  1.3  bjh21 			err(2, toutpath);
    249  1.3  bjh21 		outfile = toutpath;
    250  1.1  bjh21 		(void)atexit(cleanup);
    251  1.1  bjh21 		for (i = 0; sigtable[i]; ++i)	/* always unlink toutpath */
    252  1.1  bjh21 			sigaction(sigtable[i], &act, 0);
    253  1.3  bjh21 	} else
    254  1.3  bjh21 		outfile = outpath;
    255  1.3  bjh21 	if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
    256  1.3  bjh21 		err(2, outfile);
    257  1.1  bjh21 	if (mflag)
    258  1.3  bjh21 		fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
    259  1.1  bjh21 	else
    260  1.3  bjh21 		fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
    261  1.1  bjh21 	if (outfile != outpath) {
    262  1.1  bjh21 		if (access(outfile, 0))
    263  1.4  bjh21 			err(2, outfile);
    264  1.1  bjh21 		(void)unlink(outpath);
    265  1.1  bjh21 		if (link(outfile, outpath))
    266  1.1  bjh21 			err(2, "cannot link %s: output left in %s",
    267  1.1  bjh21 			    outpath, outfile);
    268  1.1  bjh21 		(void)unlink(outfile);
    269  1.1  bjh21 	}
    270  1.1  bjh21 	exit(0);
    271  1.1  bjh21 }
    272  1.1  bjh21 
    273  1.1  bjh21 static void
    274  1.2  bjh21 onsignal(sig)
    275  1.2  bjh21 	int sig;
    276  1.1  bjh21 {
    277  1.1  bjh21 	cleanup();
    278  1.1  bjh21 }
    279  1.1  bjh21 
    280  1.1  bjh21 static void
    281  1.1  bjh21 cleanup()
    282  1.1  bjh21 {
    283  1.1  bjh21 	if (toutpath[0])
    284  1.1  bjh21 		(void)unlink(toutpath);
    285  1.1  bjh21 }
    286  1.1  bjh21 
    287  1.1  bjh21 static void
    288  1.1  bjh21 usage(msg)
    289  1.1  bjh21 	char *msg;
    290  1.1  bjh21 {
    291  1.1  bjh21 	if (msg)
    292  1.1  bjh21 		(void)fprintf(stderr, "sort: %s\n", msg);
    293  1.1  bjh21 	(void)fprintf(stderr, "usage: [-o output] [-cmubdfinr] [-t char] ");
    294  1.1  bjh21 	(void)fprintf(stderr, "[-T char] [-k keydef] ... [files]\n");
    295  1.1  bjh21 	exit(2);
    296  1.1  bjh21 }
    297