uniq.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
  11. #include "libbb.h"
  12. int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  13. int uniq_main(int argc UNUSED_PARAM, char **argv)
  14. {
  15. const char *input_filename;
  16. unsigned skip_fields, skip_chars, max_chars;
  17. unsigned opt;
  18. char *cur_line;
  19. const char *cur_compare;
  20. enum {
  21. OPT_c = 0x1,
  22. OPT_d = 0x2, /* print only dups */
  23. OPT_u = 0x4, /* print only uniq */
  24. OPT_f = 0x8,
  25. OPT_s = 0x10,
  26. OPT_w = 0x20,
  27. };
  28. skip_fields = skip_chars = 0;
  29. max_chars = INT_MAX;
  30. opt_complementary = "f+:s+:w+";
  31. opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
  32. argv += optind;
  33. input_filename = argv[0];
  34. if (input_filename) {
  35. const char *output;
  36. if (input_filename[0] != '-' || input_filename[1]) {
  37. close(STDIN_FILENO); /* == 0 */
  38. xopen(input_filename, O_RDONLY); /* fd will be 0 */
  39. }
  40. output = argv[1];
  41. if (output) {
  42. if (argv[2])
  43. bb_show_usage();
  44. if (output[0] != '-' || output[1]) {
  45. // Won't work with "uniq - FILE" and closed stdin:
  46. //close(STDOUT_FILENO);
  47. //xopen3(output, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  48. xmove_fd(xopen3(output, O_WRONLY | O_CREAT | O_TRUNC, 0666), STDOUT_FILENO);
  49. }
  50. }
  51. }
  52. cur_compare = cur_line = NULL; /* prime the pump */
  53. do {
  54. unsigned i;
  55. unsigned long dups;
  56. char *old_line;
  57. const char *old_compare;
  58. old_line = cur_line;
  59. old_compare = cur_compare;
  60. dups = 0;
  61. /* gnu uniq ignores newlines */
  62. while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
  63. cur_compare = cur_line;
  64. for (i = skip_fields; i; i--) {
  65. cur_compare = skip_whitespace(cur_compare);
  66. cur_compare = skip_non_whitespace(cur_compare);
  67. }
  68. for (i = skip_chars; *cur_compare && i; i--) {
  69. ++cur_compare;
  70. }
  71. if (!old_line || strncmp(old_compare, cur_compare, max_chars)) {
  72. break;
  73. }
  74. free(cur_line);
  75. ++dups; /* testing for overflow seems excessive */
  76. }
  77. if (old_line) {
  78. if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
  79. if (opt & OPT_c) {
  80. /* %7lu matches GNU coreutils 6.9 */
  81. printf("%7lu ", dups + 1);
  82. }
  83. printf("%s\n", old_line);
  84. }
  85. free(old_line);
  86. }
  87. } while (cur_line);
  88. die_if_ferror(stdin, input_filename);
  89. fflush_stdout_and_exit(EXIT_SUCCESS);
  90. }