3
0

seq.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * seq implementation for busybox
  4. *
  5. * Copyright (C) 2004, Glenn McGrath
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config SEQ
  10. //config: bool "seq (3.6 kb)"
  11. //config: default y
  12. //config: help
  13. //config: print a sequence of numbers
  14. //applet:IF_SEQ(APPLET_NOEXEC(seq, seq, BB_DIR_USR_BIN, BB_SUID_DROP, seq))
  15. /* was NOFORK, but then "seq 1 999999999" can't be ^C'ed if run by hush */
  16. //kbuild:lib-$(CONFIG_SEQ) += seq.o
  17. //usage:#define seq_trivial_usage
  18. //usage: "[-w] [-s SEP] [FIRST [INC]] LAST"
  19. //usage:#define seq_full_usage "\n\n"
  20. //usage: "Print numbers from FIRST to LAST, in steps of INC.\n"
  21. //usage: "FIRST, INC default to 1.\n"
  22. //usage: "\n -w Pad to last with leading zeros"
  23. //usage: "\n -s SEP String separator"
  24. #include "libbb.h"
  25. /* This is a NOEXEC applet. Be very careful! */
  26. int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int seq_main(int argc, char **argv)
  28. {
  29. enum {
  30. OPT_w = (1 << 0),
  31. OPT_s = (1 << 1),
  32. };
  33. double first, last, increment, v;
  34. unsigned n;
  35. unsigned width;
  36. unsigned frac_part;
  37. const char *sep, *opt_s = "\n";
  38. unsigned opt;
  39. #if ENABLE_LOCALE_SUPPORT
  40. /* Undo busybox.c: on input, we want to use dot
  41. * as fractional separator, regardless of current locale */
  42. setlocale(LC_NUMERIC, "C");
  43. #endif
  44. opt = getopt32(argv, "+ws:", &opt_s);
  45. argc -= optind;
  46. argv += optind;
  47. first = increment = 1;
  48. errno = 0;
  49. switch (argc) {
  50. char *pp;
  51. case 3:
  52. increment = strtod(argv[1], &pp);
  53. errno |= *pp;
  54. case 2:
  55. first = strtod(argv[0], &pp);
  56. errno |= *pp;
  57. case 1:
  58. last = strtod(argv[argc-1], &pp);
  59. if (!errno && *pp == '\0')
  60. break;
  61. default:
  62. bb_show_usage();
  63. }
  64. #if ENABLE_LOCALE_SUPPORT
  65. setlocale(LC_NUMERIC, "");
  66. #endif
  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;
  85. }
  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. sep = "";
  94. v = first;
  95. n = 0;
  96. while (increment >= 0 ? v <= last : v >= last) {
  97. if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
  98. break; /* I/O error, bail out (yes, this really happens) */
  99. sep = opt_s;
  100. /* v += increment; - would accumulate floating point errors */
  101. n++;
  102. v = first + n * increment;
  103. }
  104. if (n) /* if while loop executed at least once */
  105. bb_putchar('\n');
  106. return fflush_all();
  107. }