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