claenups for previous commit
[oweals/busybox.git] / coreutils / seq.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * seq implementation for busybox
4  *
5  * Copyright (C) 2004, Glenn McGrath
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 //config:config SEQ
10 //config:       bool "seq (3.6 kb)"
11 //config:       default y
12 //config:       help
13 //config:       print a sequence of numbers
14
15 //applet:IF_SEQ(APPLET_NOEXEC(seq, seq, BB_DIR_USR_BIN, BB_SUID_DROP, seq))
16 /* was NOFORK, but then "seq 1 999999999" can't be ^C'ed if run by hush */
17
18 //kbuild:lib-$(CONFIG_SEQ) += seq.o
19
20 //usage:#define seq_trivial_usage
21 //usage:       "[-w] [-s SEP] [FIRST [INC]] LAST"
22 //usage:#define seq_full_usage "\n\n"
23 //usage:       "Print numbers from FIRST to LAST, in steps of INC.\n"
24 //usage:       "FIRST, INC default to 1.\n"
25 //usage:     "\n        -w      Pad to last with leading zeros"
26 //usage:     "\n        -s SEP  String separator"
27
28 #include "libbb.h"
29
30 /* This is a NOEXEC applet. Be very careful! */
31
32 int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
33 int seq_main(int argc, char **argv)
34 {
35         enum {
36                 OPT_w = (1 << 0),
37                 OPT_s = (1 << 1),
38         };
39         double first, last, increment, v;
40         unsigned n;
41         unsigned width;
42         unsigned frac_part;
43         const char *sep, *opt_s = "\n";
44         unsigned opt;
45
46 #if ENABLE_LOCALE_SUPPORT
47         /* Undo busybox.c: on input, we want to use dot
48          * as fractional separator, regardless of current locale */
49         setlocale(LC_NUMERIC, "C");
50 #endif
51
52         opt = getopt32(argv, "+ws:", &opt_s);
53         argc -= optind;
54         argv += optind;
55         first = increment = 1;
56         errno = 0;
57         switch (argc) {
58                         char *pp;
59                 case 3:
60                         increment = strtod(argv[1], &pp);
61                         errno |= *pp;
62                 case 2:
63                         first = strtod(argv[0], &pp);
64                         errno |= *pp;
65                 case 1:
66                         last = strtod(argv[argc-1], &pp);
67                         if (!errno && *pp == '\0')
68                                 break;
69                 default:
70                         bb_show_usage();
71         }
72
73 #if ENABLE_LOCALE_SUPPORT
74         setlocale(LC_NUMERIC, "");
75 #endif
76
77         /* Last checked to be compatible with: coreutils-6.10 */
78         width = 0;
79         frac_part = 0;
80         while (1) {
81                 char *dot = strchrnul(*argv, '.');
82                 int w = (dot - *argv);
83                 int f = strlen(dot);
84                 if (width < w)
85                         width = w;
86                 argv++;
87                 if (!*argv)
88                         break;
89                 /* Why do the above _before_ frac check below?
90                  * Try "seq 1 2.0" and "seq 1.0 2.0":
91                  * coreutils never pay attention to the number
92                  * of fractional digits in last arg. */
93                 if (frac_part < f)
94                         frac_part = f;
95         }
96         if (frac_part) {
97                 frac_part--;
98                 if (frac_part)
99                         width += frac_part + 1;
100         }
101         if (!(opt & OPT_w))
102                 width = 0;
103
104         sep = "";
105         v = first;
106         n = 0;
107         while (increment >= 0 ? v <= last : v >= last) {
108                 if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
109                         break; /* I/O error, bail out (yes, this really happens) */
110                 sep = opt_s;
111                 /* v += increment; - would accumulate floating point errors */
112                 n++;
113                 v = first + n * increment;
114         }
115         if (n) /* if while loop executed at least once */
116                 bb_putchar('\n');
117
118         return fflush_all();
119 }