uniq.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 const char uniq_opts[] ALIGN1 = "cdu" "f:s:" "cdu\0\1\2\4";
  13. static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
  14. {
  15. const char *n;
  16. n = *argv;
  17. if (n != NULL) {
  18. if ((*n != '-') || n[1]) {
  19. return xfopen(n, "r\0w" + read0write2);
  20. }
  21. }
  22. return (read0write2) ? stdout : stdin;
  23. }
  24. int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int uniq_main(int argc ATTRIBUTE_UNUSED, char **argv)
  26. {
  27. FILE *in, *out;
  28. unsigned long dups, skip_fields, skip_chars, i;
  29. const char *s0, *e0, *s1, *e1, *input_filename;
  30. unsigned opt;
  31. enum {
  32. OPT_c = 0x1,
  33. OPT_d = 0x2,
  34. OPT_u = 0x4,
  35. OPT_f = 0x8,
  36. OPT_s = 0x10,
  37. };
  38. skip_fields = skip_chars = 0;
  39. opt = getopt32(argv, "cduf:s:", &s0, &s1);
  40. if (opt & OPT_f)
  41. skip_fields = xatoul(s0);
  42. if (opt & OPT_s)
  43. skip_chars = xatoul(s1);
  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_getline(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 || strcmp(e0, e1)) {
  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%d " + (opt & 1), dups + 1);
  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. }