shuf.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * shuf: Write a random permutation of the input lines to standard output.
  4. *
  5. * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config SHUF
  10. //config: bool "shuf"
  11. //config: default y
  12. //config: help
  13. //config: Generate random permutations
  14. //kbuild:lib-$(CONFIG_SHUF) += shuf.o
  15. //applet:IF_SHUF(APPLET_NOEXEC(shuf, shuf, BB_DIR_USR_BIN, BB_SUID_DROP, shuf))
  16. //usage:#define shuf_trivial_usage
  17. //usage: "[-e|-i L-H] [-n NUM] [-o FILE] [-z] [FILE|ARG...]"
  18. //usage:#define shuf_full_usage "\n\n"
  19. //usage: "Randomly permute lines\n"
  20. //usage: "\n -e Treat ARGs as lines"
  21. //usage: "\n -i L-H Treat numbers L-H as lines"
  22. //usage: "\n -n NUM Output at most NUM lines"
  23. //usage: "\n -o FILE Write to FILE, not standard output"
  24. //usage: "\n -z End lines with zero byte, not newline"
  25. #include "libbb.h"
  26. /* This is a NOEXEC applet. Be very careful! */
  27. #define OPT_e (1 << 0)
  28. #define OPT_i (1 << 1)
  29. #define OPT_n (1 << 2)
  30. #define OPT_o (1 << 3)
  31. #define OPT_z (1 << 4)
  32. #define OPT_STR "ei:n:o:z"
  33. /*
  34. * Use the Fisher-Yates shuffle algorithm on an array of lines.
  35. */
  36. static void shuffle_lines(char **lines, unsigned numlines)
  37. {
  38. unsigned i;
  39. unsigned r;
  40. char *tmp;
  41. srand(monotonic_us());
  42. for (i = numlines-1; i > 0; i--) {
  43. r = rand();
  44. /* RAND_MAX can be as small as 32767 */
  45. if (i > RAND_MAX)
  46. r ^= rand() << 15;
  47. r %= i;
  48. tmp = lines[i];
  49. lines[i] = lines[r];
  50. lines[r] = tmp;
  51. }
  52. }
  53. int shuf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  54. int shuf_main(int argc, char **argv)
  55. {
  56. unsigned opts;
  57. char *opt_i_str, *opt_n_str, *opt_o_str;
  58. unsigned i;
  59. char **lines;
  60. unsigned numlines;
  61. char eol;
  62. opt_complementary = "e--i:i--e"; /* mutually exclusive */
  63. opts = getopt32(argv, OPT_STR, &opt_i_str, &opt_n_str, &opt_o_str);
  64. argc -= optind;
  65. argv += optind;
  66. /* Prepare lines for shuffling - either: */
  67. if (opts & OPT_e) {
  68. /* make lines from command-line arguments */
  69. numlines = argc;
  70. lines = argv;
  71. } else
  72. if (opts & OPT_i) {
  73. /* create a range of numbers */
  74. char *dash;
  75. unsigned lo, hi;
  76. dash = strchr(opt_i_str, '-');
  77. if (!dash) {
  78. bb_error_msg_and_die("bad range '%s'", opt_i_str);
  79. }
  80. *dash = '\0';
  81. lo = xatou(opt_i_str);
  82. hi = xatou(dash + 1);
  83. *dash = '-';
  84. if (hi < lo) {
  85. bb_error_msg_and_die("bad range '%s'", opt_i_str);
  86. }
  87. numlines = (hi+1) - lo;
  88. lines = xmalloc(numlines * sizeof(lines[0]));
  89. for (i = 0; i < numlines; i++) {
  90. lines[i] = (char*)(uintptr_t)lo;
  91. lo++;
  92. }
  93. } else {
  94. /* default - read lines from stdin or the input file */
  95. FILE *fp;
  96. if (argc > 1)
  97. bb_show_usage();
  98. fp = xfopen_stdin(argv[0] ? argv[0] : "-");
  99. lines = NULL;
  100. numlines = 0;
  101. for (;;) {
  102. char *line = xmalloc_fgetline(fp);
  103. if (!line)
  104. break;
  105. lines = xrealloc_vector(lines, 6, numlines);
  106. lines[numlines++] = line;
  107. }
  108. fclose_if_not_stdin(fp);
  109. }
  110. if (numlines != 0)
  111. shuffle_lines(lines, numlines);
  112. if (opts & OPT_o)
  113. xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
  114. if (opts & OPT_n) {
  115. unsigned maxlines;
  116. maxlines = xatou(opt_n_str);
  117. if (numlines > maxlines)
  118. numlines = maxlines;
  119. }
  120. eol = '\n';
  121. if (opts & OPT_z)
  122. eol = '\0';
  123. for (i = 0; i < numlines; i++) {
  124. if (opts & OPT_i)
  125. printf("%u%c", (unsigned)(uintptr_t)lines[i], eol);
  126. else
  127. printf("%s%c", lines[i], eol);
  128. }
  129. fflush_stdout_and_exit(EXIT_SUCCESS);
  130. }