summaryrefslogtreecommitdiff
path: root/usr.bin/sort
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1997-06-16 02:21:58 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1997-06-16 02:21:58 +0000
commite42347284e21c5295f7de3e91e5b198b3e150d95 (patch)
tree89c046acc116287da755367e61a31045dded1494 /usr.bin/sort
parentfe86e3771c413df8eecb39530c3a1412236d67e9 (diff)
Cosmetic cleanup, mostly KNF.
Diffstat (limited to 'usr.bin/sort')
-rw-r--r--usr.bin/sort/append.c23
-rw-r--r--usr.bin/sort/fields.c38
-rw-r--r--usr.bin/sort/files.c42
-rw-r--r--usr.bin/sort/fsort.c14
-rw-r--r--usr.bin/sort/init.c27
-rw-r--r--usr.bin/sort/msort.c48
-rw-r--r--usr.bin/sort/sort.c37
-rw-r--r--usr.bin/sort/tmp.c6
8 files changed, 137 insertions, 98 deletions
diff --git a/usr.bin/sort/append.c b/usr.bin/sort/append.c
index 867d412c6a7..31388c6baa1 100644
--- a/usr.bin/sort/append.c
+++ b/usr.bin/sort/append.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: append.c,v 1.1 1997/01/20 19:39:47 millert Exp $ */
+/* $OpenBSD: append.c,v 1.2 1997/06/16 02:21:53 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)append.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: append.c,v 1.1 1997/01/20 19:39:47 millert Exp $";
+static char rcsid[] = "$OpenBSD: append.c,v 1.2 1997/06/16 02:21:53 millert Exp $";
#endif
#endif /* not lint */
@@ -59,7 +59,8 @@ static char rcsid[] = "$OpenBSD: append.c,v 1.1 1997/01/20 19:39:47 millert Exp
prec = (RECHEADER *) (*ppos - sizeof(TRECHEADER));\
put(prec, fp); \
} \
- } else put(prec, fp); \
+ } else \
+ put(prec, fp); \
}
/*
@@ -108,23 +109,24 @@ append(keylist, nelem, depth, fp, put, ftbl)
pend--;
}
if (pend + 1 != *ppos) {
- if (!UNIQUE) {
- OUTPUT;
- } else
+ if (!UNIQUE)
+ OUTPUT
+ else
put(crec, fp);
ppos = cpos;
prec = crec;
}
} else {
- if (!UNIQUE) {
- OUTPUT;
- } else
+ if (!UNIQUE)
+ OUTPUT
+ else
put(crec, fp);
ppos = cpos;
prec = crec;
}
}
- if (!UNIQUE) { OUTPUT; }
+ if (!UNIQUE)
+ OUTPUT;
} else if (UNIQUE) {
ppos = keylist;
prec = (RECHEADER *) (*ppos - depth);
@@ -167,6 +169,7 @@ rd_append(binno, infl0, nfiles, outfp, buffer, bufend)
FILE *outfp;
{
struct recheader *rec;
+
rec = (RECHEADER *) buffer;
if (!getnext(binno, infl0, nfiles, (RECHEADER *) buffer, bufend, 0)) {
putline(rec, outfp);
diff --git a/usr.bin/sort/fields.c b/usr.bin/sort/fields.c
index 39551254be6..812ca00c91b 100644
--- a/usr.bin/sort/fields.c
+++ b/usr.bin/sort/fields.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fields.c,v 1.1 1997/01/20 19:39:49 millert Exp $ */
+/* $OpenBSD: fields.c,v 1.2 1997/06/16 02:21:54 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)fields.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: fields.c,v 1.1 1997/01/20 19:39:49 millert Exp $";
+static char rcsid[] = "$OpenBSD: fields.c,v 1.2 1997/06/16 02:21:54 millert Exp $";
#endif
#endif /* not lint */
@@ -110,14 +110,14 @@ enterkey(keybuf, line, size, fieldtable)
col++;
if (pos >= lineend) {
clpos->end = lineend;
- ++i;
+ i++;
break;
}
}
for (; i <= ncols; i++)
clist[i].start = clist[i].end = lineend;
if (clist[0].start < (u_char *) line->data)
- ++clist[0].start;
+ clist[0].start++;
endkey = (u_char *) keybuf + size - line->size;
for (ftpos = fieldtable + 1; ftpos->icol.num; ftpos++)
if ((keypos = enterfield(keypos, endkey, ftpos,
@@ -147,6 +147,7 @@ enterfield(tablepos, endkey, cur_fld, gflags)
struct column icol, tcol;
register u_int flags;
u_int Rflag;
+
icol = cur_fld->icol;
tcol = cur_fld->tcol;
flags = cur_fld->flags;
@@ -156,12 +157,14 @@ enterfield(tablepos, endkey, cur_fld, gflags)
blancmange(start);
start += icol.indent;
start = min(start, lineend);
+
if (!tcol.num)
end = lineend;
else {
if (tcol.indent) {
end = tcol.p->start;
- if (flags & BT) blancmange(end);
+ if (flags & BT)
+ blancmange(end);
end += tcol.indent;
end = min(end, lineend);
} else
@@ -185,7 +188,7 @@ enterfield(tablepos, endkey, cur_fld, gflags)
} else {
*tablepos++ = lweight[*start];
if (tablepos == endkey)
- return (NULL);
+ return (NULL);
}
}
*tablepos++ = lweight[0];
@@ -203,7 +206,7 @@ enterfield(tablepos, endkey, cur_fld, gflags)
* if the exponent is exactly 61, 61+252, etc--this is ok, since it's the
* only time a field delimiter can come in that position.
* Reverse order is done analagously.
-*/
+ */
u_char *
number(pos, bufend, line, lineend, Rflag)
@@ -222,8 +225,10 @@ number(pos, bufend, line, lineend, Rflag)
nweights = fnum;
if (pos > bufend - 8)
return (NULL);
- /* or_sign sets the sort direction:
- * (-r: +/-)(sign: +/-)(expsign: +/-) */
+ /*
+ * or_sign sets the sort direction:
+ * (-r: +/-)(sign: +/-)(expsign: +/-)
+ */
or_sign = sign ^ expsign ^ Rflag;
blancmange(line);
if (*line == '-') { /* set the sign */
@@ -232,7 +237,8 @@ number(pos, bufend, line, lineend, Rflag)
line++;
}
/* eat initial zeroes */
- for (; *line == '0' && line < lineend; line++);
+ for (; *line == '0' && line < lineend; line++)
+ ;
/* calculate exponents < 0 */
if (*line == DECIMAL) {
exponent = 1;
@@ -301,7 +307,7 @@ number(pos, bufend, line, lineend, Rflag)
/* This forces a gap around the record delimiter
* Thus fnum has vaues over (0,254) -> ((0,REC_D-1),(REC_D+1,255));
* rnum over (0,254) -> (255,REC_D+1),(REC_D-1,0))
-*/
+ */
void
num_init()
{
@@ -311,15 +317,15 @@ num_init()
OFF_TENS = TENS - '0';
OFF_NTENS = NEGTENS - '0';
for (i = 1; i < 10; i++) {
- TENS[i] = TENS[i-1] + 10;
- NEGTENS[i] = NEGTENS[i-1] - 10;
+ TENS[i] = TENS[i - 1] + 10;
+ NEGTENS[i] = NEGTENS[i - 1] - 10;
}
for (i = 0; i < REC_D; i++) {
fnum[i] = i;
- rnum[255-i] = i;
+ rnum[255 - i] = i;
}
for (i = REC_D; i <255; i++) {
- fnum[i] = i+1;
- rnum[255-i] = i-1;
+ fnum[i] = i + 1;
+ rnum[255 - i] = i - 1;
}
}
diff --git a/usr.bin/sort/files.c b/usr.bin/sort/files.c
index 668fb0f8cb7..2df8f6b0595 100644
--- a/usr.bin/sort/files.c
+++ b/usr.bin/sort/files.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: files.c,v 1.3 1997/01/22 06:53:13 millert Exp $ */
+/* $OpenBSD: files.c,v 1.4 1997/06/16 02:21:55 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)files.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: files.c,v 1.3 1997/01/22 06:53:13 millert Exp $";
+static char rcsid[] = "$OpenBSD: files.c,v 1.4 1997/06/16 02:21:55 millert Exp $";
#endif
#endif /* not lint */
@@ -55,8 +55,9 @@ static char rcsid[] = "$OpenBSD: files.c,v 1.3 1997/01/22 06:53:13 millert Exp $
*/
int
getnext(binno, infl0, nfiles, pos, end, dummy)
- int binno, nfiles;
+ int binno;
union f_handle infl0;
+ int nfiles;
register struct recheader *pos;
register u_char *end;
struct field *dummy;
@@ -76,7 +77,7 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
}
flag = -1;
nleft = cnt = 0;
- return(-1);
+ return (-1);
}
maxb = fstack[infl0.top].maxb;
for (; nleft == 0; cnt++) {
@@ -129,8 +130,9 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
*/
int
makeline(flno, filelist, nfiles, buffer, bufend, dummy2)
- int flno, nfiles;
+ int flno;
union f_handle filelist;
+ int nfiles;
struct recheader *buffer;
u_char *bufend;
struct field *dummy2;
@@ -149,14 +151,14 @@ makeline(flno, filelist, nfiles, buffer, bufend, dummy2)
overflow = 0;
}
for (;;) {
- if (flno >= 0) {
- if (!(fp = fstack[flno].fp))
+ if (flno >= 0 && (fp = fstack[flno].fp) == NULL)
+ return (EOF);
+ else if (fp == 0) {
+ if (fileno >= nfiles)
return (EOF);
- } else if (!fp) {
- if (fileno >= nfiles) return(EOF);
if (!(fp = fopen(filelist.names[fileno], "r")))
err(2, filelist.names[fileno]);
- ++fileno;
+ fileno++;
}
while ((pos < end) && ((c = getc(fp)) != EOF)) {
if ((*pos++ = c) == REC_D) {
@@ -177,11 +179,12 @@ makeline(flno, filelist, nfiles, buffer, bufend, dummy2)
*pos++ = REC_D;
buffer->offset = 0;
buffer->length = pos - (char *) buffer->data;
- return(0);
+ return (0);
}
FCLOSE(fp);
fp = 0;
- if(flno >= 0) fstack[flno].fp = 0;
+ if (flno >= 0)
+ fstack[flno].fp = 0;
} else {
buffer->data[100] = '\000';
warnx("line too long: ignoring %s...", buffer->data);
@@ -206,6 +209,7 @@ makekey(flno, filelist, nfiles, buffer, bufend, ftbl)
static DBT dbkey[1], line[1];
static int overflow = 0;
int c;
+
if (overflow) {
overflow = 0;
enterkey(buffer, line, bufend - (u_char *) buffer, ftbl);
@@ -215,14 +219,14 @@ makekey(flno, filelist, nfiles, buffer, bufend, ftbl)
if (flno >= 0) {
get = seq;
if (!(dbdesc = fstack[flno].fp))
- return(EOF);
+ return (EOF);
} else if (!dbdesc) {
if (fileno >= nfiles)
return (EOF);
dbdesc = fopen(filelist.names[fileno], "r");
if (!dbdesc)
err(2, filelist.names[fileno]);
- ++fileno;
+ fileno++;
get = seq;
}
if (!(c = get(dbdesc, line, dbkey))) {
@@ -239,14 +243,13 @@ makekey(flno, filelist, nfiles, buffer, bufend, ftbl)
if (c == EOF) {
FCLOSE(dbdesc);
dbdesc = 0;
- if (flno >= 0) fstack[flno].fp = 0;
+ if (flno >= 0)
+ fstack[flno].fp = 0;
} else {
-
((char *) line->data)[60] = '\000';
warnx("line too long: ignoring %.100s...",
(char *)line->data);
}
-
}
}
@@ -256,11 +259,13 @@ makekey(flno, filelist, nfiles, buffer, bufend, ftbl)
int
seq(fp, line, key)
FILE *fp;
- DBT *key, *line;
+ DBT *line;
+ DBT *key;
{
static char *buf, flag = 1;
register char *end, *pos;
register int c;
+
if (flag) {
flag = 0;
buf = (char *) linebuf;
@@ -326,6 +331,7 @@ geteasy(flno, filelist, nfiles, rec, end, dummy2)
{
int i;
FILE *fp;
+
fp = fstack[flno].fp;
if ((u_char *) rec > end - sizeof(TRECHEADER))
return (BUFFEND);
diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c
index 3537dfb9f5e..9639e737ab6 100644
--- a/usr.bin/sort/fsort.c
+++ b/usr.bin/sort/fsort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsort.c,v 1.3 1997/01/22 06:43:52 millert Exp $ */
+/* $OpenBSD: fsort.c,v 1.4 1997/06/16 02:21:55 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)fsort.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: fsort.c,v 1.3 1997/01/22 06:43:52 millert Exp $";
+static char rcsid[] = "$OpenBSD: fsort.c,v 1.4 1997/06/16 02:21:55 millert Exp $";
#endif
#endif /* not lint */
@@ -50,7 +50,7 @@ static char rcsid[] = "$OpenBSD: fsort.c,v 1.3 1997/01/22 06:43:52 millert Exp $
* and try again on smaller bins. Sort the final bin at this level
* of recursion to keep the head of fstack at 0.
* After PANIC passes, abort to merge sort.
-*/
+ */
#include "sort.h"
#include "fsort.h"
@@ -155,7 +155,7 @@ fsort(binno, depth, infiles, nfiles, outfp, ftbl)
fmerge(0, mstart, mfct, geteasy,
fstack[tfiles.top+ntfiles].fp,
putrec, ftbl);
- ++ntfiles;
+ ntfiles++;
mfct = 0;
memmove(crec->data, tmpbuf,
bufend - crec->data);
@@ -165,7 +165,7 @@ fsort(binno, depth, infiles, nfiles, outfp, ftbl)
fstack[tfiles.top + ntfiles].fp= ftmp();
onepass(keylist, depth, nelem, sizes,
weights, fstack[tfiles.top+ntfiles].fp);
- ++ntfiles;
+ ntfiles++;
}
}
}
@@ -239,7 +239,7 @@ fsort(binno, depth, infiles, nfiles, outfp, ftbl)
}
/*
- This is one pass of radix exchange, dumping the bins to disk.
+ * This is one pass of radix exchange, dumping the bins to disk.
*/
#define swap(a, b, t) t = a, a = b, b = t
void
@@ -275,7 +275,7 @@ onepass(a, depth, n, sizes, tr, fp)
if (c <= 1)
continue;
}
- for(aj = a; aj < an; *aj = r, aj = bin[c+1])
+ for (aj = a; aj < an; *aj = r, aj = bin[c+1])
for(r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
swap(*ak, r, t);
diff --git a/usr.bin/sort/init.c b/usr.bin/sort/init.c
index 06724e4f252..9c7ac643810 100644
--- a/usr.bin/sort/init.c
+++ b/usr.bin/sort/init.c
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: init.c,v 1.1 1997/01/20 19:39:52 millert Exp $";
+static char rcsid[] = "$OpenBSD: init.c,v 1.2 1997/06/16 02:21:56 millert Exp $";
#endif
#endif /* not lint */
@@ -97,11 +97,13 @@ fldreset(fldtab)
int i;
fldtab[0].tcol.p = clist+ncols-1;
for (++fldtab; fldtab->icol.num; ++fldtab) {
- for (i = 0; fldtab->icol.num != clist[i].num; i++);
+ for (i = 0; fldtab->icol.num != clist[i].num; i++)
+ ;
fldtab->icol.p = clist + i;
if (!fldtab->tcol.num)
continue;
- for (i = 0; fldtab->tcol.num != clist[i].num; i++);
+ for (i = 0; fldtab->tcol.num != clist[i].num; i++)
+ ;
fldtab->tcol.p = clist + i;
}
}
@@ -117,6 +119,7 @@ setcolumn(pos, cur_fld, gflag)
{
struct column *col;
int tmp;
+
col = cur_fld->icol.num ? (&(*cur_fld).tcol) : (&(*cur_fld).icol);
pos += sscanf(pos, "%d", &(col->num));
while (isdigit(*pos))
@@ -202,15 +205,15 @@ optval(desc, tcolflag)
switch(desc) {
case 'b':
if (!tcolflag)
- return(BI);
+ return (BI);
else
- return(BT);
- case 'd': return(D);
- case 'f': return(F);
- case 'i': return(I);
- case 'n': return(N);
- case 'r': return(R);
- default: return(0);
+ return (BT);
+ case 'd': return (D);
+ case 'f': return (F);
+ case 'i': return (I);
+ case 'n': return (N);
+ case 'r': return (R);
+ default: return (0);
}
}
@@ -308,7 +311,7 @@ settables(gflags)
}
Rascii[REC_D] = RFtable[REC_D] = REC_D;
if (REC_D >= 'A' && REC_D < 'Z')
- ++Ftable[REC_D + ('a' - 'A')];
+ Ftable[REC_D + ('a' - 'A')]++;
if (gflags & R && (!(gflags & F) || !SINGL_FLD))
wts = Rascii;
else if (!(gflags & F) || !SINGL_FLD)
diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c
index dc3c9b3c39a..9fb52005aee 100644
--- a/usr.bin/sort/msort.c
+++ b/usr.bin/sort/msort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: msort.c,v 1.3 1997/01/22 06:53:15 millert Exp $ */
+/* $OpenBSD: msort.c,v 1.4 1997/06/16 02:21:56 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)msort.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: msort.c,v 1.3 1997/01/22 06:53:15 millert Exp $";
+static char rcsid[] = "$OpenBSD: msort.c,v 1.4 1997/06/16 02:21:56 millert Exp $";
#endif
#endif /* not lint */
@@ -105,6 +105,7 @@ fmerge(binno, files, nfiles, get, outfp, fput, ftbl)
l_fstack = fstack + files.top;
else
l_fstack = fstack;
+
while (nfiles) {
put = putrec;
for (j = 0; j < nfiles; j += 16) {
@@ -121,13 +122,13 @@ fmerge(binno, files, nfiles, get, outfp, fput, ftbl)
fopen(files.names[j + i], "r")))
err(2, files.names[j+i]);
merge(MAXFCT-1-16, last, get, tout, put, ftbl);
- }
- else {
+ } else {
for (i = 0; i< last; i++)
rewind(l_fstack[i+j].fp);
merge(files.top+j, last, get, tout, put, ftbl);
}
- if (nfiles > 16) l_fstack[j/16].fp = tout;
+ if (nfiles > 16)
+ l_fstack[j/16].fp = tout;
}
nfiles = (nfiles + 15) / 16;
if (nfiles == 1)
@@ -160,8 +161,8 @@ merge(infl0, nfiles, get, outfp, put, ftbl)
for (c = 1; c == 1;) {
if (EOF == (c = get(j+infl0, dummy, nfiles,
cfile->rec, cfile->end, ftbl))) {
- --i;
- --nfiles;
+ i--;
+ nfiles--;
break;
}
if (i)
@@ -195,7 +196,7 @@ merge(infl0, nfiles, get, outfp, put, ftbl)
/*
* if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
* otherwise just inserts *rec in flist.
-*/
+ */
static int
insert(flist, rec, ttop, delete)
struct mfile **flist, **rec;
@@ -222,7 +223,7 @@ insert(flist, rec, ttop, delete)
if (!bot && cmpv)
cmpv = cmp(tmprec->rec, flist[0]->rec);
if (!cmpv)
- return(1);
+ return (1);
}
tmprec = flist[0];
if (bot)
@@ -272,21 +273,22 @@ order(infile, get, ftbl)
wts1 = ftbl->flags & R ? Rascii : ascii;
else
wts1 = 0;
- if (0 == get(-1, infile, 1, prec, end, ftbl))
- while (0 == get(-1, infile, 1, crec, end, ftbl)) {
- if (0 < (c = cmp(prec, crec))) {
- crec->data[crec->length-1] = 0;
- errx(1, "found disorder: %s", crec->data+crec->offset);
- }
- if (UNIQUE && !c) {
- crec->data[crec->length-1] = 0;
- errx(1, "found non-uniqueness: %s",
- crec->data+crec->offset);
+ if (get(-1, infile, 1, prec, end, ftbl) == 0)
+ while (0 == get(-1, infile, 1, crec, end, ftbl)) {
+ if (0 < (c = cmp(prec, crec))) {
+ crec->data[crec->length-1] = 0;
+ errx(1, "found disorder: %s",
+ crec->data+crec->offset);
+ }
+ if (UNIQUE && !c) {
+ crec->data[crec->length-1] = 0;
+ errx(1, "found non-uniqueness: %s",
+ crec->data+crec->offset);
+ }
+ trec = prec;
+ prec = crec;
+ crec = trec;
}
- trec = prec;
- prec = crec;
- crec = trec;
- }
exit(0);
}
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index 1df1d5f8688..32cf7873fc2 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sort.c,v 1.3 1997/01/26 00:02:25 deraadt Exp $ */
+/* $OpenBSD: sort.c,v 1.4 1997/06/16 02:21:56 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -46,11 +46,12 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)sort.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: sort.c,v 1.3 1997/01/26 00:02:25 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: sort.c,v 1.4 1997/06/16 02:21:56 millert Exp $";
#endif
#endif /* not lint */
-/* Sort sorts a file using an optional user-defined key.
+/*
+ * Sort sorts a file using an optional user-defined key.
* Sort uses radix sort for internal sorting, and allows
* a choice of merge sort and radix sort for external sorting.
*/
@@ -67,12 +68,14 @@ static char rcsid[] = "$OpenBSD: sort.c,v 1.3 1997/01/26 00:02:25 deraadt Exp $"
int REC_D = '\n';
u_char d_mask[NBINS]; /* flags for rec_d, field_d, <blank> */
+
/*
* weight tables. Gweights is one of ascii, Rascii..
* modified to weight rec_d = 0 (or 255)
*/
extern u_char gweights[NBINS];
u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
+
/*
* masks of ignored characters. Alltable is 256 ones
*/
@@ -96,8 +99,6 @@ main(argc, argv)
int argc;
char *argv[];
{
- extern int optind;
- extern char *optarg;
int (*get)();
int ch, i, stdinflag = 0, tmp = 0;
char cflag = 0, mflag = 0, nflag = 0;
@@ -105,6 +106,7 @@ main(argc, argv)
struct field fldtab[ND+2], *ftpos;
union f_handle filelist;
FILE *outfp = NULL;
+
memset(fldtab, 0, (ND+2)*sizeof(struct field));
memset(d_mask, 0, NBINS);
d_mask[REC_D = '\n'] = REC_D_F;
@@ -115,18 +117,18 @@ main(argc, argv)
if (!issetugid() && (outfile = getenv("TMPDIR")))
tmpdir = outfile;
while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:")) != -1) {
- switch (ch) {
+ switch (ch) {
case 'b': fldtab->flags |= BI | BT;
break;
case 'd':
- case 'i':
case 'f':
+ case 'i':
case 'r': tmp |= optval(ch, 0);
if (tmp & R && tmp & F)
fldtab->weights = RFtable;
else if (tmp & F)
fldtab->weights = Ftable;
- else if(tmp & R)
+ else if (tmp & R)
fldtab->weights = Rascii;
fldtab->flags |= tmp;
break;
@@ -134,6 +136,7 @@ main(argc, argv)
outpath = optarg;
break;
case 'n':
+ /* XXX - this does not deal with -n in with -k */
nflag = 1;
setfield("1n", ++ftpos, fldtab->flags&(~R));
break;
@@ -180,14 +183,18 @@ main(argc, argv)
default: usage("");
}
}
+
if (cflag && argc > optind+1)
errx(2, "too many input files for -c option");
+
if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
outpath = argv[argc-1];
argc -= 2;
}
+
if (mflag && argc - optind > (MAXFCT - (16+1))*16)
errx(2, "too many input files for -m option");
+
for (i = optind; i < argc; i++) {
/* allow one occurrence of /dev/stdin */
if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
@@ -201,6 +208,7 @@ main(argc, argv)
} else if ((ch = access(argv[i], R_OK)))
err(2, argv[i]);
}
+
if (!(fldtab->flags & (I|D) || fldtab[1].icol.num)) {
SINGL_FLD = 1;
fldtab[0].icol.num = 1;
@@ -217,6 +225,7 @@ main(argc, argv)
settables(fldtab[0].flags);
num_init();
fldtab->weights = gweights;
+
if (optind == argc) {
static char *names[2];
@@ -226,14 +235,17 @@ main(argc, argv)
optind--;
} else
filelist.names = argv+optind;
+
if (SINGL_FLD)
get = makeline;
else
get = makekey;
+
if (cflag) {
order(filelist, get, fldtab);
/* NOT REACHED */
}
+
if (!outpath) {
(void)snprintf(toutpath,
sizeof(toutpath), "%sstdout", _PATH_DEV);
@@ -244,15 +256,19 @@ main(argc, argv)
int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
SIGVTALRM, SIGPROF, 0};
int outfd;
+
errno = 0;
+
if (access(outpath, W_OK))
err(2, outpath);
act.sa_handler = onsig;
- (void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
+ (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXXXXXX",
+ outpath);
if ((outfd = mkstemp(toutpath)) < 0 ||
(outfp = fdopen(outfd, "w")) == 0)
err(2, toutpath);
outfile = toutpath;
+
(void)atexit(cleanup);
for (i = 0; sigtable[i]; ++i) /* always unlink toutpath */
sigaction(sigtable[i], &act, 0);
@@ -280,6 +296,7 @@ static void
onsig(s)
int s;
{
+
cleanup();
exit(2); /* return 2 on error/interrupt */
}
@@ -287,6 +304,7 @@ onsig(s)
static void
cleanup()
{
+
if (toutpath[0])
(void)unlink(toutpath);
}
@@ -295,6 +313,7 @@ static void
usage(msg)
char *msg;
{
+
if (msg)
(void)fprintf(stderr, "sort: %s\n", msg);
(void)fprintf(stderr, "usage: [-T dir] [-o output] [-cmubdfinr] ");
diff --git a/usr.bin/sort/tmp.c b/usr.bin/sort/tmp.c
index a5ca2347f71..58d5a91314f 100644
--- a/usr.bin/sort/tmp.c
+++ b/usr.bin/sort/tmp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmp.c,v 1.1 1997/01/20 19:39:57 millert Exp $ */
+/* $OpenBSD: tmp.c,v 1.2 1997/06/16 02:21:57 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)tmp.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: tmp.c,v 1.1 1997/01/20 19:39:57 millert Exp $";
+static char rcsid[] = "$OpenBSD: tmp.c,v 1.2 1997/06/16 02:21:57 millert Exp $";
#endif
#endif /* not lint */
@@ -58,7 +58,7 @@ static char rcsid[] = "$OpenBSD: tmp.c,v 1.1 1997/01/20 19:39:57 millert Exp $";
#include "sort.h"
#include "pathnames.h"
-#define _NAME_TMP "sort.XXXXXXXX"
+#define _NAME_TMP "sort.XXXXXXXXXX"
FILE *
ftmp()