Annotation of src/usr.bin/rcs/co.c, Revision 1.7
1.7 ! joris 1: /* $OpenBSD: co.c,v 1.6 2005/10/05 11:52:16 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.6 joris 58: frev = NULL;
1.4 joris 59:
60: if ((username = getlogin()) == NULL) {
61: cvs_log(LP_ERR, "failed to get username");
62: exit (1);
63: }
64:
1.7 ! joris 65: while ((ch = getopt(argc, argv, "l:qr:u:V")) != -1) {
1.1 joris 66: switch (ch) {
1.4 joris 67: case 'l':
68: if (rev != RCS_HEAD_REV)
69: cvs_log(LP_WARN,
70: "redefinition of revision number");
71:
72: if ((rev = rcsnum_parse(optarg)) == NULL) {
73: cvs_log(LP_ERR, "bad revision number");
74: exit (1);
75: }
76:
77: lock = LOCK_LOCK;
78: break;
1.3 joris 79: case 'q':
80: verbose = 0;
81: break;
1.1 joris 82: case 'r':
1.4 joris 83: if (rev != RCS_HEAD_REV)
84: cvs_log(LP_WARN,
85: "redefinition of revision number");
86:
1.1 joris 87: if ((rev = rcsnum_parse(optarg)) == NULL) {
88: cvs_log(LP_ERR, "bad revision number");
89: exit(1);
90: }
1.4 joris 91:
92: break;
93: case 'u':
94: lock = LOCK_UNLOCK;
95: if (rev != RCS_HEAD_REV)
96: cvs_log(LP_WARN,
97: "redefinition of revision number");
98:
99: if ((rev = rcsnum_parse(optarg)) == NULL) {
100: cvs_log(LP_ERR, "bad revision number");
101: exit (1);
102: }
103:
1.1 joris 104: break;
1.7 ! joris 105: case 'V':
! 106: printf("%s\n", rcs_version);
! 107: exit(0);
1.1 joris 108: default:
109: (usage)();
110: exit(1);
111: }
112: }
113:
114: argc -= optind;
115: argv += optind;
116:
117: if (argc == 0) {
118: cvs_log(LP_ERR, "no input file");
119: (usage)();
120: exit (1);
121: }
122:
123: for (i = 0; i < argc; i++) {
124: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
125: continue;
126:
1.4 joris 127: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1 joris 128: continue;
129:
1.4 joris 130: if (rev == RCS_HEAD_REV)
131: frev = file->rf_head;
132: else
133: frev = rev;
1.1 joris 134:
1.4 joris 135: rcsnum_tostr(frev, buf, sizeof(buf));
136:
137: if ((bp = rcs_getrev(file, frev)) == NULL) {
138: cvs_log(LP_ERR, "cannot find '%s' in %s", buf, fpath);
1.1 joris 139: rcs_close(file);
140: continue;
141: }
142:
1.4 joris 143: if (cvs_buf_write(bp, argv[i], 0644) < 0) {
1.1 joris 144: cvs_log(LP_ERR, "failed to write revision to file");
1.4 joris 145: cvs_buf_free(bp);
146: rcs_close(file);
147: continue;
148: }
1.1 joris 149:
150: cvs_buf_free(bp);
1.4 joris 151:
152: if (lock == LOCK_LOCK) {
153: if (rcs_lock_add(file, username, frev) < 0) {
154: if (rcs_errno != RCS_ERR_DUPENT)
155: cvs_log(LP_ERR, "failed to lock '%s'", buf);
156: else
157: cvs_log(LP_WARN, "you already have a lock");
158: }
159: } else if (lock == LOCK_UNLOCK) {
160: if (rcs_lock_remove(file, frev) < 0) {
161: if (rcs_errno != RCS_ERR_NOENT)
162: cvs_log(LP_ERR,
163: "failed to remove lock '%s'", buf);
164: }
165: }
166:
1.1 joris 167: rcs_close(file);
1.4 joris 168: if (verbose) {
1.6 joris 169: printf("revision %s ", buf);
170: if (lock == LOCK_LOCK)
171: printf("(locked)");
172: else if (lock == LOCK_UNLOCK)
173: printf("(unlocked)");
174: printf("\n");
1.4 joris 175: printf("done\n");
176: }
1.1 joris 177: }
178:
179: if (rev != RCS_HEAD_REV)
1.5 joris 180: rcsnum_free(frev);
1.1 joris 181:
182: return (0);
183: }
184:
185: void
186: checkout_usage(void)
187: {
1.7 ! joris 188: fprintf(stderr, "usage %s [-qV] [-l rev] [-r rev] [-u rev] file ...\n",
! 189: __progname);
1.1 joris 190: }