Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/sort Add an 'l' style for sorting that sorts by the ...



details:   https://anonhg.NetBSD.org/src/rev/e29bb4aeaf2a
branches:  trunk
changeset: 759875:e29bb4aeaf2a
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Dec 18 23:09:48 2010 +0000

description:
Add an 'l' style for sorting that sorts by the string length of the field.

diffstat:

 usr.bin/sort/fields.c |  18 ++++++++++++++++--
 usr.bin/sort/init.c   |  27 ++++++++++++++-------------
 usr.bin/sort/sort.1   |  10 ++++++----
 usr.bin/sort/sort.c   |  12 ++++++------
 usr.bin/sort/sort.h   |  17 +++++++++--------
 5 files changed, 51 insertions(+), 33 deletions(-)

diffs (228 lines):

diff -r f0e9f2f367bd -r e29bb4aeaf2a usr.bin/sort/fields.c
--- a/usr.bin/sort/fields.c     Sat Dec 18 20:57:41 2010 +0000
+++ b/usr.bin/sort/fields.c     Sat Dec 18 23:09:48 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fields.c,v 1.31 2009/11/06 18:34:22 joerg Exp $        */
+/*     $NetBSD: fields.c,v 1.32 2010/12/18 23:09:48 christos Exp $     */
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
 
 #include "sort.h"
 
-__RCSID("$NetBSD: fields.c,v 1.31 2009/11/06 18:34:22 joerg Exp $");
+__RCSID("$NetBSD: fields.c,v 1.32 2010/12/18 23:09:48 christos Exp $");
 
 #define SKIP_BLANKS(ptr) {                                     \
        if (BLANK & d_mask[*(ptr)])                             \
@@ -80,6 +80,7 @@
                
 static u_char *enterfield(u_char *, const u_char *, struct field *, int);
 static u_char *number(u_char *, const u_char *, u_char *, u_char *, int);
+static u_char *length(u_char *, const u_char *, u_char *, u_char *, int);
 
 #define DECIMAL_POINT '.'
 
@@ -200,6 +201,8 @@
                        end = tcol.p->end;
        }
 
+       if (flags & L)
+               return length(tablepos, endkey, start, end, flags);
        if (flags & N)
                return number(tablepos, endkey, start, end, flags);
 
@@ -361,3 +364,14 @@
 
        return (last_nz_pos);
 }
+
+static u_char *
+length(u_char *pos, const u_char *bufend, u_char *line, u_char *lineend,
+    int flag)
+{
+       u_char buf[32];
+       int l;
+       SKIP_BLANKS(line);
+       l = snprintf((char *)buf, sizeof(buf), "%td", lineend - line);
+       return number(pos, bufend, buf, buf + l, flag);
+}
diff -r f0e9f2f367bd -r e29bb4aeaf2a usr.bin/sort/init.c
--- a/usr.bin/sort/init.c       Sat Dec 18 20:57:41 2010 +0000
+++ b/usr.bin/sort/init.c       Sat Dec 18 23:09:48 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: init.c,v 1.27 2010/06/06 00:00:33 wiz Exp $    */
+/*     $NetBSD: init.c,v 1.28 2010/12/18 23:09:48 christos Exp $       */
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 
 #include "sort.h"
 
-__RCSID("$NetBSD: init.c,v 1.27 2010/06/06 00:00:33 wiz Exp $");
+__RCSID("$NetBSD: init.c,v 1.28 2010/12/18 23:09:48 christos Exp $");
 
 #include <ctype.h>
 #include <string.h>
@@ -223,17 +223,18 @@
 optval(int desc, int tcolflag)
 {
        switch(desc) {
-               case 'b':
-                       if (!tcolflag)
-                               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);
+       case 'b':
+               if (!tcolflag)
+                       return BI;
+               else
+                       return BT;
+       case 'd': return D;
+       case 'f': return F;
+       case 'i': return I;
+       case 'l': return L;
+       case 'n': return N;
+       case 'r': return R;
+       default:  return 0;
        }
 }
 
diff -r f0e9f2f367bd -r e29bb4aeaf2a usr.bin/sort/sort.1
--- a/usr.bin/sort/sort.1       Sat Dec 18 20:57:41 2010 +0000
+++ b/usr.bin/sort/sort.1       Sat Dec 18 23:09:48 2010 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: sort.1,v 1.30 2010/05/14 16:58:32 jruoho Exp $
+.\"    $NetBSD: sort.1,v 1.31 2010/12/18 23:09:48 christos Exp $
 .\"
 .\" Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -59,7 +59,7 @@
 .\"
 .\"     @(#)sort.1     8.1 (Berkeley) 6/6/93
 .\"
-.Dd May 14, 2010
+.Dd December 18, 2010
 .Dt SORT 1
 .Os
 .Sh NAME
@@ -67,7 +67,7 @@
 .Nd sort or merge text files
 .Sh SYNOPSIS
 .Nm sort
-.Op Fl bcdfHimnrSsu
+.Op Fl bcdfHilmnrSsu
 .Oo
 .Fl k
 .Ar field1 Ns Op Li \&, Ns Ar field2
@@ -155,6 +155,8 @@
 equivalents to be the same for purposes of comparison.
 .It Fl i
 Ignore all non-printable characters.
+.It Fl l
+Sort by the string length of the field, not by the field itself.
 .It Fl n
 An initial numeric string, consisting of optional blank space, optional
 minus sign, and zero or more digits (including decimal point)
@@ -275,7 +277,7 @@
 .Ar m Ns Li \&. Ns Ar n
 and can be followed by one or more of the letters
 .Cm b , d , f , i ,
-.Cm n ,
+.Cm l , n ,
 and
 .Cm r ,
 which correspond to the options discussed above.
diff -r f0e9f2f367bd -r e29bb4aeaf2a usr.bin/sort/sort.c
--- a/usr.bin/sort/sort.c       Sat Dec 18 20:57:41 2010 +0000
+++ b/usr.bin/sort/sort.c       Sat Dec 18 23:09:48 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sort.c,v 1.59 2010/06/05 17:44:51 dholland Exp $       */
+/*     $NetBSD: sort.c,v 1.60 2010/12/18 23:09:48 christos Exp $       */
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -76,7 +76,7 @@
  The Regents of the University of California.  All rights reserved.");
 #endif /* not lint */
 
-__RCSID("$NetBSD: sort.c,v 1.59 2010/06/05 17:44:51 dholland Exp $");
+__RCSID("$NetBSD: sort.c,v 1.60 2010/12/18 23:09:48 christos Exp $");
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -147,7 +147,7 @@
        fldtab = emalloc(fldtab_sz * sizeof(*fldtab));
        memset(fldtab, 0, fldtab_sz * sizeof(*fldtab));
 
-#define SORT_OPTS "bcdD:fik:mHno:rR:sSt:T:ux"
+#define SORT_OPTS "bcdD:fHik:lmno:rR:sSt:T:ux"
 
        /* Convert "+field" args to -f format */
        fixit(&argc, argv, SORT_OPTS);
@@ -167,7 +167,7 @@
                        for (i = 0; optarg[i]; i++)
                            debug_flags |= 1 << (optarg[i] & 31);
                        break;
-               case 'd': case 'f': case 'i': case 'n':
+               case 'd': case 'f': case 'i': case 'n': case 'l':
                        fldtab[0].flags |= optval(ch, 0);
                        break;
                case 'H':
@@ -286,7 +286,7 @@
 
        if (fldtab[1].icol.num == 0) {
                /* No sort key specified */
-               if (fldtab[0].flags & (I|D|F|N)) {
+               if (fldtab[0].flags & (I|D|F|N|L)) {
                        /* Modified - generate a key that covers the line */
                        fldtab[0].flags &= ~(BI|BT);
                        setfield("1", &fldtab[++fld_cnt], fldtab->flags);
@@ -398,7 +398,7 @@
        if (msg != NULL)
                (void)fprintf(stderr, "%s: %s\n", getprogname(), msg);
        (void)fprintf(stderr,
-           "usage: %s [-bcdfHimnrSsu] [-k field1[,field2]] [-o output]"
+           "usage: %s [-bcdfHilmnrSsu] [-k field1[,field2]] [-o output]"
            " [-R char] [-T dir]", getprogname());
        (void)fprintf(stderr,
            "             [-t char] [file ...]\n");
diff -r f0e9f2f367bd -r e29bb4aeaf2a usr.bin/sort/sort.h
--- a/usr.bin/sort/sort.h       Sat Dec 18 20:57:41 2010 +0000
+++ b/usr.bin/sort/sort.h       Sat Dec 18 23:09:48 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sort.h,v 1.32 2010/06/05 17:44:51 dholland Exp $       */
+/*     $NetBSD: sort.h,v 1.33 2010/12/18 23:09:48 christos Exp $       */
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -78,13 +78,14 @@
 
 /* values for masks, weights, and other flags. */
 /* R and F get used to index weight_tables[] */
-#define R 1            /* Field is reversed */
-#define F 2            /* weight lower and upper case the same */
-#define I 4            /* mask out non-printable characters */
-#define D 8            /* sort alphanumeric characters only */
-#define N 16           /* Field is a number */
-#define BI 32          /* ignore blanks in icol */
-#define BT 64          /* ignore blanks in tcol */
+#define        R       0x01    /* Field is reversed */
+#define        F       0x02    /* weight lower and upper case the same */
+#define        I       0x04    /* mask out non-printable characters */
+#define        D       0x08    /* sort alphanumeric characters only */
+#define        N       0x10    /* Field is a number */
+#define        BI      0x20    /* ignore blanks in icol */
+#define        BT      0x40    /* ignore blanks in tcol */
+#define        L       0x80    /* Sort by field length */
 
 /* masks for delimiters: blanks, fields, and termination. */
 #define BLANK 1                /* ' ', '\t'; '\n' if -R is invoked */



Home | Main Index | Thread Index | Old Index