1 /* vi: set sw=4 ts=4: */
3 * seq implementation for busybox
5 * Copyright (C) 2004, Glenn McGrath
7 * Licensed under GPLv2, see file LICENSE in this source tree.
11 /* This is a NOFORK applet. Be very careful! */
13 int seq_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
14 int seq_main(int argc
, char **argv
)
20 double first
, last
, increment
, v
;
24 const char *sep
, *opt_s
= "\n";
27 #if ENABLE_LOCALE_SUPPORT
28 /* Undo busybox.c: on input, we want to use dot
29 * as fractional separator, regardless of current locale */
30 setlocale(LC_NUMERIC
, "C");
33 opt
= getopt32(argv
, "+ws:", &opt_s
);
36 first
= increment
= 1;
41 increment
= strtod(argv
[1], &pp
);
44 first
= strtod(argv
[0], &pp
);
47 last
= strtod(argv
[argc
-1], &pp
);
48 if (!errno
&& *pp
== '\0')
54 #if ENABLE_LOCALE_SUPPORT
55 setlocale(LC_NUMERIC
, "");
58 /* Last checked to be compatible with: coreutils-6.10 */
62 char *dot
= strchrnul(*argv
, '.');
63 int w
= (dot
- *argv
);
70 /* Why do the above _before_ frac check below?
71 * Try "seq 1 2.0" and "seq 1.0 2.0":
72 * coreutils never pay attention to the number
73 * of fractional digits in last arg. */
80 width
+= frac_part
+ 1;
88 while (increment
>= 0 ? v
<= last
: v
>= last
) {
89 if (printf("%s%0*.*f", sep
, width
, frac_part
, v
) < 0)
90 break; /* I/O error, bail out (yes, this really happens) */
92 /* v += increment; - would accumulate floating point errors */
94 v
= first
+ n
* increment
;
96 if (n
) /* if while loop executed at least once */