summaryrefslogtreecommitdiff
path: root/usr.bin/sort
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2009-12-22 19:47:03 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2009-12-22 19:47:03 +0000
commit0a8391312cacecb8173803803b81679db84f3c41 (patch)
treedc62cbcd8029f172f97dc3e01f1d0329920b4356 /usr.bin/sort
parente381954210095df0c8c5cd078331aa2a6b2309c2 (diff)
implement -C (silent -c), required by POSIX.1-2008;
patch from Daniel Dickman <didickman at gmail dot com> tweaked by me; "looks ok" millert@, manual help and ok jmc@
Diffstat (limited to 'usr.bin/sort')
-rw-r--r--usr.bin/sort/extern.h5
-rw-r--r--usr.bin/sort/msort.c19
-rw-r--r--usr.bin/sort/sort.141
-rw-r--r--usr.bin/sort/sort.c18
4 files changed, 52 insertions, 31 deletions
diff --git a/usr.bin/sort/extern.h b/usr.bin/sort/extern.h
index f7c9eddfab1..afdcd9d1975 100644
--- a/usr.bin/sort/extern.h
+++ b/usr.bin/sort/extern.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: extern.h,v 1.7 2003/06/26 00:12:39 deraadt Exp $ */
+/* $OpenBSD: extern.h,v 1.8 2009/12/22 19:47:02 schwarze Exp $ */
/*-
* Copyright (c) 1993
@@ -61,7 +61,8 @@ void onepass(u_char **, int, long, long *, u_char *, FILE *);
int optval(int, int);
void order(union f_handle,
int (*)(int, union f_handle, int, RECHEADER *, u_char *, struct field *),
- struct field *);
+ struct field *,
+ int c_warn);
void putline(RECHEADER *, FILE *);
void putrec(RECHEADER *, FILE *);
void rd_append(int, union f_handle, int, FILE *, u_char *, u_char *);
diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c
index 7b208107342..78bd0e0f550 100644
--- a/usr.bin/sort/msort.c
+++ b/usr.bin/sort/msort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: msort.c,v 1.22 2009/10/27 23:59:43 deraadt Exp $ */
+/* $OpenBSD: msort.c,v 1.23 2009/12/22 19:47:02 schwarze Exp $ */
/*-
* Copyright (c) 1993
@@ -232,7 +232,8 @@ insert(struct mfile **flist, struct mfile **rec, int ttop,
void
order(union f_handle infile,
int (*get)(int, union f_handle, int, RECHEADER *, u_char *, struct field *),
- struct field *ftbl)
+ struct field *ftbl,
+ int c_warn)
{
u_char *crec_end, *prec_end, *trec_end;
int c;
@@ -256,13 +257,19 @@ order(union f_handle infile,
while (get(-1, infile, 1, crec, crec_end, ftbl) == 0) {
if (0 < (c = cmp(prec, crec))) {
crec->data[crec->length-1] = 0;
- errx(1, "found disorder: %s",
- crec->data+crec->offset);
+ if (c_warn)
+ errx(1, "found disorder: %s",
+ crec->data+crec->offset);
+ else
+ exit(1);
}
if (UNIQUE && !c) {
crec->data[crec->length-1] = 0;
- errx(1, "found non-uniqueness: %s",
- crec->data+crec->offset);
+ if (c_warn)
+ errx(1, "found non-uniqueness: %s",
+ crec->data+crec->offset);
+ else
+ exit(1);
}
/* Swap pointers so that this record is on place
* pointed to by prec and new record is read to place
diff --git a/usr.bin/sort/sort.1 b/usr.bin/sort/sort.1
index e0fd38ca1f9..b1d94806154 100644
--- a/usr.bin/sort/sort.1
+++ b/usr.bin/sort/sort.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: sort.1,v 1.34 2009/08/16 09:41:08 sobrado Exp $
+.\" $OpenBSD: sort.1,v 1.35 2009/12/22 19:47:02 schwarze Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -32,7 +32,7 @@
.\"
.\" @(#)sort.1 8.1 (Berkeley) 6/6/93
.\"
-.Dd $Mdocdate: August 16 2009 $
+.Dd $Mdocdate: December 22 2009 $
.Dt SORT 1
.Os
.Sh NAME
@@ -40,7 +40,7 @@
.Nd sort or merge text files
.Sh SYNOPSIS
.Nm sort
-.Op Fl bcdfHimnrsuz
+.Op Fl bCcdfHimnrsuz
.Sm off
.Op Fl k\ \& Ar field1 Op , Ar field2
.Sm on
@@ -63,17 +63,16 @@ regards each input line as a single field.
.Pp
The options are as follows:
.Bl -tag -width Ds
-.It Fl c
+.It Fl C
Check that the single input file is sorted.
-If the file is not sorted,
-.Nm
-produces the appropriate error messages and exits with code 1; otherwise,
-.Nm
-returns 0.
-.Nm
-.Fl c
-produces no output, except the error messages on
-.Em stderr .
+If it is, exit 0; if it's not, exit 1.
+In either case, produce no output.
+.It Fl c
+Like
+.Fl C ,
+but write a message to
+.Em stderr
+if the input file is not sorted.
.It Fl m
Merge only; the input files are assumed to be pre-sorted.
.It Fl o Ar output
@@ -95,8 +94,10 @@ does not exist.
.It Fl u
Unique: suppress all but one in each set of lines having equal keys.
If used with the
+.Fl C
+or
.Fl c
-option, also check that there are no lines with duplicate keys.
+options, also check that there are no lines with duplicate keys.
.El
.Pp
The following options override the default ordering rules.
@@ -338,15 +339,21 @@ equivalent.
.Pp
The
.Nm
-utility shall exit with one of the following values:
+utility exits with one of the following values:
.Pp
.Bl -tag -width flag -compact
.It 0
Normal behavior.
.It 1
-On disorder (or non-uniqueness) with the
+The input file is not sorted and
+.Fl C
+or
.Fl c
-option.
+was given, or there are duplicate keys and
+.Fl Cu
+or
+.Fl cu
+was given.
.It 2
An error occurred.
.El
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index 99158ffc17c..adfcca6b2de 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sort.c,v 1.38 2009/10/28 20:41:39 guenther Exp $ */
+/* $OpenBSD: sort.c,v 1.39 2009/12/22 19:47:02 schwarze Exp $ */
/*-
* Copyright (c) 1993
@@ -93,7 +93,7 @@ main(int argc, char *argv[])
{
int (*get)(int, union f_handle, int, RECHEADER *, u_char *, struct field *);
int ch, i, stdinflag = 0, tmp = 0;
- char nfields = 0, cflag = 0, mflag = 0;
+ char nfields = 0, cflag = 0, c_warn = 0, mflag = 0;
char *outfile, *outpath = 0;
struct field *fldtab, *ftpos;
union f_handle filelist;
@@ -111,7 +111,7 @@ main(int argc, char *argv[])
fixit(&argc, argv);
if (!issetugid() && (outfile = getenv("TMPDIR")))
tmpdir = outfile;
- while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:zs")) != -1) {
+ while ((ch = getopt(argc, argv, "bCcdfik:mHno:rR:t:T:uy:zs")) != -1) {
switch (ch) {
case 'b': fldtab->flags |= BI | BT;
break;
@@ -159,8 +159,13 @@ main(int argc, char *argv[])
case 'u':
UNIQUE = 1;
break;
+ case 'C':
+ cflag = 1;
+ c_warn = 0;
+ break;
case 'c':
cflag = 1;
+ c_warn = 1;
break;
case 'm':
mflag = 1;
@@ -188,7 +193,8 @@ main(int argc, char *argv[])
}
if (cflag && argc > optind+1)
- errx(2, "too many input files for -c option");
+ errx(2, "too many input files for the -%c option",
+ c_warn ? 'c' : 'C');
if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
outpath = argv[argc-1];
@@ -249,7 +255,7 @@ main(int argc, char *argv[])
}
if (cflag) {
- order(filelist, get, fldtab);
+ order(filelist, get, fldtab, c_warn);
/* NOT REACHED */
}
@@ -332,7 +338,7 @@ usage(char *msg)
if (msg != NULL)
warnx("%s", msg);
- (void)fprintf(stderr, "usage: %s [-bcdfHimnrsuz] "
+ (void)fprintf(stderr, "usage: %s [-bCcdfHimnrsuz] "
"[-k field1[,field2]] [-o output] [-R char]\n"
"\t[-T dir] [-t char] [file ...]\n", __progname);
exit(2);