blob: beb339d1ef207e282bdcd8a7c1f294eeeda4da4b [file] [log] [blame]
Glenn L McGrathefc6bf62004-07-23 06:43:29 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath82364bb2004-01-27 09:22:20 +00002/*
Glenn L McGrathefc6bf62004-07-23 06:43:29 +00003 * seq implementation for busybox
4 *
Rob Landley73a20f32006-02-23 19:54:48 +00005 * Copyright (C) 2004, Glenn McGrath
Glenn L McGrath82364bb2004-01-27 09:22:20 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Glenn L McGrath82364bb2004-01-27 09:22:20 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config SEQ
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "seq (3.8 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: print a sequence of numbers
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014
Denys Vlasenko7c40ddd2017-08-02 16:37:39 +020015//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 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010017
18//kbuild:lib-$(CONFIG_SEQ) += seq.o
Pere Orga34425382011-03-31 14:43:25 +020019
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"
Pere Orga34425382011-03-31 14:43:25 +020025//usage: "\n -w Pad to last with leading zeros"
26//usage: "\n -s SEP String separator"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Glenn L McGrath82364bb2004-01-27 09:22:20 +000029
Denys Vlasenko7c40ddd2017-08-02 16:37:39 +020030/* This is a NOEXEC applet. Be very careful! */
Denis Vlasenko99912ca2007-04-10 15:43:37 +000031
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000032int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000033int seq_main(int argc, char **argv)
Glenn L McGrath82364bb2004-01-27 09:22:20 +000034{
Denis Vlasenko7753ea42008-11-12 21:37:19 +000035 enum {
36 OPT_w = (1 << 0),
37 OPT_s = (1 << 1),
38 };
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020039 double first, last, increment, v;
40 unsigned n;
41 unsigned width;
42 unsigned frac_part;
Denis Vlasenko7753ea42008-11-12 21:37:19 +000043 const char *sep, *opt_s = "\n";
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020044 unsigned opt;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000045
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020046#if ENABLE_LOCALE_SUPPORT
47 /* Undo busybox.c: on input, we want to use dot
48 * as fractional separator, regardless of current locale */
49 setlocale(LC_NUMERIC, "C");
50#endif
51
52 opt = getopt32(argv, "+ws:", &opt_s);
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000053 argc -= optind;
54 argv += optind;
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020055 first = increment = 1;
56 errno = 0;
Rob Landley73a20f32006-02-23 19:54:48 +000057 switch (argc) {
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020058 char *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000059 case 3:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020060 increment = strtod(argv[1], &pp);
61 errno |= *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000062 case 2:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020063 first = strtod(argv[0], &pp);
64 errno |= *pp;
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000065 case 1:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020066 last = strtod(argv[argc-1], &pp);
67 if (!errno && *pp == '\0')
68 break;
Rob Landley73a20f32006-02-23 19:54:48 +000069 default:
70 bb_show_usage();
Glenn L McGrath82364bb2004-01-27 09:22:20 +000071 }
Glenn L McGrath82364bb2004-01-27 09:22:20 +000072
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020073#if ENABLE_LOCALE_SUPPORT
74 setlocale(LC_NUMERIC, "");
75#endif
76
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020077 /* Last checked to be compatible with: coreutils-6.10 */
78 width = 0;
79 frac_part = 0;
80 while (1) {
81 char *dot = strchrnul(*argv, '.');
82 int w = (dot - *argv);
83 int f = strlen(dot);
84 if (width < w)
85 width = w;
86 argv++;
87 if (!*argv)
88 break;
89 /* Why do the above _before_ frac check below?
90 * Try "seq 1 2.0" and "seq 1.0 2.0":
91 * coreutils never pay attention to the number
92 * of fractional digits in last arg. */
93 if (frac_part < f)
94 frac_part = f;
Glenn L McGrath82364bb2004-01-27 09:22:20 +000095 }
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020096 if (frac_part) {
97 frac_part--;
98 if (frac_part)
99 width += frac_part + 1;
100 }
101 if (!(opt & OPT_w))
102 width = 0;
103
104 sep = "";
105 v = first;
106 n = 0;
107 while (increment >= 0 ? v <= last : v >= last) {
Denys Vlasenko786635e2010-10-17 12:44:39 +0200108 if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
109 break; /* I/O error, bail out (yes, this really happens) */
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200110 sep = opt_s;
111 /* v += increment; - would accumulate floating point errors */
112 n++;
113 v = first + n * increment;
114 }
115 if (n) /* if while loop executed at least once */
116 bb_putchar('\n');
117
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100118 return fflush_all();
Glenn L McGrath82364bb2004-01-27 09:22:20 +0000119}