Annotation of src/usr.bin/rcs/rcsdiff.c, Revision 1.14
1.14 ! xsa 1: /* $OpenBSD: rcsdiff.c,v 1.13 2005/10/27 07:43:56 xsa 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++) {
101: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
102: continue;
103:
104: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
105: continue;
106:
107: if (rev == RCS_HEAD_REV)
108: frev = file->rf_head;
109: else
110: frev = rev;
1.14 ! xsa 111:
! 112: if (verbose == 1) {
! 113: cvs_printf("%s\n", RCS_DIFF_DIV);
! 114: cvs_printf("RCS file: %s\n", fpath);
! 115: }
1.8 joris 116:
117: diff_file = argv[i];
1.1 joris 118:
1.4 niallo 119: if (rev2 == NULL) {
120: if (rcsdiff_file(file, frev, argv[i]) < 0) {
121: cvs_log(LP_ERR, "failed to rcsdiff");
122: rcs_close(file);
1.7 niallo 123: status = 2;
1.4 niallo 124: continue;
125: }
126: } else {
127: if (rcsdiff_rev(file, rev, rev2) < 0) {
128: cvs_log(LP_ERR, "failed to rcsdiff");
129: rcs_close(file);
1.7 niallo 130: status = 2;
1.4 niallo 131: continue;
132: }
1.1 joris 133: }
134:
135: rcs_close(file);
136: }
137:
1.7 niallo 138: exit(status);
1.1 joris 139: }
140:
141: void
142: rcsdiff_usage(void)
143: {
1.9 deraadt 144: fprintf(stderr,
1.12 niallo 145: "usage: rcsdiff [-cnquV] [-rrev1 [-rrev2]] file ...\n");
1.1 joris 146: }
147:
148: static int
149: rcsdiff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
150: {
151: char path1[MAXPATHLEN], path2[MAXPATHLEN];
152: BUF *b1, *b2;
153: char rbuf[64];
154:
155: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
1.10 xsa 156: if (verbose == 1)
1.1 joris 157: printf("retrieving revision %s\n", rbuf);
158:
159: if ((b1 = rcs_getrev(rfp, rev)) == NULL) {
160: cvs_log(LP_ERR, "failed to retrieve revision");
161: return (-1);
162: }
163:
164: if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
165: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
166: cvs_buf_free(b1);
1.4 niallo 167: return (-1);
168: }
169:
1.13 xsa 170: strlcpy(path1, rcs_tmpdir, sizeof(path1));
171: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 172: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
173: cvs_log(LP_ERRNO, "could not write temporary file");
174: cvs_buf_free(b1);
175: cvs_buf_free(b2);
176: return (-1);
177: }
178: cvs_buf_free(b1);
179:
1.13 xsa 180: strlcpy(path2, rcs_tmpdir, sizeof(path2));
181: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 182: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
183: cvs_buf_free(b2);
184: (void)unlink(path1);
185: return (-1);
186: }
187: cvs_buf_free(b2);
188:
189: cvs_diffreg(path1, path2, NULL);
190: (void)unlink(path1);
191: (void)unlink(path2);
192:
193: return (0);
194: }
195:
196: static int
197: rcsdiff_rev(RCSFILE *rfp, RCSNUM *rev1, RCSNUM *rev2)
198: {
199: char path1[MAXPATHLEN], path2[MAXPATHLEN];
200: BUF *b1, *b2;
201: char rbuf[64];
202:
203: rcsnum_tostr(rev1, rbuf, sizeof(rbuf));
1.10 xsa 204: if (verbose == 1)
1.4 niallo 205: printf("retrieving revision %s\n", rbuf);
206:
207: if ((b1 = rcs_getrev(rfp, rev1)) == NULL) {
208: cvs_log(LP_ERR, "failed to retrieve revision");
209: return (-1);
210: }
211:
212: rcsnum_tostr(rev2, rbuf, sizeof(rbuf));
1.10 xsa 213: if (verbose == 1)
1.4 niallo 214: printf("retrieving revision %s\n", rbuf);
215:
216: if ((b2 = rcs_getrev(rfp, rev2)) == NULL) {
217: cvs_log(LP_ERR, "failed to retrieve revision");
1.1 joris 218: return (-1);
219: }
220:
1.13 xsa 221: strlcpy(path1, rcs_tmpdir, sizeof(path1));
222: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.1 joris 223: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
224: cvs_log(LP_ERRNO, "could not write temporary file");
225: cvs_buf_free(b1);
226: cvs_buf_free(b2);
227: return (-1);
228: }
229: cvs_buf_free(b1);
230:
1.13 xsa 231: strlcpy(path2, rcs_tmpdir, sizeof(path2));
232: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.1 joris 233: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
234: cvs_buf_free(b2);
235: (void)unlink(path1);
236: return (-1);
237: }
238: cvs_buf_free(b2);
239:
1.2 niallo 240: cvs_diffreg(path1, path2, NULL);
1.1 joris 241: (void)unlink(path1);
242: (void)unlink(path2);
243:
244: return (0);
245: }