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