[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.21

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