uniq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. //config:config UNIQ
  10. //config: bool "uniq (4.9 kb)"
  11. //config: default y
  12. //config: help
  13. //config: uniq is used to remove duplicate lines from a sorted file.
  14. //applet:IF_UNIQ(APPLET(uniq, BB_DIR_USR_BIN, BB_SUID_DROP))
  15. //kbuild:lib-$(CONFIG_UNIQ) += uniq.o
  16. /* BB_AUDIT SUSv3 compliant */
  17. /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
  18. //usage:#define uniq_trivial_usage
  19. //usage: "[-cduiz] [-f,s,w N] [FILE [OUTFILE]]"
  20. //usage:#define uniq_full_usage "\n\n"
  21. //usage: "Discard duplicate lines\n"
  22. //usage: "\n -c Prefix lines by the number of occurrences"
  23. //usage: "\n -d Only print duplicate lines"
  24. //usage: "\n -u Only print unique lines"
  25. //usage: "\n -i Ignore case"
  26. //usage: "\n -z NUL terminated output"
  27. //usage: "\n -f N Skip first N fields"
  28. //usage: "\n -s N Skip first N chars (after any skipped fields)"
  29. //usage: "\n -w N Compare N characters in line"
  30. //usage:
  31. //usage:#define uniq_example_usage
  32. //usage: "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n"
  33. //usage: "a\n"
  34. //usage: "b\n"
  35. //usage: "c\n"
  36. #include "libbb.h"
  37. int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int uniq_main(int argc UNUSED_PARAM, char **argv)
  39. {
  40. const char *input_filename;
  41. unsigned skip_fields, skip_chars, max_chars;
  42. unsigned opt;
  43. char eol;
  44. char *cur_line;
  45. const char *cur_compare;
  46. enum {
  47. OPT_c = 1 << 0,
  48. OPT_d = 1 << 1, /* print only dups */
  49. OPT_u = 1 << 2, /* print only uniq */
  50. OPT_f = 1 << 3,
  51. OPT_s = 1 << 4,
  52. OPT_w = 1 << 5,
  53. OPT_i = 1 << 6,
  54. OPT_z = 1 << 7,
  55. };
  56. skip_fields = skip_chars = 0;
  57. max_chars = INT_MAX;
  58. opt = getopt32(argv, "cduf:+s:+w:+iz", &skip_fields, &skip_chars, &max_chars);
  59. argv += optind;
  60. input_filename = argv[0];
  61. if (input_filename) {
  62. const char *output;
  63. if (input_filename[0] != '-' || input_filename[1]) {
  64. close(STDIN_FILENO); /* == 0 */
  65. xopen(input_filename, O_RDONLY); /* fd will be 0 */
  66. }
  67. output = argv[1];
  68. if (output) {
  69. if (argv[2])
  70. bb_show_usage();
  71. if (output[0] != '-' || output[1]) {
  72. // Won't work with "uniq - FILE" and closed stdin:
  73. //close(STDOUT_FILENO);
  74. //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
  75. xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
  76. }
  77. }
  78. }
  79. cur_compare = cur_line = NULL; /* prime the pump */
  80. eol = (opt & OPT_z) ? 0 : '\n';
  81. do {
  82. unsigned i;
  83. unsigned long dups;
  84. char *old_line;
  85. const char *old_compare;
  86. old_line = cur_line;
  87. old_compare = cur_compare;
  88. dups = 0;
  89. /* gnu uniq ignores newlines */
  90. while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
  91. cur_compare = cur_line;
  92. for (i = skip_fields; i; i--) {
  93. cur_compare = skip_whitespace(cur_compare);
  94. cur_compare = skip_non_whitespace(cur_compare);
  95. }
  96. for (i = skip_chars; *cur_compare && i; i--) {
  97. ++cur_compare;
  98. }
  99. if (!old_line)
  100. break;
  101. if ((opt & OPT_i)
  102. ? strncasecmp(old_compare, cur_compare, max_chars)
  103. : strncmp(old_compare, cur_compare, max_chars)
  104. ) {
  105. break;
  106. }
  107. free(cur_line);
  108. ++dups; /* testing for overflow seems excessive */
  109. }
  110. if (old_line) {
  111. if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
  112. if (opt & OPT_c) {
  113. /* %7lu matches GNU coreutils 6.9 */
  114. printf("%7lu ", dups + 1);
  115. }
  116. printf("%s%c", old_line, eol);
  117. }
  118. free(old_line);
  119. }
  120. } while (cur_line);
  121. die_if_ferror(stdin, input_filename);
  122. fflush_stdout_and_exit(EXIT_SUCCESS);
  123. }