sort.c revision 1.4 1 /* $NetBSD: sort.c,v 1.4 2000/10/07 21:12:19 bjh21 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.4 2000/10/07 21:12:19 bjh21 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 char devstdin[] = _PATH_STDIN;
83 char toutpath[_POSIX_PATH_MAX];
84 char *tmpdir = _PATH_VARTMP;
85
86 static void cleanup __P((void));
87 static void onsignal __P((int));
88 static void usage __P((char *));
89
90 int main __P((int argc, char **argv));
91
92 int
93 main(argc, argv)
94 int argc;
95 char *argv[];
96 {
97 extern int optind;
98 extern char *optarg;
99 int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
100 struct field *));
101 int ch, i, stdinflag = 0, tmp = 0;
102 char cflag = 0, mflag = 0, nflag = 0;
103 char *outfile, *outpath = 0;
104 struct field fldtab[ND+2], *ftpos;
105 union f_handle filelist;
106 FILE *outfp = NULL;
107 memset(fldtab, 0, (ND+2)*sizeof(struct field));
108 memset(d_mask, 0, NBINS);
109 d_mask[REC_D = '\n'] = REC_D_F;
110 SINGL_FLD = SEP_FLAG = 0;
111 d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
112 ftpos = fldtab;
113 fixit(&argc, argv);
114 while ((ch = getopt(argc, argv, "bcdfik:mHno:rt:T:ux")) != EOF) {
115 if ((outfile = getenv("TMPDIR")))
116 tmpdir = outfile;
117 switch (ch) {
118 case 'b': fldtab->flags |= BI | BT;
119 break;
120 case 'd':
121 case 'i':
122 case 'f':
123 case 'r': tmp |= optval(ch, 0);
124 if (tmp & R && tmp & F)
125 fldtab->weights = RFtable;
126 else if (tmp & F)
127 fldtab->weights = Ftable;
128 else if(tmp & R)
129 fldtab->weights = Rascii;
130 fldtab->flags |= tmp;
131 break;
132 case 'o':
133 outpath = optarg;
134 break;
135 case 'n':
136 nflag = 1;
137 setfield("1n", ++ftpos, fldtab->flags&(~R));
138 break;
139 case 'k':
140 setfield(optarg, ++ftpos, fldtab->flags);
141 break;
142 case 't':
143 if (SEP_FLAG)
144 usage("multiple field delimiters");
145 SEP_FLAG = 1;
146 d_mask[' '] &= ~FLD_D;
147 d_mask['\t'] &= ~FLD_D;
148 d_mask[(u_char)*optarg] |= FLD_D;
149 if (d_mask[(u_char)*optarg] & REC_D_F)
150 err(2, "record/field delimiter clash");
151 break;
152 case 'T':
153 if (REC_D != '\n')
154 usage("multiple record delimiters");
155 if ('\n' == (REC_D = *optarg))
156 break;
157 d_mask['\n'] = d_mask[' '];
158 d_mask[REC_D] = REC_D_F;
159 break;
160 case 'u':
161 UNIQUE = 1;
162 break;
163 case 'c':
164 cflag = 1;
165 break;
166 case 'm':
167 mflag = 1;
168 break;
169 case 'H':
170 PANIC = 0;
171 break;
172 case '?':
173 default: usage("");
174 }
175 }
176 if (cflag && argc > optind+1)
177 errx(2, "too many input files for -c option");
178 if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
179 outpath = argv[argc-1];
180 argc -= 2;
181 }
182 if (mflag && argc - optind > (MAXFCT - (16+1))*16)
183 errx(2, "too many input files for -m option");
184 for (i = optind; i < argc; i++) {
185 /* allow one occurrence of /dev/stdin */
186 if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
187 if (stdinflag)
188 warnx("ignoring extra \"%s\" in file list",
189 argv[i]);
190 else {
191 stdinflag = 1;
192 argv[i] = devstdin;
193 }
194 } else if ((ch = access(argv[i], R_OK)))
195 err(2, argv[i]);
196 }
197 if (!(fldtab->flags & (I|D) || fldtab[1].icol.num)) {
198 SINGL_FLD = 1;
199 fldtab[0].icol.num = 1;
200 } else {
201 if (!fldtab[1].icol.num) {
202 fldtab[0].flags &= ~(BI|BT);
203 setfield("1", ++ftpos, fldtab->flags);
204 }
205 if (nflag)
206 fldtab[1].flags |= fldtab->flags;
207 fldreset(fldtab);
208 fldtab[0].flags &= ~F;
209 }
210 settables(fldtab[0].flags);
211 num_init();
212 fldtab->weights = gweights;
213 if (optind == argc)
214 argv[--optind] = devstdin;
215 filelist.names = argv+optind;
216 if (SINGL_FLD)
217 get = makeline;
218 else
219 get = makekey;
220 if (cflag) {
221 order(filelist, get, fldtab);
222 /* NOT REACHED */
223 }
224 if (!outpath) {
225 (void)snprintf(toutpath,
226 sizeof(toutpath), "%sstdout", _PATH_DEV);
227 outfile = outpath = toutpath;
228 } else if (!(ch = access(outpath, 0)) &&
229 strncmp(_PATH_DEV, outpath, 5)) {
230 struct sigaction act = {0};
231 int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
232 SIGVTALRM, SIGPROF, 0};
233 int outfd;
234 errno = 0;
235 if (access(outpath, W_OK))
236 err(2, "%s", outpath);
237 act.sa_handler = onsignal;
238 act.sa_flags = SA_RESTART | SA_RESETHAND;
239 (void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
240 if ((outfd = mkstemp(toutpath)) < 0 ||
241 (outfp = fdopen(outfd, "w")) == 0)
242 err(2, toutpath);
243 outfile = toutpath;
244 (void)atexit(cleanup);
245 for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
246 sigaction(sigtable[i], &act, 0);
247 } else
248 outfile = outpath;
249 if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
250 err(2, outfile);
251 if (mflag)
252 fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
253 else
254 fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
255 if (outfile != outpath) {
256 if (access(outfile, 0))
257 err(2, outfile);
258 (void)unlink(outpath);
259 if (link(outfile, outpath))
260 err(2, "cannot link %s: output left in %s",
261 outpath, outfile);
262 (void)unlink(outfile);
263 }
264 exit(0);
265 }
266
267 static void
268 onsignal(sig)
269 int sig;
270 {
271 cleanup();
272 }
273
274 static void
275 cleanup()
276 {
277 if (toutpath[0])
278 (void)unlink(toutpath);
279 }
280
281 static void
282 usage(msg)
283 char *msg;
284 {
285 if (msg)
286 (void)fprintf(stderr, "sort: %s\n", msg);
287 (void)fprintf(stderr, "usage: [-o output] [-cmubdfinr] [-t char] ");
288 (void)fprintf(stderr, "[-T char] [-k keydef] ... [files]\n");
289 exit(2);
290 }
291