Mercurial > hg > toybox
comparison toys/comm.c @ 614:2b40588a3d25
Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 26 Jun 2012 20:47:01 -0500 |
parents | feb909b2e6aa |
children |
comparison
equal
deleted
inserted
replaced
613:4601e408223b | 614:2b40588a3d25 |
---|---|
4 * | 4 * |
5 * Copyright 2012 Ilya Kuzmich <ikv@safe-mail.net> | 5 * Copyright 2012 Ilya Kuzmich <ikv@safe-mail.net> |
6 * | 6 * |
7 * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/comm.html | 7 * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/comm.html |
8 | 8 |
9 USE_COMM(NEWTOY(comm, "123", TOYFLAG_USR|TOYFLAG_BIN)) | 9 // <# and ># take single digit, so 321 define flags |
10 USE_COMM(NEWTOY(comm, "<2>2321", TOYFLAG_USR|TOYFLAG_BIN)) | |
10 | 11 |
11 config COMM | 12 config COMM |
12 bool "comm" | 13 bool "comm" |
13 default n | 14 default y |
14 help | 15 help |
15 usage: comm [-123] FILE1 FILE2 | 16 usage: comm [-123] FILE1 FILE2 |
16 | 17 |
17 Reads FILE1 and FILE2, which should be ordered, and produces three text | 18 Reads FILE1 and FILE2, which should be ordered, and produces three text |
18 columns as output: lines only in FILE1; lines only in FILE2; and lines | 19 columns as output: lines only in FILE1; lines only in FILE2; and lines |
23 -3 suppress the output column of lines duplicated in FILE1 and FILE2 | 24 -3 suppress the output column of lines duplicated in FILE1 and FILE2 |
24 */ | 25 */ |
25 | 26 |
26 #include "toys.h" | 27 #include "toys.h" |
27 | 28 |
28 #define FLAG_SUPPRESS_3 1 | 29 #define FLAG_1 1 |
29 #define FLAG_SUPPRESS_2 2 | 30 #define FLAG_2 2 |
30 #define FLAG_SUPPRESS_1 4 | 31 #define FLAG_3 4 |
31 | 32 |
32 static void writeline(const char *line, int col) | 33 static void writeline(const char *line, int col) |
33 { | 34 { |
34 if (col == 0 && toys.optflags & FLAG_SUPPRESS_1) return; | 35 if (col == 0 && toys.optflags & FLAG_1) return; |
35 else if (col == 1) { | 36 else if (col == 1) { |
36 if (toys.optflags & FLAG_SUPPRESS_2) return; | 37 if (toys.optflags & FLAG_2) return; |
37 if (!(toys.optflags & FLAG_SUPPRESS_1)) putchar('\t'); | 38 if (!(toys.optflags & FLAG_1)) putchar('\t'); |
38 } else if (col == 2) { | 39 } else if (col == 2) { |
39 if (toys.optflags & FLAG_SUPPRESS_3) return; | 40 if (toys.optflags & FLAG_3) return; |
40 if (!(toys.optflags & FLAG_SUPPRESS_1)) putchar('\t'); | 41 if (!(toys.optflags & FLAG_1)) putchar('\t'); |
41 if (!(toys.optflags & FLAG_SUPPRESS_2)) putchar('\t'); | 42 if (!(toys.optflags & FLAG_2)) putchar('\t'); |
42 } | 43 } |
43 puts(line); | 44 puts(line); |
44 } | 45 } |
45 | 46 |
46 void comm_main(void) | 47 void comm_main(void) |
47 { | 48 { |
48 int file[2]; | 49 int file[2]; |
49 char *line[2]; | 50 char *line[2]; |
50 int i; | 51 int i; |
51 | 52 |
52 if (toys.optc != 2) | 53 if (toys.optflags == 7) return; |
53 perror_exit("exactly 2 operands required"); | |
54 | |
55 if (toys.optflags == (FLAG_SUPPRESS_1 | FLAG_SUPPRESS_2 | FLAG_SUPPRESS_3)) | |
56 return; | |
57 | 54 |
58 for (i = 0; i < 2; i++) { | 55 for (i = 0; i < 2; i++) { |
59 file[i] = strcmp("-", toys.optargs[i]) ? xopen(toys.optargs[i], O_RDONLY) : 0; | 56 file[i] = strcmp("-", toys.optargs[i]) ? xopen(toys.optargs[i], O_RDONLY) : 0; |
60 line[i] = get_line(file[i]); | 57 line[i] = get_line(file[i]); |
61 } | 58 } |
82 writeline(line[i], i); | 79 writeline(line[i], i); |
83 free(line[i]); | 80 free(line[i]); |
84 line[i] = get_line(file[i]); | 81 line[i] = get_line(file[i]); |
85 } | 82 } |
86 | 83 |
87 if (CFG_TOYBOX_FREE) { | 84 if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i--) xclose(file[i]); |
88 for (i = 0; i < 2; i--) | |
89 xclose(file[i]); | |
90 } | |
91 } | 85 } |