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

1.28    ! niallo      1: /*     $OpenBSD: rcsdiff.c,v 1.27 2005/12/20 09:04:17 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.25      xsa        48:        int i, ch, status;
1.4       niallo     49:        RCSNUM *rev, *rev2, *frev;
1.1       joris      50:        RCSFILE *file;
                     51:        char fpath[MAXPATHLEN];
                     52:
                     53:        rev = RCS_HEAD_REV;
1.4       niallo     54:        rev2 = NULL;
1.7       niallo     55:        status = 0;
1.1       joris      56:
1.16      xsa        57:        strlcpy(diffargs, "diff", sizeof(diffargs));
                     58:
1.19      xsa        59:        while ((ch = rcs_getopt(argc, argv, "ck:nqr:TuVx:")) != -1) {
1.1       joris      60:                switch (ch) {
1.3       joris      61:                case 'c':
1.16      xsa        62:                        strlcat(diffargs, " -c", sizeof(diffargs));
1.3       joris      63:                        diff_format = D_CONTEXT;
                     64:                        break;
1.18      xsa        65:                case 'k':
                     66:                        kflag = rcs_kflag_get(rcs_optarg);
                     67:                        if (RCS_KWEXP_INVAL(kflag)) {
                     68:                                cvs_log(LP_ERR,
                     69:                                    "invalid RCS keyword expansion mode");
                     70:                                (usage)();
                     71:                                exit(1);
                     72:                        }
                     73:                        break;
1.3       joris      74:                case 'n':
1.16      xsa        75:                        strlcat(diffargs, " -n", sizeof(diffargs));
1.3       joris      76:                        diff_format = D_RCSDIFF;
                     77:                        break;
1.1       joris      78:                case 'q':
                     79:                        verbose = 0;
1.3       joris      80:                        break;
                     81:                case 'u':
1.16      xsa        82:                        strlcat(diffargs, " -u", sizeof(diffargs));
1.3       joris      83:                        diff_format = D_UNIFIED;
1.1       joris      84:                        break;
1.4       niallo     85:                case 'r':
                     86:                        if (rev == RCS_HEAD_REV) {
1.27      xsa        87:                                if ((rev = rcsnum_parse(rcs_optarg)) == NULL)
                     88:                                        fatal("bad revision number");
1.4       niallo     89:                        } else {
1.27      xsa        90:                                if ((rev2 = rcsnum_parse(rcs_optarg)) == NULL)
                     91:                                        fatal("bad revision number");
1.4       niallo     92:                        }
1.19      xsa        93:                        break;
                     94:                case 'T':
1.21      xsa        95:                        /*
                     96:                         * kept for compatibility
                     97:                         */
1.17      xsa        98:                        break;
                     99:                case 'V':
                    100:                        printf("%s\n", rcs_version);
                    101:                        exit(0);
                    102:                case 'x':
                    103:                        rcs_suffixes = rcs_optarg;
1.4       niallo    104:                        break;
1.1       joris     105:                default:
1.11      joris     106:                        (usage)();
                    107:                        exit (1);
1.1       joris     108:                }
                    109:        }
                    110:
1.11      joris     111:        argc -= rcs_optind;
                    112:        argv += rcs_optind;
1.1       joris     113:
                    114:        if (argc == 0) {
                    115:                cvs_log(LP_ERR, "no input file");
                    116:                (usage)();
                    117:                exit(1);
                    118:        }
                    119:
                    120:        for (i = 0; i < argc; i++) {
                    121:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    122:                        continue;
                    123:
1.28    ! niallo    124:                if ((file = rcs_open(fpath, RCS_READ|RCS_PARSE_FULLY)) == NULL)
1.1       joris     125:                        continue;
1.20      xsa       126:
                    127:                if (kflag != RCS_KWEXP_ERR)
                    128:                        rcs_kwexp_set(file, kflag);
1.1       joris     129:
                    130:                if (rev == RCS_HEAD_REV)
                    131:                        frev = file->rf_head;
                    132:                else
                    133:                        frev = rev;
1.14      xsa       134:
                    135:                if (verbose == 1) {
1.18      xsa       136:                        fprintf(stderr, "%s\n", RCS_DIFF_DIV);
                    137:                        fprintf(stderr, "RCS file: %s\n", fpath);
1.14      xsa       138:                }
1.8       joris     139:
                    140:                diff_file = argv[i];
1.1       joris     141:
1.4       niallo    142:                if (rev2 == NULL) {
                    143:                        if (rcsdiff_file(file, frev, argv[i]) < 0) {
                    144:                                rcs_close(file);
1.7       niallo    145:                                status = 2;
1.4       niallo    146:                                continue;
                    147:                        }
                    148:                } else {
1.16      xsa       149:                        if (rcsdiff_rev(file, rev, rev2, argv[i]) < 0) {
1.4       niallo    150:                                rcs_close(file);
1.7       niallo    151:                                status = 2;
1.4       niallo    152:                                continue;
                    153:                        }
1.1       joris     154:                }
                    155:
                    156:                rcs_close(file);
                    157:        }
                    158:
1.26      niallo    159:        return (status);
1.1       joris     160: }
                    161:
                    162: void
                    163: rcsdiff_usage(void)
                    164: {
1.9       deraadt   165:        fprintf(stderr,
1.22      xsa       166:            "usage: rcsdiff [-cnqTuV] [-kmode] [-rrev1 [-rrev2]] "
                    167:            "[-xsuffixes] file ...\n");
1.1       joris     168: }
                    169:
                    170: static int
1.23      xsa       171: rcsdiff_file(RCSFILE *file, RCSNUM *rev, const char *filename)
1.1       joris     172: {
                    173:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    174:        BUF *b1, *b2;
                    175:        char rbuf[64];
1.15      xsa       176:        struct stat st;
1.24      niallo    177:        struct timeval tv[2], tv2[2];
                    178:        memset(&tv, 0, sizeof(tv));
                    179:        memset(&tv2, 0, sizeof(tv2));
1.15      xsa       180:
                    181:        if (stat(filename, &st) == -1) {
                    182:                cvs_log(LP_ERRNO, "%s", filename);
                    183:                return (-1);
                    184:        }
1.1       joris     185:
                    186:        rcsnum_tostr(rev, rbuf, sizeof(rbuf));
1.16      xsa       187:        if (verbose == 1) {
1.18      xsa       188:                fprintf(stderr, "retrieving revision %s\n", rbuf);
                    189:                fprintf(stderr, "%s -r%s %s\n", diffargs, rbuf, filename);
1.16      xsa       190:        }
1.1       joris     191:
1.23      xsa       192:        if ((b1 = rcs_getrev(file, rev)) == NULL) {
1.1       joris     193:                cvs_log(LP_ERR, "failed to retrieve revision");
                    194:                return (-1);
                    195:        }
1.24      niallo    196:        tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
                    197:        tv[1].tv_sec = tv[0].tv_sec;
1.1       joris     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:        }
1.24      niallo    204:        tv2[0].tv_sec = st.st_mtime;
                    205:        tv2[1].tv_sec = st.st_mtime;
1.4       niallo    206:
1.13      xsa       207:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    208:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4       niallo    209:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    210:                cvs_log(LP_ERRNO, "could not write temporary file");
                    211:                cvs_buf_free(b1);
                    212:                cvs_buf_free(b2);
                    213:                return (-1);
                    214:        }
                    215:        cvs_buf_free(b1);
1.24      niallo    216:        if (utimes(path1, (const struct timeval *)&tv) < 0)
                    217:                cvs_log(LP_ERRNO, "error setting utimes");
1.4       niallo    218:
1.13      xsa       219:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    220:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4       niallo    221:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    222:                cvs_buf_free(b2);
                    223:                (void)unlink(path1);
                    224:                return (-1);
                    225:        }
                    226:        cvs_buf_free(b2);
1.24      niallo    227:        if (utimes(path2, (const struct timeval *)&tv2) < 0)
                    228:                cvs_log(LP_ERRNO, "error setting utimes");
1.4       niallo    229:
                    230:        cvs_diffreg(path1, path2, NULL);
                    231:        (void)unlink(path1);
                    232:        (void)unlink(path2);
                    233:
                    234:        return (0);
                    235: }
                    236:
                    237: static int
1.23      xsa       238: rcsdiff_rev(RCSFILE *file, RCSNUM *rev1, RCSNUM *rev2, const char *filename)
1.4       niallo    239: {
                    240:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    241:        BUF *b1, *b2;
1.16      xsa       242:        char rbuf1[64], rbuf2[64];
1.24      niallo    243:        struct timeval tv[2], tv2[2];
                    244:
                    245:        memset(&tv, 0, sizeof(tv));
                    246:        memset(&tv2, 0, sizeof(tv2));
1.4       niallo    247:
1.16      xsa       248:        rcsnum_tostr(rev1, rbuf1, sizeof(rbuf1));
1.10      xsa       249:        if (verbose == 1)
1.16      xsa       250:                printf("retrieving revision %s\n", rbuf1);
1.4       niallo    251:
1.23      xsa       252:        if ((b1 = rcs_getrev(file, rev1)) == NULL) {
1.4       niallo    253:                cvs_log(LP_ERR, "failed to retrieve revision");
                    254:                return (-1);
                    255:        }
1.24      niallo    256:        tv[0].tv_sec = (long)rcs_rev_getdate(file, rev1);
                    257:        tv[1].tv_sec = tv[0].tv_sec;
1.4       niallo    258:
1.16      xsa       259:        rcsnum_tostr(rev2, rbuf2, sizeof(rbuf2));
1.10      xsa       260:        if (verbose == 1)
1.18      xsa       261:                fprintf(stderr, "retrieving revision %s\n", rbuf2);
1.4       niallo    262:
1.23      xsa       263:        if ((b2 = rcs_getrev(file, rev2)) == NULL) {
1.4       niallo    264:                cvs_log(LP_ERR, "failed to retrieve revision");
1.1       joris     265:                return (-1);
                    266:        }
1.24      niallo    267:        tv2[0].tv_sec = (long)rcs_rev_getdate(file, rev2);
                    268:        tv2[1].tv_sec = tv2[0].tv_sec;
1.16      xsa       269:
                    270:        if (verbose == 1)
1.18      xsa       271:                fprintf(stderr,
                    272:                    "%s -r%s -r%s %s\n", diffargs, rbuf1, rbuf2, filename);
1.1       joris     273:
1.13      xsa       274:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    275:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.1       joris     276:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    277:                cvs_log(LP_ERRNO, "could not write temporary file");
                    278:                cvs_buf_free(b1);
                    279:                cvs_buf_free(b2);
                    280:                return (-1);
                    281:        }
                    282:        cvs_buf_free(b1);
1.24      niallo    283:        if (utimes(path1, (const struct timeval *)&tv) < 0)
                    284:                cvs_log(LP_ERRNO, "error setting utimes");
1.1       joris     285:
1.13      xsa       286:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    287:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.1       joris     288:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    289:                cvs_buf_free(b2);
                    290:                (void)unlink(path1);
                    291:                return (-1);
                    292:        }
                    293:        cvs_buf_free(b2);
                    294:
1.24      niallo    295:        if (utimes(path2, (const struct timeval *)&tv2) < 0)
                    296:                cvs_log(LP_ERRNO, "error setting utimes");
                    297:
1.2       niallo    298:        cvs_diffreg(path1, path2, NULL);
1.1       joris     299:        (void)unlink(path1);
                    300:        (void)unlink(path2);
                    301:
                    302:        return (0);
                    303: }