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