Home | History | Annotate | Line # | Download | only in sort
sort.c revision 1.33
      1 /*	$NetBSD: sort.c,v 1.33 2003/08/07 11:15:55 agc Exp $	*/
      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.
     18  * 3. Neither the name of the University nor the names of its contributors
     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 /* Sort sorts a file using an optional user-defined key.
     36  * Sort uses radix sort for internal sorting, and allows
     37  * a choice of merge sort and radix sort for external sorting.
     38  */
     39 
     40 #include "sort.h"
     41 #include "fsort.h"
     42 #include "pathnames.h"
     43 
     44 #ifndef lint
     45 __COPYRIGHT("@(#) Copyright (c) 1993\n\
     46 	The Regents of the University of California.  All rights reserved.\n");
     47 #endif /* not lint */
     48 
     49 #ifndef lint
     50 __RCSID("$NetBSD: sort.c,v 1.33 2003/08/07 11:15:55 agc Exp $");
     51 __SCCSID("@(#)sort.c	8.1 (Berkeley) 6/6/93");
     52 #endif /* not lint */
     53 
     54 #include <sys/types.h>
     55 #include <sys/time.h>
     56 #include <sys/resource.h>
     57 
     58 #include <paths.h>
     59 #include <signal.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 #include <locale.h>
     64 
     65 int REC_D = '\n';
     66 u_char d_mask[NBINS];		/* flags for rec_d, field_d, <blank> */
     67 
     68 /*
     69  * weight tables.  Gweights is one of ascii, Rascii..
     70  * modified to weight rec_d = 0 (or 255)
     71  */
     72 u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
     73 int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
     74 struct coldesc clist[(ND+1)*2];
     75 int ncols = 0;
     76 
     77 /*
     78  * Default to stable sort.
     79  */
     80 int stable_sort = 1;
     81 
     82 static char toutpath[MAXPATHLEN];
     83 static struct field fldtab[ND+2];
     84 
     85 const char *tmpdir;	/* where temporary files should be put */
     86 
     87 static void cleanup __P((void));
     88 static void onsignal __P((int));
     89 static void usage __P((const char *));
     90 
     91 int main __P((int argc, char **argv));
     92 
     93 int
     94 main(argc, argv)
     95 	int argc;
     96 	char *argv[];
     97 {
     98 	get_func_t get;
     99 	int ch, i, stdinflag = 0, tmp = 0;
    100 	char cflag = 0, mflag = 0;
    101 	char *outfile, *outpath = 0;
    102 	struct field *ftpos;
    103 	struct filelist filelist;
    104 	FILE *outfp = NULL;
    105 	struct rlimit rl;
    106 
    107 	setlocale(LC_ALL, "");
    108 
    109 	/* bump RLIMIT_NOFILE to maximum our hard limit allows */
    110 	if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
    111 		err(2, "getrlimit");
    112 	rl.rlim_cur = rl.rlim_max;
    113 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
    114 		err(2, "setrlimit");
    115 
    116 	d_mask[REC_D = '\n'] = REC_D_F;
    117 	SINGL_FLD = SEP_FLAG = 0;
    118 	d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
    119 	ftpos = fldtab;
    120 
    121 	fixit(&argc, argv);
    122 	if (!(tmpdir = getenv("TMPDIR")))
    123 		tmpdir = _PATH_TMP;
    124 
    125 	while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:sSt:T:ux")) != -1) {
    126 		switch (ch) {
    127 		case 'b':
    128 			fldtab->flags |= BI | BT;
    129 			break;
    130 		case 'c':
    131 			cflag = 1;
    132 			break;
    133 		case 'd': case 'f': case 'i': case 'n': case 'r':
    134 			tmp |= optval(ch, 0);
    135 			if ((tmp & R) && (tmp & F))
    136 				fldtab->weights = RFtable;
    137 			else if (tmp & F)
    138 				fldtab->weights = Ftable;
    139 			else if (tmp & R)
    140 				fldtab->weights = Rascii;
    141 			fldtab->flags |= tmp;
    142 			break;
    143 		case 'H':
    144 			PANIC = 0;
    145 			break;
    146 		case 'k':
    147 			setfield(optarg, ++ftpos, fldtab->flags);
    148 			break;
    149 		case 'm':
    150 			mflag = 1;
    151 			break;
    152 		case 'o':
    153 			outpath = optarg;
    154 			break;
    155 		case 's':
    156 			/* for GNU sort compatibility (this is our default) */
    157 			stable_sort = 1;
    158 			break;
    159 		case 'S':
    160 			stable_sort = 0;
    161 			break;
    162 		case 't':
    163 			if (SEP_FLAG)
    164 				usage("multiple field delimiters");
    165 			SEP_FLAG = 1;
    166 			d_mask[' '] &= ~FLD_D;
    167 			d_mask['\t'] &= ~FLD_D;
    168 			d_mask[(u_char)*optarg] |= FLD_D;
    169 			if (d_mask[(u_char)*optarg] & REC_D_F)
    170 				errx(2, "record/field delimiter clash");
    171 			break;
    172 		case 'R':
    173 			if (REC_D != '\n')
    174 				usage("multiple record delimiters");
    175 			if ('\n' == (REC_D = *optarg))
    176 				break;
    177 			d_mask['\n'] = d_mask[' '];
    178 			d_mask[REC_D] = REC_D_F;
    179 			break;
    180 		case 'T':
    181 			/* -T tmpdir */
    182 			tmpdir = optarg;
    183 			break;
    184 		case 'u':
    185 			UNIQUE = 1;
    186 			break;
    187 		case '?':
    188 		default:
    189 			usage(NULL);
    190 		}
    191 	}
    192 	if (cflag && argc > optind+1)
    193 		errx(2, "too many input files for -c option");
    194 	if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
    195 		outpath = argv[argc-1];
    196 		argc -= 2;
    197 	}
    198 	if (mflag && argc - optind > (MAXFCT - (16+1))*16)
    199 		errx(2, "too many input files for -m option");
    200 	for (i = optind; i < argc; i++) {
    201 		/* allow one occurrence of /dev/stdin */
    202 		if (!strcmp(argv[i], "-") || !strcmp(argv[i], _PATH_STDIN)) {
    203 			if (stdinflag)
    204 				warnx("ignoring extra \"%s\" in file list",
    205 				    argv[i]);
    206 			else
    207 				stdinflag = 1;
    208 
    209 			/* change to /dev/stdin if '-' */
    210 			if (argv[i][0] == '-')
    211 				argv[i] = _PATH_STDIN;
    212 
    213 		} else if ((ch = access(argv[i], R_OK)))
    214 			err(2, "%s", argv[i]);
    215 	}
    216 	if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
    217 		SINGL_FLD = 1;
    218 		fldtab[0].icol.num = 1;
    219 	} else {
    220 		if (!fldtab[1].icol.num) {
    221 			fldtab[0].flags &= ~(BI|BT);
    222 			setfield("1", ++ftpos, fldtab->flags);
    223 		}
    224 		fldreset(fldtab);
    225 		fldtab[0].flags &= ~F;
    226 	}
    227 	settables(fldtab[0].flags);
    228 	num_init();
    229 	fldtab->weights = gweights;
    230 	if (optind == argc) {
    231 		static const char * const names[] = { _PATH_STDIN, NULL };
    232 
    233 		filelist.names = names;
    234 		optind--;
    235 	} else
    236 		filelist.names = (const char * const *) &argv[optind];
    237 
    238 	if (SINGL_FLD)
    239 		get = makeline;
    240 	else
    241 		get = makekey;
    242 
    243 	if (cflag) {
    244 		order(&filelist, get, fldtab);
    245 		/* NOT REACHED */
    246 	}
    247 	if (!outpath) {
    248 		(void)snprintf(toutpath,
    249 		    sizeof(toutpath), "%sstdout", _PATH_DEV);
    250 		outfile = outpath = toutpath;
    251 		outfp = stdout;
    252 	} else if (!(ch = access(outpath, 0)) &&
    253 	    strncmp(_PATH_DEV, outpath, 5)) {
    254 		struct sigaction act;
    255 		static const int sigtable[] = {SIGHUP, SIGINT, SIGPIPE,
    256 		    SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, 0};
    257 		int outfd;
    258 		errno = 0;
    259 		if (access(outpath, W_OK))
    260 			err(2, "%s", outpath);
    261 		(void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXX",
    262 		    outpath);
    263 		if ((outfd = mkstemp(toutpath)) == -1)
    264 			err(2, "Cannot create temporary file `%s'", toutpath);
    265 		if ((outfp = fdopen(outfd, "w")) == NULL)
    266 			err(2, "Cannot open temporary file `%s'", toutpath);
    267 		outfile = toutpath;
    268 		(void)atexit(cleanup);
    269 		act.sa_handler = onsignal;
    270 		(void) sigemptyset(&act.sa_mask);
    271 		act.sa_flags = SA_RESTART | SA_RESETHAND;
    272 		for (i = 0; sigtable[i]; ++i)	/* always unlink toutpath */
    273 			sigaction(sigtable[i], &act, 0);
    274 	} else
    275 		outfile = outpath;
    276 
    277 	if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
    278 		err(2, "output file %s", outfile);
    279 
    280 	if (mflag) {
    281 		fmerge(-1, 0, &filelist, argc-optind, get, outfp, putline,
    282 			fldtab);
    283 	} else
    284 		fsort(-1, 0, 0, &filelist, argc-optind, outfp, fldtab);
    285 
    286 	if (outfile != outpath) {
    287 		if (access(outfile, 0))
    288 			err(2, "%s", outfile);
    289 		(void)unlink(outpath);
    290 		if (link(outfile, outpath))
    291 			err(2, "cannot link %s: output left in %s",
    292 			    outpath, outfile);
    293 		(void)unlink(outfile);
    294 	}
    295 	exit(0);
    296 }
    297 
    298 static void
    299 onsignal(sig)
    300 	int sig;
    301 {
    302 	cleanup();
    303 }
    304 
    305 static void
    306 cleanup()
    307 {
    308 	if (toutpath[0])
    309 		(void)unlink(toutpath);
    310 }
    311 
    312 static void
    313 usage(msg)
    314 	const char *msg;
    315 {
    316 	if (msg != NULL)
    317 		(void)fprintf(stderr, "sort: %s\n", msg);
    318 	(void)fprintf(stderr, "usage: [-o output] [-cmubdfinrsS] [-t char] ");
    319 	(void)fprintf(stderr, "[-R char] [-k keydef] ... [files]\n");
    320 	exit(2);
    321 }
    322