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

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