tech-userlevel archive

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

Proposed modification to seq



All,

The seq command behaves a little differently than I expect.  I needed a comma
separated list of integers, but seq gave me this:

 $seq -s , 1 3
 1,2,3,$

Notice the extra comma and no trailing return.  The comma is troublesome for my
particular purpose.  It seems that the item separator (-s) is really an item
terminator.  I was expecting this:

 $seq -s , 1 3
 1,2,3
 $

I'm sure there is some not-obvious-to-me reason for the current behavior.
However, I implemented what I expected and build a distribution without issue.
The attached diff implements the proposed behavior.

Aran






? .gdbinit
? seq
Index: seq.1
===================================================================
RCS file: /cvsroot/src/usr.bin/seq/seq.1,v
retrieving revision 1.9
diff -u -r1.9 seq.1
--- seq.1	18 Aug 2016 22:55:28 -0000	1.9
+++ seq.1	13 Dec 2018 17:35:48 -0000
@@ -117,8 +117,7 @@
 can contain character escape sequences in backslash notation as
 defined in
 .St -ansiC .
-This option is useful when the default separator
-does not contain a
+The default is
 .Cm \en .
 .It Fl w
 Equalize the widths of all numbers by padding with zeros as necessary.
Index: seq.c
===================================================================
RCS file: /cvsroot/src/usr.bin/seq/seq.c,v
retrieving revision 1.10
diff -u -r1.10 seq.c
--- seq.c	29 Oct 2017 01:28:46 -0000	1.10
+++ seq.c	13 Dec 2018 17:35:48 -0000
@@ -85,7 +85,7 @@
 	struct lconv *locale;
 	char *fmt = NULL;
 	const char *sep = "\n";
-	const char *term = NULL;
+	const char *term = "\n";
 	char pad = ZERO;

 	/* Determine the locale's decimal point. */
@@ -132,7 +132,7 @@
 		fprintf(stderr,
 		    "usage: %s [-w] [-f format] [-s string] [-t string] [first [incr]] last\n",
 		    getprogname());
-		exit(1);
+exit(1);
 	}

 	last = e_atof(argv[argc - 1]);
@@ -171,14 +171,16 @@
 		fmt = generate_format(first, incr, last, equalize, pad);

 	if (incr > 0) {
-		for (; first <= last; first += incr) {
-			printf(fmt, first);
+		printf(fmt, first);
+		for (first += incr; first <= last; first += incr) {
 			fputs(sep, stdout);
+			printf(fmt, first);
 		}
 	} else {
-		for (; first >= last; first += incr) {
-			printf(fmt, first);
+		printf(fmt, first);
+		for (first += incr; first >= last; first += incr) {
 			fputs(sep, stdout);
+			printf(fmt, first);
 		}
 	}
 	if (term != NULL)

Attachment: pgpJsNZocom9X.pgp
Description: PGP signature



Home | Main Index | Thread Index | Old Index