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