2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $DragonFly: src/usr.bin/seq/seq.c,v 1.1 2005/01/22 19:09:40 joerg Exp $
48 #define MAX(a, b) (((a) < (b))? (b) : (a))
49 #define ISSIGN(c) ((int)(c) == '-' || (int)(c) == '+')
50 #define ISEXP(c) ((int)(c) == 'e' || (int)(c) == 'E')
51 #define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
55 const char *decimal_point = "."; /* default */
56 char default_format[] = { "%g" }; /* default */
60 double e_atof(const char *);
62 int decimal_places(const 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 [-f format] [-s str] [-t str] [-w] [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)) {
223 * valid_format - validate user specified format string
226 valid_format(const char *fmt)
230 while (*fmt != '\0') {
231 /* scan for conversions */
232 if (*fmt != '\0' && *fmt != '%') {
235 } while (*fmt != '\0' && *fmt != '%');
237 /* scan a conversion */
247 /* valid conversions */
248 if (strchr("eEfgG", *fmt) &&
253 /* flags, width and precsision */
254 if (isdigit((unsigned char)*fmt) ||
255 strchr("+- 0#.", *fmt))
258 /* oops! bad conversion format! */
260 } while (*fmt != '\0');
264 return(conversions <= 1);
268 * unescape - handle C escapes in a string
273 char c, *cp, *new = orig;
276 for (cp = orig; (*orig = *cp) != '\0'; cp++, orig++) {
281 case 'a': /* alert (bell) */
284 case 'b': /* backspace */
287 case 'e': /* escape */
290 case 'f': /* formfeed */
293 case 'n': /* newline */
296 case 'r': /* carriage return */
299 case 't': /* horizontal tab */
302 case 'v': /* vertical tab */
305 case '\\': /* backslash */
308 case '\'': /* single quote */
311 case '\"': /* double quote */
317 case '3': /* octal */
321 case '7': /* number */
323 ISODIGIT((unsigned char)*cp) && i < 3;
330 case 'x': /* hexidecimal number */
333 isxdigit((unsigned char)*cp) && i < 2;
336 if (isdigit((unsigned char)*cp))
339 c |= ((toupper((unsigned char)*cp) -
354 * e_atof - convert an ASCII string to a double
355 * exit if string is not a valid double, or if converted value would
356 * cause overflow or underflow
359 e_atof(const char *num)
365 dbl = strtod(num, &endp);
368 /* under or overflow */
370 else if (*endp != '\0')
371 /* "junk" left in number */
372 errx(2, "invalid floating point argument: %s", num);
374 /* zero shall have no sign */
381 * decimal_places - count decimal places in a number (string)
384 decimal_places(const char *number)
389 /* look for a decimal point */
390 if ((dp = strstr(number, decimal_point)) != NULL) {
391 dp += strlen(decimal_point);
393 while (isdigit((unsigned char)*dp++))
400 * generate_format - create a format string
402 * XXX to be bug for bug compatable with Plan9 and GNU return "%g"
403 * when "%g" prints as "%e" (this way no width adjustments are made)
406 generate_format(double first, double incr, double last, int equalize, char pad)
408 static char buf[256];
410 int precision, width1, width2, places;
413 return(default_format);
415 /* figure out "last" value printed */
417 last = first - incr * floor((first - last) / incr);
419 last = first + incr * floor((last - first) / incr);
421 snprintf(buf, sizeof(buf), "%g", incr);
422 if (strchr(buf, 'e'))
424 precision = decimal_places(buf);
426 width1 = snprintf(buf, sizeof(buf), "%g", first);
427 if (strchr(buf, 'e'))
429 if ((places = decimal_places(buf)))
430 width1 -= (places + strlen(decimal_point));
432 precision = MAX(places, precision);
434 width2 = snprintf(buf, sizeof(buf), "%g", last);
435 if (strchr(buf, 'e'))
437 if ((places = decimal_places(buf)))
438 width2 -= (places + strlen(decimal_point));
441 snprintf(buf, sizeof(buf), "%%%c%d.%d%c", pad,
442 MAX(width1, width2) + (int) strlen(decimal_point) +
443 precision, precision, (cc) ? cc : 'f');
445 snprintf(buf, sizeof(buf), "%%%c%d%c", pad,
446 MAX(width1, width2), (cc) ? cc : 'g');