[BACK]Return to status.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/status.c, Revision 1.36

1.36    ! xsa         1: /*     $OpenBSD: status.c,v 1.35 2005/07/08 07:22:58 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.30      xsa       208:        if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
                    209:                rf = rcs_open(rcspath, RCS_READ);
                    210:                if (rf == NULL)
                    211:                        return (CVS_EX_DATA);
                    212:        }
1.5       jfb       213:
1.16      jfb       214:        buf[0] = '\0';
1.30      xsa       215:        if (cf->cf_cvstat == CVS_FST_LOST || cf->cf_cvstat == CVS_FST_UNKNOWN)
1.27      xsa       216:                strlcpy(buf, "no file ", sizeof(buf));
1.22      jfb       217:        strlcat(buf, cf->cf_name, sizeof(buf));
1.16      jfb       218:
1.28      xsa       219:        cvs_printf(CVS_STATUS_SEP "\nFile: %-17s\tStatus: %s\n\n",
1.22      jfb       220:            buf, cvs_statstr[cf->cf_cvstat]);
1.16      jfb       221:
1.22      jfb       222:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.32      joris     223:                len = snprintf(buf, sizeof(buf), "No entry for %s",
                    224:                    cf->cf_name);
1.16      jfb       225:        } else {
1.32      joris     226:                len = snprintf(buf, sizeof(buf), "%s",
1.24      joris     227:                    rcsnum_tostr(cf->cf_lrev, buf, sizeof(buf)));
1.35      xsa       228:
                    229:                /* Display etime in local mode only. */
                    230:                if (cvs_cmdop != CVS_OP_SERVER) {
                    231:                        strlcat(buf, "\t", sizeof(buf));
                    232:
                    233:                        ctime_r(&(cf->cf_etime), timebuf);
                    234:                        n = strlen(timebuf);
                    235:                        if ((n > 0) && (timebuf[n - 1] == '\n'))
1.36    ! xsa       236:                                timebuf[--n] = '\0';
1.35      xsa       237:
                    238:                        strlcat(buf, timebuf, sizeof(buf));
                    239:                }
1.16      jfb       240:        }
1.32      joris     241:
1.34      joris     242:        if (len == -1 || len >= (int)sizeof(buf)) {
                    243:                if (rf != NULL)
                    244:                        rcs_close(rf);
1.32      joris     245:                return (CVS_EX_DATA);
1.34      joris     246:        }
1.15      jfb       247:
1.28      xsa       248:        cvs_printf("   Working revision:\t%s\n", buf);
1.30      xsa       249:
                    250:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.34      joris     251:                len = snprintf(buf, sizeof(buf), "No revision control file\n");
1.30      xsa       252:        } else {
1.34      joris     253:                len = snprintf(buf, sizeof(buf), "%s\t%s",
1.30      xsa       254:                    rcsnum_tostr(rf->rf_head, numbuf, sizeof(numbuf)),
                    255:                    rcspath);
1.34      joris     256:        }
                    257:
                    258:        if (len == -1 || len >= (int)sizeof(buf)) {
                    259:                if (rf != NULL)
                    260:                        rcs_close(rf);
                    261:                return (CVS_EX_DATA);
1.30      xsa       262:        }
                    263:
                    264:        cvs_printf("   Repository revision:\t%s\n", buf);
                    265:
                    266:        /* If the file is unknown, no other output is needed after this. */
                    267:        if (cf->cf_cvstat == CVS_FST_UNKNOWN)
                    268:                return (0);
                    269:
1.28      xsa       270:        cvs_printf("   Sticky Tag:\t\t%s\n",
1.22      jfb       271:            cf->cf_tag == NULL ? "(none)" : cf->cf_tag);
1.28      xsa       272:        cvs_printf("   Sticky Date:\t\t%s\n", "(none)");
                    273:        cvs_printf("   Sticky Options:\t%s\n",
1.22      jfb       274:            cf->cf_opts == NULL ? "(none)" : cf->cf_opts);
1.15      jfb       275:
1.25      xsa       276:        cvs_printf("\n");
1.15      jfb       277:        rcs_close(rf);
                    278:
                    279:        return (0);
1.1       jfb       280: }