Annotation of src/usr.bin/rcs/rcsdiff.c, Revision 1.3
1.3 ! joris 1: /* $OpenBSD: rcsdiff.c,v 1.2 2005/10/07 23:59:56 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: extern char *__progname;
41: static int rcsdiff_file(RCSFILE *, RCSNUM *, const char *);
42:
43: int
44: rcsdiff_main(int argc, char **argv)
45: {
46: int i, ch;
47: RCSNUM *rev, *frev;
48: RCSFILE *file;
49: char fpath[MAXPATHLEN];
50:
51: rev = RCS_HEAD_REV;
52:
1.3 ! joris 53: while ((ch = getopt(argc, argv, "cnquV")) != -1) {
1.1 joris 54: switch (ch) {
1.3 ! joris 55: case 'c':
! 56: diff_format = D_CONTEXT;
! 57: break;
! 58: case 'n':
! 59: diff_format = D_RCSDIFF;
! 60: break;
1.1 joris 61: case 'q':
62: verbose = 0;
1.3 ! joris 63: break;
! 64: case 'u':
! 65: diff_format = D_UNIFIED;
1.1 joris 66: break;
67: case 'V':
68: printf("%s\n", rcs_version);
69: exit(0);
70: default:
71: break;
72: }
73: }
74:
75: argc -= optind;
76: argv += optind;
77:
78: if (argc == 0) {
79: cvs_log(LP_ERR, "no input file");
80: (usage)();
81: exit(1);
82: }
83:
84: for (i = 0; i < argc; i++) {
85: if (verbose)
86: cvs_printf("%s\n", RCS_DIFF_DIV);
87:
88: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
89: continue;
90:
91: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
92: continue;
93:
94: if (rev == RCS_HEAD_REV)
95: frev = file->rf_head;
96: else
97: frev = rev;
98:
99: if (rcsdiff_file(file, frev, argv[i]) < 0) {
100: cvs_log(LP_ERR, "failed to rcsdiff");
101: rcs_close(file);
102: continue;
103: }
104:
105: rcs_close(file);
106: }
107:
108: return (0);
109: }
110:
111: void
112: rcsdiff_usage(void)
113: {
114: fprintf(stderr, "usage %s [-qV] file ...\n", __progname);
115: }
116:
117: static int
118: rcsdiff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
119: {
120: char path1[MAXPATHLEN], path2[MAXPATHLEN];
121: BUF *b1, *b2;
122: char rbuf[64];
123:
124: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
125: if (verbose)
126: printf("retrieving revision %s\n", rbuf);
127:
128: if ((b1 = rcs_getrev(rfp, rev)) == NULL) {
129: cvs_log(LP_ERR, "failed to retrieve revision");
130: return (-1);
131: }
132:
133: if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
134: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
135: cvs_buf_free(b1);
136: return (-1);
137: }
138:
139: strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
140: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
141: cvs_log(LP_ERRNO, "could not write temporary file");
142: cvs_buf_free(b1);
143: cvs_buf_free(b2);
144: return (-1);
145: }
146: cvs_buf_free(b1);
147:
148: strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
149: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
150: cvs_buf_free(b2);
151: (void)unlink(path1);
152: return (-1);
153: }
154: cvs_buf_free(b2);
155:
1.2 niallo 156: cvs_diffreg(path1, path2, NULL);
1.1 joris 157: (void)unlink(path1);
158: (void)unlink(path2);
159:
160: return (0);
161: }