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

Annotation of src/usr.bin/rcs/ci.c, Revision 1.86

1.86    ! xsa         1: /*     $OpenBSD: ci.c,v 1.85 2005/12/12 09:40:08 xsa Exp $     */
1.1       niallo      2: /*
                      3:  * Copyright (c) 2005 Niall O'Higgins <niallo@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 cinditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source cide must retain the above cipyright
                     11:  *    notice, this list of cinditions 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/types.h>
                     29: #include <sys/stat.h>
                     30: #include <sys/wait.h>
                     31:
1.68      xsa        32: #include <ctype.h>
1.1       niallo     33: #include <pwd.h>
                     34: #include <stdio.h>
                     35: #include <stdlib.h>
1.68      xsa        36: #include <string.h>
1.1       niallo     37: #include <unistd.h>
                     38:
                     39: #include "log.h"
                     40: #include "rcs.h"
1.4       niallo     41: #include "diff.h"
1.1       niallo     42: #include "rcsprog.h"
1.9       niallo     43:
1.80      niallo     44: #define CI_OPTSTRING    "d::f::i::j::k:l::m:M::N:n:qr::s:Tt:u::Vw:x:"
1.25      niallo     45: #define DATE_NOW        -1
                     46: #define DATE_MTIME      -2
                     47:
1.58      niallo     48: #define LOG_INIT        "Initial revision"
1.63      niallo     49: #define LOG_PROMPT      "enter log message, terminated with a single '.' "    \
1.58      niallo     50:                         "or end of file:\n>> "
1.63      niallo     51: #define DESC_PROMPT     "enter description, terminated with single '.' "      \
                     52:                        "or end of file:\nNOTE: This is NOT the log message!" \
                     53:                         "\n>> "
1.58      niallo     54:
                     55: struct checkin_params {
                     56:        int flags, openflags;
                     57:        mode_t fmode;
                     58:        time_t date;
                     59:        RCSFILE *file;
                     60:        RCSNUM *frev, *newrev;
                     61:        char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.82      joris      62:        char *author;
1.80      niallo     63:        const char *symbol, *state, *description;
1.58      niallo     64: };
                     65:
1.68      xsa        66: static int      checkin_attach_symbol(struct checkin_params *pb);
                     67: static int      checkin_checklock(struct checkin_params *pb);
                     68: static char    *checkin_choose_rcsfile(const char *);
                     69: static char    *checkin_diff_file(struct checkin_params *);
                     70: static char    *checkin_getdesc(void);
                     71: static char    *checkin_getinput(const char *);
                     72: static char    *checkin_getlogmsg(RCSNUM *, RCSNUM *);
                     73: static int      checkin_init(struct checkin_params *);
                     74: static int      checkin_mtimedate(struct checkin_params *pb);
                     75: static int      checkin_update(struct checkin_params *pb);
                     76: static void     checkin_revert(struct checkin_params *pb);
1.4       niallo     77:
1.1       niallo     78: void
                     79: checkin_usage(void)
                     80: {
                     81:        fprintf(stderr,
1.67      xsa        82:            "usage: ci [-MNqTV] [-d[date]] [-f[rev]] [-i[rev]] [-j[rev]]\n"
1.74      xsa        83:            "          [-kmode] [-l[rev]] [-M[rev]] [-mmsg] [-Nsymbol]\n"
1.85      xsa        84:            "          [-nsymbol] [-r[rev]] [-sstate] [-tfile|str] [-u[rev]]\n"
1.81      niallo     85:            "          [-wusername] [-xsuffixes] file ...\n");
1.58      niallo     86: }
                     87:
1.55      niallo     88:
1.1       niallo     89:
                     90: /*
                     91:  * checkin_main()
                     92:  *
                     93:  * Handler for the `ci' program.
                     94:  * Returns 0 on success, or >0 on error.
                     95:  */
                     96: int
                     97: checkin_main(int argc, char **argv)
                     98: {
1.58      niallo     99:        int i, ch, status;
                    100:        struct checkin_params pb;
1.1       niallo    101:
1.58      niallo    102:        pb.date = DATE_NOW;
                    103:        pb.file = NULL;
1.82      joris     104:        pb.rcs_msg = pb.username = pb.author = NULL;
1.80      niallo    105:        pb.state = pb.symbol = pb.description = NULL;
1.58      niallo    106:        pb.newrev =  NULL;
                    107:        pb.fmode = pb.flags = status = 0;
1.1       niallo    108:
1.58      niallo    109:        pb.flags = INTERACTIVE;
                    110:        pb.openflags = RCS_RDWR|RCS_CREATE;
1.9       niallo    111:
1.58      niallo    112:        while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1       niallo    113:                switch (ch) {
1.20      niallo    114:                case 'd':
1.25      niallo    115:                        if (rcs_optarg == NULL)
1.58      niallo    116:                                pb.date = DATE_MTIME;
                    117:                        else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0) {
1.20      niallo    118:                                cvs_log(LP_ERR, "invalide date");
                    119:                                exit(1);
                    120:                        }
                    121:                        break;
1.29      niallo    122:                case 'f':
1.58      niallo    123:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    124:                        pb.flags |= FORCE;
1.29      niallo    125:                        break;
1.1       niallo    126:                case 'h':
                    127:                        (usage)();
                    128:                        exit(0);
1.58      niallo    129:                case 'i':
                    130:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    131:                        pb.openflags |= RCS_CREATE;
1.66      niallo    132:                        pb.flags |= CI_INIT;
1.58      niallo    133:                        break;
                    134:                case 'j':
                    135:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    136:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    137:                        pb.flags &= ~CI_INIT;
1.58      niallo    138:                        break;
1.30      niallo    139:                case 'l':
1.58      niallo    140:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    141:                        pb.flags |= CO_LOCK;
1.54      niallo    142:                        break;
                    143:                case 'M':
1.58      niallo    144:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    145:                        pb.flags |= CO_REVDATE;
1.30      niallo    146:                        break;
1.1       niallo    147:                case 'm':
1.58      niallo    148:                        pb.rcs_msg = rcs_optarg;
                    149:                        if (pb.rcs_msg == NULL) {
                    150:                                cvs_log(LP_ERR,
                    151:                                    "missing message for -m option");
                    152:                                exit(1);
                    153:                        }
                    154:                        pb.flags &= ~INTERACTIVE;
1.3       joris     155:                        break;
1.42      niallo    156:                case 'N':
1.84      joris     157:                        pb.symbol = xstrdup(rcs_optarg);
1.58      niallo    158:                        if (rcs_sym_check(pb.symbol) != 1) {
                    159:                                cvs_log(LP_ERR, "invalid symbol `%s'",
                    160:                                    pb.symbol);
1.42      niallo    161:                                exit(1);
                    162:                        }
1.58      niallo    163:                        pb.flags |= CI_SYMFORCE;
1.42      niallo    164:                        break;
1.38      niallo    165:                case 'n':
1.84      joris     166:                        pb.symbol = xstrdup(rcs_optarg);
1.58      niallo    167:                        if (rcs_sym_check(pb.symbol) != 1) {
                    168:                                cvs_log(LP_ERR, "invalid symbol `%s'",
                    169:                                    pb.symbol);
1.38      niallo    170:                                exit(1);
                    171:                        }
                    172:                        break;
1.3       joris     173:                case 'q':
                    174:                        verbose = 0;
1.1       niallo    175:                        break;
1.30      niallo    176:                case 'r':
1.58      niallo    177:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    178:                        pb.flags |= CI_DEFAULT;
1.9       niallo    179:                        break;
1.51      niallo    180:                case 's':
1.58      niallo    181:                        pb.state = rcs_optarg;
                    182:                        if (rcs_state_check(pb.state) < 0) {
                    183:                                cvs_log(LP_ERR, "invalid state `%s'",
                    184:                                    pb.state);
1.51      niallo    185:                                exit(1);
                    186:                        }
1.67      xsa       187:                        break;
                    188:                case 'T':
                    189:                        pb.flags |= PRESERVETIME;
1.51      niallo    190:                        break;
1.80      niallo    191:                case 't':
1.84      joris     192:                        pb.description = xstrdup(rcs_optarg);
1.80      niallo    193:                        break;
1.9       niallo    194:                case 'u':
1.58      niallo    195:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    196:                        pb.flags |= CO_UNLOCK;
1.9       niallo    197:                        break;
1.30      niallo    198:                case 'V':
                    199:                        printf("%s\n", rcs_version);
                    200:                        exit(0);
1.31      niallo    201:                case 'w':
1.84      joris     202:                        pb.author = xstrdup(rcs_optarg);
1.64      xsa       203:                        break;
                    204:                case 'x':
                    205:                        rcs_suffixes = rcs_optarg;
1.31      niallo    206:                        break;
1.1       niallo    207:                default:
                    208:                        (usage)();
                    209:                        exit(1);
                    210:                }
                    211:        }
                    212:
1.24      joris     213:        argc -= rcs_optind;
                    214:        argv += rcs_optind;
                    215:
1.1       niallo    216:        if (argc == 0) {
                    217:                cvs_log(LP_ERR, "no input file");
                    218:                (usage)();
                    219:                exit(1);
                    220:        }
                    221:
1.86    ! xsa       222:        if ((pb.username = getlogin()) == NULL)
        !           223:                fatal("getlogin failed");
1.31      niallo    224:
                    225:
1.1       niallo    226:        for (i = 0; i < argc; i++) {
1.58      niallo    227:                pb.filename = argv[i];
1.1       niallo    228:
1.58      niallo    229:                /*
                    230:                 * Test for existence of ,v file. If we are expected to
                    231:                 * create one, set NEWFILE flag.
                    232:                 */
1.71      niallo    233:                if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
                    234:                        if (pb.openflags & RCS_CREATE)
                    235:                                pb.flags |= NEWFILE;
                    236:                        else {
                    237:                                cvs_log(LP_ERR, "No existing RCS file");
                    238:                                status = 1;
                    239:                                continue;
                    240:                        }
1.66      niallo    241:                } else {
                    242:                        if (pb.flags & CI_INIT) {
                    243:                                cvs_log(LP_ERR, "%s already exists", pb.fpath);
                    244:                                status = 1;
                    245:                                continue;
                    246:                        }
1.58      niallo    247:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    248:                }
1.63      niallo    249:                /*
                    250:                 * If we are to create a new ,v file, we must decide where it
                    251:                 * should go.
                    252:                 */
                    253:                if (pb.flags & NEWFILE) {
                    254:                        char *fpath = checkin_choose_rcsfile(pb.filename);
                    255:                        if (fpath == NULL) {
                    256:                                status = 1;
                    257:                                continue;
                    258:                        }
                    259:                        strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
1.84      joris     260:                        xfree(fpath);
1.63      niallo    261:                }
                    262:
1.58      niallo    263:                pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
                    264:
                    265:                if (pb.file == NULL) {
1.60      niallo    266:                        cvs_log(LP_ERR, "failed to open rcsfile '%s'",
                    267:                            pb.fpath);
1.1       niallo    268:                        exit(1);
                    269:                }
1.29      niallo    270:
1.58      niallo    271:                if (verbose == 1)
                    272:                        printf("%s  <--  %s\n", pb.fpath, pb.filename);
1.61      niallo    273:
1.63      niallo    274:                if (pb.flags & NEWFILE)
                    275:                        status = checkin_init(&pb);
1.15      niallo    276:                else
1.63      niallo    277:                        status = checkin_update(&pb);
1.4       niallo    278:        }
                    279:
1.16      niallo    280:        return (status);
1.4       niallo    281: }
                    282:
1.59      niallo    283: /*
                    284:  * checkin_diff_file()
                    285:  *
1.65      xsa       286:  * Generate the diff between the working file and a revision.
1.59      niallo    287:  * Returns pointer to a char array on success, NULL on failure.
                    288:  */
1.4       niallo    289: static char *
1.58      niallo    290: checkin_diff_file(struct checkin_params *pb)
1.4       niallo    291: {
                    292:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    293:        BUF *b1, *b2, *b3;
                    294:        char rbuf[64], *deltatext;
                    295:
1.58      niallo    296:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4       niallo    297:
1.58      niallo    298:        if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    299:                cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4       niallo    300:                return (NULL);
1.1       niallo    301:        }
                    302:
1.58      niallo    303:        if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4       niallo    304:                cvs_log(LP_ERR, "failed to load revision");
                    305:                cvs_buf_free(b1);
                    306:                return (NULL);
                    307:        }
                    308:
1.57      xsa       309:        if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4       niallo    310:                cvs_log(LP_ERR, "failed to allocated buffer for diff");
                    311:                cvs_buf_free(b1);
                    312:                cvs_buf_free(b2);
                    313:                return (NULL);
                    314:        }
                    315:
1.50      xsa       316:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    317:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4       niallo    318:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    319:                cvs_log(LP_ERRNO, "could not write temporary file");
                    320:                cvs_buf_free(b1);
                    321:                cvs_buf_free(b2);
                    322:                return (NULL);
                    323:        }
                    324:        cvs_buf_free(b1);
                    325:
1.50      xsa       326:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    327:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4       niallo    328:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    329:                cvs_buf_free(b2);
                    330:                (void)unlink(path1);
                    331:                return (NULL);
                    332:        }
                    333:        cvs_buf_free(b2);
                    334:
1.5       niallo    335:        diff_format = D_RCSDIFF;
1.4       niallo    336:        cvs_diffreg(path1, path2, b3);
                    337:        (void)unlink(path1);
                    338:        (void)unlink(path2);
                    339:
                    340:        cvs_buf_putc(b3, '\0');
                    341:        deltatext = (char *)cvs_buf_release(b3);
                    342:
                    343:        return (deltatext);
1.6       niallo    344: }
                    345:
                    346: /*
1.65      xsa       347:  * checkin_getlogmsg()
1.59      niallo    348:  *
1.6       niallo    349:  * Get log message from user interactively.
1.59      niallo    350:  * Returns pointer to a char array on success, NULL on failure.
1.6       niallo    351:  */
                    352: static char *
1.34      niallo    353: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6       niallo    354: {
1.58      niallo    355:        char   *rcs_msg, nrev[16], prev[16];
1.6       niallo    356:        RCSNUM *tmprev;
                    357:
1.7       niallo    358:        rcs_msg = NULL;
1.6       niallo    359:        tmprev = rcsnum_alloc();
                    360:        rcsnum_cpy(rev, tmprev, 16);
1.9       niallo    361:        rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12      niallo    362:        if (rev2 == NULL)
                    363:                rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
                    364:        else
                    365:                rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6       niallo    366:        rcsnum_free(tmprev);
                    367:
1.47      niallo    368:        if (verbose == 1)
                    369:                printf("new revision: %s; previous revision: %s\n", nrev,
                    370:                    prev);
1.32      joris     371:
1.58      niallo    372:        rcs_msg = checkin_getinput(LOG_PROMPT);
                    373:        return (rcs_msg);
                    374: }
                    375:
                    376:
                    377: /*
                    378:  * checkin_getdesc()
                    379:  *
                    380:  * Get file description interactively.
1.59      niallo    381:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    382:  */
                    383: static char *
                    384: checkin_getdesc()
                    385: {
                    386:        char *description;
                    387:
                    388:        description = checkin_getinput(DESC_PROMPT);
                    389:        return (description);
                    390: }
                    391:
                    392: /*
                    393:  * checkin_getinput()
                    394:  *
1.59      niallo    395:  * Get some input from the user, in RCS style, prompting with message <prompt>.
                    396:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    397:  */
                    398: static char *
                    399: checkin_getinput(const char *prompt)
                    400: {
                    401:        BUF *inputbuf;
                    402:        char *input, buf[128];
                    403:
                    404:        if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
                    405:                cvs_log(LP_ERR, "failed to allocate input buffer");
                    406:                return (NULL);
                    407:        }
                    408:
                    409:        printf(prompt);
1.6       niallo    410:        for (;;) {
                    411:                fgets(buf, (int)sizeof(buf), stdin);
1.9       niallo    412:                if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6       niallo    413:                        break;
1.58      niallo    414:                cvs_buf_append(inputbuf, buf, strlen(buf));
1.46      joris     415:                printf(">> ");
1.6       niallo    416:        }
1.32      joris     417:
1.58      niallo    418:        cvs_buf_putc(inputbuf, '\0');
                    419:        input = (char *)cvs_buf_release(inputbuf);
                    420:
                    421:        return (input);
                    422: }
                    423:
                    424: /*
1.63      niallo    425:  * checkin_update()
                    426:  *
                    427:  * Do a checkin to an existing RCS file.
                    428:  *
                    429:  * On success, return 0. On error return -1.
                    430:  */
                    431: static int
                    432: checkin_update(struct checkin_params *pb)
                    433: {
1.77      xsa       434:        char  *filec, numb1[64], numb2[64];
1.63      niallo    435:        BUF *bp;
                    436:
1.82      joris     437:        /*
                    438:         * XXX this is wrong, we need to get the revision the user
1.85      xsa       439:         * has the lock for. So we can decide if we want to create a
1.82      joris     440:         * branch or not. (if it's not current HEAD we need to branch).
                    441:         */
1.63      niallo    442:        pb->frev = pb->file->rf_head;
                    443:
1.82      joris     444:        if (checkin_checklock(pb) < 0)
                    445:                return (-1);
                    446:
                    447:        /* If revision passed on command line is less than HEAD, bail.
                    448:         * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
                    449:         * there is no lock set for the user.
                    450:         */
1.63      niallo    451:        if ((pb->newrev != NULL)
                    452:            && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77      xsa       453:                cvs_log(LP_ERR,
                    454:                    "%s: revision %s too low; must be higher than %s",
                    455:                    pb->file->rf_path,
                    456:                    rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
                    457:                    rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63      niallo    458:                rcs_close(pb->file);
                    459:                return (-1);
                    460:        }
                    461:
1.73      xsa       462:        /* Load file contents */
1.63      niallo    463:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    464:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
                    465:                return (-1);
                    466:        }
                    467:
                    468:        if (cvs_buf_putc(bp, '\0') < 0)
                    469:                return (-1);
                    470:
                    471:        filec = (char *)cvs_buf_release(bp);
                    472:
1.73      xsa       473:        /* Get RCS patch */
1.63      niallo    474:        if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
                    475:                cvs_log(LP_ERR, "failed to get diff");
                    476:                return (-1);
                    477:        }
                    478:
                    479:        /*
                    480:         * If -f is not specified and there are no differences, tell
                    481:         * the user and revert to latest version.
                    482:         */
                    483:        if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
                    484:                checkin_revert(pb);
                    485:                return (0);
                    486:        }
                    487:
1.73      xsa       488:        /* If no log message specified, get it interactively. */
1.63      niallo    489:        if (pb->flags & INTERACTIVE)
                    490:                pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
                    491:
1.82      joris     492:        if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63      niallo    493:                if (rcs_errno != RCS_ERR_NOENT)
1.82      joris     494:                        cvs_log(LP_WARN, "failed to remove lock");
                    495:                else if (!(pb->flags & CO_LOCK))
                    496:                        cvs_log(LP_WARN, "previous revision was not locked; "
                    497:                            "ignoring -l option");
1.63      niallo    498:        }
                    499:
1.73      xsa       500:        /* Current head revision gets the RCS patch as rd_text */
1.63      niallo    501:        if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1) {
                    502:                cvs_log(LP_ERR,
                    503:                    "failed to set new rd_text for head rev");
                    504:                exit (1);
                    505:        }
                    506:
                    507:        /*
                    508:         * Set the date of the revision to be the last modification
                    509:         * time of the working file if -d has no argument.
                    510:         */
                    511:        if (pb->date == DATE_MTIME
                    512:            && (checkin_mtimedate(pb) < 0))
                    513:                return (-1);
                    514:
1.73      xsa       515:        /* Now add our new revision */
1.63      niallo    516:        if (rcs_rev_add(pb->file,
                    517:            (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82      joris     518:            pb->rcs_msg, pb->date, pb->author) != 0) {
1.63      niallo    519:                cvs_log(LP_ERR, "failed to add new revision");
                    520:                return (-1);
                    521:        }
                    522:
                    523:        /*
                    524:         * If we are checking in to a non-default (ie user-specified)
                    525:         * revision, set head to this revision.
                    526:         */
                    527:        if (pb->newrev != NULL)
                    528:                rcs_head_set(pb->file, pb->newrev);
                    529:        else
                    530:                pb->newrev = pb->file->rf_head;
                    531:
1.73      xsa       532:        /* New head revision has to contain entire file; */
1.63      niallo    533:         if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1) {
                    534:                cvs_log(LP_ERR, "failed to set new head revision");
                    535:                exit(1);
                    536:        }
                    537:
1.73      xsa       538:        /* Attach a symbolic name to this revision if specified. */
1.63      niallo    539:        if (pb->symbol != NULL
                    540:            && (checkin_attach_symbol(pb) < 0))
                    541:                return (-1);
                    542:
1.73      xsa       543:        /* Set the state of this revision if specified. */
1.63      niallo    544:        if (pb->state != NULL)
                    545:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    546:
1.84      joris     547:        xfree(pb->deltatext);
                    548:        xfree(filec);
1.63      niallo    549:        (void)unlink(pb->filename);
                    550:
1.73      xsa       551:        /* Do checkout if -u or -l are specified. */
1.63      niallo    552:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    553:            && !(pb->flags & CI_DEFAULT))
                    554:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82      joris     555:                    pb->username, pb->author, NULL);
1.63      niallo    556:
                    557:        /* File will NOW be synced */
                    558:        rcs_close(pb->file);
                    559:
                    560:        if (pb->flags & INTERACTIVE) {
1.84      joris     561:                xfree(pb->rcs_msg);
1.63      niallo    562:                pb->rcs_msg = NULL;
                    563:        }
                    564:        return (0);
                    565: }
                    566:
                    567: /*
1.58      niallo    568:  * checkin_init()
1.65      xsa       569:  *
1.58      niallo    570:  * Does an initial check in, just enough to create the new ,v file
1.63      niallo    571:  * On success, return 0. On error return -1.
1.58      niallo    572:  */
1.63      niallo    573: static int
1.58      niallo    574: checkin_init(struct checkin_params *pb)
                    575: {
1.80      niallo    576:        BUF *bp, *dp;
                    577:        char *filec;
                    578:        const char *rcs_desc;
1.58      niallo    579:
1.73      xsa       580:        /* Load file contents */
1.58      niallo    581:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    582:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.63      niallo    583:                return (-1);
1.58      niallo    584:        }
                    585:
                    586:        if (cvs_buf_putc(bp, '\0') < 0)
1.63      niallo    587:                return (-1);
1.58      niallo    588:
                    589:        filec = (char *)cvs_buf_release(bp);
                    590:
1.73      xsa       591:        /* Get description from user */
1.80      niallo    592:        if (pb->description == NULL)
                    593:                rcs_desc = (const char *)checkin_getdesc();
                    594:        else {
                    595:                if (*pb->description == '-') {
                    596:                        pb->description++;
1.81      niallo    597:                        rcs_desc = (const char *)pb->description;
                    598:                } else {
1.80      niallo    599:                        dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
                    600:                        if (dp == NULL) {
                    601:                                cvs_log(LP_ERR,
                    602:                                    "failed to load description file '%s'",
                    603:                                    pb->description);
1.84      joris     604:                                xfree(filec);
1.80      niallo    605:                                return (-1);
                    606:                        }
                    607:                        if (cvs_buf_putc(dp, '\0') < 0) {
1.84      joris     608:                                xfree(filec);
1.80      niallo    609:                                return (-1);
                    610:                        }
                    611:                        rcs_desc = (const char *)cvs_buf_release(dp);
                    612:                }
                    613:        }
1.58      niallo    614:        rcs_desc_set(pb->file, rcs_desc);
                    615:
1.73      xsa       616:        /* Now add our new revision */
1.82      joris     617:        if (rcs_rev_add(pb->file, RCS_HEAD_REV, LOG_INIT, -1, pb->author) != 0) {
1.58      niallo    618:                cvs_log(LP_ERR, "failed to add new revision");
1.63      niallo    619:                return (-1);
                    620:        }
                    621:        /*
                    622:         * If we are checking in to a non-default (ie user-specified)
                    623:         * revision, set head to this revision.
                    624:         */
                    625:        if (pb->newrev != NULL)
                    626:                rcs_head_set(pb->file, pb->newrev);
                    627:        else
                    628:                pb->newrev = pb->file->rf_head;
                    629:
1.73      xsa       630:        /* New head revision has to contain entire file; */
1.66      niallo    631:        if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63      niallo    632:                cvs_log(LP_ERR, "failed to set new head revision");
                    633:                return (-1);
1.58      niallo    634:        }
1.73      xsa       635:        /* Attach a symbolic name to this revision if specified. */
1.66      niallo    636:        if (pb->symbol != NULL
                    637:            && (checkin_attach_symbol(pb) < 0))
                    638:                return (-1);
                    639:
1.73      xsa       640:        /* Set the state of this revision if specified. */
1.66      niallo    641:        if (pb->state != NULL)
                    642:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    643:
1.84      joris     644:        xfree(filec);
1.66      niallo    645:        (void)unlink(pb->filename);
                    646:
1.73      xsa       647:        /* Do checkout if -u or -l are specified. */
1.66      niallo    648:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    649:            && !(pb->flags & CI_DEFAULT))
                    650:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82      joris     651:                    pb->username, pb->author, NULL);
1.63      niallo    652:
1.66      niallo    653:        /* File will NOW be synced */
                    654:        rcs_close(pb->file);
1.63      niallo    655:        return (0);
1.58      niallo    656: }
                    657:
1.59      niallo    658: /*
                    659:  * checkin_attach_symbol()
                    660:  *
                    661:  * Attempt to attach the specified symbol to the revision.
                    662:  * On success, return 0. On error return -1.
                    663:  */
1.58      niallo    664: static int
                    665: checkin_attach_symbol(struct checkin_params *pb)
                    666: {
                    667:        char rbuf[16];
                    668:        int ret;
                    669:        if (verbose == 1)
                    670:                printf("symbol: %s\n", pb->symbol);
                    671:        if (pb->flags & CI_SYMFORCE)
                    672:                rcs_sym_remove(pb->file, pb->symbol);
                    673:        if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
                    674:            && (rcs_errno == RCS_ERR_DUPENT)) {
                    675:                rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
                    676:                    rbuf, sizeof(rbuf));
                    677:                cvs_log(LP_ERR,
                    678:                    "symbolic name %s already bound to %s",
                    679:                    pb->symbol, rbuf);
                    680:                rcs_close(pb->file);
                    681:                return (-1);
                    682:        } else if (ret == -1) {
                    683:                cvs_log(LP_ERR, "problem adding symbol: %s",
                    684:                    pb->symbol);
                    685:                rcs_close(pb->file);
                    686:                return (-1);
                    687:        }
                    688:        return (0);
                    689: }
                    690:
1.59      niallo    691: /*
                    692:  * checkin_revert()
                    693:  *
                    694:  * If there are no differences between the working file and the latest revision
                    695:  * and the -f flag is not specified, simply revert to the latest version and
                    696:  * warn the user.
                    697:  *
                    698:  */
1.58      niallo    699: static void
                    700: checkin_revert(struct checkin_params *pb)
                    701: {
                    702:        char rbuf[16];
                    703:
                    704:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
                    705:        cvs_log(LP_WARN,
                    706:            "file is unchanged; reverting to previous revision %s",
                    707:            rbuf);
                    708:        (void)unlink(pb->filename);
                    709:        if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    710:                checkout_rev(pb->file, pb->frev, pb->filename,
1.82      joris     711:                    pb->flags, pb->username, pb->author, NULL);
                    712:        rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58      niallo    713:        rcs_close(pb->file);
                    714:        if (verbose == 1)
                    715:                printf("done\n");
                    716: }
                    717:
1.59      niallo    718: /*
                    719:  * checkin_checklock()
                    720:  *
                    721:  * Check for the existence of a lock on the file.  If there are no locks, or it
                    722:  * is not locked by the correct user, return -1.  Otherwise, return 0.
                    723:  */
1.58      niallo    724: static int
                    725: checkin_checklock(struct checkin_params *pb)
                    726: {
                    727:        struct rcs_lock *lkp;
                    728:
1.82      joris     729:        TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
                    730:                if ((!strcmp(lkp->rl_name, pb->username)) &&
                    731:                    (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
                    732:                        return (0);
1.58      niallo    733:        }
1.32      joris     734:
1.82      joris     735:        cvs_log(LP_ERR,
                    736:            "%s: no lock set by %s", pb->file->rf_path, pb->username);
                    737:        rcs_close(pb->file);
                    738:        return (-1);
1.61      niallo    739: }
                    740:
                    741: /*
1.62      niallo    742:  * checkin_mtimedate()
1.61      niallo    743:  *
                    744:  * Set the date of the revision to be the last modification
1.62      niallo    745:  * time of the working file.
1.61      niallo    746:  *
                    747:  * On success, return 0. On error return -1.
                    748:  */
                    749: static int
1.62      niallo    750: checkin_mtimedate(struct checkin_params *pb)
1.61      niallo    751: {
                    752:        struct stat sb;
                    753:        if (stat(pb->filename, &sb) != 0) {
1.72      xsa       754:                cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61      niallo    755:                rcs_close(pb->file);
                    756:                return (-1);
                    757:        }
                    758:        pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58      niallo    759:        return (0);
1.63      niallo    760: }
                    761:
                    762: /*
1.75      niallo    763:  * checkin_choose_rcsfile()
1.63      niallo    764:  *
                    765:  * Given a relative filename, decide where the corresponding ,v file
1.65      xsa       766:  * should be.
1.63      niallo    767:  *
                    768:  * Returns pointer to a char array on success, NULL on failure.
                    769:  */
                    770: static char *
                    771: checkin_choose_rcsfile(const char *filename)
                    772: {
1.76      niallo    773:        char name[MAXPATHLEN], *basepath;
                    774:        const char *ptr;
1.63      niallo    775:        size_t len;
                    776:        struct stat sb;
                    777:
1.84      joris     778:        basepath = xmalloc(MAXPATHLEN);
1.76      niallo    779:        if (strchr(filename, '/') == NULL) {
                    780:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    781:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    782:                        /* <path>/RCS/<filename>,v */
                    783:                        strlcat(basepath, filename, MAXPATHLEN);
                    784:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    785:                } else {
                    786:                        /* <path>/<filename>,v */
                    787:                        memset(basepath, '\0', MAXPATHLEN);
                    788:                        strlcpy(basepath, filename, MAXPATHLEN);
                    789:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    790:                }
                    791:        } else {
                    792:                ptr = filename;
                    793:                /* Walk backwards till we find the base directory */
                    794:                len = strlen(filename);
                    795:                ptr += len + 1;
                    796:                while (filename[len] != '/') {
                    797:                        len--;
                    798:                        ptr--;
                    799:                }
1.63      niallo    800:                /*
                    801:                 * Need two bytes extra for trailing slash and
                    802:                 * NUL-termination.
                    803:                 */
                    804:                len += 2;
1.76      niallo    805:                if (len > MAXPATHLEN) {
1.84      joris     806:                        xfree(basepath);
1.63      niallo    807:                        return (NULL);
                    808:                }
1.76      niallo    809:                strlcpy(basepath, filename, len);
                    810:                strlcpy(name, ptr, MAXPATHLEN);
1.63      niallo    811:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    812:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    813:                        /* <path>/RCS/<filename>,v */
1.76      niallo    814:                        strlcat(basepath, name, MAXPATHLEN);
1.63      niallo    815:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    816:                } else {
                    817:                        /* <path>/<filename>,v */
1.69      xsa       818:                        memset(basepath, '\0', MAXPATHLEN);
1.76      niallo    819:                        strlcpy(basepath, filename, MAXPATHLEN);
1.63      niallo    820:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    821:                }
1.76      niallo    822:        }
                    823:        return (basepath);
1.1       niallo    824: }