sort.c revision 1.21 1 1.21 jdolecek /* $NetBSD: sort.c,v 1.21 2001/02/07 20:58:09 jdolecek 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.21 jdolecek __RCSID("$NetBSD: sort.c,v 1.21 2001/02/07 20:58:09 jdolecek 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.11 jdolecek #include <locale.h>
64 1.1 bjh21
65 1.1 bjh21 int REC_D = '\n';
66 1.1 bjh21 u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */
67 1.1 bjh21 /*
68 1.1 bjh21 * weight tables. Gweights is one of ascii, Rascii..
69 1.1 bjh21 * modified to weight rec_d = 0 (or 255)
70 1.1 bjh21 */
71 1.1 bjh21 u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
72 1.1 bjh21 int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
73 1.1 bjh21 struct coldesc clist[(ND+1)*2];
74 1.1 bjh21 int ncols = 0;
75 1.1 bjh21 extern struct coldesc clist[(ND+1)*2];
76 1.1 bjh21 extern int ncols;
77 1.1 bjh21
78 1.9 jdolecek /*
79 1.9 jdolecek * Default to stable sort.
80 1.9 jdolecek */
81 1.9 jdolecek int stable_sort = 1;
82 1.9 jdolecek
83 1.1 bjh21 char toutpath[_POSIX_PATH_MAX];
84 1.10 jdolecek
85 1.1 bjh21 static void cleanup __P((void));
86 1.2 bjh21 static void onsignal __P((int));
87 1.8 jdolecek static void usage __P((const char *));
88 1.1 bjh21
89 1.2 bjh21 int main __P((int argc, char **argv));
90 1.2 bjh21
91 1.1 bjh21 int
92 1.1 bjh21 main(argc, argv)
93 1.1 bjh21 int argc;
94 1.1 bjh21 char *argv[];
95 1.1 bjh21 {
96 1.13 jdolecek get_func_t get;
97 1.1 bjh21 int ch, i, stdinflag = 0, tmp = 0;
98 1.6 bjh21 char cflag = 0, mflag = 0;
99 1.1 bjh21 char *outfile, *outpath = 0;
100 1.1 bjh21 struct field fldtab[ND+2], *ftpos;
101 1.13 jdolecek struct filelist filelist;
102 1.3 bjh21 FILE *outfp = NULL;
103 1.10 jdolecek
104 1.11 jdolecek setlocale(LC_ALL, "");
105 1.11 jdolecek
106 1.1 bjh21 memset(fldtab, 0, (ND+2)*sizeof(struct field));
107 1.1 bjh21 memset(d_mask, 0, NBINS);
108 1.1 bjh21 d_mask[REC_D = '\n'] = REC_D_F;
109 1.1 bjh21 SINGL_FLD = SEP_FLAG = 0;
110 1.1 bjh21 d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
111 1.1 bjh21 ftpos = fldtab;
112 1.10 jdolecek
113 1.1 bjh21 fixit(&argc, argv);
114 1.10 jdolecek
115 1.21 jdolecek while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:sSt:T:ux")) != -1) {
116 1.10 jdolecek switch (ch) {
117 1.11 jdolecek case 'b':
118 1.11 jdolecek fldtab->flags |= BI | BT;
119 1.11 jdolecek break;
120 1.11 jdolecek case 'c':
121 1.11 jdolecek cflag = 1;
122 1.1 bjh21 break;
123 1.11 jdolecek case 'd': case 'f': case 'i': case 'n': case 'r':
124 1.11 jdolecek tmp |= optval(ch, 0);
125 1.15 jdolecek if ((tmp & R) && (tmp & F))
126 1.1 bjh21 fldtab->weights = RFtable;
127 1.1 bjh21 else if (tmp & F)
128 1.1 bjh21 fldtab->weights = Ftable;
129 1.15 jdolecek else if (tmp & R)
130 1.1 bjh21 fldtab->weights = Rascii;
131 1.1 bjh21 fldtab->flags |= tmp;
132 1.1 bjh21 break;
133 1.11 jdolecek case 'H':
134 1.11 jdolecek PANIC = 0;
135 1.11 jdolecek break;
136 1.11 jdolecek case 'k':
137 1.11 jdolecek setfield(optarg, ++ftpos, fldtab->flags);
138 1.11 jdolecek break;
139 1.11 jdolecek case 'm':
140 1.11 jdolecek mflag = 1;
141 1.11 jdolecek break;
142 1.1 bjh21 case 'o':
143 1.1 bjh21 outpath = optarg;
144 1.1 bjh21 break;
145 1.9 jdolecek case 's':
146 1.9 jdolecek /* for GNU sort compatibility (this is our default) */
147 1.9 jdolecek stable_sort = 1;
148 1.9 jdolecek break;
149 1.9 jdolecek case 'S':
150 1.9 jdolecek stable_sort = 0;
151 1.1 bjh21 break;
152 1.1 bjh21 case 't':
153 1.1 bjh21 if (SEP_FLAG)
154 1.1 bjh21 usage("multiple field delimiters");
155 1.1 bjh21 SEP_FLAG = 1;
156 1.1 bjh21 d_mask[' '] &= ~FLD_D;
157 1.1 bjh21 d_mask['\t'] &= ~FLD_D;
158 1.2 bjh21 d_mask[(u_char)*optarg] |= FLD_D;
159 1.2 bjh21 if (d_mask[(u_char)*optarg] & REC_D_F)
160 1.19 jdolecek errx(2, "record/field delimiter clash");
161 1.1 bjh21 break;
162 1.21 jdolecek case 'R':
163 1.1 bjh21 if (REC_D != '\n')
164 1.1 bjh21 usage("multiple record delimiters");
165 1.1 bjh21 if ('\n' == (REC_D = *optarg))
166 1.1 bjh21 break;
167 1.1 bjh21 d_mask['\n'] = d_mask[' '];
168 1.1 bjh21 d_mask[REC_D] = REC_D_F;
169 1.1 bjh21 break;
170 1.21 jdolecek case 'T':
171 1.21 jdolecek /* -T tmpdir, noop (not supported) */
172 1.21 jdolecek break;
173 1.1 bjh21 case 'u':
174 1.1 bjh21 UNIQUE = 1;
175 1.1 bjh21 break;
176 1.1 bjh21 case '?':
177 1.10 jdolecek default:
178 1.17 soren usage(NULL);
179 1.1 bjh21 }
180 1.1 bjh21 }
181 1.1 bjh21 if (cflag && argc > optind+1)
182 1.1 bjh21 errx(2, "too many input files for -c option");
183 1.1 bjh21 if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
184 1.1 bjh21 outpath = argv[argc-1];
185 1.1 bjh21 argc -= 2;
186 1.1 bjh21 }
187 1.1 bjh21 if (mflag && argc - optind > (MAXFCT - (16+1))*16)
188 1.1 bjh21 errx(2, "too many input files for -m option");
189 1.1 bjh21 for (i = optind; i < argc; i++) {
190 1.1 bjh21 /* allow one occurrence of /dev/stdin */
191 1.10 jdolecek if (!strcmp(argv[i], "-") || !strcmp(argv[i], _PATH_STDIN)) {
192 1.1 bjh21 if (stdinflag)
193 1.1 bjh21 warnx("ignoring extra \"%s\" in file list",
194 1.1 bjh21 argv[i]);
195 1.10 jdolecek else
196 1.1 bjh21 stdinflag = 1;
197 1.14 jdolecek
198 1.14 jdolecek /* change to /dev/stdin if '-' */
199 1.14 jdolecek if (argv[i][0] == '-')
200 1.14 jdolecek argv[i] = _PATH_STDIN;
201 1.14 jdolecek
202 1.2 bjh21 } else if ((ch = access(argv[i], R_OK)))
203 1.7 thorpej err(2, "%s", argv[i]);
204 1.1 bjh21 }
205 1.6 bjh21 if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
206 1.1 bjh21 SINGL_FLD = 1;
207 1.1 bjh21 fldtab[0].icol.num = 1;
208 1.1 bjh21 } else {
209 1.1 bjh21 if (!fldtab[1].icol.num) {
210 1.1 bjh21 fldtab[0].flags &= ~(BI|BT);
211 1.1 bjh21 setfield("1", ++ftpos, fldtab->flags);
212 1.1 bjh21 }
213 1.1 bjh21 fldreset(fldtab);
214 1.1 bjh21 fldtab[0].flags &= ~F;
215 1.1 bjh21 }
216 1.1 bjh21 settables(fldtab[0].flags);
217 1.1 bjh21 num_init();
218 1.1 bjh21 fldtab->weights = gweights;
219 1.5 bjh21 if (optind == argc) {
220 1.10 jdolecek static const char * const names[] = { _PATH_STDIN, NULL };
221 1.5 bjh21
222 1.5 bjh21 filelist.names = names;
223 1.5 bjh21 optind--;
224 1.5 bjh21 } else
225 1.10 jdolecek filelist.names = (const char * const *) &argv[optind];
226 1.15 jdolecek
227 1.1 bjh21 if (SINGL_FLD)
228 1.1 bjh21 get = makeline;
229 1.1 bjh21 else
230 1.1 bjh21 get = makekey;
231 1.15 jdolecek
232 1.1 bjh21 if (cflag) {
233 1.13 jdolecek order(&filelist, get, fldtab);
234 1.1 bjh21 /* NOT REACHED */
235 1.1 bjh21 }
236 1.1 bjh21 if (!outpath) {
237 1.1 bjh21 (void)snprintf(toutpath,
238 1.1 bjh21 sizeof(toutpath), "%sstdout", _PATH_DEV);
239 1.1 bjh21 outfile = outpath = toutpath;
240 1.1 bjh21 } else if (!(ch = access(outpath, 0)) &&
241 1.1 bjh21 strncmp(_PATH_DEV, outpath, 5)) {
242 1.16 jdolecek static const struct sigaction act =
243 1.16 jdolecek { onsignal, {{0}}, SA_RESTART | SA_RESETHAND };
244 1.16 jdolecek static const int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU,
245 1.16 jdolecek SIGXFSZ, SIGVTALRM, SIGPROF, 0};
246 1.3 bjh21 int outfd;
247 1.1 bjh21 errno = 0;
248 1.1 bjh21 if (access(outpath, W_OK))
249 1.1 bjh21 err(2, "%s", outpath);
250 1.1 bjh21 (void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
251 1.3 bjh21 if ((outfd = mkstemp(toutpath)) < 0 ||
252 1.3 bjh21 (outfp = fdopen(outfd, "w")) == 0)
253 1.8 jdolecek err(2, "temporary file %s", toutpath);
254 1.3 bjh21 outfile = toutpath;
255 1.1 bjh21 (void)atexit(cleanup);
256 1.1 bjh21 for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
257 1.1 bjh21 sigaction(sigtable[i], &act, 0);
258 1.3 bjh21 } else
259 1.3 bjh21 outfile = outpath;
260 1.13 jdolecek
261 1.3 bjh21 if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
262 1.8 jdolecek err(2, "output file %s", outfile);
263 1.13 jdolecek
264 1.13 jdolecek if (mflag) {
265 1.13 jdolecek fmerge(-1, 0, &filelist, argc-optind, get, outfp, putline,
266 1.13 jdolecek fldtab);
267 1.13 jdolecek } else
268 1.13 jdolecek fsort(-1, 0, 0, &filelist, argc-optind, outfp, fldtab);
269 1.13 jdolecek
270 1.1 bjh21 if (outfile != outpath) {
271 1.1 bjh21 if (access(outfile, 0))
272 1.7 thorpej err(2, "%s", outfile);
273 1.1 bjh21 (void)unlink(outpath);
274 1.1 bjh21 if (link(outfile, outpath))
275 1.1 bjh21 err(2, "cannot link %s: output left in %s",
276 1.1 bjh21 outpath, outfile);
277 1.1 bjh21 (void)unlink(outfile);
278 1.1 bjh21 }
279 1.1 bjh21 exit(0);
280 1.1 bjh21 }
281 1.1 bjh21
282 1.1 bjh21 static void
283 1.2 bjh21 onsignal(sig)
284 1.2 bjh21 int sig;
285 1.1 bjh21 {
286 1.1 bjh21 cleanup();
287 1.1 bjh21 }
288 1.1 bjh21
289 1.1 bjh21 static void
290 1.1 bjh21 cleanup()
291 1.1 bjh21 {
292 1.1 bjh21 if (toutpath[0])
293 1.1 bjh21 (void)unlink(toutpath);
294 1.1 bjh21 }
295 1.1 bjh21
296 1.1 bjh21 static void
297 1.1 bjh21 usage(msg)
298 1.8 jdolecek const char *msg;
299 1.1 bjh21 {
300 1.18 soren if (msg != NULL)
301 1.1 bjh21 (void)fprintf(stderr, "sort: %s\n", msg);
302 1.15 jdolecek (void)fprintf(stderr, "usage: [-o output] [-cmubdfinrsS] [-t char] ");
303 1.21 jdolecek (void)fprintf(stderr, "[-R char] [-k keydef] ... [files]\n");
304 1.1 bjh21 exit(2);
305 1.1 bjh21 }
306