blob: 8b938017501fd6d3519f35c0a9a270bbf45edb75 [file] [log] [blame]
Rob Landley2acfd7b2005-05-11 23:12:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini comm implementation for busybox
4 *
5 * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
6 *
Rob Landleye104d5f2006-04-04 19:26:37 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Rob Landley2acfd7b2005-05-11 23:12:49 +00008 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14#include "busybox.h"
15
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
22static int only_file_1;
23static int only_file_2;
24static int both;
25
26/* writeline outputs the input given, appropriately aligned according to class */
Mike Frysingerb3a6ec32005-05-12 22:41:13 +000027static void writeline(char *line, int class)
Mike Frysinger0d605e92005-05-12 22:25:41 +000028{
Mike Frysinger867a6082005-05-13 00:57:30 +000029 if (class == 0) {
30 if (!only_file_1)
Mike Frysinger0fb397e2005-05-13 00:58:18 +000031 return;
Mike Frysinger867a6082005-05-13 00:57:30 +000032 } else if (class == 1) {
Mike Frysingerdad4cf72005-05-12 22:50:12 +000033 if (!only_file_2)
34 return;
35 if (only_file_1)
36 putchar('\t');
37 }
Mike Frysinger867a6082005-05-13 00:57:30 +000038 else /*if (class == 2)*/ {
Mike Frysingerdad4cf72005-05-12 22:50:12 +000039 if (!both)
40 return;
41 if (only_file_1)
42 putchar('\t');
43 if (only_file_2)
44 putchar('\t');
Rob Landley2acfd7b2005-05-11 23:12:49 +000045 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000046 fputs(line, stdout);
Rob Landley2acfd7b2005-05-11 23:12:49 +000047}
48
49/* This is the real core of the program - lines are compared here */
Mike Frysinger867a6082005-05-13 00:57:30 +000050static void cmp_files(char **infiles)
Mike Frysinger0d605e92005-05-12 22:25:41 +000051{
Mike Frysinger867a6082005-05-13 00:57:30 +000052#define LINE_LEN 100
53#define BB_EOF_0 0x1
54#define BB_EOF_1 0x2
55 char thisline[2][LINE_LEN];
Rob Landley2acfd7b2005-05-11 23:12:49 +000056 FILE *streams[2];
Mike Frysinger867a6082005-05-13 00:57:30 +000057 int i;
Mike Frysinger0d605e92005-05-12 22:25:41 +000058
Mike Frysinger867a6082005-05-13 00:57:30 +000059 for (i = 0; i < 2; ++i) {
Mike Frysinger1ece21b2005-05-13 03:09:20 +000060 streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : bb_xfopen(infiles[i], "r"));
Mike Frysinger867a6082005-05-13 00:57:30 +000061 fgets(thisline[i], LINE_LEN, streams[i]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000062 }
63
Rob Landleye104d5f2006-04-04 19:26:37 +000064 while (*thisline[0] || *thisline[1]) {
Rob Landley2acfd7b2005-05-11 23:12:49 +000065 int order = 0;
Mike Frysinger867a6082005-05-13 00:57:30 +000066
67 i = 0;
68 if (feof(streams[0])) i |= BB_EOF_0;
69 if (feof(streams[1])) i |= BB_EOF_1;
Mike Frysinger0d605e92005-05-12 22:25:41 +000070
Rob Landleye104d5f2006-04-04 19:26:37 +000071 if (!*thisline[0])
Rob Landley2acfd7b2005-05-11 23:12:49 +000072 order = 1;
Rob Landleye104d5f2006-04-04 19:26:37 +000073 else if (!*thisline[1])
Rob Landley2acfd7b2005-05-11 23:12:49 +000074 order = -1;
75 else {
Mike Frysinger867a6082005-05-13 00:57:30 +000076 int tl0_len, tl1_len;
77 tl0_len = strlen(thisline[0]);
78 tl1_len = strlen(thisline[1]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000079 order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
80 if (!order)
81 order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
82 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000083
Mike Frysinger867a6082005-05-13 00:57:30 +000084 if (order == 0 && !i)
Rob Landley2acfd7b2005-05-11 23:12:49 +000085 writeline(thisline[1], 2);
Mike Frysinger867a6082005-05-13 00:57:30 +000086 else if (order > 0 && !(i & BB_EOF_1))
87 writeline(thisline[1], 1);
88 else if (order < 0 && !(i & BB_EOF_0))
89 writeline(thisline[0], 0);
Mike Frysinger0d605e92005-05-12 22:25:41 +000090
Mike Frysinger867a6082005-05-13 00:57:30 +000091 if (i & BB_EOF_0 & BB_EOF_1) {
Rob Landley2acfd7b2005-05-11 23:12:49 +000092 break;
Mike Frysinger0d605e92005-05-12 22:25:41 +000093
Mike Frysinger867a6082005-05-13 00:57:30 +000094 } else if (i) {
95 i = (i & BB_EOF_0 ? 1 : 0);
96 while (!feof(streams[i])) {
97 if ((order < 0 && i) || (order > 0 && !i))
98 writeline(thisline[i], i);
99 fgets(thisline[i], LINE_LEN, streams[i]);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000100 }
Rob Landley2acfd7b2005-05-11 23:12:49 +0000101 break;
Mike Frysinger0d605e92005-05-12 22:25:41 +0000102
103 } else {
Rob Landley2acfd7b2005-05-11 23:12:49 +0000104 if (order >= 0)
Mike Frysinger867a6082005-05-13 00:57:30 +0000105 fgets(thisline[1], LINE_LEN, streams[1]);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000106 if (order <= 0)
Mike Frysinger867a6082005-05-13 00:57:30 +0000107 fgets(thisline[0], LINE_LEN, streams[0]);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000108 }
109 }
110
Mike Frysinger867a6082005-05-13 00:57:30 +0000111 fclose(streams[0]);
112 fclose(streams[1]);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000113}
114
Mike Frysingerb3a6ec32005-05-12 22:41:13 +0000115int comm_main(int argc, char **argv)
Mike Frysinger0d605e92005-05-12 22:25:41 +0000116{
Mike Frysingerb3a6ec32005-05-12 22:41:13 +0000117 unsigned long flags;
Mike Frysinger0d605e92005-05-12 22:25:41 +0000118
Mike Frysingerb3a6ec32005-05-12 22:41:13 +0000119 flags = bb_getopt_ulflags(argc, argv, "123");
Mike Frysinger0d605e92005-05-12 22:25:41 +0000120
Mike Frysingerb3a6ec32005-05-12 22:41:13 +0000121 if (optind + 2 != argc)
Mike Frysinger0d605e92005-05-12 22:25:41 +0000122 bb_show_usage();
123
Mike Frysingerb3a6ec32005-05-12 22:41:13 +0000124 only_file_1 = !(flags & COMM_OPT_1);
125 only_file_2 = !(flags & COMM_OPT_2);
126 both = !(flags & COMM_OPT_3);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000127
Mike Frysinger867a6082005-05-13 00:57:30 +0000128 cmp_files(argv + optind);
129 exit(EXIT_SUCCESS);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000130}