Annotation of src/usr.bin/rcs/rcsdiff.c, Revision 1.25
1.25 ! xsa 1: /* $OpenBSD: rcsdiff.c,v 1.24 2005/12/01 23:02:27 niallo 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.25 ! xsa 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.19 xsa 59: while ((ch = rcs_getopt(argc, argv, "ck:nqr:TuVx:")) != -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.19 xsa 97: break;
98: case 'T':
1.21 xsa 99: /*
100: * kept for compatibility
101: */
1.17 xsa 102: break;
103: case 'V':
104: printf("%s\n", rcs_version);
105: exit(0);
106: case 'x':
107: rcs_suffixes = rcs_optarg;
1.4 niallo 108: break;
1.1 joris 109: default:
1.11 joris 110: (usage)();
111: exit (1);
1.1 joris 112: }
113: }
114:
1.11 joris 115: argc -= rcs_optind;
116: argv += rcs_optind;
1.1 joris 117:
118: if (argc == 0) {
119: cvs_log(LP_ERR, "no input file");
120: (usage)();
121: exit(1);
122: }
123:
124: for (i = 0; i < argc; i++) {
125: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
126: continue;
127:
128: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
129: continue;
1.20 xsa 130:
131: if (kflag != RCS_KWEXP_ERR)
132: rcs_kwexp_set(file, kflag);
1.1 joris 133:
134: if (rev == RCS_HEAD_REV)
135: frev = file->rf_head;
136: else
137: frev = rev;
1.14 xsa 138:
139: if (verbose == 1) {
1.18 xsa 140: fprintf(stderr, "%s\n", RCS_DIFF_DIV);
141: fprintf(stderr, "RCS file: %s\n", fpath);
1.14 xsa 142: }
1.8 joris 143:
144: diff_file = argv[i];
1.1 joris 145:
1.4 niallo 146: if (rev2 == NULL) {
147: if (rcsdiff_file(file, frev, argv[i]) < 0) {
148: rcs_close(file);
1.7 niallo 149: status = 2;
1.4 niallo 150: continue;
151: }
152: } else {
1.16 xsa 153: if (rcsdiff_rev(file, rev, rev2, argv[i]) < 0) {
1.4 niallo 154: rcs_close(file);
1.7 niallo 155: status = 2;
1.4 niallo 156: continue;
157: }
1.1 joris 158: }
159:
160: rcs_close(file);
161: }
162:
1.7 niallo 163: exit(status);
1.1 joris 164: }
165:
166: void
167: rcsdiff_usage(void)
168: {
1.9 deraadt 169: fprintf(stderr,
1.22 xsa 170: "usage: rcsdiff [-cnqTuV] [-kmode] [-rrev1 [-rrev2]] "
171: "[-xsuffixes] file ...\n");
1.1 joris 172: }
173:
174: static int
1.23 xsa 175: rcsdiff_file(RCSFILE *file, RCSNUM *rev, const char *filename)
1.1 joris 176: {
177: char path1[MAXPATHLEN], path2[MAXPATHLEN];
178: BUF *b1, *b2;
179: char rbuf[64];
1.15 xsa 180: struct stat st;
1.24 niallo 181: struct timeval tv[2], tv2[2];
182: memset(&tv, 0, sizeof(tv));
183: memset(&tv2, 0, sizeof(tv2));
1.15 xsa 184:
185: if (stat(filename, &st) == -1) {
186: cvs_log(LP_ERRNO, "%s", filename);
187: return (-1);
188: }
1.1 joris 189:
190: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
1.16 xsa 191: if (verbose == 1) {
1.18 xsa 192: fprintf(stderr, "retrieving revision %s\n", rbuf);
193: fprintf(stderr, "%s -r%s %s\n", diffargs, rbuf, filename);
1.16 xsa 194: }
1.1 joris 195:
1.23 xsa 196: if ((b1 = rcs_getrev(file, rev)) == NULL) {
1.1 joris 197: cvs_log(LP_ERR, "failed to retrieve revision");
198: return (-1);
199: }
1.24 niallo 200: tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
201: tv[1].tv_sec = tv[0].tv_sec;
1.1 joris 202:
203: if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
204: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
205: cvs_buf_free(b1);
1.4 niallo 206: return (-1);
207: }
1.24 niallo 208: tv2[0].tv_sec = st.st_mtime;
209: tv2[1].tv_sec = st.st_mtime;
1.4 niallo 210:
1.13 xsa 211: strlcpy(path1, rcs_tmpdir, sizeof(path1));
212: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 213: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
214: cvs_log(LP_ERRNO, "could not write temporary file");
215: cvs_buf_free(b1);
216: cvs_buf_free(b2);
217: return (-1);
218: }
219: cvs_buf_free(b1);
1.24 niallo 220: if (utimes(path1, (const struct timeval *)&tv) < 0)
221: cvs_log(LP_ERRNO, "error setting utimes");
1.4 niallo 222:
1.13 xsa 223: strlcpy(path2, rcs_tmpdir, sizeof(path2));
224: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 225: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
226: cvs_buf_free(b2);
227: (void)unlink(path1);
228: return (-1);
229: }
230: cvs_buf_free(b2);
1.24 niallo 231: if (utimes(path2, (const struct timeval *)&tv2) < 0)
232: cvs_log(LP_ERRNO, "error setting utimes");
1.4 niallo 233:
234: cvs_diffreg(path1, path2, NULL);
235: (void)unlink(path1);
236: (void)unlink(path2);
237:
238: return (0);
239: }
240:
241: static int
1.23 xsa 242: rcsdiff_rev(RCSFILE *file, RCSNUM *rev1, RCSNUM *rev2, const char *filename)
1.4 niallo 243: {
244: char path1[MAXPATHLEN], path2[MAXPATHLEN];
245: BUF *b1, *b2;
1.16 xsa 246: char rbuf1[64], rbuf2[64];
1.24 niallo 247: struct timeval tv[2], tv2[2];
248:
249: memset(&tv, 0, sizeof(tv));
250: memset(&tv2, 0, sizeof(tv2));
1.4 niallo 251:
1.16 xsa 252: rcsnum_tostr(rev1, rbuf1, sizeof(rbuf1));
1.10 xsa 253: if (verbose == 1)
1.16 xsa 254: printf("retrieving revision %s\n", rbuf1);
1.4 niallo 255:
1.23 xsa 256: if ((b1 = rcs_getrev(file, rev1)) == NULL) {
1.4 niallo 257: cvs_log(LP_ERR, "failed to retrieve revision");
258: return (-1);
259: }
1.24 niallo 260: tv[0].tv_sec = (long)rcs_rev_getdate(file, rev1);
261: tv[1].tv_sec = tv[0].tv_sec;
1.4 niallo 262:
1.16 xsa 263: rcsnum_tostr(rev2, rbuf2, sizeof(rbuf2));
1.10 xsa 264: if (verbose == 1)
1.18 xsa 265: fprintf(stderr, "retrieving revision %s\n", rbuf2);
1.4 niallo 266:
1.23 xsa 267: if ((b2 = rcs_getrev(file, rev2)) == NULL) {
1.4 niallo 268: cvs_log(LP_ERR, "failed to retrieve revision");
1.1 joris 269: return (-1);
270: }
1.24 niallo 271: tv2[0].tv_sec = (long)rcs_rev_getdate(file, rev2);
272: tv2[1].tv_sec = tv2[0].tv_sec;
1.16 xsa 273:
274: if (verbose == 1)
1.18 xsa 275: fprintf(stderr,
276: "%s -r%s -r%s %s\n", diffargs, rbuf1, rbuf2, filename);
1.1 joris 277:
1.13 xsa 278: strlcpy(path1, rcs_tmpdir, sizeof(path1));
279: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.1 joris 280: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
281: cvs_log(LP_ERRNO, "could not write temporary file");
282: cvs_buf_free(b1);
283: cvs_buf_free(b2);
284: return (-1);
285: }
286: cvs_buf_free(b1);
1.24 niallo 287: if (utimes(path1, (const struct timeval *)&tv) < 0)
288: cvs_log(LP_ERRNO, "error setting utimes");
1.1 joris 289:
1.13 xsa 290: strlcpy(path2, rcs_tmpdir, sizeof(path2));
291: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.1 joris 292: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
293: cvs_buf_free(b2);
294: (void)unlink(path1);
295: return (-1);
296: }
297: cvs_buf_free(b2);
298:
1.24 niallo 299: if (utimes(path2, (const struct timeval *)&tv2) < 0)
300: cvs_log(LP_ERRNO, "error setting utimes");
301:
1.2 niallo 302: cvs_diffreg(path1, path2, NULL);
1.1 joris 303: (void)unlink(path1);
304: (void)unlink(path2);
305:
306: return (0);
307: }