seq.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //usage:#define seq_trivial_usage
  10. //usage: "[-w] [-s SEP] [FIRST [INC]] LAST"
  11. //usage:#define seq_full_usage "\n\n"
  12. //usage: "Print numbers from FIRST to LAST, in steps of INC.\n"
  13. //usage: "FIRST, INC default to 1.\n"
  14. //usage: "\n -w Pad to last with leading zeros"
  15. //usage: "\n -s SEP String separator"
  16. #include "libbb.h"
  17. /* This is a NOFORK applet. Be very careful! */
  18. int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  19. int seq_main(int argc, char **argv)
  20. {
  21. enum {
  22. OPT_w = (1 << 0),
  23. OPT_s = (1 << 1),
  24. };
  25. double first, last, increment, v;
  26. unsigned n;
  27. unsigned width;
  28. unsigned frac_part;
  29. const char *sep, *opt_s = "\n";
  30. unsigned opt;
  31. #if ENABLE_LOCALE_SUPPORT
  32. /* Undo busybox.c: on input, we want to use dot
  33. * as fractional separator, regardless of current locale */
  34. setlocale(LC_NUMERIC, "C");
  35. #endif
  36. opt = getopt32(argv, "+ws:", &opt_s);
  37. argc -= optind;
  38. argv += optind;
  39. first = increment = 1;
  40. errno = 0;
  41. switch (argc) {
  42. char *pp;
  43. case 3:
  44. increment = strtod(argv[1], &pp);
  45. errno |= *pp;
  46. case 2:
  47. first = strtod(argv[0], &pp);
  48. errno |= *pp;
  49. case 1:
  50. last = strtod(argv[argc-1], &pp);
  51. if (!errno && *pp == '\0')
  52. break;
  53. default:
  54. bb_show_usage();
  55. }
  56. #if ENABLE_LOCALE_SUPPORT
  57. setlocale(LC_NUMERIC, "");
  58. #endif
  59. /* Last checked to be compatible with: coreutils-6.10 */
  60. width = 0;
  61. frac_part = 0;
  62. while (1) {
  63. char *dot = strchrnul(*argv, '.');
  64. int w = (dot - *argv);
  65. int f = strlen(dot);
  66. if (width < w)
  67. width = w;
  68. argv++;
  69. if (!*argv)
  70. break;
  71. /* Why do the above _before_ frac check below?
  72. * Try "seq 1 2.0" and "seq 1.0 2.0":
  73. * coreutils never pay attention to the number
  74. * of fractional digits in last arg. */
  75. if (frac_part < f)
  76. frac_part = f;
  77. }
  78. if (frac_part) {
  79. frac_part--;
  80. if (frac_part)
  81. width += frac_part + 1;
  82. }
  83. if (!(opt & OPT_w))
  84. width = 0;
  85. sep = "";
  86. v = first;
  87. n = 0;
  88. while (increment >= 0 ? v <= last : v >= last) {
  89. if (printf("%s%0*.*f", sep, width, frac_part, v) < 0)
  90. break; /* I/O error, bail out (yes, this really happens) */
  91. sep = opt_s;
  92. /* v += increment; - would accumulate floating point errors */
  93. n++;
  94. v = first + n * increment;
  95. }
  96. if (n) /* if while loop executed at least once */
  97. bb_putchar('\n');
  98. return fflush_all();
  99. }