Home | History | Annotate | Line # | Download | only in sort
sort.c revision 1.37
      1 /*	$NetBSD: sort.c,v 1.37 2004/02/17 02:38:47 itojun Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Ben Harris and Jaromir Dolecek.
      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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Peter McIlroy.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 /* Sort sorts a file using an optional user-defined key.
     72  * Sort uses radix sort for internal sorting, and allows
     73  * a choice of merge sort and radix sort for external sorting.
     74  */
     75 
     76 #include "sort.h"
     77 #include "fsort.h"
     78 #include "pathnames.h"
     79 
     80 #ifndef lint
     81 __COPYRIGHT("@(#) Copyright (c) 1993\n\
     82 	The Regents of the University of California.  All rights reserved.\n");
     83 #endif /* not lint */
     84 
     85 #ifndef lint
     86 __RCSID("$NetBSD: sort.c,v 1.37 2004/02/17 02:38:47 itojun Exp $");
     87 __SCCSID("@(#)sort.c	8.1 (Berkeley) 6/6/93");
     88 #endif /* not lint */
     89 
     90 #include <sys/types.h>
     91 #include <sys/time.h>
     92 #include <sys/resource.h>
     93 
     94 #include <paths.h>
     95 #include <signal.h>
     96 #include <stdlib.h>
     97 #include <string.h>
     98 #include <unistd.h>
     99 #include <locale.h>
    100 
    101 int REC_D = '\n';
    102 u_char d_mask[NBINS];		/* flags for rec_d, field_d, <blank> */
    103 
    104 /*
    105  * weight tables.  Gweights is one of ascii, Rascii..
    106  * modified to weight rec_d = 0 (or 255)
    107  */
    108 u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
    109 int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
    110 
    111 /*
    112  * Default to stable sort.
    113  */
    114 int stable_sort = 1;
    115 
    116 static char toutpath[MAXPATHLEN];
    117 
    118 const char *tmpdir;	/* where temporary files should be put */
    119 
    120 static void cleanup __P((void));
    121 static void onsignal __P((int));
    122 static void usage __P((const char *));
    123 
    124 int main __P((int argc, char **argv));
    125 
    126 int
    127 main(argc, argv)
    128 	int argc;
    129 	char *argv[];
    130 {
    131 	get_func_t get;
    132 	int ch, i, stdinflag = 0, tmp = 0;
    133 	char cflag = 0, mflag = 0;
    134 	char *outfile, *outpath = 0;
    135 	struct field *fldtab, *ftpos, *p;
    136 	size_t fldtab_sz = 2;
    137 	struct filelist filelist;
    138 	FILE *outfp = NULL;
    139 	struct rlimit rl;
    140 
    141 	setlocale(LC_ALL, "");
    142 
    143 	/* bump RLIMIT_NOFILE to maximum our hard limit allows */
    144 	if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
    145 		err(2, "getrlimit");
    146 	rl.rlim_cur = rl.rlim_max;
    147 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
    148 		err(2, "setrlimit");
    149 
    150 	d_mask[REC_D = '\n'] = REC_D_F;
    151 	SINGL_FLD = SEP_FLAG = 0;
    152 	d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
    153 
    154 	fldtab = malloc(fldtab_sz * sizeof(*fldtab));
    155 	memset(fldtab, 0, fldtab_sz * sizeof(*fldtab));
    156 	ftpos = fldtab;
    157 
    158 	fixit(&argc, argv);
    159 
    160 	if (!(tmpdir = getenv("TMPDIR")))
    161 		tmpdir = _PATH_TMP;
    162 
    163 	while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:sSt:T:ux")) != -1) {
    164 		switch (ch) {
    165 		case 'b':
    166 			fldtab->flags |= BI | BT;
    167 			break;
    168 		case 'c':
    169 			cflag = 1;
    170 			break;
    171 		case 'd': case 'f': case 'i': case 'n': case 'r':
    172 			tmp |= optval(ch, 0);
    173 			if ((tmp & R) && (tmp & F))
    174 				fldtab->weights = RFtable;
    175 			else if (tmp & F)
    176 				fldtab->weights = Ftable;
    177 			else if (tmp & R)
    178 				fldtab->weights = Rascii;
    179 			fldtab->flags |= tmp;
    180 			break;
    181 		case 'H':
    182 			PANIC = 0;
    183 			break;
    184 		case 'k':
    185 			p = realloc(fldtab, (fldtab_sz + 1) * sizeof(*fldtab));
    186 			if (!p)
    187 				err(1, "realloc");
    188 			fldtab = p;
    189 			memset(&fldtab[fldtab_sz], 0,
    190 			    sizeof(fldtab[fldtab_sz]));
    191 			fldtab_sz++;
    192 
    193 			setfield(optarg, ++ftpos, fldtab->flags);
    194 			break;
    195 		case 'm':
    196 			mflag = 1;
    197 			break;
    198 		case 'o':
    199 			outpath = optarg;
    200 			break;
    201 		case 's':
    202 			/* for GNU sort compatibility (this is our default) */
    203 			stable_sort = 1;
    204 			break;
    205 		case 'S':
    206 			stable_sort = 0;
    207 			break;
    208 		case 't':
    209 			if (SEP_FLAG)
    210 				usage("multiple field delimiters");
    211 			SEP_FLAG = 1;
    212 			d_mask[' '] &= ~FLD_D;
    213 			d_mask['\t'] &= ~FLD_D;
    214 			d_mask[(u_char)*optarg] |= FLD_D;
    215 			if (d_mask[(u_char)*optarg] & REC_D_F)
    216 				errx(2, "record/field delimiter clash");
    217 			break;
    218 		case 'R':
    219 			if (REC_D != '\n')
    220 				usage("multiple record delimiters");
    221 			if ('\n' == (REC_D = *optarg))
    222 				break;
    223 			d_mask['\n'] = d_mask[' '];
    224 			d_mask[REC_D] = REC_D_F;
    225 			break;
    226 		case 'T':
    227 			/* -T tmpdir */
    228 			tmpdir = optarg;
    229 			break;
    230 		case 'u':
    231 			UNIQUE = 1;
    232 			break;
    233 		case '?':
    234 		default:
    235 			usage(NULL);
    236 		}
    237 	}
    238 	if (cflag && argc > optind+1)
    239 		errx(2, "too many input files for -c option");
    240 	if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
    241 		outpath = argv[argc-1];
    242 		argc -= 2;
    243 	}
    244 	if (mflag && argc - optind > (MAXFCT - (16+1))*16)
    245 		errx(2, "too many input files for -m option");
    246 	for (i = optind; i < argc; i++) {
    247 		/* allow one occurrence of /dev/stdin */
    248 		if (!strcmp(argv[i], "-") || !strcmp(argv[i], _PATH_STDIN)) {
    249 			if (stdinflag)
    250 				warnx("ignoring extra \"%s\" in file list",
    251 				    argv[i]);
    252 			else
    253 				stdinflag = 1;
    254 
    255 			/* change to /dev/stdin if '-' */
    256 			if (argv[i][0] == '-')
    257 				argv[i] = _PATH_STDIN;
    258 
    259 		} else if ((ch = access(argv[i], R_OK)))
    260 			err(2, "%s", argv[i]);
    261 	}
    262 	if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
    263 		SINGL_FLD = 1;
    264 		fldtab[0].icol.num = 1;
    265 	} else {
    266 		if (!fldtab[1].icol.num) {
    267 			fldtab[0].flags &= ~(BI|BT);
    268 			setfield("1", ++ftpos, fldtab->flags);
    269 		}
    270 		fldreset(fldtab);
    271 		fldtab[0].flags &= ~F;
    272 	}
    273 	settables(fldtab[0].flags);
    274 	num_init();
    275 	fldtab->weights = gweights;
    276 	if (optind == argc) {
    277 		static const char * const names[] = { _PATH_STDIN, NULL };
    278 
    279 		filelist.names = names;
    280 		optind--;
    281 	} else
    282 		filelist.names = (const char * const *) &argv[optind];
    283 
    284 	if (SINGL_FLD)
    285 		get = makeline;
    286 	else
    287 		get = makekey;
    288 
    289 	if (cflag) {
    290 		order(&filelist, get, fldtab);
    291 		/* NOT REACHED */
    292 	}
    293 	if (!outpath) {
    294 		(void)snprintf(toutpath,
    295 		    sizeof(toutpath), "%sstdout", _PATH_DEV);
    296 		outfile = outpath = toutpath;
    297 		outfp = stdout;
    298 	} else if (!(ch = access(outpath, 0)) &&
    299 	    strncmp(_PATH_DEV, outpath, 5)) {
    300 		struct sigaction act;
    301 		static const int sigtable[] = {SIGHUP, SIGINT, SIGPIPE,
    302 		    SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, 0};
    303 		int outfd;
    304 		errno = 0;
    305 		if (access(outpath, W_OK))
    306 			err(2, "%s", outpath);
    307 		(void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXX",
    308 		    outpath);
    309 		if ((outfd = mkstemp(toutpath)) == -1)
    310 			err(2, "Cannot create temporary file `%s'", toutpath);
    311 		if ((outfp = fdopen(outfd, "w")) == NULL)
    312 			err(2, "Cannot open temporary file `%s'", toutpath);
    313 		outfile = toutpath;
    314 		(void)atexit(cleanup);
    315 		act.sa_handler = onsignal;
    316 		(void) sigemptyset(&act.sa_mask);
    317 		act.sa_flags = SA_RESTART | SA_RESETHAND;
    318 		for (i = 0; sigtable[i]; ++i)	/* always unlink toutpath */
    319 			sigaction(sigtable[i], &act, 0);
    320 	} else
    321 		outfile = outpath;
    322 
    323 	if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
    324 		err(2, "output file %s", outfile);
    325 
    326 	if (mflag) {
    327 		fmerge(-1, 0, &filelist, argc-optind, get, outfp, putline,
    328 			fldtab);
    329 	} else
    330 		fsort(-1, 0, 0, &filelist, argc-optind, outfp, fldtab);
    331 
    332 	if (outfile != outpath) {
    333 		if (access(outfile, 0))
    334 			err(2, "%s", outfile);
    335 		(void)unlink(outpath);
    336 		if (link(outfile, outpath))
    337 			err(2, "cannot link %s: output left in %s",
    338 			    outpath, outfile);
    339 		(void)unlink(outfile);
    340 	}
    341 	exit(0);
    342 }
    343 
    344 static void
    345 onsignal(sig)
    346 	int sig;
    347 {
    348 	cleanup();
    349 }
    350 
    351 static void
    352 cleanup()
    353 {
    354 	if (toutpath[0])
    355 		(void)unlink(toutpath);
    356 }
    357 
    358 static void
    359 usage(msg)
    360 	const char *msg;
    361 {
    362 	if (msg != NULL)
    363 		(void)fprintf(stderr, "sort: %s\n", msg);
    364 	(void)fprintf(stderr, "usage: [-o output] [-cmubdfinrsS] [-t char] ");
    365 	(void)fprintf(stderr, "[-R char] [-k keydef] ... [files]\n");
    366 	exit(2);
    367 }
    368