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.4 2008/04/30 14:22:14 ginsbach 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
);
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
)) {
226 * valid_format - validate user specified format string
229 valid_format(const char *fmt
)
233 while (*fmt
!= '\0') {
234 /* scan for conversions */
235 if (*fmt
!= '\0' && *fmt
!= '%') {
238 } while (*fmt
!= '\0' && *fmt
!= '%');
240 /* scan a conversion */
250 /* valid conversions */
251 if (strchr("eEfgG", *fmt
) &&
256 /* flags, width and precsision */
257 if (isdigit((unsigned char)*fmt
) ||
258 strchr("+- 0#.", *fmt
))
261 /* oops! bad conversion format! */
263 } while (*fmt
!= '\0');
267 return (conversions
<= 1);
271 * unescape - handle C escapes in a string
276 char c
, *cp
, *new = orig
;
279 for (cp
= orig
; (*orig
= *cp
); cp
++, orig
++) {
284 case 'a': /* alert (bell) */
287 case 'b': /* backspace */
290 case 'e': /* escape */
293 case 'f': /* formfeed */
296 case 'n': /* newline */
299 case 'r': /* carriage return */
302 case 't': /* horizontal tab */
305 case 'v': /* vertical tab */
308 case '\\': /* backslash */
311 case '\'': /* single quote */
314 case '\"': /* double quote */
320 case '3': /* octal */
324 case '7': /* number */
326 ISODIGIT((unsigned char)*cp
) && i
< 3;
334 case 'x': /* hexidecimal number */
337 isxdigit((unsigned char)*cp
) && i
< 2;
340 if (isdigit((unsigned char)*cp
))
343 c
|= ((toupper((unsigned char)*cp
) -
359 * e_atof - convert an ASCII string to a double
360 * exit if string is not a valid double, or if converted value would
361 * cause overflow or underflow
364 e_atof(const char *num
)
370 dbl
= strtod(num
, &endp
);
373 /* under or overflow */
375 else if (*endp
!= '\0')
376 /* "junk" left in number */
377 errx(2, "invalid floating point argument: %s", num
);
379 /* zero shall have no sign */
386 * decimal_places - count decimal places in a number (string)
389 decimal_places(const char *number
)
394 /* look for a decimal point */
395 if ((dp
= strstr(number
, decimal_point
))) {
396 dp
+= strlen(decimal_point
);
398 while (isdigit((unsigned char)*dp
++))
405 * generate_format - create a format string
407 * XXX to be bug for bug compatable with Plan9 and GNU return "%g"
408 * when "%g" prints as "%e" (this way no width adjustments are made)
411 generate_format(double first
, double incr
, double last
, int equalize
, char pad
)
413 static char buf
[256];
415 int precision
, width1
, width2
, places
;
418 return (default_format
);
420 /* figure out "last" value printed */
422 last
= first
- incr
* floor((first
- last
) / incr
);
424 last
= first
+ incr
* floor((last
- first
) / incr
);
426 sprintf(buf
, "%g", incr
);
427 if (strchr(buf
, 'e'))
429 precision
= decimal_places(buf
);
431 width1
= sprintf(buf
, "%g", first
);
432 if (strchr(buf
, 'e'))
434 if ((places
= decimal_places(buf
)))
435 width1
-= (places
+ strlen(decimal_point
));
437 precision
= MAX(places
, precision
);
439 width2
= sprintf(buf
, "%g", last
);
440 if (strchr(buf
, 'e'))
442 if ((places
= decimal_places(buf
)))
443 width2
-= (places
+ strlen(decimal_point
));
446 sprintf(buf
, "%%%c%d.%d%c", pad
,
447 MAX(width1
, width2
) + (int) strlen(decimal_point
) +
448 precision
, precision
, (cc
) ? cc
: 'f');
450 sprintf(buf
, "%%%c%d%c", pad
, MAX(width1
, width2
),