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