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

1.15    ! xsa         1: /*     $OpenBSD: rcsdiff.c,v 1.14 2005/11/14 11:01:04 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.4       niallo     41: static int rcsdiff_rev(RCSFILE *rfp, RCSNUM *rev, RCSNUM *rev2);
1.1       joris      42:
                     43: int
                     44: rcsdiff_main(int argc, char **argv)
                     45: {
1.7       niallo     46:        int i, ch, status;
1.4       niallo     47:        RCSNUM *rev, *rev2, *frev;
1.1       joris      48:        RCSFILE *file;
                     49:        char fpath[MAXPATHLEN];
                     50:
                     51:        rev = RCS_HEAD_REV;
1.4       niallo     52:        rev2 = NULL;
1.7       niallo     53:        status = 0;
1.1       joris      54:
1.11      joris      55:        while ((ch = rcs_getopt(argc, argv, "cnqr:uV")) != -1) {
1.1       joris      56:                switch (ch) {
1.3       joris      57:                case 'c':
                     58:                        diff_format = D_CONTEXT;
                     59:                        break;
                     60:                case 'n':
                     61:                        diff_format = D_RCSDIFF;
                     62:                        break;
1.1       joris      63:                case 'q':
                     64:                        verbose = 0;
1.3       joris      65:                        break;
                     66:                case 'u':
                     67:                        diff_format = D_UNIFIED;
1.1       joris      68:                        break;
                     69:                case 'V':
                     70:                        printf("%s\n", rcs_version);
                     71:                        exit(0);
1.4       niallo     72:                case 'r':
                     73:                        if (rev == RCS_HEAD_REV) {
1.11      joris      74:                                if ((rev = rcsnum_parse(rcs_optarg)) == NULL) {
1.4       niallo     75:                                        cvs_log(LP_ERR, "bad revision number");
                     76:                                        exit(1);
                     77:                                }
                     78:                        } else {
1.11      joris      79:                                if ((rev2 = rcsnum_parse(rcs_optarg)) == NULL) {
1.4       niallo     80:                                        cvs_log(LP_ERR, "bad revision number");
                     81:                                        exit(1);
                     82:                                }
                     83:                        }
                     84:                        break;
1.1       joris      85:                default:
1.11      joris      86:                        (usage)();
                     87:                        exit (1);
1.1       joris      88:                }
                     89:        }
                     90:
1.11      joris      91:        argc -= rcs_optind;
                     92:        argv += rcs_optind;
1.1       joris      93:
                     94:        if (argc == 0) {
                     95:                cvs_log(LP_ERR, "no input file");
                     96:                (usage)();
                     97:                exit(1);
                     98:        }
                     99:
                    100:        for (i = 0; i < argc; i++) {
                    101:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    102:                        continue;
                    103:
                    104:                if ((file = rcs_open(fpath, RCS_READ)) == NULL)
                    105:                        continue;
                    106:
                    107:                if (rev == RCS_HEAD_REV)
                    108:                        frev = file->rf_head;
                    109:                else
                    110:                        frev = rev;
1.14      xsa       111:
                    112:                if (verbose == 1) {
                    113:                        cvs_printf("%s\n", RCS_DIFF_DIV);
                    114:                        cvs_printf("RCS file: %s\n", fpath);
                    115:                }
1.8       joris     116:
                    117:                diff_file = argv[i];
1.1       joris     118:
1.4       niallo    119:                if (rev2 == NULL) {
                    120:                        if (rcsdiff_file(file, frev, argv[i]) < 0) {
                    121:                                rcs_close(file);
1.7       niallo    122:                                status = 2;
1.4       niallo    123:                                continue;
                    124:                        }
                    125:                } else {
                    126:                        if (rcsdiff_rev(file, rev, rev2) < 0) {
                    127:                                rcs_close(file);
1.7       niallo    128:                                status = 2;
1.4       niallo    129:                                continue;
                    130:                        }
1.1       joris     131:                }
                    132:
                    133:                rcs_close(file);
                    134:        }
                    135:
1.7       niallo    136:        exit(status);
1.1       joris     137: }
                    138:
                    139: void
                    140: rcsdiff_usage(void)
                    141: {
1.9       deraadt   142:        fprintf(stderr,
1.12      niallo    143:            "usage: rcsdiff [-cnquV] [-rrev1 [-rrev2]] file ...\n");
1.1       joris     144: }
                    145:
                    146: static int
                    147: rcsdiff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
                    148: {
                    149:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    150:        BUF *b1, *b2;
                    151:        char rbuf[64];
1.15    ! xsa       152:        struct stat st;
        !           153:
        !           154:        if (stat(filename, &st) == -1) {
        !           155:                cvs_log(LP_ERRNO, "%s", filename);
        !           156:                return (-1);
        !           157:        }
1.1       joris     158:
                    159:        rcsnum_tostr(rev, rbuf, sizeof(rbuf));
1.10      xsa       160:        if (verbose == 1)
1.1       joris     161:                printf("retrieving revision %s\n", rbuf);
                    162:
                    163:        if ((b1 = rcs_getrev(rfp, rev)) == NULL) {
                    164:                cvs_log(LP_ERR, "failed to retrieve revision");
                    165:                return (-1);
                    166:        }
                    167:
                    168:        if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
                    169:                cvs_log(LP_ERR, "failed to load file: '%s'", filename);
                    170:                cvs_buf_free(b1);
1.4       niallo    171:                return (-1);
                    172:        }
                    173:
1.13      xsa       174:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    175:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4       niallo    176:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    177:                cvs_log(LP_ERRNO, "could not write temporary file");
                    178:                cvs_buf_free(b1);
                    179:                cvs_buf_free(b2);
                    180:                return (-1);
                    181:        }
                    182:        cvs_buf_free(b1);
                    183:
1.13      xsa       184:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    185:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4       niallo    186:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    187:                cvs_buf_free(b2);
                    188:                (void)unlink(path1);
                    189:                return (-1);
                    190:        }
                    191:        cvs_buf_free(b2);
                    192:
                    193:        cvs_diffreg(path1, path2, NULL);
                    194:        (void)unlink(path1);
                    195:        (void)unlink(path2);
                    196:
                    197:        return (0);
                    198: }
                    199:
                    200: static int
                    201: rcsdiff_rev(RCSFILE *rfp, RCSNUM *rev1, RCSNUM *rev2)
                    202: {
                    203:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    204:        BUF *b1, *b2;
                    205:        char rbuf[64];
                    206:
                    207:        rcsnum_tostr(rev1, rbuf, sizeof(rbuf));
1.10      xsa       208:        if (verbose == 1)
1.4       niallo    209:                printf("retrieving revision %s\n", rbuf);
                    210:
                    211:        if ((b1 = rcs_getrev(rfp, rev1)) == NULL) {
                    212:                cvs_log(LP_ERR, "failed to retrieve revision");
                    213:                return (-1);
                    214:        }
                    215:
                    216:        rcsnum_tostr(rev2, rbuf, sizeof(rbuf));
1.10      xsa       217:        if (verbose == 1)
1.4       niallo    218:                printf("retrieving revision %s\n", rbuf);
                    219:
                    220:        if ((b2 = rcs_getrev(rfp, rev2)) == NULL) {
                    221:                cvs_log(LP_ERR, "failed to retrieve revision");
1.1       joris     222:                return (-1);
                    223:        }
                    224:
1.13      xsa       225:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    226:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.1       joris     227:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    228:                cvs_log(LP_ERRNO, "could not write temporary file");
                    229:                cvs_buf_free(b1);
                    230:                cvs_buf_free(b2);
                    231:                return (-1);
                    232:        }
                    233:        cvs_buf_free(b1);
                    234:
1.13      xsa       235:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    236:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.1       joris     237:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    238:                cvs_buf_free(b2);
                    239:                (void)unlink(path1);
                    240:                return (-1);
                    241:        }
                    242:        cvs_buf_free(b2);
                    243:
1.2       niallo    244:        cvs_diffreg(path1, path2, NULL);
1.1       joris     245:        (void)unlink(path1);
                    246:        (void)unlink(path2);
                    247:
                    248:        return (0);
                    249: }