adduser: prefer to call addgroup --gid, not non-std addgroup -g
[platform/upstream/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
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:     "\nOptions:"
16 //usage:     "\n        -w      Pad to last with leading zeros"
17 //usage:     "\n        -s SEP  String separator"
18
19 #include "libbb.h"
20
21 /* This is a NOFORK applet. Be very careful! */
22
23 int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24 int seq_main(int argc, char **argv)
25 {
26         enum {
27                 OPT_w = (1 << 0),
28                 OPT_s = (1 << 1),
29         };
30         double first, last, increment, v;
31         unsigned n;
32         unsigned width;
33         unsigned frac_part;
34         const char *sep, *opt_s = "\n";
35         unsigned opt;
36
37 #if ENABLE_LOCALE_SUPPORT
38         /* Undo busybox.c: on input, we want to use dot
39          * as fractional separator, regardless of current locale */
40         setlocale(LC_NUMERIC, "C");
41 #endif
42
43         opt = getopt32(argv, "+ws:", &opt_s);
44         argc -= optind;
45         argv += optind;
46         first = increment = 1;
47         errno = 0;
48         switch (argc) {
49                         char *pp;
50                 case 3:
51                         increment = strtod(argv[1], &pp);
52                         errno |= *pp;
53                 case 2:
54                         first = strtod(argv[0], &pp);
55                         errno |= *pp;
56                 case 1:
57                         last = strtod(argv[argc-1], &pp);
58                         if (!errno && *pp == '\0')
59                                 break;
60                 default:
61                         bb_show_usage();
62         }
63
64 #if ENABLE_LOCALE_SUPPORT
65         setlocale(LC_NUMERIC, "");
66 #endif
67
68         /* Last checked to be compatible with: coreutils-6.10 */
69         width = 0;
70         frac_part = 0;
71         while (1) {
72                 char *dot = strchrnul(*argv, '.');
73                 int w = (dot - *argv);
74                 int f = strlen(dot);
75                 if (width < w)
76                         width = w;
77                 argv++;
78                 if (!*argv)
79                         break;
80                 /* Why do the above _before_ frac check below?
81                  * Try "seq 1 2.0" and "seq 1.0 2.0":
82                  * coreutils never pay attention to the number
83                  * of fractional digits in last arg. */
84                 if (frac_part < f)
85                         frac_part = f;
86         }
87         if (frac_part) {
88                 frac_part--;
89                 if (frac_part)
90                         width += frac_part + 1;
91         }
92         if (!(opt & OPT_w))
93                 width = 0;
94
95         sep = "";
96         v = first;
97         n = 0;
98         while (increment >= 0 ? v <= last : v >= last) {
99                 if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
100                         break; /* I/O error, bail out (yes, this really happens) */
101                 sep = opt_s;
102                 /* v += increment; - would accumulate floating point errors */
103                 n++;
104                 v = first + n * increment;
105         }
106         if (n) /* if while loop executed at least once */
107                 bb_putchar('\n');
108
109         return fflush_all();
110 }