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.
10 //usage:#define seq_trivial_usage
11 //usage: "[-w] [-s SEP] [FIRST [INC]] LAST"
12 //usage:#define seq_full_usage "\n\n"
13 //usage: "Print numbers from FIRST to LAST, in steps of INC.\n"
14 //usage: "FIRST, INC default to 1.\n"
15 //usage: "\n -w Pad to last with leading zeros"
16 //usage: "\n -s SEP String separator"
20 /* This is a NOFORK applet. Be very careful! */
22 int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
23 int seq_main(int argc, char **argv)
29 double first, last, increment, v;
33 const char *sep, *opt_s = "\n";
36 #if ENABLE_LOCALE_SUPPORT
37 /* Undo busybox.c: on input, we want to use dot
38 * as fractional separator, regardless of current locale */
39 setlocale(LC_NUMERIC, "C");
42 opt = getopt32(argv, "+ws:", &opt_s);
45 first = increment = 1;
50 increment = strtod(argv[1], &pp);
53 first = strtod(argv[0], &pp);
56 last = strtod(argv[argc-1], &pp);
57 if (!errno && *pp == '\0')
63 #if ENABLE_LOCALE_SUPPORT
64 setlocale(LC_NUMERIC, "");
67 /* Last checked to be compatible with: coreutils-6.10 */
71 char *dot = strchrnul(*argv, '.');
72 int w = (dot - *argv);
79 /* Why do the above _before_ frac check below?
80 * Try "seq 1 2.0" and "seq 1.0 2.0":
81 * coreutils never pay attention to the number
82 * of fractional digits in last arg. */
89 width += frac_part + 1;
97 while (increment >= 0 ? v <= last : v >= last) {
98 if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
99 break; /* I/O error, bail out (yes, this really happens) */
101 /* v += increment; - would accumulate floating point errors */
103 v = first + n * increment;
105 if (n) /* if while loop executed at least once */