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