Two classes of changes from the initial OpenBSD commit of this sort(1):

FILE * variables are called "fp" rather than "fd".
Better (safer) temporary-file handling.
This commit is contained in:
bjh21 2000-10-07 20:37:06 +00:00
parent ad0e0b565c
commit e5218d1719
10 changed files with 154 additions and 143 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: append.c,v 1.2 2000/10/07 18:37:09 bjh21 Exp $ */
/* $NetBSD: append.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -39,7 +39,7 @@
#include "sort.h"
#ifndef lint
__RCSID("$NetBSD: append.c,v 1.2 2000/10/07 18:37:09 bjh21 Exp $");
__RCSID("$NetBSD: append.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $");
__SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -54,20 +54,20 @@ __SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
radixsort(ppos, n, wts1, REC_D); \
for (; ppos < cpos; ppos++) { \
prec = (RECHEADER *) (*ppos - sizeof(TRECHEADER));\
put(prec, fd); \
put(prec, fp); \
} \
} else put(prec, fd); \
} else put(prec, fp); \
}
/*
* copy sorted lines to output; check for uniqueness
*/
void
append(keylist, nelem, depth, fd, put, ftbl)
append(keylist, nelem, depth, fp, put, ftbl)
const u_char **keylist;
int nelem;
register int depth;
FILE *fd;
FILE *fp;
void (*put)(RECHEADER *, FILE *);
struct field *ftbl;
{
@ -93,7 +93,7 @@ append(keylist, nelem, depth, fd, put, ftbl)
ppos = keylist;
prec = (RECHEADER *) (*ppos - depth);
if (UNIQUE)
put(prec, fd);
put(prec, fp);
for (cpos = keylist+1; cpos < lastkey; cpos++) {
crec = (RECHEADER *) (*cpos - depth);
if (crec->length == prec->length) {
@ -108,7 +108,7 @@ append(keylist, nelem, depth, fd, put, ftbl)
if (!UNIQUE) {
OUTPUT;
} else
put(crec, fd);
put(crec, fp);
ppos = cpos;
prec = crec;
}
@ -116,7 +116,7 @@ append(keylist, nelem, depth, fd, put, ftbl)
if (!UNIQUE) {
OUTPUT;
} else
put(crec, fd);
put(crec, fp);
ppos = cpos;
prec = crec;
}
@ -125,7 +125,7 @@ append(keylist, nelem, depth, fd, put, ftbl)
} else if (UNIQUE) {
ppos = keylist;
prec = (RECHEADER *) (*ppos - depth);
put(prec, fd);
put(prec, fp);
for (cpos = keylist+1; cpos < lastkey; cpos++) {
crec = (RECHEADER *) (*cpos - depth);
if (crec->offset == prec->offset) {
@ -139,17 +139,17 @@ append(keylist, nelem, depth, fd, put, ftbl)
if (pend + 1 != *ppos) {
ppos = cpos;
prec = crec;
put(prec, fd);
put(prec, fp);
}
} else {
ppos = cpos;
prec = crec;
put(prec, fd);
put(prec, fp);
}
}
} else for (cpos = keylist; cpos < lastkey; cpos++) {
crec = (RECHEADER *) (*cpos - depth);
put(crec, fd);
put(crec, fp);
}
}
@ -157,20 +157,20 @@ append(keylist, nelem, depth, fd, put, ftbl)
* output the already sorted eol bin.
*/
void
rd_append(binno, infl0, nfiles, outfd, buffer, bufend)
rd_append(binno, infl0, nfiles, outfp, buffer, bufend)
u_char *buffer, *bufend;
int binno, nfiles;
union f_handle infl0;
FILE *outfd;
FILE *outfp;
{
struct recheader *rec;
rec = (RECHEADER *) buffer;
if (!getnext(binno, infl0, nfiles, (RECHEADER *) buffer, bufend, 0)) {
putline(rec, outfd);
putline(rec, outfp);
while (getnext(binno, infl0, nfiles, (RECHEADER *) buffer,
bufend, 0) == 0) {
if (!UNIQUE)
putline(rec, outfd);
putline(rec, outfp);
}
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: files.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: files.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -40,7 +40,7 @@
#include "fsort.h"
#ifndef lint
__RCSID("$NetBSD: files.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $");
__RCSID("$NetBSD: files.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $");
__SCCSID("@(#)files.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -63,12 +63,12 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
static long nleft = 0;
static int cnt = 0, flag = -1;
static u_char maxb = 0;
static FILE *fd;
static FILE *fp;
if (nleft == 0) {
if (binno < 0) /* reset files. */ {
for (i = 0; i < nfiles; i++) {
rewind(fstack[infl0.top + i].fd);
rewind(fstack[infl0.top + i].fp);
fstack[infl0.top + i].max_o = 0;
}
flag = -1;
@ -81,24 +81,24 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
cnt = 0;
return (EOF);
}
fd = fstack[infl0.top + cnt].fd;
fp = fstack[infl0.top + cnt].fp;
hp = (u_char *) &nleft;
for (i = sizeof(TRECHEADER); i; --i)
*hp++ = getc(fd);
*hp++ = getc(fp);
if (binno < maxb)
fstack[infl0.top+cnt].max_o
+= sizeof(nleft) + nleft;
else if (binno == maxb) {
if (binno != fstack[infl0.top].lastb) {
fseek(fd, fstack[infl0.top+
fseek(fp, fstack[infl0.top+
cnt].max_o, SEEK_SET);
fread(&nleft, sizeof(nleft), 1, fd);
fread(&nleft, sizeof(nleft), 1, fp);
}
if (nleft == 0)
fclose(fd);
fclose(fp);
} else if (binno == maxb + 1) { /* skip a bin */
fseek(fd, nleft, SEEK_CUR);
fread(&nleft, sizeof(nleft), 1, fd);
fseek(fp, nleft, SEEK_CUR);
fread(&nleft, sizeof(nleft), 1, fp);
flag = cnt;
}
}
@ -107,16 +107,16 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
return (BUFFEND);
hp = (u_char *) pos;
for (i = sizeof(TRECHEADER); i ; --i)
*hp++ = (u_char) getc(fd);
*hp++ = (u_char) getc(fp);
if (end - pos->data < pos->length) {
for (i = sizeof(TRECHEADER); i ; i--)
ungetc(*--hp, fd);
ungetc(*--hp, fp);
return (BUFFEND);
}
fread(pos->data, pos->length, 1, fd);
fread(pos->data, pos->length, 1, fp);
nleft -= pos->length + sizeof(TRECHEADER);
if (nleft == 0 && binno == fstack[infl0.top].maxb)
fclose(fd);
fclose(fp);
return (0);
}
@ -135,7 +135,7 @@ makeline(flno, filelist, nfiles, buffer, bufend, dummy2)
static char *opos;
register char *end, *pos;
static int fileno = 0, overflow = 0;
static FILE *fd = 0;
static FILE *fp = 0;
register int c;
pos = (char *) buffer->data;
@ -147,15 +147,15 @@ makeline(flno, filelist, nfiles, buffer, bufend, dummy2)
}
for (;;) {
if (flno >= 0) {
if (!(fd = fstack[flno].fd))
if (!(fp = fstack[flno].fp))
return (EOF);
} else if (!fd) {
} else if (!fp) {
if (fileno >= nfiles) return(EOF);
if (!(fd = fopen(filelist.names[fileno], "r")))
if (!(fp = fopen(filelist.names[fileno], "r")))
err(2, "%s", filelist.names[fileno]);
++fileno;
}
while ((pos < end) && ((c = getc(fd)) != EOF)) {
while ((pos < end) && ((c = getc(fp)) != EOF)) {
if ((*pos++ = c) == REC_D) {
buffer->offset = 0;
buffer->length = pos - (char *) buffer->data;
@ -176,9 +176,9 @@ makeline(flno, filelist, nfiles, buffer, bufend, dummy2)
buffer->length = pos - (char *) buffer->data;
return(0);
}
FCLOSE(fd);
fd = 0;
if(flno >= 0) fstack[flno].fd = 0;
FCLOSE(fp);
fp = 0;
if(flno >= 0) fstack[flno].fp = 0;
} else {
buffer->data[100] = '\000';
warnx("line too long:ignoring %s...", buffer->data);
@ -211,7 +211,7 @@ makekey(flno, filelist, nfiles, buffer, bufend, ftbl)
for (;;) {
if (flno >= 0) {
get = seq;
if (!(dbdesc = fstack[flno].fd))
if (!(dbdesc = fstack[flno].fp))
return(EOF);
} else if (!dbdesc) {
if (fileno >= nfiles)
@ -236,7 +236,7 @@ makekey(flno, filelist, nfiles, buffer, bufend, ftbl)
if (c == EOF) {
FCLOSE(dbdesc);
dbdesc = 0;
if (flno >= 0) fstack[flno].fd = 0;
if (flno >= 0) fstack[flno].fp = 0;
} else {
((char *) line->data)[60] = '\000';
@ -248,11 +248,11 @@ makekey(flno, filelist, nfiles, buffer, bufend, ftbl)
}
/*
* get a key/line pair from fd
* get a key/line pair from fp
*/
int
seq(fd, line, key)
FILE *fd;
seq(fp, line, key)
FILE *fp;
DBT *key, *line;
{
static char *buf, flag = 1;
@ -265,7 +265,7 @@ seq(fd, line, key)
line->data = buf;
}
pos = buf;
while ((c = getc(fd)) != EOF) {
while ((c = getc(fp)) != EOF) {
if ((*pos++ = c) == REC_D) {
line->size = pos - buf;
return (0);
@ -273,7 +273,7 @@ seq(fd, line, key)
if (pos == end) {
line->size = MAXLLEN;
*--pos = REC_D;
while ((c = getc(fd)) != EOF) {
while ((c = getc(fp)) != EOF) {
if (c == REC_D)
return (BUFFEND);
}
@ -292,22 +292,22 @@ seq(fd, line, key)
* write a key/line pair to a temporary file
*/
void
putrec(rec, fd)
putrec(rec, fp)
register struct recheader *rec;
register FILE *fd;
register FILE *fp;
{
EWRITE(rec, 1, rec->length + sizeof(TRECHEADER), fd);
EWRITE(rec, 1, rec->length + sizeof(TRECHEADER), fp);
}
/*
* write a line to output
*/
void
putline(rec, fd)
putline(rec, fp)
register struct recheader *rec;
register FILE *fd;
register FILE *fp;
{
EWRITE(rec->data+rec->offset, 1, rec->length - rec->offset, fd);
EWRITE(rec->data+rec->offset, 1, rec->length - rec->offset, fp);
}
/*
@ -322,20 +322,20 @@ geteasy(flno, filelist, nfiles, rec, end, dummy2)
struct field *dummy2;
{
int i;
FILE *fd;
fd = fstack[flno].fd;
FILE *fp;
fp = fstack[flno].fp;
if ((u_char *) rec > end - sizeof(TRECHEADER))
return (BUFFEND);
if (!fread(rec, 1, sizeof(TRECHEADER), fd)) {
fclose(fd);
fstack[flno].fd = 0;
if (!fread(rec, 1, sizeof(TRECHEADER), fp)) {
fclose(fp);
fstack[flno].fp = 0;
return (EOF);
}
if (end - rec->data < rec->length) {
for (i = sizeof(TRECHEADER) - 1; i >= 0; i--)
ungetc(*((char *) rec + i), fd);
ungetc(*((char *) rec + i), fp);
return (BUFFEND);
}
fread(rec->data, rec->length, 1, fd);
fread(rec->data, rec->length, 1, fp);
return (0);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: fsort.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: fsort.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -47,7 +47,7 @@
#include "fsort.h"
#ifndef lint
__RCSID("$NetBSD: fsort.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $");
__RCSID("$NetBSD: fsort.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $");
__SCCSID("@(#)fsort.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -62,10 +62,10 @@ extern char *toutpath;
int PANIC = FSORTMAX;
void
fsort(binno, depth, infiles, nfiles, outfd, ftbl)
fsort(binno, depth, infiles, nfiles, outfp, ftbl)
register int binno, depth, nfiles;
register union f_handle infiles;
FILE *outfd;
FILE *outfp;
register struct field *ftbl;
{
register const u_char **keypos;
@ -79,10 +79,10 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
u_char *, struct field *);
register struct recheader *crec;
struct field tfield[2];
FILE *prevfd, *tailfd[FSORTMAX+1];
FILE *prevfp, *tailfp[FSORTMAX+1];
memset(tailfd, 0, sizeof(tailfd));
prevfd = outfd;
memset(tailfp, 0, sizeof(tailfp));
prevfp = outfp;
memset(tfield, 0, sizeof(tfield));
if (ftbl[0].flags & R)
tfield[0].weights = Rascii;
@ -113,7 +113,7 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
if (binno == weights[REC_D] &&
!(SINGL_FLD && ftbl[0].flags & F)) { /* pop */
rd_append(weights[REC_D],
infiles, nfiles, prevfd, buffer, bufend);
infiles, nfiles, prevfp, buffer, bufend);
break;
} else if (binno == weights[REC_D]) {
depth = 0; /* start over on flat weights */
@ -136,12 +136,12 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
}
if (c == BUFFEND || ntfiles || mfct) { /* push */
if (panic >= PANIC) {
fstack[MAXFCT-16+mfct].fd = ftmp();
fstack[MAXFCT-16+mfct].fp = ftmp();
if (radixsort(keylist, nelem, weights,
REC_D))
err(2, NULL);
append(keylist, nelem, depth, fstack[
MAXFCT-16+mfct].fd, putrec, ftbl);
MAXFCT-16+mfct].fp, putrec, ftbl);
mfct++;
/* reduce number of open files */
if (mfct == 16 ||(c == EOF && ntfiles)) {
@ -149,10 +149,10 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
crec->data);
memmove(tmpbuf, crec->data,
bufend - crec->data);
fstack[tfiles.top + ntfiles].fd
fstack[tfiles.top + ntfiles].fp
= ftmp();
fmerge(0, mstart, mfct, geteasy,
fstack[tfiles.top+ntfiles].fd,
fstack[tfiles.top+ntfiles].fp,
putrec, ftbl);
++ntfiles;
mfct = 0;
@ -161,9 +161,9 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
free(tmpbuf);
}
} else {
fstack[tfiles.top + ntfiles].fd= ftmp();
fstack[tfiles.top + ntfiles].fp= ftmp();
onepass(keylist, depth, nelem, sizes,
weights, fstack[tfiles.top+ntfiles].fd);
weights, fstack[tfiles.top+ntfiles].fp);
++ntfiles;
}
}
@ -173,16 +173,16 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
if (nelem > 1)
if (radixsort(keylist, nelem, weights, REC_D))
err(2, NULL);
append(keylist, nelem, depth, outfd, putline, ftbl);
append(keylist, nelem, depth, outfp, putline, ftbl);
break; /* pop */
}
if (panic >= PANIC) {
if (!ntfiles)
fmerge(0, mstart, mfct, geteasy,
outfd, putline, ftbl);
outfp, putline, ftbl);
else
fmerge(0, tfiles, ntfiles, geteasy,
outfd, putline, ftbl);
outfp, putline, ftbl);
break;
}
@ -205,18 +205,18 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
if (!sizes[i]) /* bin empty; step ahead file offset */
get(i, tfiles, ntfiles, crec, bufend, 0);
else
fsort(i, depth+1, tfiles, ntfiles, outfd, ftbl);
fsort(i, depth+1, tfiles, ntfiles, outfp, ftbl);
}
if (lastb != maxb) {
if (prevfd != outfd)
tailfd[panic] = prevfd;
prevfd = ftmp();
if (prevfp != outfp)
tailfp[panic] = prevfp;
prevfp = ftmp();
for (i = maxb+1; i <= lastb; i++)
if (!sizes[i])
get(i, tfiles, ntfiles, crec, bufend,0);
else
fsort(i, depth+1, tfiles, ntfiles,
prevfd, ftbl);
prevfp, ftbl);
}
/* sort biggest (or last) bin at this level */
@ -226,14 +226,14 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
infiles.top = tfiles.top; /* getnext will free tfiles, */
nfiles = ntfiles; /* so overwrite them */
}
if (prevfd != outfd) {
concat(outfd, prevfd);
fclose(prevfd);
if (prevfp != outfp) {
concat(outfp, prevfp);
fclose(prevfp);
}
for (i = panic; i >= 0; --i)
if (tailfd[i]) {
concat(outfd, tailfd[i]);
fclose(tailfd[i]);
if (tailfp[i]) {
concat(outfp, tailfp[i]);
fclose(tailfp[i]);
}
}
@ -242,12 +242,12 @@ fsort(binno, depth, infiles, nfiles, outfd, ftbl)
*/
#define swap(a, b, t) t = a, a = b, b = t
void
onepass(a, depth, n, sizes, tr, fd)
onepass(a, depth, n, sizes, tr, fp)
const u_char **a;
int depth;
long n, sizes[];
u_char *tr;
FILE *fd;
FILE *fp;
{
long tsizes[NBINS+1];
const u_char **bin[257], ***bp, ***bpmax, **top[256], ***tp;
@ -283,9 +283,9 @@ onepass(a, depth, n, sizes, tr, fd)
n = an - ak;
tsizes[c] += n * sizeof(TRECHEADER);
/* tell getnext how many elements in this bin, this segment. */
EWRITE(tsizes+c, sizeof(long), 1, fd);
EWRITE(tsizes+c, sizeof(long), 1, fp);
sizes[c] += tsizes[c];
for (; ak < an; ++ak)
putrec((RECHEADER *) *ak, fd);
putrec((RECHEADER *) *ak, fp);
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: fsort.h,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: fsort.h,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -55,7 +55,7 @@ extern u_char *buffer, *linebuf;
* are sorted.
*/
struct tempfile {
FILE *fd;
FILE *fp;
u_char maxb;
u_char lastb;
long max_o;

View File

@ -1,4 +1,4 @@
/* $NetBSD: msort.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: msort.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -40,7 +40,7 @@
#include "fsort.h"
#ifndef lint
__RCSID("$NetBSD: msort.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $");
__RCSID("$NetBSD: msort.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $");
__SCCSID("@(#)msort.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -69,12 +69,12 @@ static int cmp __P((struct recheader *, struct recheader *));
static int insert __P((struct mfile **, struct mfile **, int, int));
void
fmerge(binno, files, nfiles, get, outfd, fput, ftbl)
fmerge(binno, files, nfiles, get, outfp, fput, ftbl)
union f_handle files;
int binno, nfiles;
int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
struct field *));
FILE *outfd;
FILE *outfp;
void (*fput) __P((struct recheader *, FILE *));
struct field *ftbl;
{
@ -106,7 +106,7 @@ fmerge(binno, files, nfiles, get, outfd, fput, ftbl)
put = putrec;
for (j = 0; j < nfiles; j += 16) {
if (nfiles <= 16) {
tout = outfd;
tout = outfp;
put = fput;
}
else
@ -114,17 +114,17 @@ fmerge(binno, files, nfiles, get, outfd, fput, ftbl)
last = min(16, nfiles - j);
if (binno < 0) {
for (i = 0; i < last; i++)
if (!(l_fstack[i+MAXFCT-1-16].fd =
if (!(l_fstack[i+MAXFCT-1-16].fp =
fopen(files.names[j + i], "r")))
err(2, "%s", files.names[j+i]);
merge(MAXFCT-1-16, last, get, tout, put, ftbl);
}
else {
for (i = 0; i< last; i++)
rewind(l_fstack[i+j].fd);
rewind(l_fstack[i+j].fp);
merge(files.top+j, last, get, tout, put, ftbl);
}
if (nfiles > 16) l_fstack[j/16].fd = tout;
if (nfiles > 16) l_fstack[j/16].fp = tout;
}
nfiles = (nfiles + 15) / 16;
if (nfiles == 1)
@ -138,12 +138,12 @@ fmerge(binno, files, nfiles, get, outfd, fput, ftbl)
}
void
merge(infl0, nfiles, get, outfd, put, ftbl)
merge(infl0, nfiles, get, outfp, put, ftbl)
int infl0, nfiles;
int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
struct field *));
void (*put)(struct recheader *, FILE *);
FILE *outfd;
FILE *outfp;
struct field *ftbl;
{
int c, i, j;
@ -175,14 +175,14 @@ merge(infl0, nfiles, get, outfd, put, ftbl)
for (c = 1; c == 1;) {
if (EOF == (c = get(cfile->flno, dummy, nfiles,
cfile->rec, cfile->end, ftbl))) {
put(flist[0]->rec, outfd);
put(flist[0]->rec, outfp);
memmove(flist, flist + 1,
sizeof(MFILE *) * (--nfiles));
cfile->flno = flist[0]->flno;
break;
}
if (!(c = insert(flist, &cfile, nfiles, DELETE)))
put(cfile->rec, outfd);
put(cfile->rec, outfp);
}
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: pathnames.h,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: pathnames.h,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -39,4 +39,3 @@
*/
#define _PATH_STDIN "/dev/stdin"
#define _PATH_SORTTMP "/var/tmp/sort.XXXXXXXX"

View File

@ -1,4 +1,4 @@
.\" $NetBSD: sort.1,v 1.2 2000/10/07 18:37:10 bjh21 Exp $
.\" $NetBSD: sort.1,v 1.3 2000/10/07 20:37:06 bjh21 Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -134,6 +134,9 @@ the
option.)
.It Fl r
Reverse the sense of comparisons.
.It Fl H
Use a merge sort instead of a radix sort. This option should be
used for files larger than 60Mb.
.El
.Pp
The treatment of field separators can be altered using the
@ -306,6 +309,17 @@ option is still supported, except for
which has no
.Fl k
equivalent.
.Sh ENVIRONMENT
If the following environment variable exists, it is utilized by
.Nm sort .
.Bl -tag -width Fl
.It Ev TMPDIR
.Nm Sort
uses the contents of the
.Ev TMPDIR
environment variable as the path in which to store
temporary files.
.El
.Sh FILES
.Bl -tag -width Pa -compact
.It Pa /var/tmp/sort.*

View File

@ -1,4 +1,4 @@
/* $NetBSD: sort.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: sort.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -51,7 +51,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\n\
#endif /* not lint */
#ifndef lint
__RCSID("$NetBSD: sort.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $");
__RCSID("$NetBSD: sort.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $");
__SCCSID("@(#)sort.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -81,6 +81,7 @@ extern int ncols;
char devstdin[] = _PATH_STDIN;
char toutpath[_POSIX_PATH_MAX];
char *tmpdir = _PATH_VARTMP;
static void cleanup __P((void));
static void onsignal __P((int));
@ -102,7 +103,7 @@ main(argc, argv)
char *outfile, *outpath = 0;
struct field fldtab[ND+2], *ftpos;
union f_handle filelist;
FILE *outfd;
FILE *outfp = NULL;
memset(fldtab, 0, (ND+2)*sizeof(struct field));
memset(d_mask, 0, NBINS);
d_mask[REC_D = '\n'] = REC_D_F;
@ -111,6 +112,8 @@ main(argc, argv)
ftpos = fldtab;
fixit(&argc, argv);
while ((ch = getopt(argc, argv, "bcdfik:mHno:rt:T:ux")) != EOF) {
if ((outfile = getenv("TMPDIR")))
tmpdir = outfile;
switch (ch) {
case 'b': fldtab->flags |= BI | BT;
break;
@ -227,25 +230,28 @@ main(argc, argv)
struct sigaction act = {0};
int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
SIGVTALRM, SIGPROF, 0};
int outfd;
errno = 0;
if (access(outpath, W_OK))
err(2, "%s", outpath);
act.sa_handler = onsignal;
act.sa_flags = SA_RESTART | SA_RESETHAND;
(void)snprintf(toutpath, sizeof(toutpath), "%sXXXX", outpath);
outfile = mktemp(toutpath);
if (!outfile)
err(2, "%s", toutpath);
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);
} else outfile = outpath;
if (!(outfd = fopen(outfile, "w")))
err(2, "%s", outfile);
} else
outfile = outpath;
if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
err(2, outfile);
if (mflag)
fmerge(-1, filelist, argc-optind, get, outfd, putline, fldtab);
fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
else
fsort(-1, 0, filelist, argc-optind, outfd, fldtab);
fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
if (outfile != outpath) {
if (access(outfile, 0))
err(2, "%s", outfile);

View File

@ -1,4 +1,4 @@
/* $NetBSD: sort.h,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: sort.h,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -140,5 +140,6 @@ extern u_char alltable[NBINS], dtable[NBINS], itable[NBINS];
extern u_char d_mask[NBINS];
extern int SINGL_FLD, SEP_FLAG, UNIQUE;
extern int REC_D;
extern char *tmpdir;
#include "extern.h"

View File

@ -1,4 +1,4 @@
/* $NetBSD: tmp.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $ */
/* $NetBSD: tmp.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $ */
/*-
* Copyright (c) 1993
@ -39,7 +39,7 @@
#include "sort.h"
#ifndef lint
__RCSID("$NetBSD: tmp.c,v 1.2 2000/10/07 18:37:10 bjh21 Exp $");
__RCSID("$NetBSD: tmp.c,v 1.3 2000/10/07 20:37:06 bjh21 Exp $");
__SCCSID("@(#)tmp.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -61,32 +61,23 @@ __SCCSID("@(#)tmp.c 8.1 (Berkeley) 6/6/93");
FILE *
ftmp()
{
static char *envtmp;
sigset_t set, oset;
static int first = 0;
FILE *fd;
FILE *fp;
int fd;
char pathb[_POSIX_PATH_MAX], *path;
path = pathb;
if (!first && !envtmp) {
envtmp = getenv("TMPDIR");
first = 1;
}
if (envtmp)
(void)snprintf(path,
sizeof(pathb), "%s/%s", envtmp, _NAME_TMP);
else {
memmove(path, _PATH_SORTTMP, sizeof(_PATH_SORTTMP));
}
(void)snprintf(path, sizeof(pathb), "%s%s%s", tmpdir,
(tmpdir[strlen(tmpdir)-1] != '/') ? "/" : "", _NAME_TMP);
sigfillset(&set);
(void)sigprocmask(SIG_BLOCK, &set, &oset);
path = mktemp(path);
if (!path)
err(2, NULL);
if (!(fd = fopen(path, "w+")))
err(2, "%s", path);
if ((fd = mkstemp(path)) < 0)
err(2, path);
if (!(fp = fdopen(fd, "w+")))
err(2, path);
(void)unlink(path);
(void)sigprocmask(SIG_SETMASK, &oset, NULL);
return (fd);
};
return (fp);
}