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

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