10fbbaa43SLionel Sambuc /* $NetBSD: sort.c,v 1.61 2011/09/16 15:39:29 joerg Exp $ */
20fbbaa43SLionel Sambuc
30fbbaa43SLionel Sambuc /*-
40fbbaa43SLionel Sambuc * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
50fbbaa43SLionel Sambuc * All rights reserved.
60fbbaa43SLionel Sambuc *
70fbbaa43SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
80fbbaa43SLionel Sambuc * by Ben Harris and Jaromir Dolecek.
90fbbaa43SLionel Sambuc *
100fbbaa43SLionel Sambuc * Redistribution and use in source and binary forms, with or without
110fbbaa43SLionel Sambuc * modification, are permitted provided that the following conditions
120fbbaa43SLionel Sambuc * are met:
130fbbaa43SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
140fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer.
150fbbaa43SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
160fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
170fbbaa43SLionel Sambuc * documentation and/or other materials provided with the distribution.
180fbbaa43SLionel Sambuc *
190fbbaa43SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200fbbaa43SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210fbbaa43SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220fbbaa43SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230fbbaa43SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240fbbaa43SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250fbbaa43SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260fbbaa43SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270fbbaa43SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280fbbaa43SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290fbbaa43SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
300fbbaa43SLionel Sambuc */
310fbbaa43SLionel Sambuc
320fbbaa43SLionel Sambuc /*-
330fbbaa43SLionel Sambuc * Copyright (c) 1993
340fbbaa43SLionel Sambuc * The Regents of the University of California. All rights reserved.
350fbbaa43SLionel Sambuc *
360fbbaa43SLionel Sambuc * This code is derived from software contributed to Berkeley by
370fbbaa43SLionel Sambuc * Peter McIlroy.
380fbbaa43SLionel Sambuc *
390fbbaa43SLionel Sambuc * Redistribution and use in source and binary forms, with or without
400fbbaa43SLionel Sambuc * modification, are permitted provided that the following conditions
410fbbaa43SLionel Sambuc * are met:
420fbbaa43SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
430fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer.
440fbbaa43SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
450fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
460fbbaa43SLionel Sambuc * documentation and/or other materials provided with the distribution.
470fbbaa43SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
480fbbaa43SLionel Sambuc * may be used to endorse or promote products derived from this software
490fbbaa43SLionel Sambuc * without specific prior written permission.
500fbbaa43SLionel Sambuc *
510fbbaa43SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
520fbbaa43SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
530fbbaa43SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
540fbbaa43SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
550fbbaa43SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
560fbbaa43SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
570fbbaa43SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
580fbbaa43SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
590fbbaa43SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
600fbbaa43SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
610fbbaa43SLionel Sambuc * SUCH DAMAGE.
620fbbaa43SLionel Sambuc */
630fbbaa43SLionel Sambuc
640fbbaa43SLionel Sambuc /* Sort sorts a file using an optional user-defined key.
650fbbaa43SLionel Sambuc * Sort uses radix sort for internal sorting, and allows
660fbbaa43SLionel Sambuc * a choice of merge sort and radix sort for external sorting.
670fbbaa43SLionel Sambuc */
680fbbaa43SLionel Sambuc
690fbbaa43SLionel Sambuc #include <util.h>
700fbbaa43SLionel Sambuc #include "sort.h"
710fbbaa43SLionel Sambuc #include "fsort.h"
720fbbaa43SLionel Sambuc #include "pathnames.h"
730fbbaa43SLionel Sambuc
740fbbaa43SLionel Sambuc #ifndef lint
750fbbaa43SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 1993\
760fbbaa43SLionel Sambuc The Regents of the University of California. All rights reserved.");
770fbbaa43SLionel Sambuc #endif /* not lint */
780fbbaa43SLionel Sambuc
790fbbaa43SLionel Sambuc __RCSID("$NetBSD: sort.c,v 1.61 2011/09/16 15:39:29 joerg Exp $");
800fbbaa43SLionel Sambuc
810fbbaa43SLionel Sambuc #include <sys/types.h>
820fbbaa43SLionel Sambuc #include <sys/time.h>
830fbbaa43SLionel Sambuc #include <sys/resource.h>
840fbbaa43SLionel Sambuc
850fbbaa43SLionel Sambuc #include <paths.h>
860fbbaa43SLionel Sambuc #include <signal.h>
870fbbaa43SLionel Sambuc #include <stdlib.h>
880fbbaa43SLionel Sambuc #include <string.h>
890fbbaa43SLionel Sambuc #include <unistd.h>
900fbbaa43SLionel Sambuc #include <locale.h>
910fbbaa43SLionel Sambuc
920fbbaa43SLionel Sambuc int REC_D = '\n';
930fbbaa43SLionel Sambuc u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */
940fbbaa43SLionel Sambuc
950fbbaa43SLionel Sambuc /*
960fbbaa43SLionel Sambuc * weight tables. Gweights is one of ascii, Rascii..
970fbbaa43SLionel Sambuc * modified to weight rec_d = 0 (or 255)
980fbbaa43SLionel Sambuc */
990fbbaa43SLionel Sambuc u_char *const weight_tables[4] = { ascii, Rascii, Ftable, RFtable };
1000fbbaa43SLionel Sambuc u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
1010fbbaa43SLionel Sambuc
1020fbbaa43SLionel Sambuc int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
1030fbbaa43SLionel Sambuc int REVERSE = 0;
1040fbbaa43SLionel Sambuc int posix_sort;
1050fbbaa43SLionel Sambuc
1060fbbaa43SLionel Sambuc unsigned int debug_flags = 0;
1070fbbaa43SLionel Sambuc
1080fbbaa43SLionel Sambuc static char toutpath[MAXPATHLEN];
1090fbbaa43SLionel Sambuc
1100fbbaa43SLionel Sambuc const char *tmpdir; /* where temporary files should be put */
1110fbbaa43SLionel Sambuc
1120fbbaa43SLionel Sambuc static void cleanup(void);
1130fbbaa43SLionel Sambuc static void onsignal(int);
1140fbbaa43SLionel Sambuc __dead static void usage(const char *);
1150fbbaa43SLionel Sambuc
1160fbbaa43SLionel Sambuc int
main(int argc,char * argv[])1170fbbaa43SLionel Sambuc main(int argc, char *argv[])
1180fbbaa43SLionel Sambuc {
1190fbbaa43SLionel Sambuc int ch, i, stdinflag = 0;
1200fbbaa43SLionel Sambuc char cflag = 0, mflag = 0;
1210fbbaa43SLionel Sambuc char *outfile, *outpath = 0;
1220fbbaa43SLionel Sambuc struct field *fldtab;
1230fbbaa43SLionel Sambuc size_t fldtab_sz, fld_cnt;
1240fbbaa43SLionel Sambuc struct filelist filelist;
1250fbbaa43SLionel Sambuc int num_input_files;
1260fbbaa43SLionel Sambuc FILE *outfp = NULL;
1270fbbaa43SLionel Sambuc struct rlimit rl;
1280fbbaa43SLionel Sambuc struct stat st;
1290fbbaa43SLionel Sambuc
1300fbbaa43SLionel Sambuc setlocale(LC_ALL, "");
1310fbbaa43SLionel Sambuc
1320fbbaa43SLionel Sambuc /* bump RLIMIT_NOFILE to maximum our hard limit allows */
1330fbbaa43SLionel Sambuc if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
1340fbbaa43SLionel Sambuc err(2, "getrlimit");
1350fbbaa43SLionel Sambuc rl.rlim_cur = rl.rlim_max;
1360fbbaa43SLionel Sambuc if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
1370fbbaa43SLionel Sambuc err(2, "setrlimit");
1380fbbaa43SLionel Sambuc
1390fbbaa43SLionel Sambuc d_mask[REC_D = '\n'] = REC_D_F;
1400fbbaa43SLionel Sambuc d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
1410fbbaa43SLionel Sambuc
1420fbbaa43SLionel Sambuc /* fldtab[0] is the global options. */
1430fbbaa43SLionel Sambuc fldtab_sz = 3;
1440fbbaa43SLionel Sambuc fld_cnt = 0;
1450fbbaa43SLionel Sambuc fldtab = emalloc(fldtab_sz * sizeof(*fldtab));
1460fbbaa43SLionel Sambuc memset(fldtab, 0, fldtab_sz * sizeof(*fldtab));
1470fbbaa43SLionel Sambuc
1480fbbaa43SLionel Sambuc #define SORT_OPTS "bcdD:fHik:lmno:rR:sSt:T:ux"
1490fbbaa43SLionel Sambuc
1500fbbaa43SLionel Sambuc /* Convert "+field" args to -f format */
1510fbbaa43SLionel Sambuc fixit(&argc, argv, SORT_OPTS);
1520fbbaa43SLionel Sambuc
1530fbbaa43SLionel Sambuc if (!(tmpdir = getenv("TMPDIR")))
1540fbbaa43SLionel Sambuc tmpdir = _PATH_TMP;
1550fbbaa43SLionel Sambuc
1560fbbaa43SLionel Sambuc while ((ch = getopt(argc, argv, SORT_OPTS)) != -1) {
1570fbbaa43SLionel Sambuc switch (ch) {
1580fbbaa43SLionel Sambuc case 'b':
1590fbbaa43SLionel Sambuc fldtab[0].flags |= BI | BT;
1600fbbaa43SLionel Sambuc break;
1610fbbaa43SLionel Sambuc case 'c':
1620fbbaa43SLionel Sambuc cflag = 1;
1630fbbaa43SLionel Sambuc break;
1640fbbaa43SLionel Sambuc case 'D': /* Debug flags */
1650fbbaa43SLionel Sambuc for (i = 0; optarg[i]; i++)
1660fbbaa43SLionel Sambuc debug_flags |= 1 << (optarg[i] & 31);
1670fbbaa43SLionel Sambuc break;
1680fbbaa43SLionel Sambuc case 'd': case 'f': case 'i': case 'n': case 'l':
169*84d9c625SLionel Sambuc #if defined(__minix)
170e286ccc0SBen Gras case 'x':
171*84d9c625SLionel Sambuc #endif /* defined(__minix) */
1720fbbaa43SLionel Sambuc fldtab[0].flags |= optval(ch, 0);
1730fbbaa43SLionel Sambuc break;
1740fbbaa43SLionel Sambuc case 'H':
1750fbbaa43SLionel Sambuc /* -H was ; use merge sort for blocks of large files' */
1760fbbaa43SLionel Sambuc /* That is now the default. */
1770fbbaa43SLionel Sambuc break;
1780fbbaa43SLionel Sambuc case 'k':
1790fbbaa43SLionel Sambuc fldtab = erealloc(fldtab, (fldtab_sz + 1) * sizeof(*fldtab));
1800fbbaa43SLionel Sambuc memset(&fldtab[fldtab_sz], 0, sizeof(fldtab[0]));
1810fbbaa43SLionel Sambuc fldtab_sz++;
1820fbbaa43SLionel Sambuc
1830fbbaa43SLionel Sambuc setfield(optarg, &fldtab[++fld_cnt], fldtab[0].flags);
1840fbbaa43SLionel Sambuc break;
1850fbbaa43SLionel Sambuc case 'm':
1860fbbaa43SLionel Sambuc mflag = 1;
1870fbbaa43SLionel Sambuc break;
1880fbbaa43SLionel Sambuc case 'o':
1890fbbaa43SLionel Sambuc outpath = optarg;
1900fbbaa43SLionel Sambuc break;
1910fbbaa43SLionel Sambuc case 'r':
1920fbbaa43SLionel Sambuc REVERSE = 1;
1930fbbaa43SLionel Sambuc break;
1940fbbaa43SLionel Sambuc case 's':
1950fbbaa43SLionel Sambuc /*
1960fbbaa43SLionel Sambuc * Nominally 'stable sort', keep lines with equal keys
1970fbbaa43SLionel Sambuc * in input file order. (Default for NetBSD)
1980fbbaa43SLionel Sambuc * (-s for GNU sort compatibility.)
1990fbbaa43SLionel Sambuc */
2000fbbaa43SLionel Sambuc posix_sort = 0;
2010fbbaa43SLionel Sambuc break;
2020fbbaa43SLionel Sambuc case 'S':
2030fbbaa43SLionel Sambuc /*
2040fbbaa43SLionel Sambuc * Reverse of -s!
2050fbbaa43SLionel Sambuc * This needs to enforce a POSIX sort where records
2060fbbaa43SLionel Sambuc * with equal keys are then sorted by the raw data.
2070fbbaa43SLionel Sambuc * Currently not implemented!
2080fbbaa43SLionel Sambuc * (using libc radixsort() v sradixsort() doesn't
2090fbbaa43SLionel Sambuc * have the desired effect.)
2100fbbaa43SLionel Sambuc */
2110fbbaa43SLionel Sambuc posix_sort = 1;
2120fbbaa43SLionel Sambuc break;
2130fbbaa43SLionel Sambuc case 't':
2140fbbaa43SLionel Sambuc if (SEP_FLAG)
2150fbbaa43SLionel Sambuc usage("multiple field delimiters");
2160fbbaa43SLionel Sambuc SEP_FLAG = 1;
2170fbbaa43SLionel Sambuc d_mask[' '] &= ~FLD_D;
2180fbbaa43SLionel Sambuc d_mask['\t'] &= ~FLD_D;
2190fbbaa43SLionel Sambuc d_mask[(u_char)*optarg] |= FLD_D;
2200fbbaa43SLionel Sambuc if (d_mask[(u_char)*optarg] & REC_D_F)
2210fbbaa43SLionel Sambuc errx(2, "record/field delimiter clash");
2220fbbaa43SLionel Sambuc break;
2230fbbaa43SLionel Sambuc case 'R':
2240fbbaa43SLionel Sambuc if (REC_D != '\n')
2250fbbaa43SLionel Sambuc usage("multiple record delimiters");
2260fbbaa43SLionel Sambuc REC_D = *optarg;
2270fbbaa43SLionel Sambuc if (REC_D == '\n')
2280fbbaa43SLionel Sambuc break;
2290fbbaa43SLionel Sambuc if (optarg[1] != '\0') {
2300fbbaa43SLionel Sambuc char *ep;
2310fbbaa43SLionel Sambuc int t = 0;
2320fbbaa43SLionel Sambuc if (optarg[0] == '\\')
2330fbbaa43SLionel Sambuc optarg++, t = 8;
2340fbbaa43SLionel Sambuc REC_D = (int)strtol(optarg, &ep, t);
2350fbbaa43SLionel Sambuc if (*ep != '\0' || REC_D < 0 ||
2360fbbaa43SLionel Sambuc REC_D >= (int)__arraycount(d_mask))
2370fbbaa43SLionel Sambuc errx(2, "invalid record delimiter %s",
2380fbbaa43SLionel Sambuc optarg);
2390fbbaa43SLionel Sambuc }
2400fbbaa43SLionel Sambuc d_mask['\n'] = d_mask[' '];
2410fbbaa43SLionel Sambuc d_mask[REC_D] = REC_D_F;
2420fbbaa43SLionel Sambuc break;
2430fbbaa43SLionel Sambuc case 'T':
2440fbbaa43SLionel Sambuc /* -T tmpdir */
2450fbbaa43SLionel Sambuc tmpdir = optarg;
2460fbbaa43SLionel Sambuc break;
2470fbbaa43SLionel Sambuc case 'u':
2480fbbaa43SLionel Sambuc UNIQUE = 1;
2490fbbaa43SLionel Sambuc break;
2500fbbaa43SLionel Sambuc case '?':
2510fbbaa43SLionel Sambuc default:
2520fbbaa43SLionel Sambuc usage(NULL);
2530fbbaa43SLionel Sambuc }
2540fbbaa43SLionel Sambuc }
2550fbbaa43SLionel Sambuc
2560fbbaa43SLionel Sambuc if (UNIQUE)
2570fbbaa43SLionel Sambuc /* Don't sort on raw record if keys match */
2580fbbaa43SLionel Sambuc posix_sort = 0;
2590fbbaa43SLionel Sambuc
2600fbbaa43SLionel Sambuc if (cflag && argc > optind+1)
2610fbbaa43SLionel Sambuc errx(2, "too many input files for -c option");
2620fbbaa43SLionel Sambuc if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
2630fbbaa43SLionel Sambuc outpath = argv[argc-1];
2640fbbaa43SLionel Sambuc argc -= 2;
2650fbbaa43SLionel Sambuc }
2660fbbaa43SLionel Sambuc if (mflag && argc - optind > (MAXFCT - (16+1))*16)
2670fbbaa43SLionel Sambuc errx(2, "too many input files for -m option");
2680fbbaa43SLionel Sambuc
2690fbbaa43SLionel Sambuc for (i = optind; i < argc; i++) {
2700fbbaa43SLionel Sambuc /* allow one occurrence of /dev/stdin */
2710fbbaa43SLionel Sambuc if (!strcmp(argv[i], "-") || !strcmp(argv[i], _PATH_STDIN)) {
2720fbbaa43SLionel Sambuc if (stdinflag)
2730fbbaa43SLionel Sambuc warnx("ignoring extra \"%s\" in file list",
2740fbbaa43SLionel Sambuc argv[i]);
2750fbbaa43SLionel Sambuc else
2760fbbaa43SLionel Sambuc stdinflag = 1;
2770fbbaa43SLionel Sambuc
2780fbbaa43SLionel Sambuc /* change to /dev/stdin if '-' */
2790fbbaa43SLionel Sambuc if (argv[i][0] == '-') {
2800fbbaa43SLionel Sambuc static char path_stdin[] = _PATH_STDIN;
2810fbbaa43SLionel Sambuc argv[i] = path_stdin;
2820fbbaa43SLionel Sambuc }
2830fbbaa43SLionel Sambuc
2840fbbaa43SLionel Sambuc } else if ((ch = access(argv[i], R_OK)))
2850fbbaa43SLionel Sambuc err(2, "%s", argv[i]);
2860fbbaa43SLionel Sambuc }
2870fbbaa43SLionel Sambuc
2880fbbaa43SLionel Sambuc if (fldtab[1].icol.num == 0) {
2890fbbaa43SLionel Sambuc /* No sort key specified */
2900fbbaa43SLionel Sambuc if (fldtab[0].flags & (I|D|F|N|L)) {
2910fbbaa43SLionel Sambuc /* Modified - generate a key that covers the line */
2920fbbaa43SLionel Sambuc fldtab[0].flags &= ~(BI|BT);
2930fbbaa43SLionel Sambuc setfield("1", &fldtab[++fld_cnt], fldtab->flags);
2940fbbaa43SLionel Sambuc fldreset(fldtab);
2950fbbaa43SLionel Sambuc } else {
2960fbbaa43SLionel Sambuc /* Unmodified, just compare the line */
2970fbbaa43SLionel Sambuc SINGL_FLD = 1;
2980fbbaa43SLionel Sambuc fldtab[0].icol.num = 1;
2990fbbaa43SLionel Sambuc }
3000fbbaa43SLionel Sambuc } else {
3010fbbaa43SLionel Sambuc fldreset(fldtab);
3020fbbaa43SLionel Sambuc }
3030fbbaa43SLionel Sambuc
3040fbbaa43SLionel Sambuc settables();
3050fbbaa43SLionel Sambuc
3060fbbaa43SLionel Sambuc if (optind == argc) {
3070fbbaa43SLionel Sambuc static const char * const names[] = { _PATH_STDIN, NULL };
3080fbbaa43SLionel Sambuc filelist.names = names;
3090fbbaa43SLionel Sambuc num_input_files = 1;
3100fbbaa43SLionel Sambuc } else {
3110fbbaa43SLionel Sambuc filelist.names = (const char * const *) &argv[optind];
3120fbbaa43SLionel Sambuc num_input_files = argc - optind;
3130fbbaa43SLionel Sambuc }
3140fbbaa43SLionel Sambuc
3150fbbaa43SLionel Sambuc if (cflag) {
3160fbbaa43SLionel Sambuc order(&filelist, fldtab);
3170fbbaa43SLionel Sambuc /* NOT REACHED */
3180fbbaa43SLionel Sambuc }
3190fbbaa43SLionel Sambuc
3200fbbaa43SLionel Sambuc if (!outpath) {
3210fbbaa43SLionel Sambuc toutpath[0] = '\0'; /* path not used in this case */
3220fbbaa43SLionel Sambuc outfile = outpath = toutpath;
3230fbbaa43SLionel Sambuc outfp = stdout;
3240fbbaa43SLionel Sambuc } else if (lstat(outpath, &st) == 0
3250fbbaa43SLionel Sambuc && !S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
3260fbbaa43SLionel Sambuc /* output file exists and isn't character or block device */
3270fbbaa43SLionel Sambuc struct sigaction act;
3280fbbaa43SLionel Sambuc static const int sigtable[] = {SIGHUP, SIGINT, SIGPIPE,
3290fbbaa43SLionel Sambuc #if defined(__minix)
3300fbbaa43SLionel Sambuc SIGVTALRM, SIGPROF, 0};
3310fbbaa43SLionel Sambuc #else
3320fbbaa43SLionel Sambuc SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, 0};
3330fbbaa43SLionel Sambuc #endif /* defined(__minix) */
3340fbbaa43SLionel Sambuc int outfd;
3350fbbaa43SLionel Sambuc errno = 0;
3360fbbaa43SLionel Sambuc if (access(outpath, W_OK))
3370fbbaa43SLionel Sambuc err(2, "%s", outpath);
3380fbbaa43SLionel Sambuc (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXX",
3390fbbaa43SLionel Sambuc outpath);
3400fbbaa43SLionel Sambuc if ((outfd = mkstemp(toutpath)) == -1)
3410fbbaa43SLionel Sambuc err(2, "Cannot create temporary file `%s'", toutpath);
3420fbbaa43SLionel Sambuc (void)atexit(cleanup);
3430fbbaa43SLionel Sambuc act.sa_handler = onsignal;
3440fbbaa43SLionel Sambuc (void) sigemptyset(&act.sa_mask);
3450fbbaa43SLionel Sambuc act.sa_flags = SA_RESTART | SA_RESETHAND;
3460fbbaa43SLionel Sambuc for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
3470fbbaa43SLionel Sambuc sigaction(sigtable[i], &act, 0);
3480fbbaa43SLionel Sambuc outfile = toutpath;
3490fbbaa43SLionel Sambuc if ((outfp = fdopen(outfd, "w")) == NULL)
3500fbbaa43SLionel Sambuc err(2, "Cannot open temporary file `%s'", toutpath);
3510fbbaa43SLionel Sambuc } else {
3520fbbaa43SLionel Sambuc outfile = outpath;
3530fbbaa43SLionel Sambuc
3540fbbaa43SLionel Sambuc if ((outfp = fopen(outfile, "w")) == NULL)
3550fbbaa43SLionel Sambuc err(2, "output file %s", outfile);
3560fbbaa43SLionel Sambuc }
3570fbbaa43SLionel Sambuc
3580fbbaa43SLionel Sambuc if (mflag)
3590fbbaa43SLionel Sambuc fmerge(&filelist, num_input_files, outfp, fldtab);
3600fbbaa43SLionel Sambuc else
3610fbbaa43SLionel Sambuc fsort(&filelist, num_input_files, outfp, fldtab);
3620fbbaa43SLionel Sambuc
3630fbbaa43SLionel Sambuc if (outfile != outpath) {
3640fbbaa43SLionel Sambuc if (access(outfile, F_OK))
3650fbbaa43SLionel Sambuc err(2, "%s", outfile);
3660fbbaa43SLionel Sambuc
3670fbbaa43SLionel Sambuc /*
3680fbbaa43SLionel Sambuc * Copy file permissions bits of the original file.
3690fbbaa43SLionel Sambuc * st is initialized above, when we create the
3700fbbaa43SLionel Sambuc * temporary spool file.
3710fbbaa43SLionel Sambuc */
3720fbbaa43SLionel Sambuc if (lchmod(outfile, st.st_mode & ALLPERMS) != 0) {
3730fbbaa43SLionel Sambuc err(2, "cannot chmod %s: output left in %s",
3740fbbaa43SLionel Sambuc outpath, outfile);
3750fbbaa43SLionel Sambuc }
3760fbbaa43SLionel Sambuc
3770fbbaa43SLionel Sambuc (void)unlink(outpath);
3780fbbaa43SLionel Sambuc if (link(outfile, outpath))
3790fbbaa43SLionel Sambuc err(2, "cannot link %s: output left in %s",
3800fbbaa43SLionel Sambuc outpath, outfile);
3810fbbaa43SLionel Sambuc (void)unlink(outfile);
3820fbbaa43SLionel Sambuc toutpath[0] = 0;
3830fbbaa43SLionel Sambuc }
3840fbbaa43SLionel Sambuc exit(0);
3850fbbaa43SLionel Sambuc }
3860fbbaa43SLionel Sambuc
3870fbbaa43SLionel Sambuc static void
onsignal(int sig)3880fbbaa43SLionel Sambuc onsignal(int sig)
3890fbbaa43SLionel Sambuc {
3900fbbaa43SLionel Sambuc cleanup();
3910fbbaa43SLionel Sambuc }
3920fbbaa43SLionel Sambuc
3930fbbaa43SLionel Sambuc static void
cleanup(void)3940fbbaa43SLionel Sambuc cleanup(void)
3950fbbaa43SLionel Sambuc {
3960fbbaa43SLionel Sambuc if (toutpath[0])
3970fbbaa43SLionel Sambuc (void)unlink(toutpath);
3980fbbaa43SLionel Sambuc }
3990fbbaa43SLionel Sambuc
4000fbbaa43SLionel Sambuc static void
usage(const char * msg)4010fbbaa43SLionel Sambuc usage(const char *msg)
4020fbbaa43SLionel Sambuc {
4030fbbaa43SLionel Sambuc if (msg != NULL)
4040fbbaa43SLionel Sambuc (void)fprintf(stderr, "%s: %s\n", getprogname(), msg);
4050fbbaa43SLionel Sambuc (void)fprintf(stderr,
4060fbbaa43SLionel Sambuc "usage: %s [-bcdfHilmnrSsu] [-k field1[,field2]] [-o output]"
4070fbbaa43SLionel Sambuc " [-R char] [-T dir]", getprogname());
4080fbbaa43SLionel Sambuc (void)fprintf(stderr,
4090fbbaa43SLionel Sambuc " [-t char] [file ...]\n");
4100fbbaa43SLionel Sambuc exit(2);
4110fbbaa43SLionel Sambuc }
4120fbbaa43SLionel Sambuc
4130fbbaa43SLionel Sambuc RECHEADER *
allocrec(RECHEADER * rec,size_t size)4140fbbaa43SLionel Sambuc allocrec(RECHEADER *rec, size_t size)
4150fbbaa43SLionel Sambuc {
4160fbbaa43SLionel Sambuc
4170fbbaa43SLionel Sambuc return (erealloc(rec, size + sizeof(long) - 1));
4180fbbaa43SLionel Sambuc }
419