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

1.19    ! joris       1: /*     $OpenBSD: co.c,v 1.18 2005/10/17 15:33:12 joris 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.18      joris      61:        while ((ch = rcs_getopt(argc, argv, "f::l::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.3       joris      71:                case 'q':
                     72:                        verbose = 0;
                     73:                        break;
1.1       joris      74:                case 'r':
1.19    ! joris      75:                        rcs_set_rev(rcs_optarg, &rev);
1.4       joris      76:                        break;
                     77:                case 'u':
1.19    ! joris      78:                        rcs_set_rev(rcs_optarg, &rev);
1.8       niallo     79:                        lock = LOCK_UNLOCK;
1.1       joris      80:                        break;
1.7       joris      81:                case 'V':
                     82:                        printf("%s\n", rcs_version);
                     83:                        exit(0);
1.1       joris      84:                default:
                     85:                        (usage)();
                     86:                        exit(1);
                     87:                }
                     88:        }
                     89:
1.13      joris      90:        argc -= rcs_optind;
                     91:        argv += rcs_optind;
1.1       joris      92:
                     93:        if (argc == 0) {
                     94:                cvs_log(LP_ERR, "no input file");
                     95:                (usage)();
                     96:                exit (1);
                     97:        }
1.11      deraadt    98:
1.1       joris      99:        for (i = 0; i < argc; i++) {
                    100:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    101:                        continue;
                    102:
1.4       joris     103:                if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1       joris     104:                        continue;
                    105:
1.4       joris     106:                if (rev == RCS_HEAD_REV)
                    107:                        frev = file->rf_head;
                    108:                else
                    109:                        frev = rev;
1.17      joris     110:
1.4       joris     111:                rcsnum_tostr(frev, buf, sizeof(buf));
1.17      joris     112:
1.18      joris     113:                if (checkout_rev(file, frev, argv[i], lock, username, fflag) < 0) {
1.8       niallo    114:                        rcs_close(file);
                    115:                        continue;
                    116:                }
1.17      joris     117:
1.1       joris     118:                rcs_close(file);
                    119:        }
                    120:
                    121:        if (rev != RCS_HEAD_REV)
1.5       joris     122:                rcsnum_free(frev);
1.1       joris     123:
                    124:        return (0);
                    125: }
                    126:
                    127: void
                    128: checkout_usage(void)
                    129: {
1.11      deraadt   130:        fprintf(stderr,
1.16      niallo    131:            "usage: co [-qV] [-l[rev]] [-r[rev]] [-u[rev]] file ...\n");
1.1       joris     132: }
1.14      niallo    133:
                    134: /*
                    135:  * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
                    136:  * <lkmode> is either LOCK_LOCK or LOCK_UNLOCK or something else
                    137:  * (which has no effect).
                    138:  * In the case of LOCK_LOCK, a lock is set for <username> if it is not NULL.
                    139:  * In the case of LOCK_UNLOCK, all locks are removed for that revision.
                    140:  *
                    141:  * Returns 0 on success, -1 on failure.
                    142:  */
                    143: int
                    144: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int lkmode,
1.18      joris     145:     const char *username, int force)
1.14      niallo    146: {
1.18      joris     147:        char buf[16], yn;
1.14      niallo    148:        mode_t mode = 0444;
                    149:        BUF *bp;
1.18      joris     150:        struct stat st;
1.14      niallo    151:
                    152:        /*
1.15      niallo    153:         * Check out the latest revision if <frev> is greater than HEAD
1.14      niallo    154:         */
1.15      niallo    155:        if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
                    156:                frev = file->rf_head;
                    157:
                    158:        rcsnum_tostr(frev, buf, sizeof(buf));
                    159:
1.18      joris     160:        if (verbose == 1)
                    161:                printf("revision %s", buf);
                    162:
1.14      niallo    163:        if ((bp = rcs_getrev(file, frev)) == NULL) {
                    164:                cvs_log(LP_ERR, "cannot find revision `%s'", buf);
                    165:                return (-1);
                    166:        }
                    167:
                    168:        if (lkmode == LOCK_LOCK) {
                    169:                if ((username != NULL)
                    170:                    && (rcs_lock_add(file, username, frev) < 0)) {
                    171:                        if (rcs_errno != RCS_ERR_DUPENT)
                    172:                                cvs_log(LP_ERR, "failed to lock '%s'", buf);
                    173:                        else
                    174:                                cvs_log(LP_WARN, "you already have a lock");
                    175:                }
1.18      joris     176:
1.14      niallo    177:                mode = 0644;
1.18      joris     178:                if (verbose == 1)
                    179:                        printf(" (locked)");
1.14      niallo    180:        } else if (lkmode == LOCK_UNLOCK) {
                    181:                if (rcs_lock_remove(file, frev) < 0) {
                    182:                        if (rcs_errno != RCS_ERR_NOENT)
                    183:                                cvs_log(LP_ERR,
                    184:                                    "failed to remove lock '%s'", buf);
                    185:                }
1.18      joris     186:
1.14      niallo    187:                mode = 0444;
1.18      joris     188:                if (verbose == 1)
                    189:                        printf(" (unlocked)");
                    190:        }
                    191:
                    192:        if (verbose == 1)
                    193:                printf("\n");
                    194:
                    195:        if ((stat(dst, &st) != -1) && force == 0) {
                    196:                if (st.st_mode & S_IWUSR) {
                    197:                        yn = 0;
                    198:                        while (yn != 'y' && yn != 'n') {
                    199:                                printf("writeable '%s' exists; ", dst);
                    200:                                printf("remove it? [ny] (n):");
                    201:                                fflush(stdout);
                    202:                                yn = getchar();
                    203:                        }
                    204:
                    205:                        if (yn == 'n') {
                    206:                                if (verbose == 1)
                    207:                                        cvs_log(LP_ERR, "checkout aborted");
                    208:                                return (-1);
                    209:                        }
                    210:                }
1.14      niallo    211:        }
1.17      joris     212:
1.14      niallo    213:        if (cvs_buf_write(bp, dst, mode) < 0) {
                    214:                cvs_log(LP_ERR, "failed to write revision to file");
                    215:                cvs_buf_free(bp);
                    216:                return (-1);
                    217:        }
1.17      joris     218:
1.14      niallo    219:        cvs_buf_free(bp);
1.17      joris     220:
1.18      joris     221:        if (verbose == 1)
                    222:                printf("done\n");
1.17      joris     223:
1.14      niallo    224:        return (0);
                    225: }
                    226: