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