xref: /minix3/usr.bin/seq/seq.c (revision bc0a39238ecee5c8a3fb76071ef9f61668673a0d)
1*bc0a3923SBen Gras /*
2*bc0a3923SBen Gras  * Copyright (c) 2005 The NetBSD Foundation, Inc.
3*bc0a3923SBen Gras  * All rights reserved.
4*bc0a3923SBen Gras  *
5*bc0a3923SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
6*bc0a3923SBen Gras  * by Brian Ginsbach.
7*bc0a3923SBen Gras  *
8*bc0a3923SBen Gras  * Redistribution and use in source and binary forms, with or without
9*bc0a3923SBen Gras  * modification, are permitted provided that the following conditions
10*bc0a3923SBen Gras  * are met:
11*bc0a3923SBen Gras  * 1. Redistributions of source code must retain the above copyright
12*bc0a3923SBen Gras  *    notice, this list of conditions and the following disclaimer.
13*bc0a3923SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
14*bc0a3923SBen Gras  *    notice, this list of conditions and the following disclaimer in the
15*bc0a3923SBen Gras  *    documentation and/or other materials provided with the distribution.
16*bc0a3923SBen Gras  *
17*bc0a3923SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18*bc0a3923SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19*bc0a3923SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20*bc0a3923SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21*bc0a3923SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*bc0a3923SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*bc0a3923SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*bc0a3923SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*bc0a3923SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*bc0a3923SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*bc0a3923SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
28*bc0a3923SBen Gras  */
29*bc0a3923SBen Gras 
30*bc0a3923SBen Gras #include <sys/cdefs.h>
31*bc0a3923SBen Gras #ifndef lint
32*bc0a3923SBen Gras __COPYRIGHT("@(#) Copyright (c) 2005\
33*bc0a3923SBen Gras  The NetBSD Foundation, Inc.  All rights reserved.");
34*bc0a3923SBen Gras __RCSID("$NetBSD: seq.c,v 1.7 2010/05/27 08:40:19 dholland Exp $");
35*bc0a3923SBen Gras #endif /* not lint */
36*bc0a3923SBen Gras 
37*bc0a3923SBen Gras #include <ctype.h>
38*bc0a3923SBen Gras #include <err.h>
39*bc0a3923SBen Gras #include <errno.h>
40*bc0a3923SBen Gras #include <math.h>
41*bc0a3923SBen Gras #include <locale.h>
42*bc0a3923SBen Gras #include <stdio.h>
43*bc0a3923SBen Gras #include <stdlib.h>
44*bc0a3923SBen Gras #include <string.h>
45*bc0a3923SBen Gras #include <unistd.h>
46*bc0a3923SBen Gras 
47*bc0a3923SBen Gras #define ZERO	'0'
48*bc0a3923SBen Gras #define SPACE	' '
49*bc0a3923SBen Gras 
50*bc0a3923SBen Gras #define MAX(a, b)	(((a) < (b))? (b) : (a))
51*bc0a3923SBen Gras #define ISSIGN(c)	((int)(c) == '-' || (int)(c) == '+')
52*bc0a3923SBen Gras #define ISEXP(c)	((int)(c) == 'e' || (int)(c) == 'E')
53*bc0a3923SBen Gras #define ISODIGIT(c)	((int)(c) >= '0' && (int)(c) <= '7')
54*bc0a3923SBen Gras 
55*bc0a3923SBen Gras /* Globals */
56*bc0a3923SBen Gras 
57*bc0a3923SBen Gras const char *decimal_point = ".";	/* default */
58*bc0a3923SBen Gras char default_format[] = { "%g" };	/* default */
59*bc0a3923SBen Gras 
60*bc0a3923SBen Gras /* Prototypes */
61*bc0a3923SBen Gras 
62*bc0a3923SBen Gras double e_atof(const char *);
63*bc0a3923SBen Gras 
64*bc0a3923SBen Gras int decimal_places(const char *);
65*bc0a3923SBen Gras int main(int, char *[]);
66*bc0a3923SBen Gras int numeric(const char *);
67*bc0a3923SBen Gras int valid_format(const char *);
68*bc0a3923SBen Gras 
69*bc0a3923SBen Gras char *generate_format(double, double, double, int, char);
70*bc0a3923SBen Gras char *unescape(char *);
71*bc0a3923SBen Gras 
72*bc0a3923SBen Gras /*
73*bc0a3923SBen Gras  * The seq command will print out a numeric sequence from 1, the default,
74*bc0a3923SBen Gras  * to a user specified upper limit by 1.  The lower bound and increment
75*bc0a3923SBen Gras  * maybe indicated by the user on the command line.  The sequence can
76*bc0a3923SBen Gras  * be either whole, the default, or decimal numbers.
77*bc0a3923SBen Gras  */
78*bc0a3923SBen Gras int
main(int argc,char * argv[])79*bc0a3923SBen Gras main(int argc, char *argv[])
80*bc0a3923SBen Gras {
81*bc0a3923SBen Gras 	int c = 0, errflg = 0;
82*bc0a3923SBen Gras 	int equalize = 0;
83*bc0a3923SBen Gras 	double first = 1.0;
84*bc0a3923SBen Gras 	double last = 0.0;
85*bc0a3923SBen Gras 	double incr = 0.0;
86*bc0a3923SBen Gras 	struct lconv *locale;
87*bc0a3923SBen Gras 	char *fmt = NULL;
88*bc0a3923SBen Gras 	const char *sep = "\n";
89*bc0a3923SBen Gras 	const char *term = NULL;
90*bc0a3923SBen Gras 	char pad = ZERO;
91*bc0a3923SBen Gras 
92*bc0a3923SBen Gras 	/* Determine the locale's decimal point. */
93*bc0a3923SBen Gras 	locale = localeconv();
94*bc0a3923SBen Gras 	if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
95*bc0a3923SBen Gras 		decimal_point = locale->decimal_point;
96*bc0a3923SBen Gras 
97*bc0a3923SBen Gras 	/*
98*bc0a3923SBen Gras          * Process options, but handle negative numbers separately
99*bc0a3923SBen Gras          * least they trip up getopt(3).
100*bc0a3923SBen Gras          */
101*bc0a3923SBen Gras 	while ((optind < argc) && !numeric(argv[optind]) &&
102*bc0a3923SBen Gras 	    (c = getopt(argc, argv, "f:hs:t:w")) != -1) {
103*bc0a3923SBen Gras 
104*bc0a3923SBen Gras 		switch (c) {
105*bc0a3923SBen Gras 		case 'f':	/* format (plan9) */
106*bc0a3923SBen Gras 			fmt = optarg;
107*bc0a3923SBen Gras 			equalize = 0;
108*bc0a3923SBen Gras 			break;
109*bc0a3923SBen Gras 		case 's':	/* separator (GNU) */
110*bc0a3923SBen Gras 			sep = unescape(optarg);
111*bc0a3923SBen Gras 			break;
112*bc0a3923SBen Gras 		case 't':	/* terminator (new) */
113*bc0a3923SBen Gras 			term = unescape(optarg);
114*bc0a3923SBen Gras 			break;
115*bc0a3923SBen Gras 		case 'w':	/* equal width (plan9) */
116*bc0a3923SBen Gras 			if (!fmt)
117*bc0a3923SBen Gras 				if (equalize++)
118*bc0a3923SBen Gras 					pad = SPACE;
119*bc0a3923SBen Gras 			break;
120*bc0a3923SBen Gras 		case 'h':	/* help (GNU) */
121*bc0a3923SBen Gras 		default:
122*bc0a3923SBen Gras 			errflg++;
123*bc0a3923SBen Gras 			break;
124*bc0a3923SBen Gras 		}
125*bc0a3923SBen Gras 	}
126*bc0a3923SBen Gras 
127*bc0a3923SBen Gras 	argc -= optind;
128*bc0a3923SBen Gras 	argv += optind;
129*bc0a3923SBen Gras 	if (argc < 1 || argc > 3)
130*bc0a3923SBen Gras 		errflg++;
131*bc0a3923SBen Gras 
132*bc0a3923SBen Gras 	if (errflg) {
133*bc0a3923SBen Gras 		fprintf(stderr,
134*bc0a3923SBen Gras 		    "usage: %s [-w] [-f format] [-s string] [-t string] [first [incr]] last\n",
135*bc0a3923SBen Gras 		    getprogname());
136*bc0a3923SBen Gras 		exit(1);
137*bc0a3923SBen Gras 	}
138*bc0a3923SBen Gras 
139*bc0a3923SBen Gras 	last = e_atof(argv[argc - 1]);
140*bc0a3923SBen Gras 
141*bc0a3923SBen Gras 	if (argc > 1)
142*bc0a3923SBen Gras 		first = e_atof(argv[0]);
143*bc0a3923SBen Gras 
144*bc0a3923SBen Gras 	if (argc > 2) {
145*bc0a3923SBen Gras 		incr = e_atof(argv[1]);
146*bc0a3923SBen Gras 		/* Plan 9/GNU don't do zero */
147*bc0a3923SBen Gras 		if (incr == 0.0)
148*bc0a3923SBen Gras 			errx(1, "zero %screment", (first < last)? "in" : "de");
149*bc0a3923SBen Gras 	}
150*bc0a3923SBen Gras 
151*bc0a3923SBen Gras 	/* default is one for Plan 9/GNU work alike */
152*bc0a3923SBen Gras 	if (incr == 0.0)
153*bc0a3923SBen Gras 		incr = (first < last) ? 1.0 : -1.0;
154*bc0a3923SBen Gras 
155*bc0a3923SBen Gras 	if (incr <= 0.0 && first < last)
156*bc0a3923SBen Gras 		errx(1, "needs positive increment");
157*bc0a3923SBen Gras 
158*bc0a3923SBen Gras 	if (incr >= 0.0 && first > last)
159*bc0a3923SBen Gras 		errx(1, "needs negative decrement");
160*bc0a3923SBen Gras 
161*bc0a3923SBen Gras 	if (fmt != NULL) {
162*bc0a3923SBen Gras 		if (!valid_format(fmt))
163*bc0a3923SBen Gras 			errx(1, "invalid format string: `%s'", fmt);
164*bc0a3923SBen Gras 		fmt = unescape(fmt);
165*bc0a3923SBen Gras 		if (!valid_format(fmt))
166*bc0a3923SBen Gras 			errx(1, "invalid format string");
167*bc0a3923SBen Gras 		/*
168*bc0a3923SBen Gras 	         * XXX to be bug for bug compatible with Plan 9 add a
169*bc0a3923SBen Gras 		 * newline if none found at the end of the format string.
170*bc0a3923SBen Gras 		 */
171*bc0a3923SBen Gras 	} else
172*bc0a3923SBen Gras 		fmt = generate_format(first, incr, last, equalize, pad);
173*bc0a3923SBen Gras 
174*bc0a3923SBen Gras 	if (incr > 0) {
175*bc0a3923SBen Gras 		for (; first <= last; first += incr) {
176*bc0a3923SBen Gras 			printf(fmt, first);
177*bc0a3923SBen Gras 			fputs(sep, stdout);
178*bc0a3923SBen Gras 		}
179*bc0a3923SBen Gras 	} else {
180*bc0a3923SBen Gras 		for (; first >= last; first += incr) {
181*bc0a3923SBen Gras 			printf(fmt, first);
182*bc0a3923SBen Gras 			fputs(sep, stdout);
183*bc0a3923SBen Gras 		}
184*bc0a3923SBen Gras 	}
185*bc0a3923SBen Gras 	if (term != NULL)
186*bc0a3923SBen Gras 		fputs(term, stdout);
187*bc0a3923SBen Gras 
188*bc0a3923SBen Gras 	return (0);
189*bc0a3923SBen Gras }
190*bc0a3923SBen Gras 
191*bc0a3923SBen Gras /*
192*bc0a3923SBen Gras  * numeric - verify that string is numeric
193*bc0a3923SBen Gras  */
194*bc0a3923SBen Gras int
numeric(const char * s)195*bc0a3923SBen Gras numeric(const char *s)
196*bc0a3923SBen Gras {
197*bc0a3923SBen Gras 	int seen_decimal_pt, decimal_pt_len;
198*bc0a3923SBen Gras 
199*bc0a3923SBen Gras 	/* skip any sign */
200*bc0a3923SBen Gras 	if (ISSIGN((unsigned char)*s))
201*bc0a3923SBen Gras 		s++;
202*bc0a3923SBen Gras 
203*bc0a3923SBen Gras 	seen_decimal_pt = 0;
204*bc0a3923SBen Gras 	decimal_pt_len = strlen(decimal_point);
205*bc0a3923SBen Gras 	while (*s) {
206*bc0a3923SBen Gras 		if (!isdigit((unsigned char)*s)) {
207*bc0a3923SBen Gras 			if (!seen_decimal_pt &&
208*bc0a3923SBen Gras 			    strncmp(s, decimal_point, decimal_pt_len) == 0) {
209*bc0a3923SBen Gras 				s += decimal_pt_len;
210*bc0a3923SBen Gras 				seen_decimal_pt = 1;
211*bc0a3923SBen Gras 				continue;
212*bc0a3923SBen Gras 			}
213*bc0a3923SBen Gras 			if (ISEXP((unsigned char)*s)) {
214*bc0a3923SBen Gras 				s++;
215*bc0a3923SBen Gras 				if (ISSIGN((unsigned char)*s)) {
216*bc0a3923SBen Gras 					s++;
217*bc0a3923SBen Gras 					continue;
218*bc0a3923SBen Gras 				}
219*bc0a3923SBen Gras 			}
220*bc0a3923SBen Gras 			break;
221*bc0a3923SBen Gras 		}
222*bc0a3923SBen Gras 		s++;
223*bc0a3923SBen Gras 	}
224*bc0a3923SBen Gras 	return (*s == '\0');
225*bc0a3923SBen Gras }
226*bc0a3923SBen Gras 
227*bc0a3923SBen Gras /*
228*bc0a3923SBen Gras  * valid_format - validate user specified format string
229*bc0a3923SBen Gras  */
230*bc0a3923SBen Gras int
valid_format(const char * fmt)231*bc0a3923SBen Gras valid_format(const char *fmt)
232*bc0a3923SBen Gras {
233*bc0a3923SBen Gras 	unsigned conversions = 0;
234*bc0a3923SBen Gras 
235*bc0a3923SBen Gras 	while (*fmt != '\0') {
236*bc0a3923SBen Gras 		/* scan for conversions */
237*bc0a3923SBen Gras 		if (*fmt != '%') {
238*bc0a3923SBen Gras 			fmt++;
239*bc0a3923SBen Gras 			continue;
240*bc0a3923SBen Gras 		}
241*bc0a3923SBen Gras 		fmt++;
242*bc0a3923SBen Gras 
243*bc0a3923SBen Gras 		/* allow %% but not things like %10% */
244*bc0a3923SBen Gras 		if (*fmt == '%') {
245*bc0a3923SBen Gras 			fmt++;
246*bc0a3923SBen Gras 			continue;
247*bc0a3923SBen Gras 		}
248*bc0a3923SBen Gras 
249*bc0a3923SBen Gras 		/* flags */
250*bc0a3923SBen Gras 		while (*fmt != '\0' && strchr("#0- +'", *fmt)) {
251*bc0a3923SBen Gras 			fmt++;
252*bc0a3923SBen Gras 		}
253*bc0a3923SBen Gras 
254*bc0a3923SBen Gras 		/* field width */
255*bc0a3923SBen Gras 		while (*fmt != '\0' && strchr("0123456789", *fmt)) {
256*bc0a3923SBen Gras 			fmt++;
257*bc0a3923SBen Gras 		}
258*bc0a3923SBen Gras 
259*bc0a3923SBen Gras 		/* precision */
260*bc0a3923SBen Gras 		if (*fmt == '.') {
261*bc0a3923SBen Gras 			fmt++;
262*bc0a3923SBen Gras 			while (*fmt != '\0' && strchr("0123456789", *fmt)) {
263*bc0a3923SBen Gras 				fmt++;
264*bc0a3923SBen Gras 			}
265*bc0a3923SBen Gras 		}
266*bc0a3923SBen Gras 
267*bc0a3923SBen Gras 		/* conversion */
268*bc0a3923SBen Gras 		switch (*fmt) {
269*bc0a3923SBen Gras 		    case 'A':
270*bc0a3923SBen Gras 		    case 'a':
271*bc0a3923SBen Gras 		    case 'E':
272*bc0a3923SBen Gras 		    case 'e':
273*bc0a3923SBen Gras 		    case 'F':
274*bc0a3923SBen Gras 		    case 'f':
275*bc0a3923SBen Gras 		    case 'G':
276*bc0a3923SBen Gras 		    case 'g':
277*bc0a3923SBen Gras 			/* floating point formats are accepted */
278*bc0a3923SBen Gras 			conversions++;
279*bc0a3923SBen Gras 			break;
280*bc0a3923SBen Gras 		    default:
281*bc0a3923SBen Gras 			/* anything else is not */
282*bc0a3923SBen Gras 			return 0;
283*bc0a3923SBen Gras 		}
284*bc0a3923SBen Gras 	}
285*bc0a3923SBen Gras 
286*bc0a3923SBen Gras 	return (conversions <= 1);
287*bc0a3923SBen Gras }
288*bc0a3923SBen Gras 
289*bc0a3923SBen Gras /*
290*bc0a3923SBen Gras  * unescape - handle C escapes in a string
291*bc0a3923SBen Gras  */
292*bc0a3923SBen Gras char *
unescape(char * orig)293*bc0a3923SBen Gras unescape(char *orig)
294*bc0a3923SBen Gras {
295*bc0a3923SBen Gras 	char c, *cp, *new = orig;
296*bc0a3923SBen Gras 	int i;
297*bc0a3923SBen Gras 
298*bc0a3923SBen Gras 	for (cp = orig; (*orig = *cp); cp++, orig++) {
299*bc0a3923SBen Gras 		if (*cp != '\\')
300*bc0a3923SBen Gras 			continue;
301*bc0a3923SBen Gras 
302*bc0a3923SBen Gras 		switch (*++cp) {
303*bc0a3923SBen Gras 		case 'a':	/* alert (bell) */
304*bc0a3923SBen Gras 			*orig = '\a';
305*bc0a3923SBen Gras 			continue;
306*bc0a3923SBen Gras 		case 'b':	/* backspace */
307*bc0a3923SBen Gras 			*orig = '\b';
308*bc0a3923SBen Gras 			continue;
309*bc0a3923SBen Gras 		case 'e':	/* escape */
310*bc0a3923SBen Gras 			*orig = '\e';
311*bc0a3923SBen Gras 			continue;
312*bc0a3923SBen Gras 		case 'f':	/* formfeed */
313*bc0a3923SBen Gras 			*orig = '\f';
314*bc0a3923SBen Gras 			continue;
315*bc0a3923SBen Gras 		case 'n':	/* newline */
316*bc0a3923SBen Gras 			*orig = '\n';
317*bc0a3923SBen Gras 			continue;
318*bc0a3923SBen Gras 		case 'r':	/* carriage return */
319*bc0a3923SBen Gras 			*orig = '\r';
320*bc0a3923SBen Gras 			continue;
321*bc0a3923SBen Gras 		case 't':	/* horizontal tab */
322*bc0a3923SBen Gras 			*orig = '\t';
323*bc0a3923SBen Gras 			continue;
324*bc0a3923SBen Gras 		case 'v':	/* vertical tab */
325*bc0a3923SBen Gras 			*orig = '\v';
326*bc0a3923SBen Gras 			continue;
327*bc0a3923SBen Gras 		case '\\':	/* backslash */
328*bc0a3923SBen Gras 			*orig = '\\';
329*bc0a3923SBen Gras 			continue;
330*bc0a3923SBen Gras 		case '\'':	/* single quote */
331*bc0a3923SBen Gras 			*orig = '\'';
332*bc0a3923SBen Gras 			continue;
333*bc0a3923SBen Gras 		case '\"':	/* double quote */
334*bc0a3923SBen Gras 			*orig = '"';
335*bc0a3923SBen Gras 			continue;
336*bc0a3923SBen Gras 		case '0':
337*bc0a3923SBen Gras 		case '1':
338*bc0a3923SBen Gras 		case '2':
339*bc0a3923SBen Gras 		case '3':	/* octal */
340*bc0a3923SBen Gras 		case '4':
341*bc0a3923SBen Gras 		case '5':
342*bc0a3923SBen Gras 		case '6':
343*bc0a3923SBen Gras 		case '7':	/* number */
344*bc0a3923SBen Gras 			for (i = 0, c = 0;
345*bc0a3923SBen Gras 			     ISODIGIT((unsigned char)*cp) && i < 3;
346*bc0a3923SBen Gras 			     i++, cp++) {
347*bc0a3923SBen Gras 				c <<= 3;
348*bc0a3923SBen Gras 				c |= (*cp - '0');
349*bc0a3923SBen Gras 			}
350*bc0a3923SBen Gras 			*orig = c;
351*bc0a3923SBen Gras 			--cp;
352*bc0a3923SBen Gras 			continue;
353*bc0a3923SBen Gras 		case 'x':	/* hexidecimal number */
354*bc0a3923SBen Gras 			cp++;	/* skip 'x' */
355*bc0a3923SBen Gras 			for (i = 0, c = 0;
356*bc0a3923SBen Gras 			     isxdigit((unsigned char)*cp) && i < 2;
357*bc0a3923SBen Gras 			     i++, cp++) {
358*bc0a3923SBen Gras 				c <<= 4;
359*bc0a3923SBen Gras 				if (isdigit((unsigned char)*cp))
360*bc0a3923SBen Gras 					c |= (*cp - '0');
361*bc0a3923SBen Gras 				else
362*bc0a3923SBen Gras 					c |= ((toupper((unsigned char)*cp) -
363*bc0a3923SBen Gras 					    'A') + 10);
364*bc0a3923SBen Gras 			}
365*bc0a3923SBen Gras 			*orig = c;
366*bc0a3923SBen Gras 			--cp;
367*bc0a3923SBen Gras 			continue;
368*bc0a3923SBen Gras 		default:
369*bc0a3923SBen Gras 			--cp;
370*bc0a3923SBen Gras 			break;
371*bc0a3923SBen Gras 		}
372*bc0a3923SBen Gras 	}
373*bc0a3923SBen Gras 
374*bc0a3923SBen Gras 	return (new);
375*bc0a3923SBen Gras }
376*bc0a3923SBen Gras 
377*bc0a3923SBen Gras /*
378*bc0a3923SBen Gras  * e_atof - convert an ASCII string to a double
379*bc0a3923SBen Gras  *	exit if string is not a valid double, or if converted value would
380*bc0a3923SBen Gras  *	cause overflow or underflow
381*bc0a3923SBen Gras  */
382*bc0a3923SBen Gras double
e_atof(const char * num)383*bc0a3923SBen Gras e_atof(const char *num)
384*bc0a3923SBen Gras {
385*bc0a3923SBen Gras 	char *endp;
386*bc0a3923SBen Gras 	double dbl;
387*bc0a3923SBen Gras 
388*bc0a3923SBen Gras 	errno = 0;
389*bc0a3923SBen Gras 	dbl = strtod(num, &endp);
390*bc0a3923SBen Gras 
391*bc0a3923SBen Gras 	if (errno == ERANGE)
392*bc0a3923SBen Gras 		/* under or overflow */
393*bc0a3923SBen Gras 		err(2, "%s", num);
394*bc0a3923SBen Gras 	else if (*endp != '\0')
395*bc0a3923SBen Gras 		/* "junk" left in number */
396*bc0a3923SBen Gras 		errx(2, "invalid floating point argument: %s", num);
397*bc0a3923SBen Gras 
398*bc0a3923SBen Gras 	/* zero shall have no sign */
399*bc0a3923SBen Gras 	if (dbl == -0.0)
400*bc0a3923SBen Gras 		dbl = 0;
401*bc0a3923SBen Gras 	return (dbl);
402*bc0a3923SBen Gras }
403*bc0a3923SBen Gras 
404*bc0a3923SBen Gras /*
405*bc0a3923SBen Gras  * decimal_places - count decimal places in a number (string)
406*bc0a3923SBen Gras  */
407*bc0a3923SBen Gras int
decimal_places(const char * number)408*bc0a3923SBen Gras decimal_places(const char *number)
409*bc0a3923SBen Gras {
410*bc0a3923SBen Gras 	int places = 0;
411*bc0a3923SBen Gras 	char *dp;
412*bc0a3923SBen Gras 
413*bc0a3923SBen Gras 	/* look for a decimal point */
414*bc0a3923SBen Gras 	if ((dp = strstr(number, decimal_point))) {
415*bc0a3923SBen Gras 		dp += strlen(decimal_point);
416*bc0a3923SBen Gras 
417*bc0a3923SBen Gras 		while (isdigit((unsigned char)*dp++))
418*bc0a3923SBen Gras 			places++;
419*bc0a3923SBen Gras 	}
420*bc0a3923SBen Gras 	return (places);
421*bc0a3923SBen Gras }
422*bc0a3923SBen Gras 
423*bc0a3923SBen Gras /*
424*bc0a3923SBen Gras  * generate_format - create a format string
425*bc0a3923SBen Gras  *
426*bc0a3923SBen Gras  * XXX to be bug for bug compatable with Plan9 and GNU return "%g"
427*bc0a3923SBen Gras  * when "%g" prints as "%e" (this way no width adjustments are made)
428*bc0a3923SBen Gras  */
429*bc0a3923SBen Gras char *
generate_format(double first,double incr,double last,int equalize,char pad)430*bc0a3923SBen Gras generate_format(double first, double incr, double last, int equalize, char pad)
431*bc0a3923SBen Gras {
432*bc0a3923SBen Gras 	static char buf[256];
433*bc0a3923SBen Gras 	char cc = '\0';
434*bc0a3923SBen Gras 	int precision, width1, width2, places;
435*bc0a3923SBen Gras 
436*bc0a3923SBen Gras 	if (equalize == 0)
437*bc0a3923SBen Gras 		return (default_format);
438*bc0a3923SBen Gras 
439*bc0a3923SBen Gras 	/* figure out "last" value printed */
440*bc0a3923SBen Gras 	if (first > last)
441*bc0a3923SBen Gras 		last = first - incr * floor((first - last) / incr);
442*bc0a3923SBen Gras 	else
443*bc0a3923SBen Gras 		last = first + incr * floor((last - first) / incr);
444*bc0a3923SBen Gras 
445*bc0a3923SBen Gras 	sprintf(buf, "%g", incr);
446*bc0a3923SBen Gras 	if (strchr(buf, 'e'))
447*bc0a3923SBen Gras 		cc = 'e';
448*bc0a3923SBen Gras 	precision = decimal_places(buf);
449*bc0a3923SBen Gras 
450*bc0a3923SBen Gras 	width1 = sprintf(buf, "%g", first);
451*bc0a3923SBen Gras 	if (strchr(buf, 'e'))
452*bc0a3923SBen Gras 		cc = 'e';
453*bc0a3923SBen Gras 	if ((places = decimal_places(buf)))
454*bc0a3923SBen Gras 		width1 -= (places + strlen(decimal_point));
455*bc0a3923SBen Gras 
456*bc0a3923SBen Gras 	precision = MAX(places, precision);
457*bc0a3923SBen Gras 
458*bc0a3923SBen Gras 	width2 = sprintf(buf, "%g", last);
459*bc0a3923SBen Gras 	if (strchr(buf, 'e'))
460*bc0a3923SBen Gras 		cc = 'e';
461*bc0a3923SBen Gras 	if ((places = decimal_places(buf)))
462*bc0a3923SBen Gras 		width2 -= (places + strlen(decimal_point));
463*bc0a3923SBen Gras 
464*bc0a3923SBen Gras 	if (precision) {
465*bc0a3923SBen Gras 		sprintf(buf, "%%%c%d.%d%c", pad,
466*bc0a3923SBen Gras 		    MAX(width1, width2) + (int) strlen(decimal_point) +
467*bc0a3923SBen Gras 		    precision, precision, (cc) ? cc : 'f');
468*bc0a3923SBen Gras 	} else {
469*bc0a3923SBen Gras 		sprintf(buf, "%%%c%d%c", pad, MAX(width1, width2),
470*bc0a3923SBen Gras 		    (cc) ? cc : 'g');
471*bc0a3923SBen Gras 	}
472*bc0a3923SBen Gras 
473*bc0a3923SBen Gras 	return (buf);
474*bc0a3923SBen Gras }
475