1 /* $NetBSD: seq.c,v 1.5 2008/07/21 14:19:26 lukem Exp $ */
3 * Copyright (c) 2005 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
47 #define MAX(a, b) (((a) < (b))? (b) : (a))
48 #define ISSIGN(c) ((int)(c) == '-' || (int)(c) == '+')
49 #define ISEXP(c) ((int)(c) == 'e' || (int)(c) == 'E')
50 #define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
54 const char *decimal_point = "."; /* default */
55 char default_format[] = { "%g" }; /* default */
59 double e_atof(const char *);
61 int decimal_places(const char *);
62 int main(int, char *[]);
63 int numeric(const char *);
64 int valid_format(const char *);
66 char *generate_format(double, double, double, int, char);
67 char *unescape(char *);
70 * The seq command will print out a numeric sequence from 1, the default,
71 * to a user specified upper limit by 1. The lower bound and increment
72 * maybe indicated by the user on the command line. The sequence can
73 * be either whole, the default, or decimal numbers.
76 main(int argc, char *argv[])
78 int c = 0, errflg = 0;
85 const char *sep = "\n";
86 const char *term = NULL;
89 /* Determine the locale's decimal point. */
90 locale = localeconv();
91 if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
92 decimal_point = locale->decimal_point;
95 * Process options, but handle negative numbers separately
96 * least they trip up getopt(3).
98 while ((optind < argc) && !numeric(argv[optind]) &&
99 (c = getopt(argc, argv, "f:hs:t:w")) != -1) {
102 case 'f': /* format (plan9) */
106 case 's': /* separator (GNU) */
107 sep = unescape(optarg);
109 case 't': /* terminator (new) */
110 term = unescape(optarg);
112 case 'w': /* equal width (plan9) */
117 case 'h': /* help (GNU) */
126 if (argc < 1 || argc > 3)
131 "usage: %s [-w] [-f format] [-s string] [-t string] [first [incr]] last\n",
136 last = e_atof(argv[argc - 1]);
139 first = e_atof(argv[0]);
142 incr = e_atof(argv[1]);
143 /* Plan 9/GNU don't do zero */
145 errx(1, "zero %screment", (first < last)? "in" : "de");
148 /* default is one for Plan 9/GNU work alike */
150 incr = (first < last) ? 1.0 : -1.0;
152 if (incr <= 0.0 && first < last)
153 errx(1, "needs positive increment");
155 if (incr >= 0.0 && first > last)
156 errx(1, "needs negative decrement");
159 if (!valid_format(fmt))
160 errx(1, "invalid format string: `%s'", fmt);
163 * XXX to be bug for bug compatible with Plan 9 add a
164 * newline if none found at the end of the format string.
167 fmt = generate_format(first, incr, last, equalize, pad);
170 for (; first <= last; first += incr) {
175 for (; first >= last; first += incr) {
187 * numeric - verify that string is numeric
190 numeric(const char *s)
192 int seen_decimal_pt, decimal_pt_len;
195 if (ISSIGN((unsigned char)*s))
199 decimal_pt_len = strlen(decimal_point);
201 if (!isdigit((unsigned char)*s)) {
202 if (!seen_decimal_pt &&
203 strncmp(s, decimal_point, decimal_pt_len) == 0) {
208 if (ISEXP((unsigned char)*s)) {
210 if (ISSIGN((unsigned char)*s) ||
211 isdigit((unsigned char)*s)) {
224 * valid_format - validate user specified format string
227 valid_format(const char *fmt)
231 while (*fmt != '\0') {
232 /* scan for conversions */
233 if (*fmt != '\0' && *fmt != '%') {
236 } while (*fmt != '\0' && *fmt != '%');
238 /* scan a conversion */
248 /* valid conversions */
249 if (strchr("eEfgG", *fmt) &&
254 /* flags, width and precsision */
255 if (isdigit((unsigned char)*fmt) ||
256 strchr("+- 0#.", *fmt))
259 /* oops! bad conversion format! */
261 } while (*fmt != '\0');
265 return (conversions <= 1);
269 * unescape - handle C escapes in a string
274 char c, *cp, *new = orig;
277 for (cp = orig; (*orig = *cp); cp++, orig++) {
282 case 'a': /* alert (bell) */
285 case 'b': /* backspace */
288 case 'e': /* escape */
291 case 'f': /* formfeed */
294 case 'n': /* newline */
297 case 'r': /* carriage return */
300 case 't': /* horizontal tab */
303 case 'v': /* vertical tab */
306 case '\\': /* backslash */
309 case '\'': /* single quote */
312 case '\"': /* double quote */
318 case '3': /* octal */
322 case '7': /* number */
324 ISODIGIT((unsigned char)*cp) && i < 3;
332 case 'x': /* hexidecimal number */
335 isxdigit((unsigned char)*cp) && i < 2;
338 if (isdigit((unsigned char)*cp))
341 c |= ((toupper((unsigned char)*cp) -
357 * e_atof - convert an ASCII string to a double
358 * exit if string is not a valid double, or if converted value would
359 * cause overflow or underflow
362 e_atof(const char *num)
368 dbl = strtod(num, &endp);
371 /* under or overflow */
373 else if (*endp != '\0')
374 /* "junk" left in number */
375 errx(2, "invalid floating point argument: %s", num);
377 /* zero shall have no sign */
384 * decimal_places - count decimal places in a number (string)
387 decimal_places(const char *number)
392 /* look for a decimal point */
393 if ((dp = strstr(number, decimal_point))) {
394 dp += strlen(decimal_point);
396 while (isdigit((unsigned char)*dp++))
403 * generate_format - create a format string
405 * XXX to be bug for bug compatable with Plan9 and GNU return "%g"
406 * when "%g" prints as "%e" (this way no width adjustments are made)
409 generate_format(double first, double incr, double last, int equalize, char pad)
411 static char buf[256];
413 int precision, width1, width2, places;
416 return (default_format);
418 /* figure out "last" value printed */
420 last = first - incr * floor((first - last) / incr);
422 last = first + incr * floor((last - first) / incr);
424 sprintf(buf, "%g", incr);
425 if (strchr(buf, 'e'))
427 precision = decimal_places(buf);
429 width1 = sprintf(buf, "%g", first);
430 if (strchr(buf, 'e'))
432 if ((places = decimal_places(buf)))
433 width1 -= (places + strlen(decimal_point));
435 precision = MAX(places, precision);
437 width2 = sprintf(buf, "%g", last);
438 if (strchr(buf, 'e'))
440 if ((places = decimal_places(buf)))
441 width2 -= (places + strlen(decimal_point));
444 sprintf(buf, "%%%c%d.%d%c", pad,
445 MAX(width1, width2) + (int) strlen(decimal_point) +
446 precision, precision, (cc) ? cc : 'f');
448 sprintf(buf, "%%%c%d%c", pad, MAX(width1, width2),