Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/coreutils/uniq.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/coreutils/uniq.c
r821 r1770 11 11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */ 12 12 13 #include "busybox.h" 14 #include <string.h> 15 #include <ctype.h> 16 #include <unistd.h> 13 #include "libbb.h" 17 14 18 static const char uniq_opts[] ="f:s:" "cdu\0\1\2\4";15 static const char uniq_opts[] ALIGN1 = "cdu" "f:s:" "cdu\0\1\2\4"; 19 16 20 17 static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) … … 22 19 const char *n; 23 20 24 if ((n = *argv) != NULL) { 21 n = *argv; 22 if (n != NULL) { 25 23 if ((*n != '-') || n[1]) { 26 return bb_xfopen(n, "r\0w" + read0write2);24 return xfopen(n, "r\0w" + read0write2); 27 25 } 28 26 } … … 30 28 } 31 29 30 int uniq_main(int argc, char **argv); 32 31 int uniq_main(int argc, char **argv) 33 32 { 34 33 FILE *in, *out; 35 unsigned long dups, skip_fields, skip_chars, i , uniq_flags;34 unsigned long dups, skip_fields, skip_chars, i; 36 35 const char *s0, *e0, *s1, *e1, *input_filename; 37 intopt;36 unsigned opt; 38 37 39 uniq_flags = skip_fields = skip_chars = 0; 38 enum { 39 OPT_c = 0x1, 40 OPT_d = 0x2, 41 OPT_u = 0x4, 42 OPT_f = 0x8, 43 OPT_s = 0x10, 44 }; 40 45 41 while ((opt = getopt(argc, argv, uniq_opts)) > 0) { 42 if ((opt == 'f') || (opt == 's')) { 43 int t = bb_xgetularg10(optarg); 44 if (opt == 'f') { 45 skip_fields = t; 46 } else { 47 skip_chars = t; 48 } 49 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) { 50 uniq_flags |= s0[4]; 51 } else { 52 bb_show_usage(); 53 } 54 } 46 skip_fields = skip_chars = 0; 55 47 56 input_filename = *(argv += optind); 48 opt = getopt32(argv, "cduf:s:", &s0, &s1); 49 if (opt & OPT_f) 50 skip_fields = xatoul(s0); 51 if (opt & OPT_s) 52 skip_chars = xatoul(s1); 53 argv += optind; 54 55 input_filename = *argv; 57 56 58 57 in = xgetoptfile_uniq_s(argv, 0); … … 73 72 74 73 /* gnu uniq ignores newlines */ 75 while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {74 while ((s1 = xmalloc_getline(in)) != NULL) { 76 75 e1 = s1; 77 for (i =skip_fields ; i; i--) {76 for (i = skip_fields; i; i--) { 78 77 e1 = skip_whitespace(e1); 79 while (*e1 && !isspace(*e1)) { 80 ++e1; 81 } 78 e1 = skip_non_whitespace(e1); 82 79 } 83 for (i = skip_chars ; *e1 && i; i--) {80 for (i = skip_chars; *e1 && i; i--) { 84 81 ++e1; 85 82 } … … 93 90 94 91 if (s0) { 95 if (!( uniq_flags & (2 << !!dups))) {96 bb_fprintf(out, "\0%d " + (uniq_flags& 1), dups + 1);97 bb_fprintf(out, "%s\n", s0);92 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */ 93 fprintf(out, "\0%d " + (opt & 1), dups + 1); 94 fprintf(out, "%s\n", s0); 98 95 } 99 96 free((void *)s0); … … 101 98 } while (s1); 102 99 103 bb_xferror(in, input_filename);100 die_if_ferror(in, input_filename); 104 101 105 bb_fflush_stdout_and_exit(EXIT_SUCCESS);102 fflush_stdout_and_exit(EXIT_SUCCESS); 106 103 }
Note:
See TracChangeset
for help on using the changeset viewer.