Annotation of src/usr.bin/rcs/rcsdiff.c, Revision 1.18
1.18 ! xsa 1: /* $OpenBSD: rcsdiff.c,v 1.17 2005/11/21 16:20:29 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.16 xsa 41: static int rcsdiff_rev(RCSFILE *, RCSNUM *, RCSNUM *, const char *);
1.1 joris 42:
1.18 ! xsa 43: static int kflag = RCS_KWEXP_ERR;
! 44:
1.1 joris 45: int
46: rcsdiff_main(int argc, char **argv)
47: {
1.7 niallo 48: int i, ch, status;
1.4 niallo 49: RCSNUM *rev, *rev2, *frev;
1.1 joris 50: RCSFILE *file;
51: char fpath[MAXPATHLEN];
52:
53: rev = RCS_HEAD_REV;
1.4 niallo 54: rev2 = NULL;
1.7 niallo 55: status = 0;
1.1 joris 56:
1.16 xsa 57: strlcpy(diffargs, "diff", sizeof(diffargs));
58:
1.18 ! xsa 59: while ((ch = rcs_getopt(argc, argv, "ck:nqr:uVx:")) != -1) {
1.1 joris 60: switch (ch) {
1.3 joris 61: case 'c':
1.16 xsa 62: strlcat(diffargs, " -c", sizeof(diffargs));
1.3 joris 63: diff_format = D_CONTEXT;
64: break;
1.18 ! xsa 65: case 'k':
! 66: kflag = rcs_kflag_get(rcs_optarg);
! 67: if (RCS_KWEXP_INVAL(kflag)) {
! 68: cvs_log(LP_ERR,
! 69: "invalid RCS keyword expansion mode");
! 70: (usage)();
! 71: exit(1);
! 72: }
! 73: break;
1.3 joris 74: case 'n':
1.16 xsa 75: strlcat(diffargs, " -n", sizeof(diffargs));
1.3 joris 76: diff_format = D_RCSDIFF;
77: break;
1.1 joris 78: case 'q':
79: verbose = 0;
1.3 joris 80: break;
81: case 'u':
1.16 xsa 82: strlcat(diffargs, " -u", sizeof(diffargs));
1.3 joris 83: diff_format = D_UNIFIED;
1.1 joris 84: break;
1.4 niallo 85: case 'r':
86: if (rev == RCS_HEAD_REV) {
1.11 joris 87: if ((rev = rcsnum_parse(rcs_optarg)) == NULL) {
1.4 niallo 88: cvs_log(LP_ERR, "bad revision number");
89: exit(1);
90: }
91: } else {
1.11 joris 92: if ((rev2 = rcsnum_parse(rcs_optarg)) == NULL) {
1.4 niallo 93: cvs_log(LP_ERR, "bad revision number");
94: exit(1);
95: }
96: }
1.17 xsa 97: break;
98: case 'V':
99: printf("%s\n", rcs_version);
100: exit(0);
101: case 'x':
102: rcs_suffixes = rcs_optarg;
1.4 niallo 103: break;
1.1 joris 104: default:
1.11 joris 105: (usage)();
106: exit (1);
1.1 joris 107: }
108: }
109:
1.11 joris 110: argc -= rcs_optind;
111: argv += rcs_optind;
1.1 joris 112:
113: if (argc == 0) {
114: cvs_log(LP_ERR, "no input file");
115: (usage)();
116: exit(1);
117: }
118:
119: for (i = 0; i < argc; i++) {
120: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
121: continue;
122:
123: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
124: continue;
125:
126: if (rev == RCS_HEAD_REV)
127: frev = file->rf_head;
128: else
129: frev = rev;
1.14 xsa 130:
131: if (verbose == 1) {
1.18 ! xsa 132: fprintf(stderr, "%s\n", RCS_DIFF_DIV);
! 133: fprintf(stderr, "RCS file: %s\n", fpath);
1.14 xsa 134: }
1.8 joris 135:
136: diff_file = argv[i];
1.1 joris 137:
1.4 niallo 138: if (rev2 == NULL) {
139: if (rcsdiff_file(file, frev, argv[i]) < 0) {
140: rcs_close(file);
1.7 niallo 141: status = 2;
1.4 niallo 142: continue;
143: }
144: } else {
1.16 xsa 145: if (rcsdiff_rev(file, rev, rev2, argv[i]) < 0) {
1.4 niallo 146: rcs_close(file);
1.7 niallo 147: status = 2;
1.4 niallo 148: continue;
149: }
1.1 joris 150: }
151:
152: rcs_close(file);
153: }
154:
1.7 niallo 155: exit(status);
1.1 joris 156: }
157:
158: void
159: rcsdiff_usage(void)
160: {
1.9 deraadt 161: fprintf(stderr,
1.18 ! xsa 162: "usage: rcsdiff [-cnquV] [-kmode] [-rrev1 [-rrev2]] file ...\n");
1.1 joris 163: }
164:
165: static int
166: rcsdiff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
167: {
168: char path1[MAXPATHLEN], path2[MAXPATHLEN];
169: BUF *b1, *b2;
170: char rbuf[64];
1.15 xsa 171: struct stat st;
172:
173: if (stat(filename, &st) == -1) {
174: cvs_log(LP_ERRNO, "%s", filename);
175: return (-1);
176: }
1.1 joris 177:
178: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
1.16 xsa 179: if (verbose == 1) {
1.18 ! xsa 180: fprintf(stderr, "retrieving revision %s\n", rbuf);
! 181: fprintf(stderr, "%s -r%s %s\n", diffargs, rbuf, filename);
1.16 xsa 182: }
1.1 joris 183:
184: if ((b1 = rcs_getrev(rfp, rev)) == NULL) {
185: cvs_log(LP_ERR, "failed to retrieve revision");
186: return (-1);
187: }
188:
189: if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
190: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
191: cvs_buf_free(b1);
1.4 niallo 192: return (-1);
193: }
194:
1.13 xsa 195: strlcpy(path1, rcs_tmpdir, sizeof(path1));
196: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 197: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
198: cvs_log(LP_ERRNO, "could not write temporary file");
199: cvs_buf_free(b1);
200: cvs_buf_free(b2);
201: return (-1);
202: }
203: cvs_buf_free(b1);
204:
1.13 xsa 205: strlcpy(path2, rcs_tmpdir, sizeof(path2));
206: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 207: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
208: cvs_buf_free(b2);
209: (void)unlink(path1);
210: return (-1);
211: }
212: cvs_buf_free(b2);
213:
214: cvs_diffreg(path1, path2, NULL);
215: (void)unlink(path1);
216: (void)unlink(path2);
217:
218: return (0);
219: }
220:
221: static int
1.16 xsa 222: rcsdiff_rev(RCSFILE *rfp, RCSNUM *rev1, RCSNUM *rev2, const char *filename)
1.4 niallo 223: {
224: char path1[MAXPATHLEN], path2[MAXPATHLEN];
225: BUF *b1, *b2;
1.16 xsa 226: char rbuf1[64], rbuf2[64];
1.4 niallo 227:
1.16 xsa 228: rcsnum_tostr(rev1, rbuf1, sizeof(rbuf1));
1.10 xsa 229: if (verbose == 1)
1.16 xsa 230: printf("retrieving revision %s\n", rbuf1);
1.4 niallo 231:
232: if ((b1 = rcs_getrev(rfp, rev1)) == NULL) {
233: cvs_log(LP_ERR, "failed to retrieve revision");
234: return (-1);
235: }
236:
1.16 xsa 237: rcsnum_tostr(rev2, rbuf2, sizeof(rbuf2));
1.10 xsa 238: if (verbose == 1)
1.18 ! xsa 239: fprintf(stderr, "retrieving revision %s\n", rbuf2);
1.4 niallo 240:
241: if ((b2 = rcs_getrev(rfp, rev2)) == NULL) {
242: cvs_log(LP_ERR, "failed to retrieve revision");
1.1 joris 243: return (-1);
244: }
1.16 xsa 245:
246: if (verbose == 1)
1.18 ! xsa 247: fprintf(stderr,
! 248: "%s -r%s -r%s %s\n", diffargs, rbuf1, rbuf2, filename);
1.1 joris 249:
1.13 xsa 250: strlcpy(path1, rcs_tmpdir, sizeof(path1));
251: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.1 joris 252: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
253: cvs_log(LP_ERRNO, "could not write temporary file");
254: cvs_buf_free(b1);
255: cvs_buf_free(b2);
256: return (-1);
257: }
258: cvs_buf_free(b1);
259:
1.13 xsa 260: strlcpy(path2, rcs_tmpdir, sizeof(path2));
261: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.1 joris 262: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
263: cvs_buf_free(b2);
264: (void)unlink(path1);
265: return (-1);
266: }
267: cvs_buf_free(b2);
268:
1.2 niallo 269: cvs_diffreg(path1, path2, NULL);
1.1 joris 270: (void)unlink(path1);
271: (void)unlink(path2);
272:
273: return (0);
274: }