4b853c698643b77476b99e8767b3fa6234103ce3
[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 the GPL v2, see the file LICENSE in this tarball.
8  */
9
10 #include "libbb.h"
11
12 /* This is a NOFORK applet. Be very careful! */
13
14
15 int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
16 int seq_main(int argc, char **argv)
17 {
18         enum {
19                 OPT_w = (1 << 0),
20                 OPT_s = (1 << 1),
21         };
22         double last, increment, i;
23         const char *sep, *opt_s = "\n";
24         unsigned opt = getopt32(argv, "+ws:", &opt_s);
25         unsigned width = 0;
26
27         argc -= optind;
28         argv += optind;
29         i = increment = 1;
30         switch (argc) {
31                 case 3:
32                         increment = atof(argv[1]);
33                 case 2:
34                         i = atof(*argv);
35                 case 1:
36                         last = atof(argv[argc-1]);
37                         break;
38                 default:
39                         bb_show_usage();
40         }
41         if (opt & OPT_w) /* Pad to length of start or last */
42                 width = MAX(strlen(*argv), strlen(argv[argc-1]));
43
44         /* You should note that this is pos-5.0.91 semantics, -- FK. */
45         sep = "";
46         while ((increment > 0 && i <= last) || (increment < 0 && i >= last)) {
47                 printf("%s%0*g", sep, width, i);
48                 sep = opt_s;
49                 i += increment;
50         }
51         bb_putchar('\n');
52         return fflush(stdout);
53 }