3
0

uniq.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: "[-cdu][-f,s,w N] [INPUT [OUTPUT]]"
  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 -f N Skip first N fields"
  27. //usage: "\n -s N Skip first N chars (after any skipped fields)"
  28. //usage: "\n -w N Compare N characters in line"
  29. //usage:
  30. //usage:#define uniq_example_usage
  31. //usage: "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n"
  32. //usage: "a\n"
  33. //usage: "b\n"
  34. //usage: "c\n"
  35. #include "libbb.h"
  36. int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  37. int uniq_main(int argc UNUSED_PARAM, char **argv)
  38. {
  39. const char *input_filename;
  40. unsigned skip_fields, skip_chars, max_chars;
  41. unsigned opt;
  42. char *cur_line;
  43. const char *cur_compare;
  44. enum {
  45. OPT_c = 0x1,
  46. OPT_d = 0x2, /* print only dups */
  47. OPT_u = 0x4, /* print only uniq */
  48. OPT_f = 0x8,
  49. OPT_s = 0x10,
  50. OPT_w = 0x20,
  51. OPT_i = 0x40,
  52. };
  53. skip_fields = skip_chars = 0;
  54. max_chars = INT_MAX;
  55. opt = getopt32(argv, "cduf:+s:+w:+i", &skip_fields, &skip_chars, &max_chars);
  56. argv += optind;
  57. input_filename = argv[0];
  58. if (input_filename) {
  59. const char *output;
  60. if (input_filename[0] != '-' || input_filename[1]) {
  61. close(STDIN_FILENO); /* == 0 */
  62. xopen(input_filename, O_RDONLY); /* fd will be 0 */
  63. }
  64. output = argv[1];
  65. if (output) {
  66. if (argv[2])
  67. bb_show_usage();
  68. if (output[0] != '-' || output[1]) {
  69. // Won't work with "uniq - FILE" and closed stdin:
  70. //close(STDOUT_FILENO);
  71. //xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
  72. xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
  73. }
  74. }
  75. }
  76. cur_compare = cur_line = NULL; /* prime the pump */
  77. do {
  78. unsigned i;
  79. unsigned long dups;
  80. char *old_line;
  81. const char *old_compare;
  82. old_line = cur_line;
  83. old_compare = cur_compare;
  84. dups = 0;
  85. /* gnu uniq ignores newlines */
  86. while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
  87. cur_compare = cur_line;
  88. for (i = skip_fields; i; i--) {
  89. cur_compare = skip_whitespace(cur_compare);
  90. cur_compare = skip_non_whitespace(cur_compare);
  91. }
  92. for (i = skip_chars; *cur_compare && i; i--) {
  93. ++cur_compare;
  94. }
  95. if (!old_line)
  96. break;
  97. if ((opt & OPT_i)
  98. ? strncasecmp(old_compare, cur_compare, max_chars)
  99. : strncmp(old_compare, cur_compare, max_chars)
  100. ) {
  101. break;
  102. }
  103. free(cur_line);
  104. ++dups; /* testing for overflow seems excessive */
  105. }
  106. if (old_line) {
  107. if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
  108. if (opt & OPT_c) {
  109. /* %7lu matches GNU coreutils 6.9 */
  110. printf("%7lu ", dups + 1);
  111. }
  112. puts(old_line);
  113. }
  114. free(old_line);
  115. }
  116. } while (cur_line);
  117. die_if_ferror(stdin, input_filename);
  118. fflush_stdout_and_exit(EXIT_SUCCESS);
  119. }