- add seq -w support (Natanael Copa)
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Wed, 12 Nov 2008 12:59:56 +0000 (12:59 -0000)
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Wed, 12 Nov 2008 12:59:56 +0000 (12:59 -0000)
coreutils/seq.c
include/usage.h
testsuite/seq.tests

index 01d71f256b9bc4df5c0105f6c773a933f6e252bd..899cd696bc68416d80bd7d610308719c21a65630 100644 (file)
@@ -16,23 +16,30 @@ int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int seq_main(int argc, char **argv)
 {
        double last, increment, i;
+       enum { OPT_w = 1 };
+       unsigned opt = getopt32(argv, "+w");
+       unsigned width = 0;
 
+       argc -= optind;
+       argv += optind;
        i = increment = 1;
        switch (argc) {
-               case 4:
-                       increment = atof(argv[2]);
                case 3:
-                       i = atof(argv[1]);
+                       increment = atof(argv[1]);
                case 2:
+                       i = atof(*argv);
+               case 1:
                        last = atof(argv[argc-1]);
                        break;
                default:
                        bb_show_usage();
        }
+       if (opt & OPT_w) /* Pad to length of start or last */
+               width = MAX(strlen(*argv), strlen(argv[argc-1]));
 
        /* You should note that this is pos-5.0.91 semantics, -- FK. */
        while ((increment > 0 && i <= last) || (increment < 0 && i >= last)) {
-               printf("%g\n", i);
+               printf("%0*g\n", width, i);
                i += increment;
        }
 
index 71f221828203a3705daa802bb46e7506fa2b6464..4360edba69f074e2bdc939eac2143e5cef4aae57 100644 (file)
      "\nOther options are silently ignored; -oi is implied" \
 
 #define seq_trivial_usage \
-       "[first [increment]] last"
+       "[-w] [first [increment]] last"
 #define seq_full_usage "\n\n" \
        "Print numbers from FIRST to LAST, in steps of INCREMENT.\n" \
        "FIRST, INCREMENT default to 1\n" \
      "\nArguments:" \
+     "\n       -w          Pad to last with leading zeros" \
      "\n       LAST" \
      "\n       FIRST LAST" \
      "\n       FIRST INCREMENT LAST" \
index ebb44e7a63dc4eb6032b1eda7437e26ea3e3512c..cea4eefb8d778f6e1f2961abc1f1644bc7c767c5 100755 (executable)
@@ -29,8 +29,13 @@ testing "seq count up by 2" "seq 4 2 8" "4\n6\n8\n" "" ""
 testing "seq count down by 2" "seq 8 -2 4" "8\n6\n4\n" "" ""
 testing "seq count wrong way #1" "seq 4 -2 8" "" "" ""
 testing "seq count wrong way #2" "seq 8 2 4" "" "" ""
-testing "seq count by .3" "seq 3 .3 4" "3\n3.3\n3.6\n3.9\n" "" ""
+testing "seq count by .3" "seq 3 .3 4" "3.0\n3.3\n3.6\n3.9\n" "" ""
 testing "seq count by -.9" "seq .7 -.9 -2.2" "0.7\n-0.2\n-1.1\n-2\n" "" ""
 testing "seq count by zero" "seq 4 0 8 | head -n 10" "" "" ""
 
+testing "seq one argument with padding" "seq -w 003" "001\n002\n003\n" "" ""
+testing "seq two arguments with padding" "seq -w 005 7" "005\n006\n007\n" "" ""
+testing "seq count down by 3 with padding" "seq -w 8 -3 04" "08\n05\n" "" ""
+testing "seq count by .3 with padding" "seq -w 03 .3 0004" "003.0\n003.3\n003.6\n003.9\n" "" ""
+
 exit $FAILCOUNT