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

1.9     ! tedu        1: /*     $OpenBSD: status.c,v 1.8 2005/02/27 00:22:08 jfb 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>
                     31: #include <stdio.h>
                     32: #include <fcntl.h>
                     33: #include <stdlib.h>
                     34: #include <unistd.h>
                     35: #include <string.h>
                     36: #include <sysexits.h>
                     37:
                     38: #include "cvs.h"
                     39: #include "log.h"
                     40: #include "proto.h"
                     41:
                     42:
                     43: const char *cvs_statstr[] = {
                     44:        "Unknown",
                     45:        "Up to date",
                     46:        "Locally Modified",
                     47:        "Added",
                     48:        "Removed",
                     49:        "Conflict",
                     50:        "Patched",
                     51: };
                     52:
                     53:
                     54: int cvs_status_file (CVSFILE *, void *);
                     55:
                     56:
                     57: /*
                     58:  * cvs_status()
                     59:  *
                     60:  * Handler for the `cvs status' command.
                     61:  * Returns 0 on success, or one of the known exit codes on error.
                     62:  */
                     63: int
                     64: cvs_status(int argc, char **argv)
                     65: {
1.7       jfb        66:        int i, ch, flags, verbose;
1.5       jfb        67:        struct cvsroot *root;
1.1       jfb        68:
1.7       jfb        69:        verbose = 0;
1.1       jfb        70:        flags = CF_SORT|CF_IGNORE|CF_RECURSE;
                     71:
1.7       jfb        72:        while ((ch = getopt(argc, argv, "lRv")) != -1) {
1.1       jfb        73:                switch (ch) {
1.7       jfb        74:                case 'l':
                     75:                        flags &= ~CF_RECURSE;
                     76:                        break;
                     77:                case 'R':
                     78:                        flags |= CF_RECURSE;
                     79:                        break;
                     80:                case 'v':
                     81:                        verbose = 1;
                     82:                        break;
1.1       jfb        83:                default:
                     84:                        return (EX_USAGE);
                     85:                }
                     86:        }
                     87:
                     88:        argc -= optind;
                     89:        argv += optind;
                     90:
                     91:        if (argc == 0) {
1.5       jfb        92:                cvs_files = cvs_file_get(".", flags);
                     93:        } else {
                     94:                cvs_files = cvs_file_getspec(argv, argc, 0);
                     95:        }
                     96:        if (cvs_files == NULL)
                     97:                return (EX_DATAERR);
                     98:
                     99:        root = CVS_DIR_ROOT(cvs_files);
                    100:        if (root == NULL) {
                    101:                cvs_log(LP_ERR,
                    102:                    "No CVSROOT specified!  Please use the `-d' option");
                    103:                cvs_log(LP_ERR,
                    104:                    "or set the CVSROOT environment variable.");
                    105:                return (EX_USAGE);
                    106:        }
                    107:
1.7       jfb       108:        if (root->cr_method != CVS_METHOD_LOCAL) {
                    109:                if (cvs_connect(root) < 0)
                    110:                        return (EX_PROTOCOL);
                    111:                if (verbose && (cvs_sendarg(root, "-v", 0) < 0))
                    112:                        return (EX_PROTOCOL);
                    113:        }
1.5       jfb       114:
                    115:        cvs_file_examine(cvs_files, cvs_status_file, NULL);
1.1       jfb       116:
1.5       jfb       117:        if (root->cr_method != CVS_METHOD_LOCAL) {
                    118:                if (cvs_senddir(root, cvs_files) < 0)
                    119:                        return (EX_PROTOCOL);
                    120:                for (i = 0; i < argc; i++)
                    121:                        if (cvs_sendarg(root, argv[i], 0) < 0)
                    122:                                return (EX_PROTOCOL);
                    123:                if (cvs_sendreq(root, CVS_REQ_STATUS, NULL) < 0)
                    124:                        return (EX_PROTOCOL);
1.1       jfb       125:        }
                    126:
                    127:        return (0);
                    128: }
                    129:
                    130:
                    131: /*
                    132:  * cvs_status_file()
                    133:  *
                    134:  * Get the status of a single file.
                    135:  */
                    136: int
1.2       jfb       137: cvs_status_file(CVSFILE *cfp, void *arg)
1.1       jfb       138: {
1.5       jfb       139:        int ret;
1.2       jfb       140:        char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.1       jfb       141:        RCSFILE *rf;
                    142:        struct cvs_ent *entp;
                    143:        struct cvsroot *root;
                    144:
1.5       jfb       145:        ret = 0;
1.1       jfb       146:        rf = NULL;
1.5       jfb       147:        root = CVS_DIR_ROOT(cfp);
                    148:        repo = CVS_DIR_REPO(cfp);
1.1       jfb       149:
1.6       jfb       150:        if (cfp->cf_type == DT_DIR) {
                    151:                if (root->cr_method != CVS_METHOD_LOCAL) {
                    152:                        if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
                    153:                                ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
                    154:                                    CVS_FILE_NAME(cfp));
                    155:                        else
                    156:                                ret = cvs_senddir(root, cfp);
                    157:                }
                    158:
                    159:                return (ret);
                    160:        }
1.1       jfb       161:
1.5       jfb       162:        cvs_file_getpath(cfp, fpath, sizeof(fpath));
1.2       jfb       163:        entp = cvs_ent_getent(fpath);
1.1       jfb       164:
                    165:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.5       jfb       166:                if ((entp != NULL) && (cvs_sendentry(root, entp) < 0)) {
1.1       jfb       167:                        cvs_ent_free(entp);
                    168:                        return (-1);
                    169:                }
                    170:
1.5       jfb       171:                switch (cfp->cf_cvstat) {
                    172:                case CVS_FST_UNKNOWN:
                    173:                        ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
                    174:                            CVS_FILE_NAME(cfp));
                    175:                        break;
                    176:                case CVS_FST_UPTODATE:
                    177:                        ret = cvs_sendreq(root, CVS_REQ_UNCHANGED,
                    178:                            CVS_FILE_NAME(cfp));
                    179:                        break;
                    180:                case CVS_FST_MODIFIED:
                    181:                        ret = cvs_sendreq(root, CVS_REQ_MODIFIED,
1.2       jfb       182:                            CVS_FILE_NAME(cfp));
1.5       jfb       183:                        if (ret == 0)
                    184:                                ret = cvs_sendfile(root, fpath);
                    185:                default:
                    186:                        break;
                    187:                }
                    188:        } else {
                    189:                if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
                    190:                        cvs_log(LP_WARN, "I know nothing about %s", fpath);
                    191:                        return (0);
                    192:                }
1.1       jfb       193:
                    194:                snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
1.2       jfb       195:                    root->cr_dir, repo, CVS_FILE_NAME(cfp), RCS_FILE_EXT);
1.1       jfb       196:
1.8       jfb       197:                rf = rcs_open(rcspath, RCS_READ);
1.1       jfb       198:                if (rf == NULL) {
1.9     ! tedu      199:                        if (entp != NULL)
        !           200:                                cvs_ent_free(entp);
1.1       jfb       201:                        return (-1);
                    202:                }
                    203:
                    204:                rcs_close(rf);
                    205:        }
1.5       jfb       206:
                    207:        if (entp != NULL)
                    208:                cvs_ent_free(entp);
                    209:        return (ret);
1.1       jfb       210: }