2004-07-23 06:43:29 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2004-01-27 09:22:20 +00:00
|
|
|
/*
|
2004-07-23 06:43:29 +00:00
|
|
|
* seq implementation for busybox
|
|
|
|
*
|
2006-02-23 19:54:48 +00:00
|
|
|
* Copyright (C) 2004, Glenn McGrath
|
2004-01-27 09:22:20 +00:00
|
|
|
*
|
2006-02-23 19:54:48 +00:00
|
|
|
* Licensed under the GPL v2, see the file LICENSE in this tarball.
|
2004-01-27 09:22:20 +00:00
|
|
|
*/
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2004-01-27 09:22:20 +00:00
|
|
|
|
2007-04-10 15:43:37 +00:00
|
|
|
/* This is a NOFORK applet. Be very careful! */
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2006-03-06 20:47:33 +00:00
|
|
|
int seq_main(int argc, char **argv)
|
2004-01-27 09:22:20 +00:00
|
|
|
{
|
2008-11-12 21:37:19 +00:00
|
|
|
enum {
|
|
|
|
OPT_w = (1 << 0),
|
|
|
|
OPT_s = (1 << 1),
|
|
|
|
};
|
2009-06-15 07:16:27 +00:00
|
|
|
double first, last, increment, v;
|
|
|
|
unsigned n;
|
|
|
|
unsigned width;
|
|
|
|
unsigned frac_part;
|
2008-11-12 21:37:19 +00:00
|
|
|
const char *sep, *opt_s = "\n";
|
2009-06-15 13:47:58 +00:00
|
|
|
unsigned opt;
|
2006-09-17 16:28:10 +00:00
|
|
|
|
2009-06-15 13:47:58 +00:00
|
|
|
#if ENABLE_LOCALE_SUPPORT
|
|
|
|
/* Undo busybox.c: on input, we want to use dot
|
|
|
|
* as fractional separator, regardless of current locale */
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
opt = getopt32(argv, "+ws:", &opt_s);
|
2008-11-12 12:59:56 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2009-06-15 07:16:27 +00:00
|
|
|
first = increment = 1;
|
|
|
|
errno = 0;
|
2006-02-23 19:54:48 +00:00
|
|
|
switch (argc) {
|
2009-06-15 07:16:27 +00:00
|
|
|
char *pp;
|
2006-02-23 19:54:48 +00:00
|
|
|
case 3:
|
2009-06-15 07:16:27 +00:00
|
|
|
increment = strtod(argv[1], &pp);
|
|
|
|
errno |= *pp;
|
2006-02-23 19:54:48 +00:00
|
|
|
case 2:
|
2009-06-15 07:16:27 +00:00
|
|
|
first = strtod(argv[0], &pp);
|
|
|
|
errno |= *pp;
|
2008-11-12 12:59:56 +00:00
|
|
|
case 1:
|
2009-06-15 07:16:27 +00:00
|
|
|
last = strtod(argv[argc-1], &pp);
|
|
|
|
if (!errno && *pp == '\0')
|
|
|
|
break;
|
2006-02-23 19:54:48 +00:00
|
|
|
default:
|
|
|
|
bb_show_usage();
|
2004-01-27 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2009-06-15 13:47:58 +00:00
|
|
|
#if ENABLE_LOCALE_SUPPORT
|
|
|
|
setlocale(LC_NUMERIC, "");
|
|
|
|
#endif
|
|
|
|
|
2009-06-15 07:16:27 +00:00
|
|
|
/* Last checked to be compatible with: coreutils-6.10 */
|
|
|
|
width = 0;
|
|
|
|
frac_part = 0;
|
|
|
|
while (1) {
|
|
|
|
char *dot = strchrnul(*argv, '.');
|
|
|
|
int w = (dot - *argv);
|
|
|
|
int f = strlen(dot);
|
|
|
|
if (width < w)
|
|
|
|
width = w;
|
|
|
|
argv++;
|
|
|
|
if (!*argv)
|
|
|
|
break;
|
|
|
|
/* Why do the above _before_ frac check below?
|
|
|
|
* Try "seq 1 2.0" and "seq 1.0 2.0":
|
|
|
|
* coreutils never pay attention to the number
|
|
|
|
* of fractional digits in last arg. */
|
|
|
|
if (frac_part < f)
|
|
|
|
frac_part = f;
|
|
|
|
}
|
|
|
|
if (frac_part) {
|
|
|
|
frac_part--;
|
|
|
|
if (frac_part)
|
|
|
|
width += frac_part + 1;
|
|
|
|
}
|
|
|
|
if (!(opt & OPT_w))
|
|
|
|
width = 0;
|
|
|
|
|
2008-11-12 21:37:19 +00:00
|
|
|
sep = "";
|
2009-06-15 07:16:27 +00:00
|
|
|
v = first;
|
|
|
|
n = 0;
|
|
|
|
while (increment >= 0 ? v <= last : v >= last) {
|
|
|
|
printf("%s%0*.*f", sep, width, frac_part, v);
|
2008-11-12 21:37:19 +00:00
|
|
|
sep = opt_s;
|
2009-06-15 07:16:27 +00:00
|
|
|
/* v += increment; - would accumulate floating point errors */
|
|
|
|
n++;
|
|
|
|
v = first + n * increment;
|
2004-01-27 09:22:20 +00:00
|
|
|
}
|
2009-06-15 07:16:27 +00:00
|
|
|
if (n) /* if while loop executed at least once */
|
|
|
|
bb_putchar('\n');
|
|
|
|
|
2009-11-02 13:19:51 +00:00
|
|
|
return fflush_all();
|
2004-01-27 09:22:20 +00:00
|
|
|
}
|