Annotation of src/usr.bin/rcs/co.c, Revision 1.14
1.14 ! niallo 1: /* $OpenBSD: co.c,v 1.13 2005/10/13 12:35:30 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.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;
130: rcsnum_tostr(frev, buf, sizeof(buf));
1.14 ! niallo 131: if (checkout_rev(file, frev, argv[i], lock, username) < 0) {
1.8 niallo 132: rcs_close(file);
133: continue;
134: }
1.1 joris 135: rcs_close(file);
1.12 xsa 136: if (verbose == 1) {
1.6 joris 137: printf("revision %s ", buf);
138: if (lock == LOCK_LOCK)
139: printf("(locked)");
140: else if (lock == LOCK_UNLOCK)
141: printf("(unlocked)");
142: printf("\n");
1.4 joris 143: printf("done\n");
144: }
1.1 joris 145: }
146:
147: if (rev != RCS_HEAD_REV)
1.5 joris 148: rcsnum_free(frev);
1.1 joris 149:
150: return (0);
151: }
152:
153: void
154: checkout_usage(void)
155: {
1.11 deraadt 156: fprintf(stderr,
157: "usage: co [-qV] [-l [rev]] [-r [rev]] [-u [rev]] file ...\n");
1.1 joris 158: }
1.14 ! niallo 159:
! 160: /*
! 161: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
! 162: * <lkmode> is either LOCK_LOCK or LOCK_UNLOCK or something else
! 163: * (which has no effect).
! 164: * In the case of LOCK_LOCK, a lock is set for <username> if it is not NULL.
! 165: * In the case of LOCK_UNLOCK, all locks are removed for that revision.
! 166: *
! 167: * Returns 0 on success, -1 on failure.
! 168: */
! 169: int
! 170: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int lkmode,
! 171: const char *username)
! 172: {
! 173: char buf[16];
! 174: mode_t mode = 0444;
! 175: BUF *bp;
! 176: rcsnum_tostr(frev, buf, sizeof(buf));
! 177:
! 178: /*
! 179: * XXX: GNU RCS will check out the latest revision if <frev> is
! 180: * greater than HEAD
! 181: */
! 182: if ((bp = rcs_getrev(file, frev)) == NULL) {
! 183: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
! 184: return (-1);
! 185: }
! 186:
! 187: if (lkmode == LOCK_LOCK) {
! 188: if ((username != NULL)
! 189: && (rcs_lock_add(file, username, frev) < 0)) {
! 190: if (rcs_errno != RCS_ERR_DUPENT)
! 191: cvs_log(LP_ERR, "failed to lock '%s'", buf);
! 192: else
! 193: cvs_log(LP_WARN, "you already have a lock");
! 194: }
! 195: mode = 0644;
! 196: } else if (lkmode == LOCK_UNLOCK) {
! 197: if (rcs_lock_remove(file, frev) < 0) {
! 198: if (rcs_errno != RCS_ERR_NOENT)
! 199: cvs_log(LP_ERR,
! 200: "failed to remove lock '%s'", buf);
! 201: }
! 202: mode = 0444;
! 203: }
! 204: if (cvs_buf_write(bp, dst, mode) < 0) {
! 205: cvs_log(LP_ERR, "failed to write revision to file");
! 206: cvs_buf_free(bp);
! 207: return (-1);
! 208: }
! 209: cvs_buf_free(bp);
! 210: return (0);
! 211: }
! 212: