Annotation of src/usr.bin/cvs/status.c, Revision 1.38
1.38 ! xsa 1: /* $OpenBSD: status.c,v 1.37 2005/07/08 11:46:23 xsa Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.4 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.4 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.4 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.4 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 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
1.4 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28: #include <sys/stat.h>
29:
30: #include <errno.h>
1.23 xsa 31: #include <fcntl.h>
1.1 jfb 32: #include <stdio.h>
33: #include <stdlib.h>
1.23 xsa 34: #include <string.h>
1.1 jfb 35: #include <unistd.h>
36:
37: #include "cvs.h"
38: #include "log.h"
39: #include "proto.h"
40:
41:
1.15 jfb 42: #define CVS_STATUS_SEP \
43: "==================================================================="
44:
1.31 xsa 45: /* Keep this sorted as it is now. See file.h for status values. */
1.1 jfb 46: const char *cvs_statstr[] = {
47: "Unknown",
1.15 jfb 48: "Up-to-date",
1.1 jfb 49: "Locally Modified",
1.29 xsa 50: "Locally Added",
51: "Locally Removed",
52: "Unresolved Conflict",
1.1 jfb 53: "Patched",
1.16 jfb 54: "Needs Checkout",
1.1 jfb 55: };
56:
1.33 xsa 57: extern int verbosity;
58:
1.1 jfb 59:
1.22 jfb 60: static int cvs_status_init (struct cvs_cmd *, int, char **, int *);
61: static int cvs_status_remote (CVSFILE *, void *);
62: static int cvs_status_local (CVSFILE *, void *);
63: static int cvs_status_pre_exec (struct cvsroot *);
64:
65: struct cvs_cmd cvs_cmd_status = {
66: CVS_OP_STATUS, CVS_REQ_STATUS, "status",
67: { "st", "stat" },
68: "Display status information on checked out files",
69: "[-lRv]",
70: "lRv",
71: NULL,
72: CF_SORT | CF_IGNORE | CF_RECURSE,
73: cvs_status_init,
74: cvs_status_pre_exec,
1.20 jfb 75: cvs_status_remote,
1.22 jfb 76: cvs_status_local,
77: NULL,
78: NULL,
1.10 joris 79: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
80: };
1.1 jfb 81:
1.10 joris 82: static int verbose = 0;
1.1 jfb 83:
1.22 jfb 84: static int
85: cvs_status_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 86: {
1.10 joris 87: int ch;
1.1 jfb 88:
1.22 jfb 89: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 90: switch (ch) {
1.7 jfb 91: case 'l':
1.22 jfb 92: cmd->file_flags &= ~CF_RECURSE;
1.7 jfb 93: break;
94: case 'R':
1.22 jfb 95: cmd->file_flags |= CF_RECURSE;
1.7 jfb 96: break;
97: case 'v':
98: verbose = 1;
99: break;
1.1 jfb 100: default:
1.14 joris 101: return (CVS_EX_USAGE);
1.1 jfb 102: }
103: }
104:
1.10 joris 105: *arg = optind;
106: return (0);
107: }
1.1 jfb 108:
1.22 jfb 109: static int
110: cvs_status_pre_exec(struct cvsroot *root)
1.10 joris 111: {
1.26 xsa 112: if (root->cr_method != CVS_METHOD_LOCAL) {
113: if (verbose && (cvs_sendarg(root, "-v", 0) < 0))
114: return (CVS_EX_PROTO);
115: }
116:
1.1 jfb 117: return (0);
118: }
119:
120: /*
1.20 jfb 121: * cvs_status_remote()
1.1 jfb 122: *
123: * Get the status of a single file.
124: */
1.22 jfb 125: static int
1.20 jfb 126: cvs_status_remote(CVSFILE *cfp, void *arg)
1.1 jfb 127: {
1.5 jfb 128: int ret;
1.15 jfb 129: char *repo, fpath[MAXPATHLEN];
1.1 jfb 130: RCSFILE *rf;
131: struct cvsroot *root;
132:
1.5 jfb 133: ret = 0;
1.1 jfb 134: rf = NULL;
1.5 jfb 135: root = CVS_DIR_ROOT(cfp);
136: repo = CVS_DIR_REPO(cfp);
1.1 jfb 137:
1.6 jfb 138: if (cfp->cf_type == DT_DIR) {
1.15 jfb 139: if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
140: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
141: CVS_FILE_NAME(cfp));
142: else
143: ret = cvs_senddir(root, cfp);
1.21 joris 144:
145: if (ret == -1)
146: ret = CVS_EX_PROTO;
147:
1.6 jfb 148: return (ret);
149: }
1.1 jfb 150:
1.5 jfb 151: cvs_file_getpath(cfp, fpath, sizeof(fpath));
1.1 jfb 152:
1.21 joris 153: if (cvs_sendentry(root, cfp) < 0)
154: return (CVS_EX_PROTO);
1.15 jfb 155:
156: switch (cfp->cf_cvstat) {
157: case CVS_FST_UNKNOWN:
1.20 jfb 158: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.15 jfb 159: break;
160: case CVS_FST_UPTODATE:
1.20 jfb 161: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cfp->cf_name);
1.15 jfb 162: break;
1.17 joris 163: case CVS_FST_ADDED:
1.15 jfb 164: case CVS_FST_MODIFIED:
1.20 jfb 165: ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cfp->cf_name);
1.15 jfb 166: if (ret == 0)
167: ret = cvs_sendfile(root, fpath);
168: default:
169: break;
170: }
171:
1.21 joris 172: if (ret == -1)
173: ret = CVS_EX_PROTO;
174:
1.15 jfb 175: return (ret);
176: }
177:
1.22 jfb 178: static int
179: cvs_status_local(CVSFILE *cf, void *arg)
1.15 jfb 180: {
1.22 jfb 181: int len;
1.35 xsa 182: size_t n;
183: char buf[MAXNAMLEN], fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
184: char numbuf[64], timebuf[32];
1.28 xsa 185: char *repo;
1.15 jfb 186: RCSFILE *rf;
187: struct cvsroot *root;
188:
1.33 xsa 189: if (cf->cf_type == DT_DIR) {
190: if (verbosity > 1)
191: cvs_log(LP_INFO, "Examining %s", cf->cf_name);
1.15 jfb 192: return (0);
1.33 xsa 193: }
1.15 jfb 194:
1.22 jfb 195: root = CVS_DIR_ROOT(cf);
196: repo = CVS_DIR_REPO(cf);
1.1 jfb 197:
1.22 jfb 198: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 jfb 199:
1.22 jfb 200: len = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
201: root->cr_dir, repo, CVS_FILE_NAME(cf), RCS_FILE_EXT);
202: if (len == -1 || len >= (int)sizeof(rcspath)) {
1.18 xsa 203: errno = ENAMETOOLONG;
204: cvs_log(LP_ERRNO, "%s", rcspath);
1.21 joris 205: return (CVS_EX_DATA);
1.18 xsa 206: }
1.1 jfb 207:
1.37 xsa 208: if (cf->cf_cvstat != CVS_FST_UNKNOWN &&
209: cf->cf_cvstat != CVS_FST_ADDED) {
1.30 xsa 210: rf = rcs_open(rcspath, RCS_READ);
211: if (rf == NULL)
212: return (CVS_EX_DATA);
213: }
1.5 jfb 214:
1.16 jfb 215: buf[0] = '\0';
1.30 xsa 216: if (cf->cf_cvstat == CVS_FST_LOST || cf->cf_cvstat == CVS_FST_UNKNOWN)
1.27 xsa 217: strlcpy(buf, "no file ", sizeof(buf));
1.22 jfb 218: strlcat(buf, cf->cf_name, sizeof(buf));
1.16 jfb 219:
1.28 xsa 220: cvs_printf(CVS_STATUS_SEP "\nFile: %-17s\tStatus: %s\n\n",
1.22 jfb 221: buf, cvs_statstr[cf->cf_cvstat]);
1.16 jfb 222:
1.22 jfb 223: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.32 joris 224: len = snprintf(buf, sizeof(buf), "No entry for %s",
225: cf->cf_name);
1.37 xsa 226: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
227: len = snprintf(buf, sizeof(buf), "New file!");
1.16 jfb 228: } else {
1.37 xsa 229: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
230: len = snprintf(buf, sizeof(buf), "%s", numbuf);
1.35 xsa 231:
232: /* Display etime in local mode only. */
233: if (cvs_cmdop != CVS_OP_SERVER) {
234: strlcat(buf, "\t", sizeof(buf));
235:
236: ctime_r(&(cf->cf_etime), timebuf);
237: n = strlen(timebuf);
238: if ((n > 0) && (timebuf[n - 1] == '\n'))
1.36 xsa 239: timebuf[--n] = '\0';
1.35 xsa 240:
241: strlcat(buf, timebuf, sizeof(buf));
242: }
1.16 jfb 243: }
1.32 joris 244:
1.34 joris 245: if (len == -1 || len >= (int)sizeof(buf)) {
246: if (rf != NULL)
247: rcs_close(rf);
1.32 joris 248: return (CVS_EX_DATA);
1.34 joris 249: }
1.15 jfb 250:
1.28 xsa 251: cvs_printf(" Working revision:\t%s\n", buf);
1.30 xsa 252:
1.37 xsa 253: if (cf->cf_cvstat == CVS_FST_UNKNOWN ||
254: cf->cf_cvstat == CVS_FST_ADDED) {
255: len = snprintf(buf, sizeof(buf), "No revision control file");
1.30 xsa 256: } else {
1.34 joris 257: len = snprintf(buf, sizeof(buf), "%s\t%s",
1.30 xsa 258: rcsnum_tostr(rf->rf_head, numbuf, sizeof(numbuf)),
259: rcspath);
1.34 joris 260: }
261:
262: if (len == -1 || len >= (int)sizeof(buf)) {
263: if (rf != NULL)
264: rcs_close(rf);
265: return (CVS_EX_DATA);
1.30 xsa 266: }
267:
268: cvs_printf(" Repository revision:\t%s\n", buf);
269:
270: /* If the file is unknown, no other output is needed after this. */
1.37 xsa 271: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
272: cvs_printf("\n");
1.30 xsa 273: return (0);
1.37 xsa 274: }
1.30 xsa 275:
1.38 ! xsa 276: if (cf->cf_tag != NULL)
! 277: cvs_printf(" Sticky Tag:\t\t%s\n", cf->cf_tag);
! 278: else if (verbosity > 0)
! 279: cvs_printf(" Sticky Tag:\t\t(none)\n");
! 280:
! 281: /* XXX */
! 282: if (verbosity > 0)
! 283: cvs_printf(" Sticky Date:\t\t%s\n", "(none)");
! 284:
! 285: if (cf->cf_opts != NULL)
! 286: cvs_printf(" Sticky Options:\t%s\n", cf->cf_opts);
! 287: else if (verbosity > 0)
! 288: cvs_printf(" Sticky Options:\t(none)\n");
1.15 jfb 289:
1.25 xsa 290: cvs_printf("\n");
1.15 jfb 291: rcs_close(rf);
292:
293: return (0);
1.1 jfb 294: }