uniq.c 3.3 KB

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