seq.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * seq implementation for busybox
  4. *
  5. * Copyright (C) 2004, Glenn McGrath
  6. *
  7. * Licensed under the GPL v2, see the file LICENSE in this tarball.
  8. */
  9. #include "libbb.h"
  10. /* This is a NOFORK applet. Be very careful! */
  11. int seq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  12. int seq_main(int argc, char **argv)
  13. {
  14. enum {
  15. OPT_w = (1 << 0),
  16. OPT_s = (1 << 1),
  17. };
  18. double first, last, increment, v;
  19. unsigned n;
  20. unsigned width;
  21. unsigned frac_part;
  22. const char *sep, *opt_s = "\n";
  23. unsigned opt;
  24. #if ENABLE_LOCALE_SUPPORT
  25. /* Undo busybox.c: on input, we want to use dot
  26. * as fractional separator, regardless of current locale */
  27. setlocale(LC_NUMERIC, "C");
  28. #endif
  29. opt = getopt32(argv, "+ws:", &opt_s);
  30. argc -= optind;
  31. argv += optind;
  32. first = increment = 1;
  33. errno = 0;
  34. switch (argc) {
  35. char *pp;
  36. case 3:
  37. increment = strtod(argv[1], &pp);
  38. errno |= *pp;
  39. case 2:
  40. first = strtod(argv[0], &pp);
  41. errno |= *pp;
  42. case 1:
  43. last = strtod(argv[argc-1], &pp);
  44. if (!errno && *pp == '\0')
  45. break;
  46. default:
  47. bb_show_usage();
  48. }
  49. #if ENABLE_LOCALE_SUPPORT
  50. setlocale(LC_NUMERIC, "");
  51. #endif
  52. /* Last checked to be compatible with: coreutils-6.10 */
  53. width = 0;
  54. frac_part = 0;
  55. while (1) {
  56. char *dot = strchrnul(*argv, '.');
  57. int w = (dot - *argv);
  58. int f = strlen(dot);
  59. if (width < w)
  60. width = w;
  61. argv++;
  62. if (!*argv)
  63. break;
  64. /* Why do the above _before_ frac check below?
  65. * Try "seq 1 2.0" and "seq 1.0 2.0":
  66. * coreutils never pay attention to the number
  67. * of fractional digits in last arg. */
  68. if (frac_part < f)
  69. frac_part = f;
  70. }
  71. if (frac_part) {
  72. frac_part--;
  73. if (frac_part)
  74. width += frac_part + 1;
  75. }
  76. if (!(opt & OPT_w))
  77. width = 0;
  78. sep = "";
  79. v = first;
  80. n = 0;
  81. while (increment >= 0 ? v <= last : v >= last) {
  82. printf("%s%0*.*f", sep, width, frac_part, v);
  83. sep = opt_s;
  84. /* v += increment; - would accumulate floating point errors */
  85. n++;
  86. v = first + n * increment;
  87. }
  88. if (n) /* if while loop executed at least once */
  89. bb_putchar('\n');
  90. return fflush_all();
  91. }