shuf.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 (5.4 kb)"
  11. //config: default y
  12. //config: help
  13. //config: Generate random permutations
  14. //applet:IF_SHUF(APPLET_NOEXEC(shuf, shuf, BB_DIR_USR_BIN, BB_SUID_DROP, shuf))
  15. //kbuild:lib-$(CONFIG_SHUF) += shuf.o
  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 + 1;
  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. opts = getopt32(argv, "^"
  63. OPT_STR
  64. "\0" "e--i:i--e"/* mutually exclusive */,
  65. &opt_i_str, &opt_n_str, &opt_o_str
  66. );
  67. argc -= optind;
  68. argv += optind;
  69. /* Prepare lines for shuffling - either: */
  70. if (opts & OPT_e) {
  71. /* make lines from command-line arguments */
  72. numlines = argc;
  73. lines = argv;
  74. } else
  75. if (opts & OPT_i) {
  76. /* create a range of numbers */
  77. char *dash;
  78. unsigned lo, hi;
  79. dash = strchr(opt_i_str, '-');
  80. if (!dash) {
  81. bb_error_msg_and_die("bad range '%s'", opt_i_str);
  82. }
  83. *dash = '\0';
  84. lo = xatou(opt_i_str);
  85. hi = xatou(dash + 1);
  86. *dash = '-';
  87. if (hi < lo) {
  88. bb_error_msg_and_die("bad range '%s'", opt_i_str);
  89. }
  90. numlines = (hi+1) - lo;
  91. lines = xmalloc(numlines * sizeof(lines[0]));
  92. for (i = 0; i < numlines; i++) {
  93. lines[i] = (char*)(uintptr_t)lo;
  94. lo++;
  95. }
  96. } else {
  97. /* default - read lines from stdin or the input file */
  98. FILE *fp;
  99. if (argc > 1)
  100. bb_show_usage();
  101. fp = xfopen_stdin(argv[0] ? argv[0] : "-");
  102. lines = NULL;
  103. numlines = 0;
  104. for (;;) {
  105. char *line = xmalloc_fgetline(fp);
  106. if (!line)
  107. break;
  108. lines = xrealloc_vector(lines, 6, numlines);
  109. lines[numlines++] = line;
  110. }
  111. fclose_if_not_stdin(fp);
  112. }
  113. if (numlines != 0)
  114. shuffle_lines(lines, numlines);
  115. if (opts & OPT_o)
  116. xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
  117. if (opts & OPT_n) {
  118. unsigned maxlines;
  119. maxlines = xatou(opt_n_str);
  120. if (numlines > maxlines)
  121. numlines = maxlines;
  122. }
  123. eol = '\n';
  124. if (opts & OPT_z)
  125. eol = '\0';
  126. for (i = 0; i < numlines; i++) {
  127. if (opts & OPT_i)
  128. printf("%u%c", (unsigned)(uintptr_t)lines[i], eol);
  129. else
  130. printf("%s%c", lines[i], eol);
  131. }
  132. fflush_stdout_and_exit(EXIT_SUCCESS);
  133. }