[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.39

1.39    ! xsa         1: /*     $OpenBSD: status.c,v 1.38 2005/07/08 12:45:10 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: };
1.33      xsa        56:
1.1       jfb        57:
1.22      jfb        58: static int cvs_status_init      (struct cvs_cmd *, int, char **, int *);
                     59: static int cvs_status_remote    (CVSFILE *, void *);
                     60: static int cvs_status_local     (CVSFILE *, void *);
                     61: static int cvs_status_pre_exec (struct cvsroot *);
                     62:
                     63: struct cvs_cmd cvs_cmd_status = {
                     64:        CVS_OP_STATUS, CVS_REQ_STATUS, "status",
                     65:        { "st", "stat" },
                     66:        "Display status information on checked out files",
                     67:        "[-lRv]",
                     68:        "lRv",
                     69:        NULL,
                     70:        CF_SORT | CF_IGNORE | CF_RECURSE,
                     71:        cvs_status_init,
                     72:        cvs_status_pre_exec,
1.20      jfb        73:        cvs_status_remote,
1.22      jfb        74:        cvs_status_local,
                     75:        NULL,
                     76:        NULL,
1.10      joris      77:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
                     78: };
1.1       jfb        79:
1.10      joris      80: static int verbose = 0;
1.1       jfb        81:
1.22      jfb        82: static int
                     83: cvs_status_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        84: {
1.10      joris      85:        int ch;
1.1       jfb        86:
1.22      jfb        87:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        88:                switch (ch) {
1.7       jfb        89:                case 'l':
1.22      jfb        90:                        cmd->file_flags &= ~CF_RECURSE;
1.7       jfb        91:                        break;
                     92:                case 'R':
1.22      jfb        93:                        cmd->file_flags |= CF_RECURSE;
1.7       jfb        94:                        break;
                     95:                case 'v':
                     96:                        verbose = 1;
                     97:                        break;
1.1       jfb        98:                default:
1.14      joris      99:                        return (CVS_EX_USAGE);
1.1       jfb       100:                }
                    101:        }
                    102:
1.10      joris     103:        *arg = optind;
                    104:        return (0);
                    105: }
1.1       jfb       106:
1.22      jfb       107: static int
                    108: cvs_status_pre_exec(struct cvsroot *root)
1.10      joris     109: {
1.26      xsa       110:        if (root->cr_method != CVS_METHOD_LOCAL) {
                    111:                if (verbose && (cvs_sendarg(root, "-v", 0) < 0))
                    112:                        return (CVS_EX_PROTO);
                    113:        }
                    114:
1.1       jfb       115:        return (0);
                    116: }
                    117:
                    118: /*
1.20      jfb       119:  * cvs_status_remote()
1.1       jfb       120:  *
                    121:  * Get the status of a single file.
                    122:  */
1.22      jfb       123: static int
1.20      jfb       124: cvs_status_remote(CVSFILE *cfp, void *arg)
1.1       jfb       125: {
1.5       jfb       126:        int ret;
1.15      jfb       127:        char *repo, fpath[MAXPATHLEN];
1.1       jfb       128:        RCSFILE *rf;
                    129:        struct cvsroot *root;
                    130:
1.5       jfb       131:        ret = 0;
1.1       jfb       132:        rf = NULL;
1.5       jfb       133:        root = CVS_DIR_ROOT(cfp);
                    134:        repo = CVS_DIR_REPO(cfp);
1.1       jfb       135:
1.6       jfb       136:        if (cfp->cf_type == DT_DIR) {
1.15      jfb       137:                if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
                    138:                        ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
                    139:                            CVS_FILE_NAME(cfp));
                    140:                else
                    141:                        ret = cvs_senddir(root, cfp);
1.21      joris     142:
                    143:                if (ret == -1)
                    144:                        ret = CVS_EX_PROTO;
                    145:
1.6       jfb       146:                return (ret);
                    147:        }
1.1       jfb       148:
1.5       jfb       149:        cvs_file_getpath(cfp, fpath, sizeof(fpath));
1.1       jfb       150:
1.21      joris     151:        if (cvs_sendentry(root, cfp) < 0)
                    152:                return (CVS_EX_PROTO);
1.15      jfb       153:
                    154:        switch (cfp->cf_cvstat) {
                    155:        case CVS_FST_UNKNOWN:
1.20      jfb       156:                ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.15      jfb       157:                break;
                    158:        case CVS_FST_UPTODATE:
1.20      jfb       159:                ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cfp->cf_name);
1.15      jfb       160:                break;
1.17      joris     161:        case CVS_FST_ADDED:
1.15      jfb       162:        case CVS_FST_MODIFIED:
1.20      jfb       163:                ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cfp->cf_name);
1.15      jfb       164:                if (ret == 0)
                    165:                        ret = cvs_sendfile(root, fpath);
                    166:        default:
                    167:                break;
                    168:        }
                    169:
1.21      joris     170:        if (ret == -1)
                    171:                ret = CVS_EX_PROTO;
                    172:
1.15      jfb       173:        return (ret);
                    174: }
                    175:
1.22      jfb       176: static int
                    177: cvs_status_local(CVSFILE *cf, void *arg)
1.15      jfb       178: {
1.22      jfb       179:        int len;
1.35      xsa       180:        size_t n;
                    181:        char buf[MAXNAMLEN], fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
                    182:        char numbuf[64], timebuf[32];
1.28      xsa       183:        char *repo;
1.15      jfb       184:        RCSFILE *rf;
                    185:        struct cvsroot *root;
                    186:
1.33      xsa       187:        if (cf->cf_type == DT_DIR) {
                    188:                if (verbosity > 1)
                    189:                        cvs_log(LP_INFO, "Examining %s", cf->cf_name);
1.15      jfb       190:                return (0);
1.33      xsa       191:        }
1.15      jfb       192:
1.22      jfb       193:        root = CVS_DIR_ROOT(cf);
                    194:        repo = CVS_DIR_REPO(cf);
1.1       jfb       195:
1.22      jfb       196:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       jfb       197:
1.22      jfb       198:        len = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
                    199:            root->cr_dir, repo, CVS_FILE_NAME(cf), RCS_FILE_EXT);
                    200:        if (len == -1 || len >= (int)sizeof(rcspath)) {
1.18      xsa       201:                errno = ENAMETOOLONG;
                    202:                cvs_log(LP_ERRNO, "%s", rcspath);
1.21      joris     203:                return (CVS_EX_DATA);
1.18      xsa       204:        }
1.1       jfb       205:
1.37      xsa       206:        if (cf->cf_cvstat != CVS_FST_UNKNOWN &&
                    207:            cf->cf_cvstat != CVS_FST_ADDED) {
1.30      xsa       208:                rf = rcs_open(rcspath, RCS_READ);
                    209:                if (rf == NULL)
                    210:                        return (CVS_EX_DATA);
                    211:        }
1.5       jfb       212:
1.16      jfb       213:        buf[0] = '\0';
1.30      xsa       214:        if (cf->cf_cvstat == CVS_FST_LOST || cf->cf_cvstat == CVS_FST_UNKNOWN)
1.27      xsa       215:                strlcpy(buf, "no file ", sizeof(buf));
1.22      jfb       216:        strlcat(buf, cf->cf_name, sizeof(buf));
1.16      jfb       217:
1.28      xsa       218:        cvs_printf(CVS_STATUS_SEP "\nFile: %-17s\tStatus: %s\n\n",
1.22      jfb       219:            buf, cvs_statstr[cf->cf_cvstat]);
1.16      jfb       220:
1.22      jfb       221:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.32      joris     222:                len = snprintf(buf, sizeof(buf), "No entry for %s",
                    223:                    cf->cf_name);
1.37      xsa       224:        } else if (cf->cf_cvstat == CVS_FST_ADDED) {
                    225:                len = snprintf(buf, sizeof(buf), "New file!");
1.16      jfb       226:        } else {
1.37      xsa       227:                rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
                    228:                len = snprintf(buf, sizeof(buf), "%s", numbuf);
1.35      xsa       229:
                    230:                /* Display etime in local mode only. */
                    231:                if (cvs_cmdop != CVS_OP_SERVER) {
                    232:                        strlcat(buf, "\t", sizeof(buf));
                    233:
                    234:                        ctime_r(&(cf->cf_etime), timebuf);
                    235:                        n = strlen(timebuf);
                    236:                        if ((n > 0) && (timebuf[n - 1] == '\n'))
1.36      xsa       237:                                timebuf[--n] = '\0';
1.35      xsa       238:
                    239:                        strlcat(buf, timebuf, sizeof(buf));
                    240:                }
1.16      jfb       241:        }
1.32      joris     242:
1.34      joris     243:        if (len == -1 || len >= (int)sizeof(buf)) {
                    244:                if (rf != NULL)
                    245:                        rcs_close(rf);
1.32      joris     246:                return (CVS_EX_DATA);
1.34      joris     247:        }
1.15      jfb       248:
1.28      xsa       249:        cvs_printf("   Working revision:\t%s\n", buf);
1.30      xsa       250:
1.37      xsa       251:        if (cf->cf_cvstat == CVS_FST_UNKNOWN ||
                    252:            cf->cf_cvstat == CVS_FST_ADDED) {
                    253:                len = snprintf(buf, sizeof(buf), "No revision control file");
1.30      xsa       254:        } else {
1.34      joris     255:                len = snprintf(buf, sizeof(buf), "%s\t%s",
1.30      xsa       256:                    rcsnum_tostr(rf->rf_head, numbuf, sizeof(numbuf)),
                    257:                    rcspath);
1.34      joris     258:        }
                    259:
                    260:        if (len == -1 || len >= (int)sizeof(buf)) {
                    261:                if (rf != NULL)
                    262:                        rcs_close(rf);
                    263:                return (CVS_EX_DATA);
1.30      xsa       264:        }
                    265:
                    266:        cvs_printf("   Repository revision:\t%s\n", buf);
                    267:
                    268:        /* If the file is unknown, no other output is needed after this. */
1.37      xsa       269:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
                    270:                cvs_printf("\n");
1.30      xsa       271:                return (0);
1.37      xsa       272:        }
1.30      xsa       273:
1.38      xsa       274:        if (cf->cf_tag != NULL)
                    275:                cvs_printf("   Sticky Tag:\t\t%s\n", cf->cf_tag);
                    276:        else if (verbosity > 0)
                    277:                cvs_printf("   Sticky Tag:\t\t(none)\n");
                    278:
                    279:        /* XXX */
                    280:        if (verbosity > 0)
                    281:                cvs_printf("   Sticky Date:\t\t%s\n", "(none)");
                    282:
                    283:        if (cf->cf_opts != NULL)
                    284:                cvs_printf("   Sticky Options:\t%s\n", cf->cf_opts);
                    285:        else if (verbosity > 0)
                    286:                cvs_printf("   Sticky Options:\t(none)\n");
1.15      jfb       287:
1.25      xsa       288:        cvs_printf("\n");
1.15      jfb       289:        rcs_close(rf);
                    290:
                    291:        return (0);
1.1       jfb       292: }