Annotation of src/usr.bin/rcs/rcsdiff.c, Revision 1.12
1.12 ! niallo 1: /* $OpenBSD: rcsdiff.c,v 1.11 2005/10/13 12:35:30 joris Exp $ */
1.1 joris 2: /*
3: * Copyright (c) 2005 Joris Vink <joris@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <stdio.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34:
35: #include "log.h"
36: #include "rcs.h"
1.2 niallo 37: #include "diff.h"
1.1 joris 38: #include "rcsprog.h"
39:
40: static int rcsdiff_file(RCSFILE *, RCSNUM *, const char *);
1.4 niallo 41: static int rcsdiff_rev(RCSFILE *rfp, RCSNUM *rev, RCSNUM *rev2);
1.1 joris 42:
43: int
44: rcsdiff_main(int argc, char **argv)
45: {
1.7 niallo 46: int i, ch, status;
1.4 niallo 47: RCSNUM *rev, *rev2, *frev;
1.1 joris 48: RCSFILE *file;
49: char fpath[MAXPATHLEN];
50:
51: rev = RCS_HEAD_REV;
1.4 niallo 52: rev2 = NULL;
1.7 niallo 53: status = 0;
1.1 joris 54:
1.11 joris 55: while ((ch = rcs_getopt(argc, argv, "cnqr:uV")) != -1) {
1.1 joris 56: switch (ch) {
1.3 joris 57: case 'c':
58: diff_format = D_CONTEXT;
59: break;
60: case 'n':
61: diff_format = D_RCSDIFF;
62: break;
1.1 joris 63: case 'q':
64: verbose = 0;
1.3 joris 65: break;
66: case 'u':
67: diff_format = D_UNIFIED;
1.1 joris 68: break;
69: case 'V':
70: printf("%s\n", rcs_version);
71: exit(0);
1.4 niallo 72: case 'r':
73: if (rev == RCS_HEAD_REV) {
1.11 joris 74: if ((rev = rcsnum_parse(rcs_optarg)) == NULL) {
1.4 niallo 75: cvs_log(LP_ERR, "bad revision number");
76: exit(1);
77: }
78: } else {
1.11 joris 79: if ((rev2 = rcsnum_parse(rcs_optarg)) == NULL) {
1.4 niallo 80: cvs_log(LP_ERR, "bad revision number");
81: exit(1);
82: }
83: }
84: break;
1.1 joris 85: default:
1.11 joris 86: (usage)();
87: exit (1);
1.1 joris 88: }
89: }
90:
1.11 joris 91: argc -= rcs_optind;
92: argv += rcs_optind;
1.1 joris 93:
94: if (argc == 0) {
95: cvs_log(LP_ERR, "no input file");
96: (usage)();
97: exit(1);
98: }
99:
100: for (i = 0; i < argc; i++) {
1.10 xsa 101: if (verbose == 1)
1.1 joris 102: cvs_printf("%s\n", RCS_DIFF_DIV);
103:
104: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
105: continue;
106:
107: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
108: continue;
109:
110: if (rev == RCS_HEAD_REV)
111: frev = file->rf_head;
112: else
113: frev = rev;
1.8 joris 114:
115: diff_file = argv[i];
1.1 joris 116:
1.4 niallo 117: if (rev2 == NULL) {
118: if (rcsdiff_file(file, frev, argv[i]) < 0) {
119: cvs_log(LP_ERR, "failed to rcsdiff");
120: rcs_close(file);
1.7 niallo 121: status = 2;
1.4 niallo 122: continue;
123: }
124: } else {
125: if (rcsdiff_rev(file, rev, rev2) < 0) {
126: cvs_log(LP_ERR, "failed to rcsdiff");
127: rcs_close(file);
1.7 niallo 128: status = 2;
1.4 niallo 129: continue;
130: }
1.1 joris 131: }
132:
133: rcs_close(file);
134: }
135:
1.7 niallo 136: exit(status);
1.1 joris 137: }
138:
139: void
140: rcsdiff_usage(void)
141: {
1.9 deraadt 142: fprintf(stderr,
1.12 ! niallo 143: "usage: rcsdiff [-cnquV] [-rrev1 [-rrev2]] file ...\n");
1.1 joris 144: }
145:
146: static int
147: rcsdiff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
148: {
149: char path1[MAXPATHLEN], path2[MAXPATHLEN];
150: BUF *b1, *b2;
151: char rbuf[64];
152:
153: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
1.10 xsa 154: if (verbose == 1)
1.1 joris 155: printf("retrieving revision %s\n", rbuf);
156:
157: if ((b1 = rcs_getrev(rfp, rev)) == NULL) {
158: cvs_log(LP_ERR, "failed to retrieve revision");
159: return (-1);
160: }
161:
162: if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
163: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
164: cvs_buf_free(b1);
1.4 niallo 165: return (-1);
166: }
167:
168: strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
169: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
170: cvs_log(LP_ERRNO, "could not write temporary file");
171: cvs_buf_free(b1);
172: cvs_buf_free(b2);
173: return (-1);
174: }
175: cvs_buf_free(b1);
176:
177: strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
178: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
179: cvs_buf_free(b2);
180: (void)unlink(path1);
181: return (-1);
182: }
183: cvs_buf_free(b2);
184:
185: cvs_diffreg(path1, path2, NULL);
186: (void)unlink(path1);
187: (void)unlink(path2);
188:
189: return (0);
190: }
191:
192: static int
193: rcsdiff_rev(RCSFILE *rfp, RCSNUM *rev1, RCSNUM *rev2)
194: {
195: char path1[MAXPATHLEN], path2[MAXPATHLEN];
196: BUF *b1, *b2;
197: char rbuf[64];
198:
199: rcsnum_tostr(rev1, rbuf, sizeof(rbuf));
1.10 xsa 200: if (verbose == 1)
1.4 niallo 201: printf("retrieving revision %s\n", rbuf);
202:
203: if ((b1 = rcs_getrev(rfp, rev1)) == NULL) {
204: cvs_log(LP_ERR, "failed to retrieve revision");
205: return (-1);
206: }
207:
208: rcsnum_tostr(rev2, rbuf, sizeof(rbuf));
1.10 xsa 209: if (verbose == 1)
1.4 niallo 210: printf("retrieving revision %s\n", rbuf);
211:
212: if ((b2 = rcs_getrev(rfp, rev2)) == NULL) {
213: cvs_log(LP_ERR, "failed to retrieve revision");
1.1 joris 214: return (-1);
215: }
216:
217: strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
218: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
219: cvs_log(LP_ERRNO, "could not write temporary file");
220: cvs_buf_free(b1);
221: cvs_buf_free(b2);
222: return (-1);
223: }
224: cvs_buf_free(b1);
225:
226: strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
227: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
228: cvs_buf_free(b2);
229: (void)unlink(path1);
230: return (-1);
231: }
232: cvs_buf_free(b2);
233:
1.2 niallo 234: cvs_diffreg(path1, path2, NULL);
1.1 joris 235: (void)unlink(path1);
236: (void)unlink(path2);
237:
238: return (0);
239: }