Module Name:    src
Committed By:   christos
Date:           Sat Dec 18 23:09:48 UTC 2010

Modified Files:
        src/usr.bin/sort: fields.c init.c sort.1 sort.c sort.h

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


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/usr.bin/sort/fields.c
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/sort/init.c
cvs rdiff -u -r1.30 -r1.31 src/usr.bin/sort/sort.1
cvs rdiff -u -r1.59 -r1.60 src/usr.bin/sort/sort.c
cvs rdiff -u -r1.32 -r1.33 src/usr.bin/sort/sort.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/sort/fields.c
diff -u src/usr.bin/sort/fields.c:1.31 src/usr.bin/sort/fields.c:1.32
--- src/usr.bin/sort/fields.c:1.31	Fri Nov  6 13:34:22 2009
+++ src/usr.bin/sort/fields.c	Sat Dec 18 18:09:48 2010
@@ -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);
+}

Index: src/usr.bin/sort/init.c
diff -u src/usr.bin/sort/init.c:1.27 src/usr.bin/sort/init.c:1.28
--- src/usr.bin/sort/init.c:1.27	Sat Jun  5 20:00:33 2010
+++ src/usr.bin/sort/init.c	Sat Dec 18 18:09:48 2010
@@ -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;
 	}
 }
 

Index: src/usr.bin/sort/sort.1
diff -u src/usr.bin/sort/sort.1:1.30 src/usr.bin/sort/sort.1:1.31
--- src/usr.bin/sort/sort.1:1.30	Fri May 14 12:58:32 2010
+++ src/usr.bin/sort/sort.1	Sat Dec 18 18:09:48 2010
@@ -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.

Index: src/usr.bin/sort/sort.c
diff -u src/usr.bin/sort/sort.c:1.59 src/usr.bin/sort/sort.c:1.60
--- src/usr.bin/sort/sort.c:1.59	Sat Jun  5 13:44:51 2010
+++ src/usr.bin/sort/sort.c	Sat Dec 18 18:09:48 2010
@@ -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");

Index: src/usr.bin/sort/sort.h
diff -u src/usr.bin/sort/sort.h:1.32 src/usr.bin/sort/sort.h:1.33
--- src/usr.bin/sort/sort.h:1.32	Sat Jun  5 13:44:51 2010
+++ src/usr.bin/sort/sort.h	Sat Dec 18 18:09:48 2010
@@ -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 */

Reply via email to