cf856bf0416dcb0d61e5e0f93f9bf235591c0443
[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 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         double last, increment, i;
19         enum { OPT_w = 1, OPT_s };
20         const char *sep = "\n";
21         bool is_consecutive = 0;
22         unsigned opt = getopt32(argv, "+ws:", &sep);
23         unsigned width = 0;
24
25         argc -= optind;
26         argv += optind;
27         i = increment = 1;
28         switch (argc) {
29                 case 3:
30                         increment = atof(argv[1]);
31                 case 2:
32                         i = atof(*argv);
33                 case 1:
34                         last = atof(argv[argc-1]);
35                         break;
36                 default:
37                         bb_show_usage();
38         }
39         if (opt & OPT_w) /* Pad to length of start or last */
40                 width = MAX(strlen(*argv), strlen(argv[argc-1]));
41
42         /* You should note that this is pos-5.0.91 semantics, -- FK. */
43         while ((increment > 0 && i <= last) || (increment < 0 && i >= last)) {
44                 if (is_consecutive++) {
45                         printf("%s", sep);
46                 }
47                 printf("%0*g", width, i);
48                 i += increment;
49         }
50         bb_putchar('\n');
51         return fflush(stdout);
52 }