Annotation of src/usr.bin/rcs/co.c, Revision 1.5
1.5 ! joris 1: /* $OpenBSD: co.c,v 1.4 2005/10/04 23:04:33 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:
39: extern char *__progname;
40:
1.4 joris 41: #define LOCK_LOCK 1
42: #define LOCK_UNLOCK 2
43:
1.1 joris 44: int
45: checkout_main(int argc, char **argv)
46: {
47: int i, ch;
1.4 joris 48: int lock;
49: RCSNUM *frev, *rev;
1.1 joris 50: RCSFILE *file;
51: BUF *bp;
52: char buf[16];
1.4 joris 53: char fpath[MAXPATHLEN];
54: char *username;
1.1 joris 55:
1.4 joris 56: lock = 0;
1.1 joris 57: rev = RCS_HEAD_REV;
1.4 joris 58:
59: if ((username = getlogin()) == NULL) {
60: cvs_log(LP_ERR, "failed to get username");
61: exit (1);
62: }
63:
64: while ((ch = getopt(argc, argv, "l:qr:u:")) != -1) {
1.1 joris 65: switch (ch) {
1.4 joris 66: case 'l':
67: if (rev != RCS_HEAD_REV)
68: cvs_log(LP_WARN,
69: "redefinition of revision number");
70:
71: if ((rev = rcsnum_parse(optarg)) == NULL) {
72: cvs_log(LP_ERR, "bad revision number");
73: exit (1);
74: }
75:
76: lock = LOCK_LOCK;
77: break;
1.3 joris 78: case 'q':
79: verbose = 0;
80: break;
1.1 joris 81: case 'r':
1.4 joris 82: if (rev != RCS_HEAD_REV)
83: cvs_log(LP_WARN,
84: "redefinition of revision number");
85:
1.1 joris 86: if ((rev = rcsnum_parse(optarg)) == NULL) {
87: cvs_log(LP_ERR, "bad revision number");
88: exit(1);
89: }
1.4 joris 90:
91: break;
92: case 'u':
93: lock = LOCK_UNLOCK;
94: if (rev != RCS_HEAD_REV)
95: cvs_log(LP_WARN,
96: "redefinition of revision number");
97:
98: if ((rev = rcsnum_parse(optarg)) == NULL) {
99: cvs_log(LP_ERR, "bad revision number");
100: exit (1);
101: }
102:
1.1 joris 103: break;
104: default:
105: (usage)();
106: exit(1);
107: }
108: }
109:
110: argc -= optind;
111: argv += optind;
112:
113: if (argc == 0) {
114: cvs_log(LP_ERR, "no input file");
115: (usage)();
116: exit (1);
117: }
118:
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.1 joris 130:
1.4 joris 131: rcsnum_tostr(frev, buf, sizeof(buf));
132:
133: if ((bp = rcs_getrev(file, frev)) == NULL) {
134: cvs_log(LP_ERR, "cannot find '%s' in %s", buf, fpath);
1.1 joris 135: rcs_close(file);
136: continue;
137: }
138:
1.4 joris 139: if (cvs_buf_write(bp, argv[i], 0644) < 0) {
1.1 joris 140: cvs_log(LP_ERR, "failed to write revision to file");
1.4 joris 141: cvs_buf_free(bp);
142: rcs_close(file);
143: continue;
144: }
1.1 joris 145:
146: cvs_buf_free(bp);
1.4 joris 147:
148: if (lock == LOCK_LOCK) {
149: if (rcs_lock_add(file, username, frev) < 0) {
150: if (rcs_errno != RCS_ERR_DUPENT)
151: cvs_log(LP_ERR, "failed to lock '%s'", buf);
152: else
153: cvs_log(LP_WARN, "you already have a lock");
154: }
155: } else if (lock == LOCK_UNLOCK) {
156: if (rcs_lock_remove(file, frev) < 0) {
157: if (rcs_errno != RCS_ERR_NOENT)
158: cvs_log(LP_ERR,
159: "failed to remove lock '%s'", buf);
160: }
161: }
162:
1.1 joris 163: rcs_close(file);
1.4 joris 164: if (verbose) {
165: printf("revision %s (%s)\n", buf, (lock == LOCK_LOCK) ?
166: "locked" : "unlocked");
167: printf("done\n");
168: }
1.1 joris 169: }
170:
171: if (rev != RCS_HEAD_REV)
1.5 ! joris 172: rcsnum_free(frev);
1.1 joris 173:
174: return (0);
175: }
176:
177: void
178: checkout_usage(void)
179: {
1.2 joris 180: fprintf(stderr, "usage %s [-r rev] file ...\n", __progname);
1.1 joris 181: }