2 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
32 __COPYRIGHT("@(#) Copyright (c) 2005\
33 The NetBSD Foundation, Inc. All rights reserved.");
34 __RCSID("$NetBSD: seq.c,v 1.7 2010/05/27 08:40:19 dholland Exp $");
50 #define MAX(a, b) (((a) < (b))? (b) : (a))
51 #define ISSIGN(c) ((int)(c) == '-' || (int)(c) == '+')
52 #define ISEXP(c) ((int)(c) == 'e' || (int)(c) == 'E')
53 #define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
57 const char *decimal_point
= "."; /* default */
58 char default_format
[] = { "%g" }; /* default */
62 double e_atof(const char *);
64 int decimal_places(const char *);
65 int main(int, char *[]);
66 int numeric(const char *);
67 int valid_format(const char *);
69 char *generate_format(double, double, double, int, char);
70 char *unescape(char *);
73 * The seq command will print out a numeric sequence from 1, the default,
74 * to a user specified upper limit by 1. The lower bound and increment
75 * maybe indicated by the user on the command line. The sequence can
76 * be either whole, the default, or decimal numbers.
79 main(int argc
, char *argv
[])
81 int c
= 0, errflg
= 0;
88 const char *sep
= "\n";
89 const char *term
= NULL
;
92 /* Determine the locale's decimal point. */
93 locale
= localeconv();
94 if (locale
&& locale
->decimal_point
&& locale
->decimal_point
[0] != '\0')
95 decimal_point
= locale
->decimal_point
;
98 * Process options, but handle negative numbers separately
99 * least they trip up getopt(3).
101 while ((optind
< argc
) && !numeric(argv
[optind
]) &&
102 (c
= getopt(argc
, argv
, "f:hs:t:w")) != -1) {
105 case 'f': /* format (plan9) */
109 case 's': /* separator (GNU) */
110 sep
= unescape(optarg
);
112 case 't': /* terminator (new) */
113 term
= unescape(optarg
);
115 case 'w': /* equal width (plan9) */
120 case 'h': /* help (GNU) */
129 if (argc
< 1 || argc
> 3)
134 "usage: %s [-w] [-f format] [-s string] [-t string] [first [incr]] last\n",
139 last
= e_atof(argv
[argc
- 1]);
142 first
= e_atof(argv
[0]);
145 incr
= e_atof(argv
[1]);
146 /* Plan 9/GNU don't do zero */
148 errx(1, "zero %screment", (first
< last
)? "in" : "de");
151 /* default is one for Plan 9/GNU work alike */
153 incr
= (first
< last
) ? 1.0 : -1.0;
155 if (incr
<= 0.0 && first
< last
)
156 errx(1, "needs positive increment");
158 if (incr
>= 0.0 && first
> last
)
159 errx(1, "needs negative decrement");
162 if (!valid_format(fmt
))
163 errx(1, "invalid format string: `%s'", fmt
);
165 if (!valid_format(fmt
))
166 errx(1, "invalid format string");
168 * XXX to be bug for bug compatible with Plan 9 add a
169 * newline if none found at the end of the format string.
172 fmt
= generate_format(first
, incr
, last
, equalize
, pad
);
175 for (; first
<= last
; first
+= incr
) {
180 for (; first
>= last
; first
+= incr
) {
192 * numeric - verify that string is numeric
195 numeric(const char *s
)
197 int seen_decimal_pt
, decimal_pt_len
;
200 if (ISSIGN((unsigned char)*s
))
204 decimal_pt_len
= strlen(decimal_point
);
206 if (!isdigit((unsigned char)*s
)) {
207 if (!seen_decimal_pt
&&
208 strncmp(s
, decimal_point
, decimal_pt_len
) == 0) {
213 if (ISEXP((unsigned char)*s
)) {
215 if (ISSIGN((unsigned char)*s
)) {
228 * valid_format - validate user specified format string
231 valid_format(const char *fmt
)
233 unsigned conversions
= 0;
235 while (*fmt
!= '\0') {
236 /* scan for conversions */
243 /* allow %% but not things like %10% */
250 while (*fmt
!= '\0' && strchr("#0- +'", *fmt
)) {
255 while (*fmt
!= '\0' && strchr("0123456789", *fmt
)) {
262 while (*fmt
!= '\0' && strchr("0123456789", *fmt
)) {
277 /* floating point formats are accepted */
281 /* anything else is not */
286 return (conversions
<= 1);
290 * unescape - handle C escapes in a string
295 char c
, *cp
, *new = orig
;
298 for (cp
= orig
; (*orig
= *cp
); cp
++, orig
++) {
303 case 'a': /* alert (bell) */
306 case 'b': /* backspace */
309 case 'e': /* escape */
312 case 'f': /* formfeed */
315 case 'n': /* newline */
318 case 'r': /* carriage return */
321 case 't': /* horizontal tab */
324 case 'v': /* vertical tab */
327 case '\\': /* backslash */
330 case '\'': /* single quote */
333 case '\"': /* double quote */
339 case '3': /* octal */
343 case '7': /* number */
345 ISODIGIT((unsigned char)*cp
) && i
< 3;
353 case 'x': /* hexidecimal number */
356 isxdigit((unsigned char)*cp
) && i
< 2;
359 if (isdigit((unsigned char)*cp
))
362 c
|= ((toupper((unsigned char)*cp
) -
378 * e_atof - convert an ASCII string to a double
379 * exit if string is not a valid double, or if converted value would
380 * cause overflow or underflow
383 e_atof(const char *num
)
389 dbl
= strtod(num
, &endp
);
392 /* under or overflow */
394 else if (*endp
!= '\0')
395 /* "junk" left in number */
396 errx(2, "invalid floating point argument: %s", num
);
398 /* zero shall have no sign */
405 * decimal_places - count decimal places in a number (string)
408 decimal_places(const char *number
)
413 /* look for a decimal point */
414 if ((dp
= strstr(number
, decimal_point
))) {
415 dp
+= strlen(decimal_point
);
417 while (isdigit((unsigned char)*dp
++))
424 * generate_format - create a format string
426 * XXX to be bug for bug compatable with Plan9 and GNU return "%g"
427 * when "%g" prints as "%e" (this way no width adjustments are made)
430 generate_format(double first
, double incr
, double last
, int equalize
, char pad
)
432 static char buf
[256];
434 int precision
, width1
, width2
, places
;
437 return (default_format
);
439 /* figure out "last" value printed */
441 last
= first
- incr
* floor((first
- last
) / incr
);
443 last
= first
+ incr
* floor((last
- first
) / incr
);
445 sprintf(buf
, "%g", incr
);
446 if (strchr(buf
, 'e'))
448 precision
= decimal_places(buf
);
450 width1
= sprintf(buf
, "%g", first
);
451 if (strchr(buf
, 'e'))
453 if ((places
= decimal_places(buf
)))
454 width1
-= (places
+ strlen(decimal_point
));
456 precision
= MAX(places
, precision
);
458 width2
= sprintf(buf
, "%g", last
);
459 if (strchr(buf
, 'e'))
461 if ((places
= decimal_places(buf
)))
462 width2
-= (places
+ strlen(decimal_point
));
465 sprintf(buf
, "%%%c%d.%d%c", pad
,
466 MAX(width1
, width2
) + (int) strlen(decimal_point
) +
467 precision
, precision
, (cc
) ? cc
: 'f');
469 sprintf(buf
, "%%%c%d%c", pad
, MAX(width1
, width2
),