GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/comm/comm.c Lines: 0 53 0.0 %
Date: 2017-11-07 Branches: 0 61 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: comm.c,v 1.10 2015/10/09 01:37:07 deraadt Exp $	*/
2
/*	$NetBSD: comm.c,v 1.10 1995/09/05 19:57:43 jtc Exp $	*/
3
4
/*
5
 * Copyright (c) 1989, 1993, 1994
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * This code is derived from software contributed to Berkeley by
9
 * Case Larsen.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 */
35
36
#include <err.h>
37
#include <limits.h>
38
#include <locale.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <unistd.h>
43
44
#define	MAXLINELEN	(LINE_MAX + 1)
45
46
char *tabs[] = { "", "\t", "\t\t" };
47
48
FILE   *file(const char *);
49
void	show(FILE *, char *, char *);
50
void	usage(void);
51
52
int
53
main(int argc, char *argv[])
54
{
55
	int comp, file1done, file2done, read1, read2;
56
	int ch, flag1, flag2, flag3;
57
	FILE *fp1, *fp2;
58
	char *col1, *col2, *col3;
59
	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
60
	int (*compare)(const char * ,const char *);
61
62
	setlocale(LC_ALL, "");
63
64
	if (pledge("stdio rpath flock cpath wpath", NULL) == -1)
65
		err(1, "pledge");
66
67
	flag1 = flag2 = flag3 = 1;
68
	compare = strcoll;
69
	while ((ch = getopt(argc, argv, "123f")) != -1)
70
		switch(ch) {
71
		case '1':
72
			flag1 = 0;
73
			break;
74
		case '2':
75
			flag2 = 0;
76
			break;
77
		case '3':
78
			flag3 = 0;
79
			break;
80
		case 'f':
81
			compare = strcasecmp;
82
			break;
83
		case '?':
84
		default:
85
			usage();
86
		}
87
	argc -= optind;
88
	argv += optind;
89
90
	if (argc != 2)
91
		usage();
92
93
	fp1 = file(argv[0]);
94
	fp2 = file(argv[1]);
95
96
	/* for each column printed, add another tab offset */
97
	p = tabs;
98
	col1 = col2 = col3 = NULL;
99
	if (flag1)
100
		col1 = *p++;
101
	if (flag2)
102
		col2 = *p++;
103
	if (flag3)
104
		col3 = *p;
105
106
	for (read1 = read2 = 1;;) {
107
		/* read next line, check for EOF */
108
		if (read1)
109
			file1done = !fgets(line1, MAXLINELEN, fp1);
110
		if (read2)
111
			file2done = !fgets(line2, MAXLINELEN, fp2);
112
113
		/* if one file done, display the rest of the other file */
114
		if (file1done) {
115
			if (!file2done && col2)
116
				show(fp2, col2, line2);
117
			break;
118
		}
119
		if (file2done) {
120
			if (!file1done && col1)
121
				show(fp1, col1, line1);
122
			break;
123
		}
124
125
		/* lines are the same */
126
		if (!(comp = compare(line1, line2))) {
127
			read1 = read2 = 1;
128
			if (col3)
129
				if (printf("%s%s", col3, line1) < 0)
130
					break;
131
			continue;
132
		}
133
134
		/* lines are different */
135
		if (comp < 0) {
136
			read1 = 1;
137
			read2 = 0;
138
			if (col1)
139
				if (printf("%s%s", col1, line1) < 0)
140
					break;
141
		} else {
142
			read1 = 0;
143
			read2 = 1;
144
			if (col2)
145
				if (printf("%s%s", col2, line2) < 0)
146
					break;
147
		}
148
	}
149
150
	if (ferror (stdout) || fclose (stdout) == EOF)
151
		err(1, "stdout");
152
153
	exit(0);
154
}
155
156
void
157
show(FILE *fp, char *offset, char *buf)
158
{
159
	while (printf("%s%s", offset, buf) >= 0 && fgets(buf, MAXLINELEN, fp))
160
		;
161
}
162
163
FILE *
164
file(const char *name)
165
{
166
	FILE *fp;
167
168
	if (!strcmp(name, "-"))
169
		return (stdin);
170
	if ((fp = fopen(name, "r")) == NULL)
171
		err(1, "%s", name);
172
	return (fp);
173
}
174
175
void
176
usage(void)
177
{
178
	(void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
179
	exit(1);
180
}