uniq.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * uniq implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 compliant */
  23. /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <unistd.h>
  29. #include "busybox.h"
  30. #include "libcoreutils/coreutils.h"
  31. static const char uniq_opts[] = "f:s:cdu\0\7\3\5\1\2\4";
  32. int uniq_main(int argc, char **argv)
  33. {
  34. FILE *in, *out;
  35. /* Note: Ignore the warning about dups and e0 being used uninitialized.
  36. * They will be initialized on the fist pass of the loop (since s0 is NULL). */
  37. unsigned long dups, skip_fields, skip_chars, i;
  38. const char *s0, *e0, *s1, *e1, *input_filename;
  39. int opt;
  40. int uniq_flags = 6; /* -u */
  41. skip_fields = skip_chars = 0;
  42. while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
  43. if (opt == 'f') {
  44. skip_fields = bb_xgetularg10(optarg);
  45. } else if (opt == 's') {
  46. skip_chars = bb_xgetularg10(optarg);
  47. } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
  48. uniq_flags &= s0[4];
  49. uniq_flags |= s0[7];
  50. } else {
  51. bb_show_usage();
  52. }
  53. }
  54. input_filename = *(argv += optind);
  55. in = xgetoptfile_sort_uniq(argv, "r");
  56. if (*argv) {
  57. ++argv;
  58. }
  59. out = xgetoptfile_sort_uniq(argv, "w");
  60. if (*argv && argv[1]) {
  61. bb_show_usage();
  62. }
  63. s0 = NULL;
  64. /* gnu uniq ignores newlines */
  65. while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
  66. e1 = s1;
  67. for (i=skip_fields ; i ; i--) {
  68. e1 = bb_skip_whitespace(e1);
  69. while (*e1 && !isspace(*e1)) {
  70. ++e1;
  71. }
  72. }
  73. for (i = skip_chars ; *e1 && i ; i--) {
  74. ++e1;
  75. }
  76. if (s0) {
  77. if (strcmp(e0, e1) == 0) {
  78. ++dups; /* Note: Testing for overflow seems excessive. */
  79. continue;
  80. }
  81. DO_LAST:
  82. if ((dups && (uniq_flags & 2)) || (!dups && (uniq_flags & 4))) {
  83. bb_fprintf(out, "\0%7d\t" + (uniq_flags & 1), dups + 1);
  84. bb_fprintf(out, "%s\n", s0);
  85. }
  86. free((void *)s0);
  87. }
  88. s0 = s1;
  89. e0 = e1;
  90. dups = 0;
  91. }
  92. if (s0) {
  93. e1 = NULL;
  94. goto DO_LAST;
  95. }
  96. bb_xferror(in, input_filename);
  97. bb_fflush_stdout_and_exit(EXIT_SUCCESS);
  98. }