CVS commit: src/usr.bin/seq

2024-05-04 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sat May  4 13:29:41 UTC 2024

Modified Files:
src/usr.bin/seq: seq.c

Log Message:
Bail if increment is too small for the precision to avoid an infinite loop.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/usr.bin/seq/seq.c

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/seq/seq.c
diff -u src/usr.bin/seq/seq.c:1.13 src/usr.bin/seq/seq.c:1.14
--- src/usr.bin/seq/seq.c:1.13	Sat Feb 24 10:10:04 2024
+++ src/usr.bin/seq/seq.c	Sat May  4 13:29:41 2024
@@ -31,7 +31,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 2005\
  The NetBSD Foundation, Inc.  All rights reserved.");
-__RCSID("$NetBSD: seq.c,v 1.13 2024/02/24 10:10:04 mlelstv Exp $");
+__RCSID("$NetBSD: seq.c,v 1.14 2024/05/04 13:29:41 mlelstv Exp $");
 #endif /* not lint */
 
 #include 
@@ -105,6 +105,7 @@ main(int argc, char *argv[])
 	double first = 1.0;
 	double last = 0.0;
 	double incr = 0.0;
+	double prev;
 	struct lconv *locale;
 	char *fmt = NULL;
 	const char *sep = "\n";
@@ -208,15 +209,23 @@ main(int argc, char *argv[])
 
 	if (incr > 0) {
 		printf(fmt, first);
+		prev = first;
 		for (first += incr; first <= last; first += incr) {
+			if (first <= prev)
+errx(1, "increment too small\n");
 			fputs(sep, stdout);
 			printf(fmt, first);
+			prev = first;
 		}
 	} else {
 		printf(fmt, first);
+		prev = first;
 		for (first += incr; first >= last; first += incr) {
+			if (first >= prev)
+errx(1, "increment too small\n");
 			fputs(sep, stdout);
 			printf(fmt, first);
+			prev = first;
 		}
 	}
 	if (term != NULL)



CVS commit: src/usr.bin/seq

2024-05-04 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sat May  4 13:29:41 UTC 2024

Modified Files:
src/usr.bin/seq: seq.c

Log Message:
Bail if increment is too small for the precision to avoid an infinite loop.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/usr.bin/seq/seq.c

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



CVS commit: src/usr.bin/seq

2024-02-24 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sat Feb 24 10:10:05 UTC 2024

Modified Files:
src/usr.bin/seq: seq.c

Log Message:
Chose better number format.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/usr.bin/seq/seq.c

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/seq/seq.c
diff -u src/usr.bin/seq/seq.c:1.12 src/usr.bin/seq/seq.c:1.13
--- src/usr.bin/seq/seq.c:1.12	Sat Mar 20 22:10:17 2021
+++ src/usr.bin/seq/seq.c	Sat Feb 24 10:10:04 2024
@@ -31,7 +31,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 2005\
  The NetBSD Foundation, Inc.  All rights reserved.");
-__RCSID("$NetBSD: seq.c,v 1.12 2021/03/20 22:10:17 cheusov Exp $");
+__RCSID("$NetBSD: seq.c,v 1.13 2024/02/24 10:10:04 mlelstv Exp $");
 #endif /* not lint */
 
 #include 
@@ -56,6 +56,8 @@ __RCSID("$NetBSD: seq.c,v 1.12 2021/03/2
 
 const char *decimal_point = ".";	/* default */
 char default_format[] = { "%g" };	/* default */
+char default_format_fmt[] = { "%%.%uf" };
+#define MAXPRECISION 40
 
 /* Prototypes */
 
@@ -65,9 +67,29 @@ int decimal_places(const char *);
 int numeric(const char *);
 int valid_format(const char *);
 
-char *generate_format(double, double, double, int, char);
+unsigned get_precision(const char *, unsigned);
+char *generate_format(double, double, double, int, char, char *);
 char *unescape(char *);
 
+unsigned
+get_precision(const char *number, unsigned minprec)
+{
+	const char *p;
+	unsigned prec;
+
+	p = strstr(number, decimal_point);
+	if (p) {
+		prec = strlen(number) - (p - number);
+		if (prec > 0)
+			prec -= 1;
+		if (prec > MAXPRECISION)
+			prec = MAXPRECISION;
+	} else
+		prec = 0;
+
+	return prec < minprec ? minprec : prec;
+}
+
 /*
  * The seq command will print out a numeric sequence from 1, the default,
  * to a user specified upper limit by 1.  The lower bound and increment
@@ -79,6 +101,7 @@ main(int argc, char *argv[])
 {
 	int c = 0, errflg = 0;
 	int equalize = 0;
+	unsigned prec;
 	double first = 1.0;
 	double last = 0.0;
 	double incr = 0.0;
@@ -87,6 +110,8 @@ main(int argc, char *argv[])
 	const char *sep = "\n";
 	const char *term = "\n";
 	char pad = ZERO;
+	char buf[6]; /* %.MAXPRECISIONf */
+
 
 	/* Determine the locale's decimal point. */
 	locale = localeconv();
@@ -136,12 +161,16 @@ main(int argc, char *argv[])
 	}
 
 	last = e_atof(argv[argc - 1]);
+	prec = get_precision(argv[argc - 1], 0);
 
-	if (argc > 1)
+	if (argc > 1) {
 		first = e_atof(argv[0]);
+		prec = get_precision(argv[0], prec);
+	}
 	
 	if (argc > 2) {
 		incr = e_atof(argv[1]);
+		prec = get_precision(argv[1], prec);
 		/* Plan 9/GNU don't do zero */
 		if (incr == 0.0)
 			errx(1, "zero %screment", (first < last)? "in" : "de");
@@ -167,8 +196,15 @@ main(int argc, char *argv[])
 	 * XXX to be bug for bug compatible with Plan 9 add a
 		 * newline if none found at the end of the format string.
 		 */
-	} else
-		fmt = generate_format(first, incr, last, equalize, pad);
+	} else {
+		if (prec == 0)
+			fmt = default_format;
+		else {
+			sprintf(buf, default_format_fmt, prec);
+			fmt = buf;
+		}
+		fmt = generate_format(first, incr, last, equalize, pad, fmt);
+	}
 
 	if (incr > 0) {
 		printf(fmt, first);
@@ -428,14 +464,15 @@ decimal_places(const char *number)
  * when "%g" prints as "%e" (this way no width adjustments are made)
  */
 char *
-generate_format(double first, double incr, double last, int equalize, char pad)
+generate_format(double first, double incr, double last,
+int equalize, char pad, char *deffmt)
 {
 	static char buf[256];
 	char cc = '\0';
 	int precision, width1, width2, places;
 
 	if (equalize == 0)
-		return (default_format);
+		return deffmt;
 
 	/* figure out "last" value printed */
 	if (first > last)
@@ -443,12 +480,12 @@ generate_format(double first, double inc
 	else
 		last = first + incr * floor((last - first) / incr);
 
-	sprintf(buf, "%g", incr);
+	sprintf(buf, deffmt, incr);
 	if (strchr(buf, 'e'))
 		cc = 'e';
 	precision = decimal_places(buf);
 
-	width1 = sprintf(buf, "%g", first);
+	width1 = sprintf(buf, deffmt, first);
 	if (strchr(buf, 'e'))
 		cc = 'e';
 	if ((places = decimal_places(buf)))
@@ -456,7 +493,7 @@ generate_format(double first, double inc
 
 	precision = MAX(places, precision);
 
-	width2 = sprintf(buf, "%g", last);
+	width2 = sprintf(buf, deffmt, last);
 	if (strchr(buf, 'e'))
 		cc = 'e';
 	if ((places = decimal_places(buf)))



CVS commit: src/usr.bin/seq

2024-02-24 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sat Feb 24 10:10:05 UTC 2024

Modified Files:
src/usr.bin/seq: seq.c

Log Message:
Chose better number format.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/usr.bin/seq/seq.c

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



CVS commit: src/usr.bin/seq

2022-02-28 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Mon Feb 28 13:49:50 UTC 2022

Modified Files:
src/usr.bin/seq: seq.1

Log Message:
seq(1): add more examples, improve wording

>From OpenBSD via jmc@OpenBSD


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/seq/seq.1

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/seq/seq.1
diff -u src/usr.bin/seq/seq.1:1.11 src/usr.bin/seq/seq.1:1.12
--- src/usr.bin/seq/seq.1:1.11	Mon Nov  1 21:28:03 2021
+++ src/usr.bin/seq/seq.1	Mon Feb 28 13:49:50 2022
@@ -1,4 +1,4 @@
-.\"	$NetBSD: seq.1,v 1.11 2021/11/01 21:28:03 andvar Exp $
+.\"	$NetBSD: seq.1,v 1.12 2022/02/28 13:49:50 wiz Exp $
 .\"
 .\" Copyright (c) 2005 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -45,12 +45,11 @@
 .Sh DESCRIPTION
 The
 .Nm
-utility prints a sequence of numbers, one per line
-.Pq default ,
+utility prints a sequence of numbers, one per line by default,
 from
 .Ar first
-.Pq default 1 ,
-to near
+.Pq default 1
+to as near
 .Ar last
 as possible, in increments of
 .Ar incr
@@ -131,22 +130,51 @@ the default conversion is changed to
 .Sh EXIT STATUS
 .Ex -std
 .Sh EXAMPLES
+Generate a sequence from 1 to 3 (inclusive) with a default increment of 1:
 .Bd -literal -offset indent
-# seq 1 3
+$ seq 1 3
 1
 2
 3
-
-# seq 3 1
+.Ed
+.Pp
+Generate a sequence from 3 to 1 (inclusive) with a default increment of -1:
+.Bd -literal -offset indent
+$ seq 3 1
 3
 2
 1
-
-# seq -w 0 .05 .1
+.Ed
+.Pp
+Generate a sequence from 0 to 0.1 (inclusive) with an increment of 0.05
+and padding with leading zeroes:
+.Bd -literal -offset indent
+$ seq -w 0 .05 .1
 0.00
 0.05
 0.10
 .Ed
+.Pp
+Generate a sequence from 1 to 3 (inclusive) with a default increment of 1,
+and a custom separator string:
+.Bd -literal -offset indent
+$ seq -s "," 1 3
+1,2,3
+.Ed
+.Pp
+Generate a sequence from 1 to 2 (inclusive) with an increment of 0.2 and
+print the results with two digits after the decimal point (using a
+.Xr printf 3
+style format):
+.Bd -literal -offset indent
+$ seq -f %.2f 1 0.2 2
+1.00
+1.20
+1.40
+1.60
+1.80
+2.00
+.Ed
 .Sh SEE ALSO
 .Xr jot 1 ,
 .Xr printf 1 ,



CVS commit: src/usr.bin/seq

2022-02-28 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Mon Feb 28 13:49:50 UTC 2022

Modified Files:
src/usr.bin/seq: seq.1

Log Message:
seq(1): add more examples, improve wording

>From OpenBSD via jmc@OpenBSD


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/seq/seq.1

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