Annotation of src/usr.bin/rcs/rcsdiff.c, Revision 1.22
1.22 ! xsa 1: /* $OpenBSD: rcsdiff.c,v 1.21 2005/11/28 10:17:20 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
176: rcsdiff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
177: {
178: char path1[MAXPATHLEN], path2[MAXPATHLEN];
179: BUF *b1, *b2;
180: char rbuf[64];
1.15 xsa 181: struct stat st;
182:
183: if (stat(filename, &st) == -1) {
184: cvs_log(LP_ERRNO, "%s", filename);
185: return (-1);
186: }
1.1 joris 187:
188: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
1.16 xsa 189: if (verbose == 1) {
1.18 xsa 190: fprintf(stderr, "retrieving revision %s\n", rbuf);
191: fprintf(stderr, "%s -r%s %s\n", diffargs, rbuf, filename);
1.16 xsa 192: }
1.1 joris 193:
194: if ((b1 = rcs_getrev(rfp, rev)) == NULL) {
195: cvs_log(LP_ERR, "failed to retrieve revision");
196: return (-1);
197: }
198:
199: if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
200: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
201: cvs_buf_free(b1);
1.4 niallo 202: return (-1);
203: }
204:
1.13 xsa 205: strlcpy(path1, rcs_tmpdir, sizeof(path1));
206: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 207: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
208: cvs_log(LP_ERRNO, "could not write temporary file");
209: cvs_buf_free(b1);
210: cvs_buf_free(b2);
211: return (-1);
212: }
213: cvs_buf_free(b1);
214:
1.13 xsa 215: strlcpy(path2, rcs_tmpdir, sizeof(path2));
216: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 217: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
218: cvs_buf_free(b2);
219: (void)unlink(path1);
220: return (-1);
221: }
222: cvs_buf_free(b2);
223:
224: cvs_diffreg(path1, path2, NULL);
225: (void)unlink(path1);
226: (void)unlink(path2);
227:
228: return (0);
229: }
230:
231: static int
1.16 xsa 232: rcsdiff_rev(RCSFILE *rfp, RCSNUM *rev1, RCSNUM *rev2, const char *filename)
1.4 niallo 233: {
234: char path1[MAXPATHLEN], path2[MAXPATHLEN];
235: BUF *b1, *b2;
1.16 xsa 236: char rbuf1[64], rbuf2[64];
1.4 niallo 237:
1.16 xsa 238: rcsnum_tostr(rev1, rbuf1, sizeof(rbuf1));
1.10 xsa 239: if (verbose == 1)
1.16 xsa 240: printf("retrieving revision %s\n", rbuf1);
1.4 niallo 241:
242: if ((b1 = rcs_getrev(rfp, rev1)) == NULL) {
243: cvs_log(LP_ERR, "failed to retrieve revision");
244: return (-1);
245: }
246:
1.16 xsa 247: rcsnum_tostr(rev2, rbuf2, sizeof(rbuf2));
1.10 xsa 248: if (verbose == 1)
1.18 xsa 249: fprintf(stderr, "retrieving revision %s\n", rbuf2);
1.4 niallo 250:
251: if ((b2 = rcs_getrev(rfp, rev2)) == NULL) {
252: cvs_log(LP_ERR, "failed to retrieve revision");
1.1 joris 253: return (-1);
254: }
1.16 xsa 255:
256: if (verbose == 1)
1.18 xsa 257: fprintf(stderr,
258: "%s -r%s -r%s %s\n", diffargs, rbuf1, rbuf2, filename);
1.1 joris 259:
1.13 xsa 260: strlcpy(path1, rcs_tmpdir, sizeof(path1));
261: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.1 joris 262: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
263: cvs_log(LP_ERRNO, "could not write temporary file");
264: cvs_buf_free(b1);
265: cvs_buf_free(b2);
266: return (-1);
267: }
268: cvs_buf_free(b1);
269:
1.13 xsa 270: strlcpy(path2, rcs_tmpdir, sizeof(path2));
271: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.1 joris 272: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
273: cvs_buf_free(b2);
274: (void)unlink(path1);
275: return (-1);
276: }
277: cvs_buf_free(b2);
278:
1.2 niallo 279: cvs_diffreg(path1, path2, NULL);
1.1 joris 280: (void)unlink(path1);
281: (void)unlink(path2);
282:
283: return (0);
284: }