1 /* $NetBSD: seq.c,v 1.7 2010/05/27 08:40:19 dholland Exp $ */
3 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 * Copyright (c) 2005 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
49 #define MAX(a, b) (((a) < (b))? (b) : (a))
50 #define ISSIGN(c) ((int)(c) == '-' || (int)(c) == '+')
51 #define ISEXP(c) ((int)(c) == 'e' || (int)(c) == 'E')
52 #define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
56 static const char *decimal_point = "."; /* default */
57 static char default_format[] = { "%g" }; /* default */
61 static double e_atof(const char *);
63 static int decimal_places(const char *);
64 static int numeric(const char *);
65 static int valid_format(const char *);
67 static char *generate_format(double, double, double, int, char);
68 static char *unescape(char *);
71 * The seq command will print out a numeric sequence from 1, the default,
72 * to a user specified upper limit by 1. The lower bound and increment
73 * maybe indicated by the user on the command line. The sequence can
74 * be either whole, the default, or decimal numbers.
77 main(int argc, char *argv[])
79 int c = 0, errflg = 0;
86 const char *sep = "\n";
87 const char *term = NULL;
90 /* Determine the locale's decimal point. */
91 locale = localeconv();
92 if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
93 decimal_point = locale->decimal_point;
96 * Process options, but handle negative numbers separately
97 * least they trip up getopt(3).
99 while ((optind < argc) && !numeric(argv[optind]) &&
100 (c = getopt(argc, argv, "f:hs:t:w")) != -1) {
103 case 'f': /* format (plan9) */
107 case 's': /* separator (GNU) */
108 sep = unescape(optarg);
110 case 't': /* terminator (new) */
111 term = unescape(optarg);
113 case 'w': /* equal width (plan9) */
118 case 'h': /* help (GNU) */
127 if (argc < 1 || argc > 3)
132 "usage: %s [-w] [-f format] [-s string] [-t string] [first [incr]] last\n",
137 last = e_atof(argv[argc - 1]);
140 first = e_atof(argv[0]);
143 incr = e_atof(argv[1]);
144 /* Plan 9/GNU don't do zero */
146 errx(1, "zero %screment", (first < last)? "in" : "de");
149 /* default is one for Plan 9/GNU work alike */
151 incr = (first < last) ? 1.0 : -1.0;
153 if (incr <= 0.0 && first < last)
154 errx(1, "needs positive increment");
156 if (incr >= 0.0 && first > last)
157 errx(1, "needs negative decrement");
160 if (!valid_format(fmt))
161 errx(1, "invalid format string: `%s'", fmt);
163 if (!valid_format(fmt))
164 errx(1, "invalid format string");
166 * XXX to be bug for bug compatible with Plan 9 add a
167 * newline if none found at the end of the format string.
170 fmt = generate_format(first, incr, last, equalize, pad);
173 for (; first <= last; first += incr) {
178 for (; first >= last; first += incr) {
190 * numeric - verify that string is numeric
193 numeric(const char *s)
195 int seen_decimal_pt, decimal_pt_len;
198 if (ISSIGN((unsigned char)*s))
202 decimal_pt_len = strlen(decimal_point);
204 if (!isdigit((unsigned char)*s)) {
205 if (!seen_decimal_pt &&
206 strncmp(s, decimal_point, decimal_pt_len) == 0) {
211 if (ISEXP((unsigned char)*s)) {
213 if (ISSIGN((unsigned char)*s) ||
214 isdigit((unsigned char)*s)) {
227 * valid_format - validate user specified format string
230 valid_format(const char *fmt)
232 unsigned conversions = 0;
234 while (*fmt != '\0') {
235 /* scan for conversions */
242 /* allow %% but not things like %10% */
249 while (*fmt != '\0' && strchr("#0- +'", *fmt)) {
254 while (*fmt != '\0' && strchr("0123456789", *fmt)) {
261 while (*fmt != '\0' && strchr("0123456789", *fmt)) {
276 /* floating point formats are accepted */
280 /* anything else is not */
285 return (conversions <= 1);
289 * unescape - handle C escapes in a string
294 char c, *cp, *new = orig;
297 for (cp = orig; (*orig = *cp); cp++, orig++) {
302 case 'a': /* alert (bell) */
305 case 'b': /* backspace */
308 case 'e': /* escape */
311 case 'f': /* formfeed */
314 case 'n': /* newline */
317 case 'r': /* carriage return */
320 case 't': /* horizontal tab */
323 case 'v': /* vertical tab */
326 case '\\': /* backslash */
329 case '\'': /* single quote */
332 case '\"': /* double quote */
338 case '3': /* octal */
342 case '7': /* number */
344 ISODIGIT((unsigned char)*cp) && i < 3;
352 case 'x': /* hexadecimal number */
355 isxdigit((unsigned char)*cp) && i < 2;
358 if (isdigit((unsigned char)*cp))
361 c |= ((toupper((unsigned char)*cp) -
377 * e_atof - convert an ASCII string to a double
378 * exit if string is not a valid double, or if converted value would
379 * cause overflow or underflow
382 e_atof(const char *num)
388 dbl = strtod(num, &endp);
391 /* under or overflow */
393 else if (*endp != '\0')
394 /* "junk" left in number */
395 errx(2, "invalid floating point argument: %s", num);
397 /* zero shall have no sign */
404 * decimal_places - count decimal places in a number (string)
407 decimal_places(const char *number)
412 /* look for a decimal point */
413 if ((dp = strstr(number, decimal_point))) {
414 dp += strlen(decimal_point);
416 while (isdigit((unsigned char)*dp++))
423 * generate_format - create a format string
425 * XXX to be bug for bug compatible with Plan9 and GNU return "%g"
426 * when "%g" prints as "%e" (this way no width adjustments are made)
429 generate_format(double first, double incr, double last, int equalize, char pad)
431 static char buf[256];
433 int precision, width1, width2, places;
436 return (default_format);
438 /* figure out "last" value printed */
440 last = first - incr * floor((first - last) / incr);
442 last = first + incr * floor((last - first) / incr);
444 sprintf(buf, "%g", incr);
445 if (strchr(buf, 'e'))
447 precision = decimal_places(buf);
449 width1 = sprintf(buf, "%g", first);
450 if (strchr(buf, 'e'))
452 if ((places = decimal_places(buf)))
453 width1 -= (places + strlen(decimal_point));
455 precision = MAX(places, precision);
457 width2 = sprintf(buf, "%g", last);
458 if (strchr(buf, 'e'))
460 if ((places = decimal_places(buf)))
461 width2 -= (places + strlen(decimal_point));
464 sprintf(buf, "%%%c%d.%d%c", pad,
465 MAX(width1, width2) + (int) strlen(decimal_point) +
466 precision, precision, (cc) ? cc : 'f');
468 sprintf(buf, "%%%c%d%c", pad, MAX(width1, width2),