1 /* $NetBSD: sort.c,v 1.59 2010/06/05 17:44:51 dholland Exp $ */
4 * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ben Harris and Jaromir Dolecek.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 /* Sort sorts a file using an optional user-defined key.
65 * Sort uses radix sort for internal sorting, and allows
66 * a choice of merge sort and radix sort for external sorting.
72 #include "pathnames.h"
75 __COPYRIGHT("@(#) Copyright (c) 1993\
76 The Regents of the University of California. All rights reserved.");
79 __RCSID("$NetBSD: sort.c,v 1.59 2010/06/05 17:44:51 dholland Exp $");
81 #include <sys/types.h>
83 #include <sys/resource.h>
93 u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */
96 * weight tables. Gweights is one of ascii, Rascii..
97 * modified to weight rec_d = 0 (or 255)
99 u_char *const weight_tables[4] = { ascii, Rascii, Ftable, RFtable };
100 u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
102 int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
106 unsigned int debug_flags = 0;
108 static char toutpath[MAXPATHLEN];
110 const char *tmpdir; /* where temporary files should be put */
112 static void cleanup(void);
113 static void onsignal(int);
114 static void usage(const char *);
116 int main(int argc, char **argv);
119 main(int argc, char *argv[])
121 int ch, i, stdinflag = 0;
122 char cflag = 0, mflag = 0;
123 char *outfile, *outpath = 0;
124 struct field *fldtab;
125 size_t fldtab_sz, fld_cnt;
126 struct filelist filelist;
132 setlocale(LC_ALL, "");
134 /* bump RLIMIT_NOFILE to maximum our hard limit allows */
135 if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
137 rl.rlim_cur = rl.rlim_max;
138 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
141 d_mask[REC_D = '\n'] = REC_D_F;
142 d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
144 /* fldtab[0] is the global options. */
147 fldtab = emalloc(fldtab_sz * sizeof(*fldtab));
148 memset(fldtab, 0, fldtab_sz * sizeof(*fldtab));
150 #define SORT_OPTS "bcdD:fHik:lmno:rR:sSt:T:ux"
152 /* Convert "+field" args to -f format */
153 fixit(&argc, argv, SORT_OPTS);
155 if (!(tmpdir = getenv("TMPDIR")))
158 while ((ch = getopt(argc, argv, SORT_OPTS)) != -1) {
161 fldtab[0].flags |= BI | BT;
166 case 'D': /* Debug flags */
167 for (i = 0; optarg[i]; i++)
168 debug_flags |= 1 << (optarg[i] & 31);
170 case 'd': case 'f': case 'i': case 'n': case 'l':
171 fldtab[0].flags |= optval(ch, 0);
174 /* -H was ; use merge sort for blocks of large files' */
175 /* That is now the default. */
178 fldtab = erealloc(fldtab, (fldtab_sz + 1) * sizeof(*fldtab));
179 memset(&fldtab[fldtab_sz], 0, sizeof(fldtab[0]));
182 setfield(optarg, &fldtab[++fld_cnt], fldtab[0].flags);
195 * Nominally 'stable sort', keep lines with equal keys
196 * in input file order. (Default for NetBSD)
197 * (-s for GNU sort compatibility.)
204 * This needs to enforce a POSIX sort where records
205 * with equal keys are then sorted by the raw data.
206 * Currently not implemented!
207 * (using libc radixsort() v sradixsort() doesn't
208 * have the desired effect.)
214 usage("multiple field delimiters");
216 d_mask[' '] &= ~FLD_D;
217 d_mask['\t'] &= ~FLD_D;
218 d_mask[(u_char)*optarg] |= FLD_D;
219 if (d_mask[(u_char)*optarg] & REC_D_F)
220 errx(2, "record/field delimiter clash");
224 usage("multiple record delimiters");
228 if (optarg[1] != '\0') {
231 if (optarg[0] == '\\')
233 REC_D = (int)strtol(optarg, &ep, t);
234 if (*ep != '\0' || REC_D < 0 ||
235 REC_D >= (int)__arraycount(d_mask))
236 errx(2, "invalid record delimiter %s",
239 d_mask['\n'] = d_mask[' '];
240 d_mask[REC_D] = REC_D_F;
256 /* Don't sort on raw record if keys match */
259 if (cflag && argc > optind+1)
260 errx(2, "too many input files for -c option");
261 if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
262 outpath = argv[argc-1];
265 if (mflag && argc - optind > (MAXFCT - (16+1))*16)
266 errx(2, "too many input files for -m option");
268 for (i = optind; i < argc; i++) {
269 /* allow one occurrence of /dev/stdin */
270 if (!strcmp(argv[i], "-") || !strcmp(argv[i], _PATH_STDIN)) {
272 warnx("ignoring extra \"%s\" in file list",
277 /* change to /dev/stdin if '-' */
278 if (argv[i][0] == '-') {
279 static char path_stdin[] = _PATH_STDIN;
280 argv[i] = path_stdin;
283 } else if ((ch = access(argv[i], R_OK)))
284 err(2, "%s", argv[i]);
287 if (fldtab[1].icol.num == 0) {
288 /* No sort key specified */
289 if (fldtab[0].flags & (I|D|F|N|L)) {
290 /* Modified - generate a key that covers the line */
291 fldtab[0].flags &= ~(BI|BT);
292 setfield("1", &fldtab[++fld_cnt], fldtab->flags);
295 /* Unmodified, just compare the line */
297 fldtab[0].icol.num = 1;
305 if (optind == argc) {
306 static const char * const names[] = { _PATH_STDIN, NULL };
307 filelist.names = names;
310 filelist.names = (const char * const *) &argv[optind];
311 num_input_files = argc - optind;
315 order(&filelist, fldtab);
320 toutpath[0] = '\0'; /* path not used in this case */
321 outfile = outpath = toutpath;
323 } else if (lstat(outpath, &st) == 0
324 && !S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
325 /* output file exists and isn't character or block device */
326 struct sigaction act;
327 static const int sigtable[] = {SIGHUP, SIGINT, SIGPIPE,
328 SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, 0};
331 if (access(outpath, W_OK))
332 err(2, "%s", outpath);
333 (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXX",
335 if ((outfd = mkstemp(toutpath)) == -1)
336 err(2, "Cannot create temporary file `%s'", toutpath);
337 (void)atexit(cleanup);
338 act.sa_handler = onsignal;
339 (void) sigemptyset(&act.sa_mask);
340 act.sa_flags = SA_RESTART | SA_RESETHAND;
341 for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
342 sigaction(sigtable[i], &act, 0);
344 if ((outfp = fdopen(outfd, "w")) == NULL)
345 err(2, "Cannot open temporary file `%s'", toutpath);
349 if ((outfp = fopen(outfile, "w")) == NULL)
350 err(2, "output file %s", outfile);
354 fmerge(&filelist, num_input_files, outfp, fldtab);
356 fsort(&filelist, num_input_files, outfp, fldtab);
358 if (outfile != outpath) {
359 if (access(outfile, F_OK))
360 err(2, "%s", outfile);
363 * Copy file permissions bits of the original file.
364 * st is initialized above, when we create the
365 * temporary spool file.
367 if (lchmod(outfile, st.st_mode & ALLPERMS) != 0) {
368 err(2, "cannot chmod %s: output left in %s",
372 (void)unlink(outpath);
373 if (link(outfile, outpath))
374 err(2, "cannot link %s: output left in %s",
376 (void)unlink(outfile);
392 (void)unlink(toutpath);
396 usage(const char *msg)
399 (void)fprintf(stderr, "%s: %s\n", getprogname(), msg);
400 (void)fprintf(stderr,
401 "usage: %s [-bcdfHilmnrSsu] [-k field1[,field2]] [-o output]"
402 " [-R char] [-T dir]", getprogname());
403 (void)fprintf(stderr,
404 " [-t char] [file ...]\n");
409 allocrec(RECHEADER *rec, size_t size)
412 return (erealloc(rec, size + sizeof(long) - 1));