3
0

uniq.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * uniq implementation for busybox
  4. *
  5. * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
  11. //usage:#define uniq_trivial_usage
  12. //usage: "[-cdu][-f,s,w N] [INPUT [OUTPUT]]"
  13. //usage:#define uniq_full_usage "\n\n"
  14. //usage: "Discard duplicate lines\n"
  15. //usage: "\n -c Prefix lines by the number of occurrences"
  16. //usage: "\n -d Only print duplicate lines"
  17. //usage: "\n -u Only print unique lines"
  18. //usage: "\n -f N Skip first N fields"
  19. //usage: "\n -s N Skip first N chars (after any skipped fields)"
  20. //usage: "\n -w N Compare N characters in line"
  21. //usage:
  22. //usage:#define uniq_example_usage
  23. //usage: "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n"
  24. //usage: "a\n"
  25. //usage: "b\n"
  26. //usage: "c\n"
  27. #include "libbb.h"
  28. int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  29. int uniq_main(int argc UNUSED_PARAM, char **argv)
  30. {
  31. const char *input_filename;
  32. unsigned skip_fields, skip_chars, max_chars;
  33. unsigned opt;
  34. char *cur_line;
  35. const char *cur_compare;
  36. enum {
  37. OPT_c = 0x1,
  38. OPT_d = 0x2, /* print only dups */
  39. OPT_u = 0x4, /* print only uniq */
  40. OPT_f = 0x8,
  41. OPT_s = 0x10,
  42. OPT_w = 0x20,
  43. };
  44. skip_fields = skip_chars = 0;
  45. max_chars = INT_MAX;
  46. opt_complementary = "f+:s+:w+";
  47. opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
  48. argv += optind;
  49. input_filename = argv[0];
  50. if (input_filename) {
  51. const char *output;
  52. if (input_filename[0] != '-' || input_filename[1]) {
  53. close(STDIN_FILENO); /* == 0 */
  54. xopen(input_filename, O_RDONLY); /* fd will be 0 */
  55. }
  56. output = argv[1];
  57. if (output) {
  58. if (argv[2])
  59. bb_show_usage();
  60. if (output[0] != '-' || output[1]) {
  61. // Won't work with "uniq - FILE" and closed stdin:
  62. //close(STDOUT_FILENO);
  63. //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
  64. xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
  65. }
  66. }
  67. }
  68. cur_compare = cur_line = NULL; /* prime the pump */
  69. do {
  70. unsigned i;
  71. unsigned long dups;
  72. char *old_line;
  73. const char *old_compare;
  74. old_line = cur_line;
  75. old_compare = cur_compare;
  76. dups = 0;
  77. /* gnu uniq ignores newlines */
  78. while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
  79. cur_compare = cur_line;
  80. for (i = skip_fields; i; i--) {
  81. cur_compare = skip_whitespace(cur_compare);
  82. cur_compare = skip_non_whitespace(cur_compare);
  83. }
  84. for (i = skip_chars; *cur_compare && i; i--) {
  85. ++cur_compare;
  86. }
  87. if (!old_line || strncmp(old_compare, cur_compare, max_chars)) {
  88. break;
  89. }
  90. free(cur_line);
  91. ++dups; /* testing for overflow seems excessive */
  92. }
  93. if (old_line) {
  94. if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
  95. if (opt & OPT_c) {
  96. /* %7lu matches GNU coreutils 6.9 */
  97. printf("%7lu ", dups + 1);
  98. }
  99. puts(old_line);
  100. }
  101. free(old_line);
  102. }
  103. } while (cur_line);
  104. die_if_ferror(stdin, input_filename);
  105. fflush_stdout_and_exit(EXIT_SUCCESS);
  106. }