blob: 84d11fd8c7f9d9b26ff759b292eb3ffb4cb23192 [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 *
Rob Landley73a20f32006-02-23 19:54:48 +00007 * Licensed under the GPL v2, see the file LICENSE in this tarball.
Glenn L McGrath82364bb2004-01-27 09:22:20 +00008 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Glenn L McGrath82364bb2004-01-27 09:22:20 +000010
Denis Vlasenko99912ca2007-04-10 15:43:37 +000011/* This is a NOFORK applet. Be very careful! */
12
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000013int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000014int seq_main(int argc, char **argv)
Glenn L McGrath82364bb2004-01-27 09:22:20 +000015{
Denis Vlasenko7753ea42008-11-12 21:37:19 +000016 enum {
17 OPT_w = (1 << 0),
18 OPT_s = (1 << 1),
19 };
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020020 double first, last, increment, v;
21 unsigned n;
22 unsigned width;
23 unsigned frac_part;
Denis Vlasenko7753ea42008-11-12 21:37:19 +000024 const char *sep, *opt_s = "\n";
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020025 unsigned opt;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000026
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020027#if ENABLE_LOCALE_SUPPORT
28 /* Undo busybox.c: on input, we want to use dot
29 * as fractional separator, regardless of current locale */
30 setlocale(LC_NUMERIC, "C");
31#endif
32
33 opt = getopt32(argv, "+ws:", &opt_s);
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000034 argc -= optind;
35 argv += optind;
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020036 first = increment = 1;
37 errno = 0;
Rob Landley73a20f32006-02-23 19:54:48 +000038 switch (argc) {
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020039 char *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000040 case 3:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020041 increment = strtod(argv[1], &pp);
42 errno |= *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000043 case 2:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020044 first = strtod(argv[0], &pp);
45 errno |= *pp;
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000046 case 1:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020047 last = strtod(argv[argc-1], &pp);
48 if (!errno && *pp == '\0')
49 break;
Rob Landley73a20f32006-02-23 19:54:48 +000050 default:
51 bb_show_usage();
Glenn L McGrath82364bb2004-01-27 09:22:20 +000052 }
Glenn L McGrath82364bb2004-01-27 09:22:20 +000053
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020054#if ENABLE_LOCALE_SUPPORT
55 setlocale(LC_NUMERIC, "");
56#endif
57
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020058 /* Last checked to be compatible with: coreutils-6.10 */
59 width = 0;
60 frac_part = 0;
61 while (1) {
62 char *dot = strchrnul(*argv, '.');
63 int w = (dot - *argv);
64 int f = strlen(dot);
65 if (width < w)
66 width = w;
67 argv++;
68 if (!*argv)
69 break;
70 /* Why do the above _before_ frac check below?
71 * Try "seq 1 2.0" and "seq 1.0 2.0":
72 * coreutils never pay attention to the number
73 * of fractional digits in last arg. */
74 if (frac_part < f)
75 frac_part = f;
Glenn L McGrath82364bb2004-01-27 09:22:20 +000076 }
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020077 if (frac_part) {
78 frac_part--;
79 if (frac_part)
80 width += frac_part + 1;
81 }
82 if (!(opt & OPT_w))
83 width = 0;
84
85 sep = "";
86 v = first;
87 n = 0;
88 while (increment >= 0 ? v <= last : v >= last) {
89 printf("%s%0*.*f", sep, width, frac_part, v);
90 sep = opt_s;
91 /* v += increment; - would accumulate floating point errors */
92 n++;
93 v = first + n * increment;
94 }
95 if (n) /* if while loop executed at least once */
96 bb_putchar('\n');
97
Denys Vlasenko8131eea2009-11-02 14:19:51 +010098 return fflush_all();
Glenn L McGrath82364bb2004-01-27 09:22:20 +000099}