blob: ed4946b05f07b1de5070ebd3b84680bf3792bb46 [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
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
Pere Orga34425382011-03-31 14:43:25 +020018
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"
Pere Orga34425382011-03-31 14:43:25 +020024//usage: "\n -w Pad to last with leading zeros"
25//usage: "\n -s SEP String separator"
26
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027#include "libbb.h"
Glenn L McGrath82364bb2004-01-27 09:22:20 +000028
Denis Vlasenko99912ca2007-04-10 15:43:37 +000029/* This is a NOFORK applet. Be very careful! */
30
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000031int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000032int seq_main(int argc, char **argv)
Glenn L McGrath82364bb2004-01-27 09:22:20 +000033{
Denis Vlasenko7753ea42008-11-12 21:37:19 +000034 enum {
35 OPT_w = (1 << 0),
36 OPT_s = (1 << 1),
37 };
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020038 double first, last, increment, v;
39 unsigned n;
40 unsigned width;
41 unsigned frac_part;
Denis Vlasenko7753ea42008-11-12 21:37:19 +000042 const char *sep, *opt_s = "\n";
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020043 unsigned opt;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000044
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020045#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);
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000052 argc -= optind;
53 argv += optind;
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020054 first = increment = 1;
55 errno = 0;
Rob Landley73a20f32006-02-23 19:54:48 +000056 switch (argc) {
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020057 char *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000058 case 3:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020059 increment = strtod(argv[1], &pp);
60 errno |= *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000061 case 2:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020062 first = strtod(argv[0], &pp);
63 errno |= *pp;
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000064 case 1:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020065 last = strtod(argv[argc-1], &pp);
66 if (!errno && *pp == '\0')
67 break;
Rob Landley73a20f32006-02-23 19:54:48 +000068 default:
69 bb_show_usage();
Glenn L McGrath82364bb2004-01-27 09:22:20 +000070 }
Glenn L McGrath82364bb2004-01-27 09:22:20 +000071
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020072#if ENABLE_LOCALE_SUPPORT
73 setlocale(LC_NUMERIC, "");
74#endif
75
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020076 /* 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 int 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;
Glenn L McGrath82364bb2004-01-27 09:22:20 +000094 }
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020095 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) {
Denys Vlasenko786635e2010-10-17 12:44:39 +0200107 if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
108 break; /* I/O error, bail out (yes, this really happens) */
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200109 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
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100117 return fflush_all();
Glenn L McGrath82364bb2004-01-27 09:22:20 +0000118}