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