util-linux/lsusb.c: print manufacturer/product strings if available
[busybox-git.git] / coreutils / seq.c
blob094f74e877474db224950d8554fde24d728609e0
1 /* vi: set sw=4 ts=4: */
2 /*
3 * seq implementation for busybox
5 * Copyright (C) 2004, Glenn McGrath
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9 //config:config SEQ
10 //config: bool "seq (4 kb)"
11 //config: default y
12 //config: help
13 //config: print a sequence of numbers
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 */
18 //kbuild:lib-$(CONFIG_SEQ) += seq.o
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 with leading zeros"
26 //usage: "\n -s SEP String separator"
28 #include "libbb.h"
30 /* This is a NOEXEC applet. Be very careful! */
32 int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
33 int seq_main(int argc, char **argv)
35 enum {
36 OPT_w = (1 << 0),
37 OPT_s = (1 << 1),
39 double first, last, increment, v;
40 unsigned n;
41 unsigned width;
42 unsigned frac_part;
43 const char *sep, *opt_s = "\n";
44 char *saved;
45 unsigned opt;
47 #if ENABLE_LOCALE_SUPPORT
48 /* Undo busybox.c: on input, we want to use dot
49 * as fractional separator, regardless of current locale */
50 setlocale(LC_NUMERIC, "C");
51 #endif
53 /* Cater for negative arguments: if we see one, truncate argv[] on it */
54 n = 0;
55 for (;;) {
56 char c;
57 saved = argv[++n];
58 if (!saved)
59 break;
60 if (saved[0] != '-') {
61 // break; // "seq -s : -1 1" won't be treated correctly
62 continue;
64 // "seq -s -1 1 9" is not treated correctly, but such usage
65 // (delimiter string which looks like negative number) is very unlikely
66 c = saved[1];
67 if (c == '.' || (c >= '0' && c <= '9')) {
68 argv[n] = NULL;
69 break;
72 opt = getopt32(argv, "+ws:", &opt_s); /* "+": stop at first non-option */
73 /* Restore possibly truncated argv[] */
74 argv[n] = saved;
76 argc -= optind;
77 argv += optind;
78 first = increment = 1;
79 errno = 0;
80 switch (argc) {
81 char *pp;
82 case 3:
83 increment = strtod(argv[1], &pp);
84 errno |= *pp;
85 case 2:
86 first = strtod(argv[0], &pp);
87 errno |= *pp;
88 case 1:
89 last = strtod(argv[argc-1], &pp);
90 if (!errno && *pp == '\0')
91 break;
92 default:
93 bb_show_usage();
96 #if ENABLE_LOCALE_SUPPORT
97 setlocale(LC_NUMERIC, "");
98 #endif
100 /* Last checked to be compatible with: coreutils-6.10 */
101 width = 0;
102 frac_part = 0;
103 while (1) {
104 char *dot = strchrnul(*argv, '.');
105 int w = (dot - *argv);
106 int f = strlen(dot);
107 if (width < w)
108 width = w;
109 argv++;
110 if (!*argv)
111 break;
112 /* Why do the above _before_ frac check below?
113 * Try "seq 1 2.0" and "seq 1.0 2.0":
114 * coreutils never pay attention to the number
115 * of fractional digits in last arg. */
116 if (frac_part < f)
117 frac_part = f;
119 if (frac_part) {
120 frac_part--;
121 if (frac_part)
122 width += frac_part + 1;
124 if (!(opt & OPT_w))
125 width = 0;
127 sep = "";
128 v = first;
129 n = 0;
130 while (increment >= 0 ? v <= last : v >= last) {
131 if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
132 break; /* I/O error, bail out (yes, this really happens) */
133 sep = opt_s;
134 /* v += increment; - would accumulate floating point errors */
135 n++;
136 v = first + n * increment;
138 if (n) /* if while loop executed at least once */
139 bb_putchar('\n');
141 return fflush_all();