commit 74c0b4775f7a3ebffe02e8028b01066b37e771e7
parent 6b950e436b9d81edb982d740d02198ba184402fb
Author: Joachim Lundberg <joachim.lundberg@telia.com>
Date: Wed, 3 Apr 2019 13:53:51 +0200
Add a header flag for sort.
Diffstat:
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/sort.1 b/sort.1
@@ -39,6 +39,8 @@ indicating the location of the disorder.
Skip non-whitespace and non-alphanumeric characters.
.It Fl f
Ignore letter case when sorting.
+.It FL h
+First line is header line; do not sort it.
.It FL i
Skip non-printable characters.
.It Fl k Ar key
diff --git a/sort.c b/sort.c
@@ -31,6 +31,7 @@ enum {
MOD_D = 1 << 4,
MOD_F = 1 << 5,
MOD_I = 1 << 6,
+ MOD_H = 1 << 7,
};
static TAILQ_HEAD(kdhead, keydef) kdhead = TAILQ_HEAD_INITIALIZER(kdhead);
@@ -319,7 +320,7 @@ addkeydef(char *kdstr, int flags)
static void
usage(void)
{
- enprintf(2, "usage: %s [-Cbcdfimnru] [-o outfile] [-t delim] "
+ enprintf(2, "usage: %s [-Cbcdfhimnru] [-o outfile] [-t delim] "
"[-k def]... [file ...]\n", argv0);
}
@@ -348,6 +349,9 @@ main(int argc, char *argv[])
case 'f':
global_flags |= MOD_F;
break;
+ case 'h':
+ global_flags |= MOD_H;
+ break;
case 'i':
global_flags |= MOD_I;
break;
@@ -417,8 +421,13 @@ main(int argc, char *argv[])
if (outfile && !(ofp = fopen(outfile, "w")))
eprintf("fopen %s:", outfile);
- qsort(linebuf.lines, linebuf.nlines, sizeof(*linebuf.lines),
- (int (*)(const void *, const void *))slinecmp);
+ if ((global_flags & MOD_H) && linebuf.nlines > 1) {
+ qsort(linebuf.lines + 1, linebuf.nlines - 1, sizeof(*linebuf.lines),
+ (int (*)(const void *, const void *))slinecmp);
+ } else {
+ qsort(linebuf.lines, linebuf.nlines, sizeof(*linebuf.lines),
+ (int (*)(const void *, const void *))slinecmp);
+ }
for (i = 0; i < linebuf.nlines; i++) {
if (!uflag || i == 0 ||