comm: eliminate statics

This commit is contained in:
Denis Vlasenko 2007-04-07 00:45:27 +00:00
parent 04c99ebb4f
commit bb5b01c7c7

View File

@ -9,41 +9,34 @@
#include "busybox.h" #include "busybox.h"
#define COMM_OPT_1 0x01 #define COMM_OPT_1 (1 << 0)
#define COMM_OPT_2 0x02 #define COMM_OPT_2 (1 << 1)
#define COMM_OPT_3 0x04 #define COMM_OPT_3 (1 << 2)
/* These three variables control behaviour if non-zero */
static int only_file_1;
static int only_file_2;
static int both;
/* writeline outputs the input given, appropriately aligned according to class */ /* writeline outputs the input given, appropriately aligned according to class */
static void writeline(char *line, int class) static void writeline(char *line, int class, int flags)
{ {
if (class == 0) { if (class == 0) {
if (!only_file_1) if (flags & COMM_OPT_1)
return; return;
} else if (class == 1) { } else if (class == 1) {
if (!only_file_2) if (flags & COMM_OPT_2)
return; return;
if (only_file_1) if (!(flags & COMM_OPT_1))
putchar('\t'); putchar('\t');
} } else /*if (class == 2)*/ {
else /*if (class == 2)*/ { if (flags & COMM_OPT_3)
if (!both)
return; return;
if (only_file_1) if (!(flags & COMM_OPT_1))
putchar('\t'); putchar('\t');
if (only_file_2) if (!(flags & COMM_OPT_2))
putchar('\t'); putchar('\t');
} }
fputs(line, stdout); fputs(line, stdout);
} }
/* This is the real core of the program - lines are compared here */ int comm_main(int argc, char **argv);
static void cmp_files(char **infiles) int comm_main(int argc, char **argv)
{ {
#define LINE_LEN 100 #define LINE_LEN 100
#define BB_EOF_0 0x1 #define BB_EOF_0 0x1
@ -51,12 +44,19 @@ static void cmp_files(char **infiles)
char thisline[2][LINE_LEN]; char thisline[2][LINE_LEN];
FILE *streams[2]; FILE *streams[2];
int i; int i;
unsigned flags;
opt_complementary = "=2";
flags = getopt32(argc, argv, "123");
argv += optind;
for (i = 0; i < 2; ++i) { for (i = 0; i < 2; ++i) {
streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : xfopen(infiles[i], "r")); streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r");
fgets(thisline[i], LINE_LEN, streams[i]); fgets(thisline[i], LINE_LEN, streams[i]);
} }
/* This is the real core of the program - lines are compared here */
while (*thisline[0] || *thisline[1]) { while (*thisline[0] || *thisline[1]) {
int order = 0; int order = 0;
@ -78,11 +78,11 @@ static void cmp_files(char **infiles)
} }
if (order == 0 && !i) if (order == 0 && !i)
writeline(thisline[1], 2); writeline(thisline[1], 2, flags);
else if (order > 0 && !(i & BB_EOF_1)) else if (order > 0 && !(i & BB_EOF_1))
writeline(thisline[1], 1); writeline(thisline[1], 1, flags);
else if (order < 0 && !(i & BB_EOF_0)) else if (order < 0 && !(i & BB_EOF_0))
writeline(thisline[0], 0); writeline(thisline[0], 0, flags);
if (i & BB_EOF_0 & BB_EOF_1) { if (i & BB_EOF_0 & BB_EOF_1) {
break; break;
@ -91,7 +91,7 @@ static void cmp_files(char **infiles)
i = (i & BB_EOF_0 ? 1 : 0); i = (i & BB_EOF_0 ? 1 : 0);
while (!feof(streams[i])) { while (!feof(streams[i])) {
if ((order < 0 && i) || (order > 0 && !i)) if ((order < 0 && i) || (order > 0 && !i))
writeline(thisline[i], i); writeline(thisline[i], i, flags);
fgets(thisline[i], LINE_LEN, streams[i]); fgets(thisline[i], LINE_LEN, streams[i]);
} }
break; break;
@ -104,24 +104,10 @@ static void cmp_files(char **infiles)
} }
} }
fclose(streams[0]); if (ENABLE_FEATURE_CLEAN_UP) {
fclose(streams[1]); fclose(streams[0]);
} fclose(streams[1]);
}
int comm_main(int argc, char **argv);
int comm_main(int argc, char **argv) return EXIT_SUCCESS;
{
unsigned long flags;
flags = getopt32(argc, argv, "123");
if (optind + 2 != argc)
bb_show_usage();
only_file_1 = !(flags & COMM_OPT_1);
only_file_2 = !(flags & COMM_OPT_2);
both = !(flags & COMM_OPT_3);
cmp_files(argv + optind);
exit(EXIT_SUCCESS);
} }