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