Annotation of src/usr.bin/rcs/co.c, Revision 1.20
1.20 ! joris 1: /* $OpenBSD: co.c,v 1.19 2005/10/18 01:22:14 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.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.4 joris 107: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1 joris 108: continue;
109:
1.4 joris 110: if (rev == RCS_HEAD_REV)
111: frev = file->rf_head;
112: else
113: frev = rev;
1.17 joris 114:
1.4 joris 115: rcsnum_tostr(frev, buf, sizeof(buf));
1.17 joris 116:
1.18 joris 117: if (checkout_rev(file, frev, argv[i], lock, username, fflag) < 0) {
1.8 niallo 118: rcs_close(file);
119: continue;
120: }
1.17 joris 121:
1.1 joris 122: rcs_close(file);
123: }
124:
125: if (rev != RCS_HEAD_REV)
1.5 joris 126: rcsnum_free(frev);
1.1 joris 127:
128: return (0);
129: }
130:
131: void
132: checkout_usage(void)
133: {
1.11 deraadt 134: fprintf(stderr,
1.16 niallo 135: "usage: co [-qV] [-l[rev]] [-r[rev]] [-u[rev]] file ...\n");
1.1 joris 136: }
1.14 niallo 137:
138: /*
139: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
140: * <lkmode> is either LOCK_LOCK or LOCK_UNLOCK or something else
141: * (which has no effect).
142: * In the case of LOCK_LOCK, a lock is set for <username> if it is not NULL.
143: * In the case of LOCK_UNLOCK, all locks are removed for that revision.
144: *
145: * Returns 0 on success, -1 on failure.
146: */
147: int
148: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int lkmode,
1.18 joris 149: const char *username, int force)
1.14 niallo 150: {
1.18 joris 151: char buf[16], yn;
1.14 niallo 152: mode_t mode = 0444;
153: BUF *bp;
1.18 joris 154: struct stat st;
1.20 ! joris 155: char *content;
1.14 niallo 156:
157: /*
1.15 niallo 158: * Check out the latest revision if <frev> is greater than HEAD
1.14 niallo 159: */
1.15 niallo 160: if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
161: frev = file->rf_head;
162:
163: rcsnum_tostr(frev, buf, sizeof(buf));
164:
1.18 joris 165: if (verbose == 1)
166: printf("revision %s", buf);
167:
1.14 niallo 168: if ((bp = rcs_getrev(file, frev)) == NULL) {
169: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
170: return (-1);
171: }
172:
173: if (lkmode == LOCK_LOCK) {
174: if ((username != NULL)
175: && (rcs_lock_add(file, username, frev) < 0)) {
176: if (rcs_errno != RCS_ERR_DUPENT)
177: cvs_log(LP_ERR, "failed to lock '%s'", buf);
178: else
179: cvs_log(LP_WARN, "you already have a lock");
180: }
1.18 joris 181:
1.14 niallo 182: mode = 0644;
1.18 joris 183: if (verbose == 1)
184: printf(" (locked)");
1.14 niallo 185: } else if (lkmode == LOCK_UNLOCK) {
186: if (rcs_lock_remove(file, frev) < 0) {
187: if (rcs_errno != RCS_ERR_NOENT)
188: cvs_log(LP_ERR,
189: "failed to remove lock '%s'", buf);
190: }
1.18 joris 191:
1.14 niallo 192: mode = 0444;
1.18 joris 193: if (verbose == 1)
194: printf(" (unlocked)");
195: }
196:
197: if (verbose == 1)
198: printf("\n");
199:
200: if ((stat(dst, &st) != -1) && force == 0) {
201: if (st.st_mode & S_IWUSR) {
202: yn = 0;
203: while (yn != 'y' && yn != 'n') {
204: printf("writeable '%s' exists; ", dst);
205: printf("remove it? [ny] (n):");
206: fflush(stdout);
207: yn = getchar();
208: }
209:
210: if (yn == 'n') {
211: if (verbose == 1)
212: cvs_log(LP_ERR, "checkout aborted");
213: return (-1);
214: }
215: }
1.14 niallo 216: }
1.17 joris 217:
1.20 ! joris 218: if (pipeout == 1) {
! 219: cvs_buf_putc(bp, '\0');
! 220: content = cvs_buf_release(bp);
! 221: printf("%s", content);
! 222: free(content);
! 223: } else {
! 224: if (cvs_buf_write(bp, dst, mode) < 0) {
! 225: cvs_log(LP_ERR, "failed to write revision to file");
! 226: cvs_buf_free(bp);
! 227: return (-1);
! 228: }
1.14 niallo 229: cvs_buf_free(bp);
1.20 ! joris 230:
! 231: if (verbose == 1)
! 232: printf("done\n");
1.14 niallo 233: }
1.17 joris 234:
1.14 niallo 235: return (0);
236: }
237: