[BACK]Return to rcsdiff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/rcsdiff.c, Revision 1.20

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