Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/uniq.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (hide annotations) (download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 2535 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 niro 532 /* 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    
10     /* BB_AUDIT SUSv3 compliant */
11     /* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
12    
13 niro 816 #include "libbb.h"
14 niro 532
15 niro 816 int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
16     int uniq_main(int argc UNUSED_PARAM, char **argv)
17 niro 532 {
18 niro 984 const char *input_filename;
19 niro 816 unsigned skip_fields, skip_chars, max_chars;
20     unsigned opt;
21 niro 984 char *cur_line;
22     const char *cur_compare;
23 niro 532
24 niro 816 enum {
25     OPT_c = 0x1,
26 niro 984 OPT_d = 0x2, /* print only dups */
27     OPT_u = 0x4, /* print only uniq */
28 niro 816 OPT_f = 0x8,
29     OPT_s = 0x10,
30     OPT_w = 0x20,
31     };
32 niro 532
33 niro 816 skip_fields = skip_chars = 0;
34 niro 984 max_chars = INT_MAX;
35 niro 532
36 niro 816 opt_complementary = "f+:s+:w+";
37     opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
38     argv += optind;
39 niro 532
40 niro 984 input_filename = argv[0];
41     if (input_filename) {
42     const char *output;
43 niro 816
44 niro 984 if (input_filename[0] != '-' || input_filename[1]) {
45     close(STDIN_FILENO); /* == 0 */
46     xopen(input_filename, O_RDONLY); /* fd will be 0 */
47     }
48     output = argv[1];
49     if (output) {
50     if (argv[2])
51     bb_show_usage();
52     if (output[0] != '-' || output[1]) {
53     // Won't work with "uniq - FILE" and closed stdin:
54     //close(STDOUT_FILENO);
55     //xopen3(output, O_WRONLY | O_CREAT | O_TRUNC, 0666);
56     xmove_fd(xopen3(output, O_WRONLY | O_CREAT | O_TRUNC, 0666), STDOUT_FILENO);
57     }
58     }
59 niro 532 }
60    
61 niro 984 cur_compare = cur_line = NULL; /* prime the pump */
62 niro 532
63     do {
64 niro 984 unsigned i;
65     unsigned long dups;
66     char *old_line;
67     const char *old_compare;
68    
69     old_line = cur_line;
70     old_compare = cur_compare;
71 niro 532 dups = 0;
72    
73     /* gnu uniq ignores newlines */
74 niro 984 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
75     cur_compare = cur_line;
76 niro 532 for (i = skip_fields; i; i--) {
77 niro 984 cur_compare = skip_whitespace(cur_compare);
78     cur_compare = skip_non_whitespace(cur_compare);
79 niro 532 }
80 niro 984 for (i = skip_chars; *cur_compare && i; i--) {
81     ++cur_compare;
82 niro 532 }
83    
84 niro 984 if (!old_line || strncmp(old_compare, cur_compare, max_chars)) {
85 niro 532 break;
86     }
87    
88 niro 984 free(cur_line);
89     ++dups; /* testing for overflow seems excessive */
90 niro 532 }
91    
92 niro 984 if (old_line) {
93     if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
94     if (opt & OPT_c) {
95     /* %7lu matches GNU coreutils 6.9 */
96     printf("%7lu ", dups + 1);
97     }
98     printf("%s\n", old_line);
99 niro 532 }
100 niro 984 free(old_line);
101 niro 532 }
102 niro 984 } while (cur_line);
103 niro 532
104 niro 984 die_if_ferror(stdin, input_filename);
105 niro 532
106     fflush_stdout_and_exit(EXIT_SUCCESS);
107     }