Annotation of src/usr.bin/rcs/co.c, Revision 1.13
1.13 ! joris 1: /* $OpenBSD: co.c,v 1.12 2005/10/12 17:43:18 xsa 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;
49: BUF *bp;
50: char buf[16];
1.4 joris 51: char fpath[MAXPATHLEN];
52: char *username;
1.8 niallo 53: mode_t mode = 0444;
1.1 joris 54:
1.4 joris 55: lock = 0;
1.1 joris 56: rev = RCS_HEAD_REV;
1.6 joris 57: frev = NULL;
1.4 joris 58:
59: if ((username = getlogin()) == NULL) {
1.10 xsa 60: cvs_log(LP_ERRNO, "failed to get username");
1.4 joris 61: exit (1);
62: }
63:
1.13 ! joris 64: while ((ch = rcs_getopt(argc, argv, "l::qr::u::V")) != -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");
1.13 ! joris 70: if (rcs_optarg != NULL) {
! 71: if ((rev = rcsnum_parse(rcs_optarg)) == NULL) {
1.8 niallo 72: cvs_log(LP_ERR, "bad revision number");
73: exit (1);
74: }
1.4 joris 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");
1.13 ! joris 85: if (rcs_optarg != NULL) {
! 86: if ((rev = rcsnum_parse(rcs_optarg)) == NULL) {
1.8 niallo 87: cvs_log(LP_ERR, "bad revision number");
88: exit (1);
89: }
1.1 joris 90: }
1.4 joris 91: break;
92: case 'u':
93: if (rev != RCS_HEAD_REV)
94: cvs_log(LP_WARN,
95: "redefinition of revision number");
1.13 ! joris 96: if (rcs_optarg != NULL) {
! 97: if ((rev = rcsnum_parse(rcs_optarg)) == NULL) {
1.8 niallo 98: cvs_log(LP_ERR, "bad revision number");
99: exit (1);
100: }
1.4 joris 101: }
1.8 niallo 102: lock = LOCK_UNLOCK;
1.1 joris 103: break;
1.7 joris 104: case 'V':
105: printf("%s\n", rcs_version);
106: exit(0);
1.1 joris 107: default:
108: (usage)();
109: exit(1);
110: }
111: }
112:
1.13 ! joris 113: argc -= rcs_optind;
! 114: argv += rcs_optind;
1.1 joris 115:
116: if (argc == 0) {
117: cvs_log(LP_ERR, "no input file");
118: (usage)();
119: exit (1);
120: }
1.11 deraadt 121:
1.1 joris 122: for (i = 0; i < argc; i++) {
123: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
124: continue;
125:
1.4 joris 126: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1 joris 127: continue;
128:
1.4 joris 129: if (rev == RCS_HEAD_REV)
130: frev = file->rf_head;
131: else
132: frev = rev;
1.1 joris 133:
1.4 joris 134: rcsnum_tostr(frev, buf, sizeof(buf));
135:
136: if ((bp = rcs_getrev(file, frev)) == NULL) {
137: cvs_log(LP_ERR, "cannot find '%s' in %s", buf, fpath);
1.1 joris 138: rcs_close(file);
139: continue;
140: }
141:
1.4 joris 142: if (lock == LOCK_LOCK) {
143: if (rcs_lock_add(file, username, frev) < 0) {
144: if (rcs_errno != RCS_ERR_DUPENT)
145: cvs_log(LP_ERR, "failed to lock '%s'", buf);
146: else
147: cvs_log(LP_WARN, "you already have a lock");
148: }
1.8 niallo 149: mode = 0644;
150: } else if (lock == LOCK_UNLOCK) {
1.4 joris 151: if (rcs_lock_remove(file, frev) < 0) {
152: if (rcs_errno != RCS_ERR_NOENT)
153: cvs_log(LP_ERR,
154: "failed to remove lock '%s'", buf);
155: }
1.8 niallo 156: mode = 0444;
1.4 joris 157: }
1.8 niallo 158: if (cvs_buf_write(bp, argv[i], mode) < 0) {
159: cvs_log(LP_ERR, "failed to write revision to file");
160: cvs_buf_free(bp);
161: rcs_close(file);
162: continue;
163: }
164:
165: cvs_buf_free(bp);
1.4 joris 166:
1.1 joris 167: rcs_close(file);
1.12 xsa 168: if (verbose == 1) {
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.11 deraadt 188: fprintf(stderr,
189: "usage: co [-qV] [-l [rev]] [-r [rev]] [-u [rev]] file ...\n");
1.1 joris 190: }