xref: /openbsd/usr.bin/seq/seq.c (revision 874bfdc5)
1*874bfdc5Smillert /*	$OpenBSD: seq.c,v 1.8 2023/06/13 21:10:41 millert Exp $	*/
299b9de83Smillert 
399b9de83Smillert /*-
499b9de83Smillert  * Copyright (c) 2005 The NetBSD Foundation, Inc.
599b9de83Smillert  * All rights reserved.
699b9de83Smillert  *
799b9de83Smillert  * This code is derived from software contributed to The NetBSD Foundation
899b9de83Smillert  * by Brian Ginsbach.
999b9de83Smillert  *
1099b9de83Smillert  * Redistribution and use in source and binary forms, with or without
1199b9de83Smillert  * modification, are permitted provided that the following conditions
1299b9de83Smillert  * are met:
1399b9de83Smillert  * 1. Redistributions of source code must retain the above copyright
1499b9de83Smillert  *    notice, this list of conditions and the following disclaimer.
1599b9de83Smillert  * 2. Redistributions in binary form must reproduce the above copyright
1699b9de83Smillert  *    notice, this list of conditions and the following disclaimer in the
1799b9de83Smillert  *    documentation and/or other materials provided with the distribution.
1899b9de83Smillert  *
1999b9de83Smillert  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2099b9de83Smillert  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2199b9de83Smillert  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2299b9de83Smillert  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2399b9de83Smillert  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2499b9de83Smillert  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2599b9de83Smillert  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2699b9de83Smillert  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2799b9de83Smillert  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2899b9de83Smillert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2999b9de83Smillert  * POSSIBILITY OF SUCH DAMAGE.
3099b9de83Smillert  */
3199b9de83Smillert 
3299b9de83Smillert #include <ctype.h>
3399b9de83Smillert #include <err.h>
3499b9de83Smillert #include <errno.h>
3599b9de83Smillert #include <getopt.h>
3699b9de83Smillert #include <math.h>
3799b9de83Smillert #include <locale.h>
3899b9de83Smillert #include <stdio.h>
3999b9de83Smillert #include <stdlib.h>
4099b9de83Smillert #include <string.h>
4199b9de83Smillert #include <unistd.h>
4299b9de83Smillert 
4399b9de83Smillert #define VERSION	"1.0"
4499b9de83Smillert #define ZERO	'0'
4599b9de83Smillert #define SPACE	' '
4699b9de83Smillert 
4799b9de83Smillert #define MAXIMUM(a, b)	(((a) < (b))? (b) : (a))
4899b9de83Smillert #define ISSIGN(c)	((int)(c) == '-' || (int)(c) == '+')
4999b9de83Smillert #define ISEXP(c)	((int)(c) == 'e' || (int)(c) == 'E')
5099b9de83Smillert #define ISODIGIT(c)	((int)(c) >= '0' && (int)(c) <= '7')
5199b9de83Smillert 
5299b9de83Smillert /* Globals */
5399b9de83Smillert 
5499b9de83Smillert static const char *decimal_point = ".";	/* default */
5599b9de83Smillert static char default_format[] = { "%g" };	/* default */
5699b9de83Smillert 
572052df0cStb static const struct option long_opts[] = {
5899b9de83Smillert 	{"format",	required_argument,	NULL, 'f'},
5999b9de83Smillert 	{"help",	no_argument,		NULL, 'h'},
6099b9de83Smillert 	{"separator",	required_argument,	NULL, 's'},
6199b9de83Smillert 	{"version",	no_argument,		NULL, 'v'},
6299b9de83Smillert 	{"equal-width",	no_argument,		NULL, 'w'},
6399b9de83Smillert 	{NULL,		no_argument,		NULL, 0}
6499b9de83Smillert };
6599b9de83Smillert 
6699b9de83Smillert /* Prototypes */
6799b9de83Smillert 
6899b9de83Smillert static double e_atof(const char *);
6999b9de83Smillert 
7099b9de83Smillert static int decimal_places(const char *);
7199b9de83Smillert static int numeric(const char *);
7299b9de83Smillert static int valid_format(const char *);
7399b9de83Smillert 
7499b9de83Smillert static char *generate_format(double, double, double, int, char);
7599b9de83Smillert 
7699b9de83Smillert static __dead void usage(int error);
7799b9de83Smillert 
7899b9de83Smillert /*
7999b9de83Smillert  * The seq command will print out a numeric sequence from 1, the default,
8099b9de83Smillert  * to a user specified upper limit by 1.  The lower bound and increment
8199b9de83Smillert  * maybe indicated by the user on the command line.  The sequence can
8299b9de83Smillert  * be either whole, the default, or decimal numbers.
8399b9de83Smillert  */
8499b9de83Smillert int
main(int argc,char * argv[])8599b9de83Smillert main(int argc, char *argv[])
8699b9de83Smillert {
8799b9de83Smillert 	int c = 0;
8899b9de83Smillert 	int equalize = 0;
8999b9de83Smillert 	double first = 1.0;
9099b9de83Smillert 	double last = 0.0;
9199b9de83Smillert 	double incr = 0.0;
9230f0fd29Smillert 	double prev = 0.0;
9399b9de83Smillert 	double cur, step;
9499b9de83Smillert 	struct lconv *locale;
9599b9de83Smillert 	char *fmt = NULL;
9699b9de83Smillert 	const char *sep = "\n";
9799b9de83Smillert 	const char *term = "\n";
9830f0fd29Smillert 	char *cur_print, *last_print, *prev_print;
9999b9de83Smillert 	char pad = ZERO;
10099b9de83Smillert 
10178fd5390Srob 	if (pledge("stdio", NULL) == -1)
10278fd5390Srob 		err(1, "pledge");
10378fd5390Srob 
10499b9de83Smillert 	/* Determine the locale's decimal point. */
10599b9de83Smillert 	locale = localeconv();
10699b9de83Smillert 	if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
10799b9de83Smillert 		decimal_point = locale->decimal_point;
10899b9de83Smillert 
10999b9de83Smillert 	/*
11099b9de83Smillert 	 * Process options, but handle negative numbers separately
11199b9de83Smillert 	 * least they trip up getopt(3).
11299b9de83Smillert 	 */
11399b9de83Smillert 	while ((optind < argc) && !numeric(argv[optind]) &&
11499b9de83Smillert 	    (c = getopt_long(argc, argv, "+f:s:w", long_opts, NULL)) != -1) {
11599b9de83Smillert 
11699b9de83Smillert 		switch (c) {
11799b9de83Smillert 		case 'f':	/* format (plan9/GNU) */
11899b9de83Smillert 			fmt = optarg;
11999b9de83Smillert 			equalize = 0;
12099b9de83Smillert 			break;
12199b9de83Smillert 		case 's':	/* separator (GNU) */
12299b9de83Smillert 			sep = optarg;
12399b9de83Smillert 			break;
12499b9de83Smillert 		case 'v':	/* version (GNU) */
12599b9de83Smillert 			printf("seq version %s\n", VERSION);
12699b9de83Smillert 			return 0;
12799b9de83Smillert 		case 'w':	/* equal width (plan9/GNU) */
12899b9de83Smillert 			if (fmt == NULL) {
12999b9de83Smillert 				if (equalize++)
13099b9de83Smillert 					pad = SPACE;
13199b9de83Smillert 			}
13299b9de83Smillert 			break;
13399b9de83Smillert 		case 'h':	/* help (GNU) */
13499b9de83Smillert 			usage(0);
13599b9de83Smillert 			break;
13699b9de83Smillert 		default:
13799b9de83Smillert 			usage(1);
13899b9de83Smillert 			break;
13999b9de83Smillert 		}
14099b9de83Smillert 	}
14199b9de83Smillert 
14299b9de83Smillert 	argc -= optind;
14399b9de83Smillert 	argv += optind;
14499b9de83Smillert 	if (argc < 1 || argc > 3)
14599b9de83Smillert 		usage(1);
14699b9de83Smillert 
14799b9de83Smillert 	last = e_atof(argv[argc - 1]);
14899b9de83Smillert 
14999b9de83Smillert 	if (argc > 1)
15099b9de83Smillert 		first = e_atof(argv[0]);
15199b9de83Smillert 
15299b9de83Smillert 	if (argc > 2) {
15399b9de83Smillert 		incr = e_atof(argv[1]);
15499b9de83Smillert 		/* Plan 9/GNU don't do zero */
15599b9de83Smillert 		if (incr == 0.0)
15699b9de83Smillert 			errx(1, "zero %screment", (first < last) ? "in" : "de");
15799b9de83Smillert 	}
15899b9de83Smillert 
15999b9de83Smillert 	/* default is one for Plan 9/GNU work alike */
16099b9de83Smillert 	if (incr == 0.0)
16199b9de83Smillert 		incr = (first < last) ? 1.0 : -1.0;
16299b9de83Smillert 
16399b9de83Smillert 	if (incr <= 0.0 && first < last)
16499b9de83Smillert 		errx(1, "needs positive increment");
16599b9de83Smillert 
16699b9de83Smillert 	if (incr >= 0.0 && first > last)
16799b9de83Smillert 		errx(1, "needs negative decrement");
16899b9de83Smillert 
16999b9de83Smillert 	if (fmt != NULL) {
17099b9de83Smillert 		if (!valid_format(fmt))
17199b9de83Smillert 			errx(1, "invalid format string: `%s'", fmt);
17299b9de83Smillert 		/*
17399b9de83Smillert 		 * XXX to be bug for bug compatible with Plan 9 add a
17499b9de83Smillert 		 * newline if none found at the end of the format string.
17599b9de83Smillert 		 */
17699b9de83Smillert 	} else
17799b9de83Smillert 		fmt = generate_format(first, incr, last, equalize, pad);
17899b9de83Smillert 
17999b9de83Smillert 	for (step = 1, cur = first; incr > 0 ? cur <= last : cur >= last;
18099b9de83Smillert 	    cur = first + incr * step++) {
18199b9de83Smillert 		if (cur != first)
18299b9de83Smillert 			fputs(sep, stdout);
18399b9de83Smillert 		printf(fmt, cur);
18430f0fd29Smillert 		prev = cur;
18599b9de83Smillert 	}
18699b9de83Smillert 
18799b9de83Smillert 	/*
18899b9de83Smillert 	 * Did we miss the last value of the range in the loop above?
18999b9de83Smillert 	 *
19099b9de83Smillert 	 * We might have, so check if the printable version of the last
19130f0fd29Smillert 	 * computed value ('cur') and desired 'last' value are equal.  If
19230f0fd29Smillert 	 * they are equal after formatting truncation, but 'cur' and 'prev'
19330f0fd29Smillert 	 * are different, it means the exit condition of the loop held true
19430f0fd29Smillert 	 * due to a rounding error and we still need to print 'last'.
19599b9de83Smillert 	 */
196bb34e7d9Stb 	if (asprintf(&cur_print, fmt, cur) == -1 ||
19730f0fd29Smillert 	    asprintf(&last_print, fmt, last) == -1 ||
19830f0fd29Smillert 	    asprintf(&prev_print, fmt, prev) == -1)
199bb34e7d9Stb 		err(1, "asprintf");
20030f0fd29Smillert 	if (strcmp(cur_print, last_print) == 0 &&
20130f0fd29Smillert 	    strcmp(cur_print, prev_print) != 0) {
20299b9de83Smillert 		if (cur != first)
20399b9de83Smillert 			fputs(sep, stdout);
20499b9de83Smillert 		fputs(last_print, stdout);
20599b9de83Smillert 	}
20699b9de83Smillert 	free(cur_print);
20799b9de83Smillert 	free(last_print);
20830f0fd29Smillert 	free(prev_print);
20999b9de83Smillert 
21099b9de83Smillert 	fputs(term, stdout);
21199b9de83Smillert 
21299b9de83Smillert 	return 0;
21399b9de83Smillert }
21499b9de83Smillert 
21599b9de83Smillert /*
21699b9de83Smillert  * numeric - verify that string is numeric
21799b9de83Smillert  */
21899b9de83Smillert static int
numeric(const char * s)21999b9de83Smillert numeric(const char *s)
22099b9de83Smillert {
22199b9de83Smillert 	int seen_decimal_pt, decimal_pt_len;
22299b9de83Smillert 
22399b9de83Smillert 	/* skip any sign */
22499b9de83Smillert 	if (ISSIGN((unsigned char)*s))
22599b9de83Smillert 		s++;
22699b9de83Smillert 
22799b9de83Smillert 	seen_decimal_pt = 0;
22899b9de83Smillert 	decimal_pt_len = strlen(decimal_point);
22999b9de83Smillert 	while (*s) {
23099b9de83Smillert 		if (!isdigit((unsigned char)*s)) {
23199b9de83Smillert 			if (!seen_decimal_pt &&
23299b9de83Smillert 			    strncmp(s, decimal_point, decimal_pt_len) == 0) {
23399b9de83Smillert 				s += decimal_pt_len;
23499b9de83Smillert 				seen_decimal_pt = 1;
23599b9de83Smillert 				continue;
23699b9de83Smillert 			}
23799b9de83Smillert 			if (ISEXP((unsigned char)*s)) {
23899b9de83Smillert 				s++;
23999b9de83Smillert 				if (ISSIGN((unsigned char)*s) ||
24099b9de83Smillert 				    isdigit((unsigned char)*s)) {
24199b9de83Smillert 					s++;
24299b9de83Smillert 					continue;
24399b9de83Smillert 				}
24499b9de83Smillert 			}
24599b9de83Smillert 			break;
24699b9de83Smillert 		}
24799b9de83Smillert 		s++;
24899b9de83Smillert 	}
24999b9de83Smillert 	return *s == '\0';
25099b9de83Smillert }
25199b9de83Smillert 
25299b9de83Smillert /*
25399b9de83Smillert  * valid_format - validate user specified format string
25499b9de83Smillert  */
25599b9de83Smillert static int
valid_format(const char * fmt)25699b9de83Smillert valid_format(const char *fmt)
25799b9de83Smillert {
25899b9de83Smillert 	unsigned conversions = 0;
25999b9de83Smillert 
26099b9de83Smillert 	while (*fmt != '\0') {
26199b9de83Smillert 		/* scan for conversions */
26299b9de83Smillert 		if (*fmt != '%') {
26399b9de83Smillert 			fmt++;
26499b9de83Smillert 			continue;
26599b9de83Smillert 		}
26699b9de83Smillert 		fmt++;
26799b9de83Smillert 
26899b9de83Smillert 		/* allow %% but not things like %10% */
26999b9de83Smillert 		if (*fmt == '%') {
27099b9de83Smillert 			fmt++;
27199b9de83Smillert 			continue;
27299b9de83Smillert 		}
27399b9de83Smillert 
27499b9de83Smillert 		/* flags */
27599b9de83Smillert 		while (*fmt != '\0' && strchr("#0- +'", *fmt)) {
27699b9de83Smillert 			fmt++;
27799b9de83Smillert 		}
27899b9de83Smillert 
27999b9de83Smillert 		/* field width */
28099b9de83Smillert 		while (*fmt != '\0' && strchr("0123456789", *fmt)) {
28199b9de83Smillert 			fmt++;
28299b9de83Smillert 		}
28399b9de83Smillert 
28499b9de83Smillert 		/* precision */
28599b9de83Smillert 		if (*fmt == '.') {
28699b9de83Smillert 			fmt++;
28799b9de83Smillert 			while (*fmt != '\0' && strchr("0123456789", *fmt)) {
28899b9de83Smillert 				fmt++;
28999b9de83Smillert 			}
29099b9de83Smillert 		}
29199b9de83Smillert 
29299b9de83Smillert 		/* conversion */
29399b9de83Smillert 		switch (*fmt) {
29499b9de83Smillert 		case 'A':
29599b9de83Smillert 		case 'a':
29699b9de83Smillert 		case 'E':
29799b9de83Smillert 		case 'e':
29899b9de83Smillert 		case 'F':
29999b9de83Smillert 		case 'f':
30099b9de83Smillert 		case 'G':
30199b9de83Smillert 		case 'g':
30299b9de83Smillert 			/* floating point formats are accepted */
30399b9de83Smillert 			conversions++;
30499b9de83Smillert 			break;
30599b9de83Smillert 		default:
30699b9de83Smillert 			/* anything else is not */
30799b9de83Smillert 			return 0;
30899b9de83Smillert 		}
30999b9de83Smillert 	}
31099b9de83Smillert 
31199b9de83Smillert 	/* PR 236347 -- user format strings must have a conversion */
31299b9de83Smillert 	return conversions == 1;
31399b9de83Smillert }
31499b9de83Smillert 
31599b9de83Smillert /*
31699b9de83Smillert  * e_atof - convert an ASCII string to a double
31799b9de83Smillert  *	exit if string is not a valid double, or if converted value would
31899b9de83Smillert  *	cause overflow or underflow
31999b9de83Smillert  */
32099b9de83Smillert static double
e_atof(const char * num)32199b9de83Smillert e_atof(const char *num)
32299b9de83Smillert {
32399b9de83Smillert 	char *endp;
32499b9de83Smillert 	double dbl;
32599b9de83Smillert 
32699b9de83Smillert 	errno = 0;
32799b9de83Smillert 	dbl = strtod(num, &endp);
32899b9de83Smillert 
32999b9de83Smillert 	if (errno == ERANGE)
33099b9de83Smillert 		/* under or overflow */
33199b9de83Smillert 		err(2, "%s", num);
33299b9de83Smillert 	else if (*endp != '\0')
33399b9de83Smillert 		/* "junk" left in number */
33499b9de83Smillert 		errx(2, "invalid floating point argument: %s", num);
33599b9de83Smillert 
33699b9de83Smillert 	/* zero shall have no sign */
33799b9de83Smillert 	if (dbl == -0.0)
33899b9de83Smillert 		dbl = 0;
33999b9de83Smillert 	return dbl;
34099b9de83Smillert }
34199b9de83Smillert 
34299b9de83Smillert /*
34399b9de83Smillert  * decimal_places - count decimal places in a number (string)
34499b9de83Smillert  */
34599b9de83Smillert static int
decimal_places(const char * number)34699b9de83Smillert decimal_places(const char *number)
34799b9de83Smillert {
34899b9de83Smillert 	int places = 0;
34999b9de83Smillert 	char *dp;
35099b9de83Smillert 
35199b9de83Smillert 	/* look for a decimal point */
35299b9de83Smillert 	if ((dp = strstr(number, decimal_point))) {
35399b9de83Smillert 		dp += strlen(decimal_point);
35499b9de83Smillert 
35599b9de83Smillert 		while (isdigit((unsigned char)*dp++))
35699b9de83Smillert 			places++;
35799b9de83Smillert 	}
35899b9de83Smillert 	return places;
35999b9de83Smillert }
36099b9de83Smillert 
36199b9de83Smillert /*
36299b9de83Smillert  * generate_format - create a format string
36399b9de83Smillert  *
36499b9de83Smillert  * XXX to be bug for bug compatible with Plan9 and GNU return "%g"
36599b9de83Smillert  * when "%g" prints as "%e" (this way no width adjustments are made)
36699b9de83Smillert  */
36799b9de83Smillert static char *
generate_format(double first,double incr,double last,int equalize,char pad)36899b9de83Smillert generate_format(double first, double incr, double last, int equalize, char pad)
36999b9de83Smillert {
37099b9de83Smillert 	static char buf[256];
37199b9de83Smillert 	char cc = '\0';
37299b9de83Smillert 	int precision, width1, width2, places;
37399b9de83Smillert 
37499b9de83Smillert 	if (equalize == 0)
37599b9de83Smillert 		return default_format;
37699b9de83Smillert 
37799b9de83Smillert 	/* figure out "last" value printed */
37899b9de83Smillert 	if (first > last)
37999b9de83Smillert 		last = first - incr * floor((first - last) / incr);
38099b9de83Smillert 	else
38199b9de83Smillert 		last = first + incr * floor((last - first) / incr);
38299b9de83Smillert 
38399b9de83Smillert 	snprintf(buf, sizeof(buf), "%g", incr);
38499b9de83Smillert 	if (strchr(buf, 'e'))
38599b9de83Smillert 		cc = 'e';
38699b9de83Smillert 	precision = decimal_places(buf);
38799b9de83Smillert 
38899b9de83Smillert 	width1 = snprintf(buf, sizeof(buf), "%g", first);
38999b9de83Smillert 	if (strchr(buf, 'e'))
39099b9de83Smillert 		cc = 'e';
39199b9de83Smillert 	if ((places = decimal_places(buf)))
39299b9de83Smillert 		width1 -= (places + strlen(decimal_point));
39399b9de83Smillert 
39499b9de83Smillert 	precision = MAXIMUM(places, precision);
39599b9de83Smillert 
39699b9de83Smillert 	width2 = snprintf(buf, sizeof(buf), "%g", last);
39799b9de83Smillert 	if (strchr(buf, 'e'))
39899b9de83Smillert 		cc = 'e';
39999b9de83Smillert 	if ((places = decimal_places(buf)))
40099b9de83Smillert 		width2 -= (places + strlen(decimal_point));
40199b9de83Smillert 
40299b9de83Smillert 	/* XXX if incr is floating point fix the precision */
40399b9de83Smillert 	if (precision) {
40499b9de83Smillert 		snprintf(buf, sizeof(buf), "%%%c%d.%d%c", pad,
40599b9de83Smillert 		    MAXIMUM(width1, width2) + (int)strlen(decimal_point) +
40699b9de83Smillert 		    precision, precision, (cc) ? cc : 'f');
40799b9de83Smillert 	} else {
40899b9de83Smillert 		snprintf(buf, sizeof(buf), "%%%c%d%c", pad,
40999b9de83Smillert 		    MAXIMUM(width1, width2), (cc) ? cc : 'g');
41099b9de83Smillert 	}
41199b9de83Smillert 
41299b9de83Smillert 	return buf;
41399b9de83Smillert }
41499b9de83Smillert 
41599b9de83Smillert static __dead void
usage(int error)41699b9de83Smillert usage(int error)
41799b9de83Smillert {
41899b9de83Smillert 	fprintf(stderr,
419c7e1d345Srob 	    "usage: %s [-w] [-f format] [-s string] [first [incr]] last\n",
42099b9de83Smillert 	    getprogname());
42199b9de83Smillert 	exit(error);
42299b9de83Smillert }
423