1 /*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Peter McIlroy. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char copyright[] = 13 "@(#) Copyright (c) 1993\n\ 14 The Regents of the University of California. All rights reserved.\n"; 15 #endif /* not lint */ 16 17 #ifndef lint 18 static char sccsid[] = "@(#)sort.c 8.1 (Berkeley) 06/06/93"; 19 #endif /* not lint */ 20 21 /* Sort sorts a file using an optional user-defined key. 22 * Sort uses radix sort for internal sorting, and allows 23 * a choice of merge sort and radix sort for external sorting. 24 */ 25 26 #include "sort.h" 27 #include "fsort.h" 28 #include "pathnames.h" 29 30 #include <paths.h> 31 #include <signal.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <unistd.h> 35 36 int REC_D = '\n'; 37 u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */ 38 /* 39 * weight tables. Gweights is one of ascii, Rascii.. 40 * modified to weight rec_d = 0 (or 255) 41 */ 42 extern u_char gweights[NBINS]; 43 u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS]; 44 /* 45 * masks of ignored characters. Alltable is 256 ones 46 */ 47 u_char dtable[NBINS], itable[NBINS], alltable[NBINS]; 48 int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0; 49 struct coldesc clist[(ND+1)*2]; 50 int ncols = 0; 51 extern struct coldesc clist[(ND+1)*2]; 52 extern int ncols; 53 54 char devstdin[] = _PATH_STDIN; 55 char toutpath[_POSIX_PATH_MAX]; 56 57 static void cleanup __P((void)); 58 static void onsig __P((int)); 59 static void usage __P((char *)); 60 61 int 62 main(argc, argv) 63 int argc; 64 char *argv[]; 65 { 66 extern int optind; 67 extern char *optarg; 68 int (*get)(); 69 int ch, i, stdinflag = 0, tmp = 0; 70 char cflag = 0, mflag = 0, nflag = 0; 71 char *outfile, *outpath = 0; 72 struct field fldtab[ND+2], *ftpos; 73 union f_handle filelist; 74 FILE *outfd; 75 memset(fldtab, 0, (ND+2)*sizeof(struct field)); 76 memset(d_mask, 0, NBINS); 77 d_mask[REC_D = '\n'] = REC_D_F; 78 SINGL_FLD = SEP_FLAG = 0; 79 d_mask['\t'] = d_mask[' '] = BLANK | FLD_D; 80 ftpos = fldtab; 81 fixit(&argc, argv); 82 while ((ch = getopt(argc, argv, "bcdfik:mHno:rt:T:ux")) != EOF) { 83 switch (ch) { 84 case 'b': fldtab->flags |= BI | BT; 85 break; 86 case 'd': 87 case 'i': 88 case 'f': 89 case 'r': tmp |= optval(ch, 0); 90 if (tmp & R && tmp & F) 91 fldtab->weights = RFtable; 92 else if (tmp & F) 93 fldtab->weights = Ftable; 94 else if(tmp & R) 95 fldtab->weights = Rascii; 96 fldtab->flags |= tmp; 97 break; 98 case 'o': 99 outpath = optarg; 100 break; 101 case 'n': 102 nflag = 1; 103 setfield("1n", ++ftpos, fldtab->flags&(~R)); 104 break; 105 case 'k': 106 setfield(optarg, ++ftpos, fldtab->flags); 107 break; 108 case 't': 109 if (SEP_FLAG) 110 usage("multiple field delimiters"); 111 SEP_FLAG = 1; 112 d_mask[' '] &= ~FLD_D; 113 d_mask['\t'] &= ~FLD_D; 114 d_mask[*optarg] |= FLD_D; 115 if (d_mask[*optarg] & REC_D_F) 116 err(2, "record/field delimiter clash"); 117 break; 118 case 'T': 119 if (REC_D != '\n') 120 usage("multiple record delimiters"); 121 if ('\n' == (REC_D = *optarg)) 122 break; 123 d_mask['\n'] = d_mask[' ']; 124 d_mask[REC_D] = REC_D_F; 125 break; 126 case 'u': 127 UNIQUE = 1; 128 break; 129 case 'c': 130 cflag = 1; 131 break; 132 case 'm': 133 mflag = 1; 134 break; 135 case 'H': 136 PANIC = 0; 137 break; 138 case '?': 139 default: usage(""); 140 } 141 } 142 if (cflag && argc > optind+1) 143 errx(2, "too many input files for -c option"); 144 if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) { 145 outpath = argv[argc-1]; 146 argc -= 2; 147 } 148 if (mflag && argc - optind > (MAXFCT - (16+1))*16) 149 errx(2, "too many input files for -m option"); 150 for (i = optind; i < argc; i++) { 151 /* allow one occurrence of /dev/stdin */ 152 if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) { 153 if (stdinflag) 154 warnx("ignoring extra \"%s\" in file list", 155 argv[i]); 156 else { 157 stdinflag = 1; 158 argv[i] = devstdin; 159 } 160 } else if (ch = access(argv[i], R_OK)) 161 err(2, "%s", argv[i]); 162 } 163 if (!(fldtab->flags & (I|D) || fldtab[1].icol.num)) { 164 SINGL_FLD = 1; 165 fldtab[0].icol.num = 1; 166 } else { 167 if (!fldtab[1].icol.num) { 168 fldtab[0].flags &= ~(BI|BT); 169 setfield("1", ++ftpos, fldtab->flags); 170 } 171 if (nflag) 172 fldtab[1].flags |= fldtab->flags; 173 fldreset(fldtab); 174 fldtab[0].flags &= ~F; 175 } 176 settables(fldtab[0].flags); 177 num_init(); 178 fldtab->weights = gweights; 179 if (optind == argc) 180 argv[--optind] = devstdin; 181 filelist.names = argv+optind; 182 if (SINGL_FLD) 183 get = makeline; 184 else 185 get = makekey; 186 if (cflag) { 187 order(filelist, get, fldtab); 188 /* NOT REACHED */ 189 } 190 if (!outpath) { 191 (void)snprintf(toutpath, 192 sizeof(toutpath), "%sstdout", _PATH_DEV); 193 outfile = outpath = toutpath; 194 } else if (!(ch = access(outpath, 0)) && 195 strncmp(_PATH_DEV, outpath, 5)) { 196 struct sigaction act = {0, SIG_BLOCK, 6}; 197 int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ, 198 SIGVTALRM, SIGPROF, 0}; 199 errno = 0; 200 if (access(outpath, W_OK)) 201 err(2, "%s", outpath); 202 act.sa_handler = cleanup; 203 (void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath); 204 outfile = mktemp(toutpath); 205 if (!outfile) 206 err(2, "%s", toutpath); 207 (void)atexit(cleanup); 208 for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */ 209 sigaction(sigtable[i], &act, 0); 210 } else outfile = outpath; 211 if (!(outfd = fopen(outfile, "w"))) 212 err(2, "%s", outfile); 213 if (mflag) 214 fmerge(-1, filelist, argc-optind, get, outfd, putline, fldtab); 215 else 216 fsort(-1, 0, filelist, argc-optind, outfd, fldtab); 217 if (outfile != outpath) { 218 if (access(outfile, 0)) 219 err(2, "%s", outfile); 220 (void)unlink(outpath); 221 if (link(outfile, outpath)) 222 err(2, "cannot link %s: output left in %s", 223 outpath, outfile); 224 (void)unlink(outfile); 225 } 226 exit(0); 227 } 228 229 static void 230 onsig(s) 231 int s; 232 { 233 cleanup(); 234 exit(2); /* return 2 on error/interrupt */ 235 } 236 237 static void 238 cleanup() 239 { 240 if (toutpath[0]) 241 (void)unlink(toutpath); 242 } 243 244 static void 245 usage(msg) 246 char *msg; 247 { 248 if (msg) 249 (void)fprintf(stderr, "sort: %s\n", msg); 250 (void)fprintf(stderr, "usage: [-o output] [-cmubdfinr] [-t char] "); 251 (void)fprintf(stderr, "[-T char] [-k keydef] ... [files]\n"); 252 exit(2); 253 } 254