Glenn L McGrath | efc6bf6 | 2004-07-23 06:43:29 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 2 | /* |
Glenn L McGrath | efc6bf6 | 2004-07-23 06:43:29 +0000 | [diff] [blame] | 3 | * seq implementation for busybox |
| 4 | * |
Rob Landley | 73a20f3 | 2006-02-23 19:54:48 +0000 | [diff] [blame] | 5 | * Copyright (C) 2004, Glenn McGrath |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 8 | */ |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 9 | |
| 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" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 15 | //usage: "\n -w Pad to last with leading zeros" |
| 16 | //usage: "\n -s SEP String separator" |
| 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 20 | /* This is a NOFORK applet. Be very careful! */ |
| 21 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 22 | int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 23 | int seq_main(int argc, char **argv) |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 24 | { |
Denis Vlasenko | 7753ea4 | 2008-11-12 21:37:19 +0000 | [diff] [blame] | 25 | enum { |
| 26 | OPT_w = (1 << 0), |
| 27 | OPT_s = (1 << 1), |
| 28 | }; |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 29 | double first, last, increment, v; |
| 30 | unsigned n; |
| 31 | unsigned width; |
| 32 | unsigned frac_part; |
Denis Vlasenko | 7753ea4 | 2008-11-12 21:37:19 +0000 | [diff] [blame] | 33 | const char *sep, *opt_s = "\n"; |
Denys Vlasenko | 9517d8a | 2009-06-15 15:47:58 +0200 | [diff] [blame] | 34 | unsigned opt; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 35 | |
Denys Vlasenko | 9517d8a | 2009-06-15 15:47:58 +0200 | [diff] [blame] | 36 | #if ENABLE_LOCALE_SUPPORT |
| 37 | /* Undo busybox.c: on input, we want to use dot |
| 38 | * as fractional separator, regardless of current locale */ |
| 39 | setlocale(LC_NUMERIC, "C"); |
| 40 | #endif |
| 41 | |
| 42 | opt = getopt32(argv, "+ws:", &opt_s); |
Bernhard Reutner-Fischer | 2598f76 | 2008-11-12 12:59:56 +0000 | [diff] [blame] | 43 | argc -= optind; |
| 44 | argv += optind; |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 45 | first = increment = 1; |
| 46 | errno = 0; |
Rob Landley | 73a20f3 | 2006-02-23 19:54:48 +0000 | [diff] [blame] | 47 | switch (argc) { |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 48 | char *pp; |
Rob Landley | 73a20f3 | 2006-02-23 19:54:48 +0000 | [diff] [blame] | 49 | case 3: |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 50 | increment = strtod(argv[1], &pp); |
| 51 | errno |= *pp; |
Rob Landley | 73a20f3 | 2006-02-23 19:54:48 +0000 | [diff] [blame] | 52 | case 2: |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 53 | first = strtod(argv[0], &pp); |
| 54 | errno |= *pp; |
Bernhard Reutner-Fischer | 2598f76 | 2008-11-12 12:59:56 +0000 | [diff] [blame] | 55 | case 1: |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 56 | last = strtod(argv[argc-1], &pp); |
| 57 | if (!errno && *pp == '\0') |
| 58 | break; |
Rob Landley | 73a20f3 | 2006-02-23 19:54:48 +0000 | [diff] [blame] | 59 | default: |
| 60 | bb_show_usage(); |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 61 | } |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | 9517d8a | 2009-06-15 15:47:58 +0200 | [diff] [blame] | 63 | #if ENABLE_LOCALE_SUPPORT |
| 64 | setlocale(LC_NUMERIC, ""); |
| 65 | #endif |
| 66 | |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 67 | /* Last checked to be compatible with: coreutils-6.10 */ |
| 68 | width = 0; |
| 69 | frac_part = 0; |
| 70 | while (1) { |
| 71 | char *dot = strchrnul(*argv, '.'); |
| 72 | int w = (dot - *argv); |
| 73 | int f = strlen(dot); |
| 74 | if (width < w) |
| 75 | width = w; |
| 76 | argv++; |
| 77 | if (!*argv) |
| 78 | break; |
| 79 | /* Why do the above _before_ frac check below? |
| 80 | * Try "seq 1 2.0" and "seq 1.0 2.0": |
| 81 | * coreutils never pay attention to the number |
| 82 | * of fractional digits in last arg. */ |
| 83 | if (frac_part < f) |
| 84 | frac_part = f; |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 85 | } |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 86 | if (frac_part) { |
| 87 | frac_part--; |
| 88 | if (frac_part) |
| 89 | width += frac_part + 1; |
| 90 | } |
| 91 | if (!(opt & OPT_w)) |
| 92 | width = 0; |
| 93 | |
| 94 | sep = ""; |
| 95 | v = first; |
| 96 | n = 0; |
| 97 | while (increment >= 0 ? v <= last : v >= last) { |
Denys Vlasenko | 786635e | 2010-10-17 12:44:39 +0200 | [diff] [blame] | 98 | if (printf("%s%0*.*f", sep, width, frac_part, v) < 0) |
| 99 | break; /* I/O error, bail out (yes, this really happens) */ |
Denys Vlasenko | cd3dd42 | 2009-06-15 09:16:27 +0200 | [diff] [blame] | 100 | sep = opt_s; |
| 101 | /* v += increment; - would accumulate floating point errors */ |
| 102 | n++; |
| 103 | v = first + n * increment; |
| 104 | } |
| 105 | if (n) /* if while loop executed at least once */ |
| 106 | bb_putchar('\n'); |
| 107 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 108 | return fflush_all(); |
Glenn L McGrath | 82364bb | 2004-01-27 09:22:20 +0000 | [diff] [blame] | 109 | } |