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

Annotation of src/usr.bin/rcs/co.c, Revision 1.22

1.22    ! xsa         1: /*     $OpenBSD: co.c,v 1.21 2005/10/19 11:37:11 niallo 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>
1.4       joris      33: #include <unistd.h>
1.1       joris      34:
                     35: #include "log.h"
                     36: #include "rcs.h"
                     37: #include "rcsprog.h"
                     38:
1.4       joris      39: #define LOCK_LOCK      1
                     40: #define LOCK_UNLOCK    2
                     41:
1.1       joris      42: int
                     43: checkout_main(int argc, char **argv)
                     44: {
                     45:        int i, ch;
1.18      joris      46:        int fflag, lock;
1.4       joris      47:        RCSNUM *frev, *rev;
1.1       joris      48:        RCSFILE *file;
1.14      niallo     49:        char fpath[MAXPATHLEN], buf[16];
1.4       joris      50:        char *username;
1.1       joris      51:
1.18      joris      52:        fflag = lock = 0;
1.1       joris      53:        rev = RCS_HEAD_REV;
1.6       joris      54:        frev = NULL;
1.4       joris      55:
                     56:        if ((username = getlogin()) == NULL) {
1.10      xsa        57:                cvs_log(LP_ERRNO, "failed to get username");
1.4       joris      58:                exit (1);
                     59:        }
                     60:
1.20      joris      61:        while ((ch = rcs_getopt(argc, argv, "f::l::p::qr::u::V")) != -1) {
1.1       joris      62:                switch (ch) {
1.18      joris      63:                case 'f':
1.19      joris      64:                        rcs_set_rev(rcs_optarg, &rev);
1.18      joris      65:                        fflag = 1;
                     66:                        break;
1.4       joris      67:                case 'l':
1.19      joris      68:                        rcs_set_rev(rcs_optarg, &rev);
1.4       joris      69:                        lock = LOCK_LOCK;
                     70:                        break;
1.20      joris      71:                case 'p':
                     72:                        rcs_set_rev(rcs_optarg, &rev);
                     73:                        pipeout = 1;
                     74:                        break;
1.3       joris      75:                case 'q':
                     76:                        verbose = 0;
                     77:                        break;
1.1       joris      78:                case 'r':
1.19      joris      79:                        rcs_set_rev(rcs_optarg, &rev);
1.4       joris      80:                        break;
                     81:                case 'u':
1.19      joris      82:                        rcs_set_rev(rcs_optarg, &rev);
1.8       niallo     83:                        lock = LOCK_UNLOCK;
1.1       joris      84:                        break;
1.7       joris      85:                case 'V':
                     86:                        printf("%s\n", rcs_version);
                     87:                        exit(0);
1.1       joris      88:                default:
                     89:                        (usage)();
                     90:                        exit(1);
                     91:                }
                     92:        }
                     93:
1.13      joris      94:        argc -= rcs_optind;
                     95:        argv += rcs_optind;
1.1       joris      96:
                     97:        if (argc == 0) {
                     98:                cvs_log(LP_ERR, "no input file");
                     99:                (usage)();
                    100:                exit (1);
                    101:        }
1.11      deraadt   102:
1.1       joris     103:        for (i = 0; i < argc; i++) {
                    104:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    105:                        continue;
                    106:
1.21      niallo    107:                if (verbose == 1)
1.22    ! xsa       108:                        printf("%s  -->  %s\n", fpath,
        !           109:                            (pipeout == 1) ? "standard output" : argv[i]);
1.21      niallo    110:
1.4       joris     111:                if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1       joris     112:                        continue;
                    113:
1.4       joris     114:                if (rev == RCS_HEAD_REV)
                    115:                        frev = file->rf_head;
                    116:                else
                    117:                        frev = rev;
1.17      joris     118:
1.4       joris     119:                rcsnum_tostr(frev, buf, sizeof(buf));
1.17      joris     120:
1.18      joris     121:                if (checkout_rev(file, frev, argv[i], lock, username, fflag) < 0) {
1.8       niallo    122:                        rcs_close(file);
                    123:                        continue;
                    124:                }
1.17      joris     125:
1.1       joris     126:                rcs_close(file);
                    127:        }
                    128:
                    129:        if (rev != RCS_HEAD_REV)
1.5       joris     130:                rcsnum_free(frev);
1.1       joris     131:
                    132:        return (0);
                    133: }
                    134:
                    135: void
                    136: checkout_usage(void)
                    137: {
1.11      deraadt   138:        fprintf(stderr,
1.16      niallo    139:            "usage: co [-qV] [-l[rev]] [-r[rev]] [-u[rev]] file ...\n");
1.1       joris     140: }
1.14      niallo    141:
                    142: /*
                    143:  * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
                    144:  * <lkmode> is either LOCK_LOCK or LOCK_UNLOCK or something else
                    145:  * (which has no effect).
                    146:  * In the case of LOCK_LOCK, a lock is set for <username> if it is not NULL.
                    147:  * In the case of LOCK_UNLOCK, all locks are removed for that revision.
                    148:  *
                    149:  * Returns 0 on success, -1 on failure.
                    150:  */
                    151: int
                    152: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int lkmode,
1.18      joris     153:     const char *username, int force)
1.14      niallo    154: {
1.18      joris     155:        char buf[16], yn;
1.14      niallo    156:        mode_t mode = 0444;
                    157:        BUF *bp;
1.18      joris     158:        struct stat st;
1.20      joris     159:        char *content;
1.14      niallo    160:
                    161:        /*
1.15      niallo    162:         * Check out the latest revision if <frev> is greater than HEAD
1.14      niallo    163:         */
1.15      niallo    164:        if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
                    165:                frev = file->rf_head;
                    166:
                    167:        rcsnum_tostr(frev, buf, sizeof(buf));
                    168:
1.18      joris     169:        if (verbose == 1)
                    170:                printf("revision %s", buf);
                    171:
1.14      niallo    172:        if ((bp = rcs_getrev(file, frev)) == NULL) {
                    173:                cvs_log(LP_ERR, "cannot find revision `%s'", buf);
                    174:                return (-1);
                    175:        }
                    176:
                    177:        if (lkmode == LOCK_LOCK) {
                    178:                if ((username != NULL)
                    179:                    && (rcs_lock_add(file, username, frev) < 0)) {
1.21      niallo    180:                        if ((rcs_errno != RCS_ERR_DUPENT) && (verbose == 1))
1.14      niallo    181:                                cvs_log(LP_ERR, "failed to lock '%s'", buf);
                    182:                }
1.18      joris     183:
1.14      niallo    184:                mode = 0644;
1.18      joris     185:                if (verbose == 1)
                    186:                        printf(" (locked)");
1.14      niallo    187:        } else if (lkmode == LOCK_UNLOCK) {
                    188:                if (rcs_lock_remove(file, frev) < 0) {
                    189:                        if (rcs_errno != RCS_ERR_NOENT)
                    190:                                cvs_log(LP_ERR,
                    191:                                    "failed to remove lock '%s'", buf);
                    192:                }
1.18      joris     193:
1.14      niallo    194:                mode = 0444;
1.18      joris     195:                if (verbose == 1)
                    196:                        printf(" (unlocked)");
                    197:        }
                    198:
                    199:        if (verbose == 1)
                    200:                printf("\n");
                    201:
                    202:        if ((stat(dst, &st) != -1) && force == 0) {
                    203:                if (st.st_mode & S_IWUSR) {
                    204:                        yn = 0;
1.21      niallo    205:                        if (verbose == 0) {
                    206:                                cvs_log(LP_ERR,
                    207:                                    "writeable %s exists; checkout aborted",
                    208:                                    dst);
                    209:                                return (-1);
                    210:                        }
1.18      joris     211:                        while (yn != 'y' && yn != 'n') {
1.21      niallo    212:                                printf("writeable %s exists; ", dst);
                    213:                                printf("remove it? [ny](n): ");
1.18      joris     214:                                fflush(stdout);
                    215:                                yn = getchar();
                    216:                        }
                    217:
                    218:                        if (yn == 'n') {
1.21      niallo    219:                                cvs_log(LP_ERR, "checkout aborted");
1.18      joris     220:                                return (-1);
                    221:                        }
                    222:                }
1.14      niallo    223:        }
1.17      joris     224:
1.20      joris     225:        if (pipeout == 1) {
                    226:                cvs_buf_putc(bp, '\0');
                    227:                content = cvs_buf_release(bp);
                    228:                printf("%s", content);
                    229:                free(content);
                    230:        } else {
                    231:                if (cvs_buf_write(bp, dst, mode) < 0) {
                    232:                        cvs_log(LP_ERR, "failed to write revision to file");
                    233:                        cvs_buf_free(bp);
                    234:                        return (-1);
                    235:                }
1.14      niallo    236:                cvs_buf_free(bp);
1.20      joris     237:
                    238:                if (verbose == 1)
                    239:                        printf("done\n");
1.14      niallo    240:        }
1.17      joris     241:
1.14      niallo    242:        return (0);
                    243: }
                    244: