Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/coreutils/comm.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/coreutils/comm.c
r821 r1770 8 8 */ 9 9 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <unistd.h> 14 #include "busybox.h" 10 #include "libbb.h" 15 11 16 #define COMM_OPT_1 0x01 17 #define COMM_OPT_2 0x02 18 #define COMM_OPT_3 0x04 19 20 /* These three variables control behaviour if non-zero */ 21 22 static int only_file_1; 23 static int only_file_2; 24 static int both; 12 #define COMM_OPT_1 (1 << 0) 13 #define COMM_OPT_2 (1 << 1) 14 #define COMM_OPT_3 (1 << 2) 25 15 26 16 /* writeline outputs the input given, appropriately aligned according to class */ 27 static void writeline(char *line, int class )17 static void writeline(char *line, int class, int flags) 28 18 { 29 19 if (class == 0) { 30 if ( !only_file_1)20 if (flags & COMM_OPT_1) 31 21 return; 32 22 } else if (class == 1) { 33 if ( !only_file_2)23 if (flags & COMM_OPT_2) 34 24 return; 35 if ( only_file_1)25 if (!(flags & COMM_OPT_1)) 36 26 putchar('\t'); 37 } 38 else /*if (class == 2)*/ { 39 if (!both) 27 } else /*if (class == 2)*/ { 28 if (flags & COMM_OPT_3) 40 29 return; 41 if ( only_file_1)30 if (!(flags & COMM_OPT_1)) 42 31 putchar('\t'); 43 if ( only_file_2)32 if (!(flags & COMM_OPT_2)) 44 33 putchar('\t'); 45 34 } … … 47 36 } 48 37 49 /* This is the real core of the program - lines are compared here */ 50 static void cmp_files(char **infiles)38 int comm_main(int argc, char **argv); 39 int comm_main(int argc, char **argv) 51 40 { 52 41 #define LINE_LEN 100 … … 56 45 FILE *streams[2]; 57 46 int i; 47 unsigned flags; 48 49 opt_complementary = "=2"; 50 flags = getopt32(argv, "123"); 51 argv += optind; 58 52 59 53 for (i = 0; i < 2; ++i) { 60 streams[i] = ( (infiles[i][0] == '=' && infiles[i][1]) ? stdin : bb_xfopen(infiles[i], "r"));54 streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r"); 61 55 fgets(thisline[i], LINE_LEN, streams[i]); 62 56 } 57 58 /* This is the real core of the program - lines are compared here */ 63 59 64 60 while (*thisline[0] || *thisline[1]) { … … 83 79 84 80 if (order == 0 && !i) 85 writeline(thisline[1], 2 );81 writeline(thisline[1], 2, flags); 86 82 else if (order > 0 && !(i & BB_EOF_1)) 87 writeline(thisline[1], 1 );83 writeline(thisline[1], 1, flags); 88 84 else if (order < 0 && !(i & BB_EOF_0)) 89 writeline(thisline[0], 0 );85 writeline(thisline[0], 0, flags); 90 86 91 87 if (i & BB_EOF_0 & BB_EOF_1) { … … 96 92 while (!feof(streams[i])) { 97 93 if ((order < 0 && i) || (order > 0 && !i)) 98 writeline(thisline[i], i );94 writeline(thisline[i], i, flags); 99 95 fgets(thisline[i], LINE_LEN, streams[i]); 100 96 } … … 109 105 } 110 106 111 fclose(streams[0]); 112 fclose(streams[1]); 107 if (ENABLE_FEATURE_CLEAN_UP) { 108 fclose(streams[0]); 109 fclose(streams[1]); 110 } 111 112 return EXIT_SUCCESS; 113 113 } 114 115 int comm_main(int argc, char **argv)116 {117 unsigned long flags;118 119 flags = bb_getopt_ulflags(argc, argv, "123");120 121 if (optind + 2 != argc)122 bb_show_usage();123 124 only_file_1 = !(flags & COMM_OPT_1);125 only_file_2 = !(flags & COMM_OPT_2);126 both = !(flags & COMM_OPT_3);127 128 cmp_files(argv + optind);129 exit(EXIT_SUCCESS);130 }
Note:
See TracChangeset
for help on using the changeset viewer.