blob: b600266fdc568d0a345f77f1d845672e9cff5fee [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 */
Pere Orga34425382011-03-31 14:43:25 +02009
10//usage:#define seq_trivial_usage
11//usage: "[-w] [-s SEP] [FIRST [INC]] LAST"
12//usage:#define seq_full_usage "\n\n"
13//usage: "Print numbers from FIRST to LAST, in steps of INC.\n"
14//usage: "FIRST, INC default to 1.\n"
15//usage: "\nOptions:"
16//usage: "\n -w Pad to last with leading zeros"
17//usage: "\n -s SEP String separator"
18
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Glenn L McGrath82364bb2004-01-27 09:22:20 +000020
Denis Vlasenko99912ca2007-04-10 15:43:37 +000021/* This is a NOFORK applet. Be very careful! */
22
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000023int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000024int seq_main(int argc, char **argv)
Glenn L McGrath82364bb2004-01-27 09:22:20 +000025{
Denis Vlasenko7753ea42008-11-12 21:37:19 +000026 enum {
27 OPT_w = (1 << 0),
28 OPT_s = (1 << 1),
29 };
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020030 double first, last, increment, v;
31 unsigned n;
32 unsigned width;
33 unsigned frac_part;
Denis Vlasenko7753ea42008-11-12 21:37:19 +000034 const char *sep, *opt_s = "\n";
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020035 unsigned opt;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000036
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020037#if ENABLE_LOCALE_SUPPORT
38 /* Undo busybox.c: on input, we want to use dot
39 * as fractional separator, regardless of current locale */
40 setlocale(LC_NUMERIC, "C");
41#endif
42
43 opt = getopt32(argv, "+ws:", &opt_s);
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000044 argc -= optind;
45 argv += optind;
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020046 first = increment = 1;
47 errno = 0;
Rob Landley73a20f32006-02-23 19:54:48 +000048 switch (argc) {
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020049 char *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000050 case 3:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020051 increment = strtod(argv[1], &pp);
52 errno |= *pp;
Rob Landley73a20f32006-02-23 19:54:48 +000053 case 2:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020054 first = strtod(argv[0], &pp);
55 errno |= *pp;
Bernhard Reutner-Fischer2598f762008-11-12 12:59:56 +000056 case 1:
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020057 last = strtod(argv[argc-1], &pp);
58 if (!errno && *pp == '\0')
59 break;
Rob Landley73a20f32006-02-23 19:54:48 +000060 default:
61 bb_show_usage();
Glenn L McGrath82364bb2004-01-27 09:22:20 +000062 }
Glenn L McGrath82364bb2004-01-27 09:22:20 +000063
Denys Vlasenko9517d8a2009-06-15 15:47:58 +020064#if ENABLE_LOCALE_SUPPORT
65 setlocale(LC_NUMERIC, "");
66#endif
67
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020068 /* Last checked to be compatible with: coreutils-6.10 */
69 width = 0;
70 frac_part = 0;
71 while (1) {
72 char *dot = strchrnul(*argv, '.');
73 int w = (dot - *argv);
74 int f = strlen(dot);
75 if (width < w)
76 width = w;
77 argv++;
78 if (!*argv)
79 break;
80 /* Why do the above _before_ frac check below?
81 * Try "seq 1 2.0" and "seq 1.0 2.0":
82 * coreutils never pay attention to the number
83 * of fractional digits in last arg. */
84 if (frac_part < f)
85 frac_part = f;
Glenn L McGrath82364bb2004-01-27 09:22:20 +000086 }
Denys Vlasenkocd3dd422009-06-15 09:16:27 +020087 if (frac_part) {
88 frac_part--;
89 if (frac_part)
90 width += frac_part + 1;
91 }
92 if (!(opt & OPT_w))
93 width = 0;
94
95 sep = "";
96 v = first;
97 n = 0;
98 while (increment >= 0 ? v <= last : v >= last) {
Denys Vlasenko786635e2010-10-17 12:44:39 +020099 if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
100 break; /* I/O error, bail out (yes, this really happens) */
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200101 sep = opt_s;
102 /* v += increment; - would accumulate floating point errors */
103 n++;
104 v = first + n * increment;
105 }
106 if (n) /* if while loop executed at least once */
107 bb_putchar('\n');
108
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100109 return fflush_all();
Glenn L McGrath82364bb2004-01-27 09:22:20 +0000110}