3
0

uniq.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
  13. {
  14. const char *n;
  15. n = *argv;
  16. if (n != NULL) {
  17. if ((*n != '-') || n[1]) {
  18. return xfopen(n, "r\0w" + read0write2);
  19. }
  20. }
  21. return (read0write2) ? stdout : stdin;
  22. }
  23. int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  24. int uniq_main(int argc UNUSED_PARAM, char **argv)
  25. {
  26. FILE *in, *out;
  27. const char *s0, *e0, *s1, *e1, *input_filename;
  28. unsigned long dups;
  29. unsigned skip_fields, skip_chars, max_chars;
  30. unsigned opt;
  31. unsigned i;
  32. enum {
  33. OPT_c = 0x1,
  34. OPT_d = 0x2,
  35. OPT_u = 0x4,
  36. OPT_f = 0x8,
  37. OPT_s = 0x10,
  38. OPT_w = 0x20,
  39. };
  40. skip_fields = skip_chars = 0;
  41. max_chars = -1;
  42. opt_complementary = "f+:s+:w+";
  43. opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
  44. argv += optind;
  45. input_filename = *argv;
  46. in = xgetoptfile_uniq_s(argv, 0);
  47. if (*argv) {
  48. ++argv;
  49. }
  50. out = xgetoptfile_uniq_s(argv, 2);
  51. if (*argv && argv[1]) {
  52. bb_show_usage();
  53. }
  54. s1 = e1 = NULL; /* prime the pump */
  55. do {
  56. s0 = s1;
  57. e0 = e1;
  58. dups = 0;
  59. /* gnu uniq ignores newlines */
  60. while ((s1 = xmalloc_fgetline(in)) != NULL) {
  61. e1 = s1;
  62. for (i = skip_fields; i; i--) {
  63. e1 = skip_whitespace(e1);
  64. e1 = skip_non_whitespace(e1);
  65. }
  66. for (i = skip_chars; *e1 && i; i--) {
  67. ++e1;
  68. }
  69. if (!s0 || strncmp(e0, e1, max_chars)) {
  70. break;
  71. }
  72. ++dups; /* note: testing for overflow seems excessive. */
  73. }
  74. if (s0) {
  75. if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */
  76. fprintf(out, "\0%ld " + (opt & 1), dups + 1); /* 1 == OPT_c */
  77. fprintf(out, "%s\n", s0);
  78. }
  79. free((void *)s0);
  80. }
  81. } while (s1);
  82. die_if_ferror(in, input_filename);
  83. fflush_stdout_and_exit(EXIT_SUCCESS);
  84. }