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

Annotation of src/usr.bin/cvs/client.c, Revision 1.106

1.106   ! joris       1: /*     $OpenBSD: client.c,v 1.105 2008/02/11 20:33:11 tobias Exp $     */
1.1       jfb         2: /*
1.9       joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.9       joris       5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       jfb        16:  */
                     17:
1.59      otto       18: #include <sys/param.h>
                     19: #include <sys/dirent.h>
                     20: #include <sys/stat.h>
1.97      tobias     21: #include <sys/time.h>
1.59      otto       22:
                     23: #include <errno.h>
                     24: #include <fcntl.h>
                     25: #include <libgen.h>
                     26: #include <limits.h>
                     27: #include <pwd.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30: #include <unistd.h>
1.1       jfb        31:
                     32: #include "cvs.h"
1.9       joris      33: #include "remote.h"
1.1       jfb        34:
1.9       joris      35: struct cvs_req cvs_requests[] = {
                     36:        /* this is what our client will use, the server should support it */
                     37:        { "Root",               1,      cvs_server_root, REQ_NEEDED },
                     38:        { "Valid-responses",    1,      cvs_server_validresp, REQ_NEEDED },
                     39:        { "valid-requests",     1,      cvs_server_validreq, REQ_NEEDED },
                     40:        { "Directory",          0,      cvs_server_directory, REQ_NEEDED },
1.66      joris      41:        { "Static-directory",   0,      cvs_server_static_directory,
                     42:            REQ_NEEDED | REQ_NEEDDIR },
                     43:        { "Sticky",             0,      cvs_server_sticky,
                     44:            REQ_NEEDED | REQ_NEEDDIR },
                     45:        { "Entry",              0,      cvs_server_entry,
                     46:            REQ_NEEDED | REQ_NEEDDIR },
                     47:        { "Modified",           0,      cvs_server_modified,
                     48:            REQ_NEEDED | REQ_NEEDDIR },
1.9       joris      49:        { "UseUnchanged",       0,      cvs_server_useunchanged, REQ_NEEDED },
1.66      joris      50:        { "Unchanged",          0,      cvs_server_unchanged,
                     51:            REQ_NEEDED | REQ_NEEDDIR },
1.9       joris      52:        { "Questionable",       0,      cvs_server_questionable, REQ_NEEDED },
                     53:        { "Argument",           0,      cvs_server_argument, REQ_NEEDED },
1.24      xsa        54:        { "Argumentx",          0,      cvs_server_argumentx, REQ_NEEDED },
                     55:        { "Global_option",      0,      cvs_server_globalopt, REQ_NEEDED },
1.26      xsa        56:        { "Set",                0,      cvs_server_set, REQ_NEEDED },
1.1       jfb        57:
1.9       joris      58:        /*
                     59:         * used to tell the server what is going on in our
                     60:         * working copy, unsupported until we are told otherwise
                     61:         */
                     62:        { "Max-dotdot",                 0,      NULL, 0 },
                     63:        { "Checkin-prog",               0,      NULL, 0 },
                     64:        { "Update-prog",                0,      NULL, 0 },
                     65:        { "Kopt",                       0,      NULL, 0 },
                     66:        { "Checkin-time",               0,      NULL, 0 },
                     67:        { "Is-modified",                0,      NULL, 0 },
                     68:        { "Notify",                     0,      NULL, 0 },
                     69:        { "Case",                       0,      NULL, 0 },
                     70:        { "Gzip-stream",                0,      NULL, 0 },
1.25      xsa        71:        { "wrapper-sendme-rcsOptions",  0,      NULL, 0 },
1.9       joris      72:        { "Kerberos-encrypt",           0,      NULL, 0 },
                     73:        { "Gssapi-encrypt",             0,      NULL, 0 },
                     74:        { "Gssapi-authenticate",        0,      NULL, 0 },
                     75:        { "expand-modules",             0,      NULL, 0 },
                     76:
                     77:        /* commands that might be supported */
1.95      xsa        78:        { "ci",                 0,      cvs_server_commit,      REQ_NEEDDIR },
                     79:        { "co",                 0,      cvs_server_checkout,    REQ_NEEDDIR },
                     80:        { "update",             0,      cvs_server_update,      REQ_NEEDDIR },
                     81:        { "diff",               0,      cvs_server_diff,        REQ_NEEDDIR },
                     82:        { "log",                0,      cvs_server_log,         REQ_NEEDDIR },
                     83:        { "rlog",               0,      cvs_server_rlog, 0 },
                     84:        { "add",                0,      cvs_server_add,         REQ_NEEDDIR },
                     85:        { "remove",             0,      cvs_server_remove,      REQ_NEEDDIR },
                     86:        { "update-patches",     0,      cvs_server_update_patches, 0 },
                     87:        { "gzip-file-contents", 0,      NULL, 0 },
                     88:        { "status",             0,      cvs_server_status,      REQ_NEEDDIR },
1.99      tobias     89:        { "rdiff",              0,      cvs_server_rdiff, 0 },
1.95      xsa        90:        { "tag",                0,      cvs_server_tag,         REQ_NEEDDIR },
                     91:        { "rtag",               0,      cvs_server_rtag, 0 },
                     92:        { "import",             0,      cvs_server_import,      REQ_NEEDDIR },
                     93:        { "admin",              0,      cvs_server_admin,       REQ_NEEDDIR },
                     94:        { "export",             0,      cvs_server_export,      REQ_NEEDDIR },
                     95:        { "history",            0,      NULL, 0 },
                     96:        { "release",            0,      cvs_server_release,     REQ_NEEDDIR },
                     97:        { "watch-on",           0,      NULL, 0 },
                     98:        { "watch-off",          0,      NULL, 0 },
                     99:        { "watch-add",          0,      NULL, 0 },
                    100:        { "watch-remove",       0,      NULL, 0 },
                    101:        { "watchers",           0,      NULL, 0 },
                    102:        { "editors",            0,      NULL, 0 },
                    103:        { "init",               0,      cvs_server_init, 0 },
                    104:        { "annotate",           0,      cvs_server_annotate,    REQ_NEEDDIR },
                    105:        { "rannotate",          0,      cvs_server_rannotate, 0 },
                    106:        { "noop",               0,      NULL, 0 },
                    107:        { "version",            0,      cvs_server_version, 0 },
                    108:        { "",                   -1,     NULL, 0 }
1.9       joris     109: };
                    110:
1.98      joris     111: static void     client_check_directory(char *, char *);
1.16      xsa       112: static char    *client_get_supported_responses(void);
                    113: static char    *lastdir = NULL;
                    114: static int      end_of_response = 0;
                    115:
                    116: static void    cvs_client_initlog(void);
                    117:
                    118: /*
                    119:  * File descriptors for protocol logging when the CVS_CLIENT_LOG environment
                    120:  * variable is set.
                    121:  */
                    122: static int     cvs_client_logon = 0;
1.42      joris     123: int    cvs_client_inlog_fd = -1;
                    124: int    cvs_client_outlog_fd = -1;
1.16      xsa       125:
1.9       joris     126:
1.37      joris     127: int server_response = SERVER_OK;
                    128:
1.9       joris     129: static char *
                    130: client_get_supported_responses(void)
                    131: {
                    132:        BUF *bp;
                    133:        char *d;
                    134:        int i, first;
                    135:
                    136:        first = 0;
1.105     tobias    137:        bp = cvs_buf_alloc(512);
1.9       joris     138:        for (i = 0; cvs_responses[i].supported != -1; i++) {
                    139:                if (cvs_responses[i].hdlr == NULL)
                    140:                        continue;
                    141:
                    142:                if (first != 0)
                    143:                        cvs_buf_append(bp, " ", 1);
                    144:                else
                    145:                        first++;
                    146:                cvs_buf_append(bp, cvs_responses[i].name,
                    147:                    strlen(cvs_responses[i].name));
                    148:        }
                    149:
                    150:        cvs_buf_putc(bp, '\0');
                    151:        d = cvs_buf_release(bp);
                    152:        return (d);
                    153: }
1.1       jfb       154:
1.10      joris     155: static void
1.98      joris     156: client_check_directory(char *data, char *repository)
1.10      joris     157: {
                    158:        CVSENTRIES *entlist;
1.102     xsa       159:        char *entry, *parent, *base, *p;
1.10      joris     160:
                    161:        STRIP_SLASH(data);
                    162:
1.98      joris     163:        /* first directory we get is our module root */
1.101     joris     164:        if (module_repo_root == NULL && checkout_target_dir != NULL) {
1.98      joris     165:                p = repository + strlen(current_cvsroot->cr_dir) + 1;
                    166:                module_repo_root = xstrdup(p);
                    167:                p = strrchr(module_repo_root, '/');
                    168:                if (p != NULL)
                    169:                        *p = '\0';
                    170:        }
                    171:
1.69      joris     172:        cvs_mkpath(data, NULL);
1.10      joris     173:
1.90      tobias    174:        if (cvs_cmdop == CVS_OP_EXPORT)
                    175:                return;
                    176:
1.10      joris     177:        if ((base = basename(data)) == NULL)
                    178:                fatal("client_check_directory: overflow");
                    179:
                    180:        if ((parent = dirname(data)) == NULL)
                    181:                fatal("client_check_directory: overflow");
1.11      joris     182:
                    183:        if (!strcmp(parent, "."))
                    184:                return;
1.10      joris     185:
1.102     xsa       186:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    187:        cvs_ent_line_str(base, NULL, NULL, NULL, NULL, 1, 0, entry,
                    188:            CVS_ENT_MAXLINELEN);
1.10      joris     189:
                    190:        entlist = cvs_ent_open(parent);
                    191:        cvs_ent_add(entlist, entry);
                    192:        cvs_ent_close(entlist, ENT_SYNC);
1.102     xsa       193:
                    194:        xfree(entry);
1.10      joris     195: }
                    196:
1.9       joris     197: void
                    198: cvs_client_connect_to_server(void)
                    199: {
1.26      xsa       200:        struct cvs_var *vp;
1.9       joris     201:        char *cmd, *argv[9], *resp;
                    202:        int ifd[2], ofd[2], argc;
1.45      joris     203:
                    204:        if (cvs_server_active == 1)
                    205:                fatal("cvs_client_connect: I was already connected to server");
1.1       jfb       206:
1.9       joris     207:        switch (current_cvsroot->cr_method) {
                    208:        case CVS_METHOD_PSERVER:
                    209:        case CVS_METHOD_KSERVER:
                    210:        case CVS_METHOD_GSERVER:
                    211:        case CVS_METHOD_FORK:
                    212:        case CVS_METHOD_EXT:
1.14      joris     213:                fatal("the specified connection method is not supported");
1.9       joris     214:        default:
                    215:                break;
                    216:        }
                    217:
                    218:        if (pipe(ifd) == -1)
                    219:                fatal("cvs_client_connect: %s", strerror(errno));
                    220:        if (pipe(ofd) == -1)
                    221:                fatal("cvs_client_connect: %s", strerror(errno));
                    222:
                    223:        switch (fork()) {
                    224:        case -1:
                    225:                fatal("cvs_client_connect: fork failed: %s", strerror(errno));
                    226:        case 0:
                    227:                if (dup2(ifd[0], STDIN_FILENO) == -1)
                    228:                        fatal("cvs_client_connect: %s", strerror(errno));
                    229:                if (dup2(ofd[1], STDOUT_FILENO) == -1)
                    230:                        fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       231:
1.9       joris     232:                close(ifd[1]);
                    233:                close(ofd[0]);
1.1       jfb       234:
1.9       joris     235:                if ((cmd = getenv("CVS_SERVER")) == NULL)
                    236:                        cmd = CVS_SERVER_DEFAULT;
1.1       jfb       237:
                    238:                argc = 0;
                    239:                argv[argc++] = cvs_rsh;
                    240:
1.9       joris     241:                if (current_cvsroot->cr_user != NULL) {
1.1       jfb       242:                        argv[argc++] = "-l";
1.9       joris     243:                        argv[argc++] = current_cvsroot->cr_user;
1.1       jfb       244:                }
                    245:
1.9       joris     246:                argv[argc++] = current_cvsroot->cr_host;
                    247:                argv[argc++] = cmd;
1.1       jfb       248:                argv[argc++] = "server";
                    249:                argv[argc] = NULL;
                    250:
1.9       joris     251:                cvs_log(LP_TRACE, "connecting to server %s",
                    252:                    current_cvsroot->cr_host);
                    253:
1.1       jfb       254:                execvp(argv[0], argv);
1.9       joris     255:                fatal("cvs_client_connect: failed to execute cvs server");
                    256:        default:
                    257:                break;
1.1       jfb       258:        }
                    259:
1.9       joris     260:        close(ifd[0]);
                    261:        close(ofd[1]);
                    262:
                    263:        if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
                    264:                fatal("cvs_client_connect: %s", strerror(errno));
                    265:        if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
                    266:                fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       267:
1.9       joris     268:        setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
                    269:        setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
1.1       jfb       270:
1.16      xsa       271:        cvs_client_initlog();
                    272:
1.18      xsa       273:        if (cvs_cmdop != CVS_OP_INIT)
                    274:                cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
1.1       jfb       275:
1.9       joris     276:        resp = client_get_supported_responses();
                    277:        cvs_client_send_request("Valid-responses %s", resp);
                    278:        xfree(resp);
1.1       jfb       279:
1.9       joris     280:        cvs_client_send_request("valid-requests");
                    281:        cvs_client_get_responses();
1.1       jfb       282:
1.28      xsa       283:        cvs_client_send_request("UseUnchanged");
                    284:
1.20      xsa       285:        if (cvs_nolog == 1)
                    286:                cvs_client_send_request("Global_option -l");
                    287:
1.19      xsa       288:        if (cvs_noexec == 1)
                    289:                cvs_client_send_request("Global_option -n");
                    290:
1.76      tobias    291:        switch (verbosity) {
                    292:        case 0:
1.19      xsa       293:                cvs_client_send_request("Global_option -Q");
1.76      tobias    294:                break;
                    295:        case 1:
                    296:                /* Be quiet. This is the default in OpenCVS. */
                    297:                cvs_client_send_request("Global_option -q");
                    298:                break;
                    299:        default:
                    300:                break;
                    301:        }
1.19      xsa       302:
                    303:        if (cvs_readonly == 1)
                    304:                cvs_client_send_request("Global_option -r");
                    305:
                    306:        if (cvs_trace == 1)
1.9       joris     307:                cvs_client_send_request("Global_option -t");
1.26      xsa       308:
                    309:        /* XXX: If 'Set' is supported? */
                    310:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    311:                cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1       jfb       312: }
                    313:
1.9       joris     314: void
                    315: cvs_client_send_request(char *fmt, ...)
                    316: {
                    317:        va_list ap;
                    318:        char *data, *s;
                    319:        struct cvs_req *req;
                    320:
                    321:        va_start(ap, fmt);
                    322:        vasprintf(&data, fmt, ap);
                    323:        va_end(ap);
1.1       jfb       324:
1.9       joris     325:        if ((s = strchr(data, ' ')) != NULL)
                    326:                *s = '\0';
1.1       jfb       327:
1.9       joris     328:        req = cvs_remote_get_request_info(data);
                    329:        if (req == NULL)
                    330:                fatal("'%s' is an unknown request", data);
1.1       jfb       331:
1.9       joris     332:        if (req->supported != 1)
                    333:                fatal("remote cvs server does not support '%s'", data);
1.1       jfb       334:
1.9       joris     335:        if (s != NULL)
                    336:                *s = ' ';
1.1       jfb       337:
1.12      joris     338:        cvs_log(LP_TRACE, "%s", data);
1.16      xsa       339:
1.9       joris     340:        cvs_remote_output(data);
                    341:        xfree(data);
                    342: }
1.1       jfb       343:
1.9       joris     344: void
                    345: cvs_client_read_response(void)
1.1       jfb       346: {
1.9       joris     347:        char *cmd, *data;
                    348:        struct cvs_resp *resp;
1.1       jfb       349:
1.9       joris     350:        cmd = cvs_remote_input();
                    351:        if ((data = strchr(cmd, ' ')) != NULL)
                    352:                (*data++) = '\0';
1.1       jfb       353:
1.9       joris     354:        resp = cvs_remote_get_response_info(cmd);
                    355:        if (resp == NULL)
                    356:                fatal("response '%s' is not supported by our client", cmd);
1.1       jfb       357:
1.9       joris     358:        if (resp->hdlr == NULL)
                    359:                fatal("opencvs client does not support '%s'", cmd);
1.16      xsa       360:
1.9       joris     361:        (*resp->hdlr)(data);
1.16      xsa       362:
1.9       joris     363:        xfree(cmd);
1.1       jfb       364: }
                    365:
1.9       joris     366: void
                    367: cvs_client_get_responses(void)
                    368: {
                    369:        while (end_of_response != 1)
                    370:                cvs_client_read_response();
1.1       jfb       371:
1.9       joris     372:        end_of_response = 0;
1.72      joris     373: }
                    374:
                    375: void
                    376: cvs_client_send_logmsg(char *msg)
                    377: {
                    378:        char *buf, *p, *q;
                    379:
                    380:        (void)xasprintf(&buf, "%s\n", msg);
                    381:
                    382:        cvs_client_send_request("Argument -m");
                    383:        if ((p = strchr(buf, '\n')) != NULL)
                    384:                *p++ = '\0';
                    385:        cvs_client_send_request("Argument %s", buf);
                    386:        for (q = p; p != NULL; q = p) {
                    387:                if ((p = strchr(q, '\n')) != NULL) {
                    388:                        *p++ = '\0';
                    389:                        cvs_client_send_request("Argumentx %s", q);
                    390:                }
                    391:        }
                    392:
1.73      joris     393:        xfree(buf);
1.9       joris     394: }
1.1       jfb       395:
1.9       joris     396: void
                    397: cvs_client_senddir(const char *dir)
1.1       jfb       398: {
1.34      xsa       399:        struct stat st;
1.29      xsa       400:        int nb;
1.50      otto      401:        char *d, *date, fpath[MAXPATHLEN], repo[MAXPATHLEN], *tag;
1.29      xsa       402:
                    403:        d = NULL;
1.9       joris     404:
                    405:        if (lastdir != NULL && !strcmp(dir, lastdir))
                    406:                return;
1.1       jfb       407:
1.9       joris     408:        cvs_get_repository_path(dir, repo, MAXPATHLEN);
1.31      xsa       409:
1.81      tobias    410:        if (cvs_cmdop != CVS_OP_RLOG)
1.70      xsa       411:                cvs_client_send_request("Directory %s\n%s", dir, repo);
1.31      xsa       412:
1.58      xsa       413:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    414:            dir, CVS_PATH_STATICENTRIES);
1.34      xsa       415:
                    416:        if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
                    417:                cvs_client_send_request("Static-directory");
                    418:
1.29      xsa       419:        d = xstrdup(dir);
                    420:        cvs_parse_tagfile(d, &tag, &date, &nb);
                    421:
                    422:        if (tag != NULL || date != NULL) {
                    423:                char buf[128];
                    424:
1.82      chl       425:                if (tag != NULL && nb != 0) {
1.29      xsa       426:                        if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
                    427:                                fatal("cvs_client_senddir: truncation");
                    428:                } else if (tag != NULL) {
                    429:                        if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
                    430:                                fatal("cvs_client_senddir: truncation");
                    431:                } else {
                    432:                        if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
                    433:                                fatal("cvs_client_senddir: truncation");
                    434:                }
                    435:
                    436:                if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
                    437:                        fatal("cvs_client_senddir: truncation");
                    438:
                    439:                cvs_client_send_request("Sticky %s", buf);
                    440:
                    441:                if (tag != NULL)
                    442:                        xfree(tag);
                    443:                if (date != NULL)
                    444:                        xfree(date);
                    445:        }
                    446:        if (d != NULL)
                    447:                xfree(d);
1.1       jfb       448:
1.9       joris     449:        if (lastdir != NULL)
                    450:                xfree(lastdir);
                    451:        lastdir = xstrdup(dir);
                    452: }
1.1       jfb       453:
1.9       joris     454: void
                    455: cvs_client_sendfile(struct cvs_file *cf)
1.1       jfb       456: {
1.9       joris     457:        size_t len;
1.103     joris     458:        struct tm *datetm;
1.92      xsa       459:        char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[CVS_REV_BUFSZ];
1.1       jfb       460:
1.9       joris     461:        if (cf->file_type != CVS_FILE)
                    462:                return;
1.1       jfb       463:
1.9       joris     464:        cvs_client_senddir(cf->file_wd);
                    465:        cvs_remote_classify_file(cf);
                    466:
1.10      joris     467:        if (cf->file_type == CVS_DIR)
                    468:                return;
                    469:
1.85      tobias    470:        if (cf->file_ent != NULL && cvs_cmdop != CVS_OP_IMPORT) {
1.9       joris     471:                if (cf->file_status == FILE_ADDED) {
                    472:                        len = strlcpy(rev, "0", sizeof(rev));
                    473:                        if (len >= sizeof(rev))
                    474:                                fatal("cvs_client_sendfile: truncation");
                    475:
                    476:                        len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    477:                        if (len >= sizeof(timebuf))
                    478:                                fatal("cvs_client_sendfile: truncation");
                    479:
                    480:                        len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
                    481:                        if (len >= sizeof(timebuf))
                    482:                                fatal("cvs_client_sendfile: truncation");
                    483:                } else {
                    484:                        rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
                    485:                        ctime_r(&cf->file_ent->ce_mtime, timebuf);
                    486:                }
1.1       jfb       487:
1.9       joris     488:                if (cf->file_ent->ce_conflict == NULL) {
1.88      tobias    489:                        timebuf[strcspn(timebuf, "\n")] = '\0';
1.9       joris     490:                } else {
                    491:                        len = strlcpy(timebuf, cf->file_ent->ce_conflict,
                    492:                            sizeof(timebuf));
                    493:                        if (len >= sizeof(timebuf))
                    494:                                fatal("cvs_client_sendfile: truncation");
1.56      joris     495:                        len = strlcat(timebuf, "+", sizeof(timebuf));
                    496:                        if (len >= sizeof(timebuf))
                    497:                                fatal("cvs_client_sendfile: truncation");
1.9       joris     498:                }
1.1       jfb       499:
1.9       joris     500:                sticky[0] = '\0';
                    501:                if (cf->file_ent->ce_tag != NULL) {
1.57      xsa       502:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.9       joris     503:                            cf->file_ent->ce_tag);
1.103     joris     504:                } else if (cf->file_ent->ce_date != -1) {
                    505:                        datetm = gmtime(&(cf->file_ent->ce_date));
1.104     xsa       506:                        (void)strftime(sticky, sizeof(sticky),
                    507:                            "D"CVS_DATE_FMT, datetm);
1.1       jfb       508:                }
                    509:
1.55      otto      510:                cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
1.9       joris     511:                    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
1.53      xsa       512:                    rev, timebuf, cf->file_ent->ce_opts ?
                    513:                    cf->file_ent->ce_opts : "", sticky);
1.9       joris     514:        }
                    515:
1.84      tobias    516:        if (cvs_cmdop == CVS_OP_ADD)
                    517:                cf->file_status = FILE_MODIFIED;
                    518:
1.9       joris     519:        switch (cf->file_status) {
                    520:        case FILE_UNKNOWN:
                    521:                if (cf->fd != -1)
                    522:                        cvs_client_send_request("Questionable %s",
                    523:                            cf->file_name);
                    524:                break;
                    525:        case FILE_ADDED:
                    526:        case FILE_MODIFIED:
                    527:                cvs_client_send_request("Modified %s", cf->file_name);
1.106   ! joris     528:                cvs_remote_send_file(cf->file_path, cf->fd);
1.9       joris     529:                break;
                    530:        case FILE_UPTODATE:
                    531:                cvs_client_send_request("Unchanged %s", cf->file_name);
                    532:                break;
1.1       jfb       533:        }
1.9       joris     534: }
1.1       jfb       535:
1.9       joris     536: void
                    537: cvs_client_send_files(char **argv, int argc)
                    538: {
                    539:        int i;
1.1       jfb       540:
1.9       joris     541:        for (i = 0; i < argc; i++)
                    542:                cvs_client_send_request("Argument %s", argv[i]);
                    543: }
1.1       jfb       544:
1.9       joris     545: void
                    546: cvs_client_ok(char *data)
                    547: {
                    548:        end_of_response = 1;
1.37      joris     549:        server_response = SERVER_OK;
1.1       jfb       550: }
                    551:
1.9       joris     552: void
                    553: cvs_client_error(char *data)
                    554: {
                    555:        end_of_response = 1;
1.37      joris     556:        server_response = SERVER_ERROR;
1.9       joris     557: }
1.1       jfb       558:
1.9       joris     559: void
                    560: cvs_client_validreq(char *data)
1.1       jfb       561: {
1.9       joris     562:        int i;
                    563:        char *sp, *ep;
                    564:        struct cvs_req *req;
                    565:
1.62      ray       566:        if ((sp = data) == NULL)
                    567:                fatal("Missing argument for Valid-requests");
                    568:
1.9       joris     569:        do {
                    570:                if ((ep = strchr(sp, ' ')) != NULL)
                    571:                        *ep = '\0';
                    572:
                    573:                req = cvs_remote_get_request_info(sp);
                    574:                if (req != NULL)
                    575:                        req->supported = 1;
                    576:
                    577:                if (ep != NULL)
                    578:                        sp = ep + 1;
                    579:        } while (ep != NULL);
                    580:
                    581:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    582:                req = &cvs_requests[i];
                    583:                if ((req->flags & REQ_NEEDED) &&
                    584:                    req->supported != 1) {
                    585:                        fatal("server does not support required '%s'",
                    586:                            req->name);
                    587:                }
1.1       jfb       588:        }
1.9       joris     589: }
1.1       jfb       590:
1.9       joris     591: void
                    592: cvs_client_e(char *data)
                    593: {
1.62      ray       594:        if (data == NULL)
                    595:                fatal("Missing argument for E");
                    596:
1.80      tobias    597:        fprintf(stderr, "%s\n", data);
1.9       joris     598: }
1.1       jfb       599:
1.9       joris     600: void
                    601: cvs_client_m(char *data)
                    602: {
1.62      ray       603:        if (data == NULL)
                    604:                fatal("Missing argument for M");
                    605:
1.80      tobias    606:        puts(data);
1.9       joris     607: }
1.1       jfb       608:
1.9       joris     609: void
                    610: cvs_client_checkedin(char *data)
                    611: {
                    612:        CVSENTRIES *entlist;
                    613:        struct cvs_ent *ent, *newent;
1.84      tobias    614:        size_t len;
1.103     joris     615:        struct tm *datetm;
1.102     xsa       616:        char *dir, *e, *entry, rev[CVS_REV_BUFSZ];
1.67      xsa       617:        char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
1.1       jfb       618:
1.62      ray       619:        if (data == NULL)
                    620:                fatal("Missing argument for Checked-in");
                    621:
1.9       joris     622:        dir = cvs_remote_input();
1.54      otto      623:        e = cvs_remote_input();
1.9       joris     624:        xfree(dir);
1.1       jfb       625:
1.9       joris     626:        entlist = cvs_ent_open(data);
1.54      otto      627:        newent = cvs_ent_parse(e);
1.9       joris     628:        ent = cvs_ent_get(entlist, newent->ce_name);
1.54      otto      629:        xfree(e);
1.1       jfb       630:
1.9       joris     631:        rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
1.1       jfb       632:
1.9       joris     633:        sticky[0] = '\0';
1.84      tobias    634:        if (ent == NULL) {
                    635:                len = strlcpy(rev, "0", sizeof(rev));
                    636:                if (len >= sizeof(rev))
                    637:                        fatal("cvs_client_sendfile: truncation");
                    638:
                    639:                len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    640:                if (len >= sizeof(timebuf))
                    641:                        fatal("cvs_client_sendfile: truncation");
                    642:
                    643:                len = strlcat(timebuf, newent->ce_name, sizeof(timebuf));
                    644:                if (len >= sizeof(timebuf))
                    645:                        fatal("cvs_client_sendfile: truncation");
                    646:        } else {
                    647:                ctime_r(&ent->ce_mtime, timebuf);
                    648:                timebuf[strcspn(timebuf, "\n")] = '\0';
                    649:
1.103     joris     650:                if (newent->ce_tag != NULL) {
1.84      tobias    651:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.100     tobias    652:                            newent->ce_tag);
1.103     joris     653:                } else if (newent->ce_date != -1) {
                    654:                        datetm = gmtime(&(newent->ce_date));
1.104     xsa       655:                        (void)strftime(sticky, sizeof(sticky),
                    656:                            "D"CVS_DATE_FMT, datetm);
1.103     joris     657:                }
1.84      tobias    658:
                    659:                cvs_ent_free(ent);
                    660:        }
1.1       jfb       661:
1.102     xsa       662:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    663:        cvs_ent_line_str(newent->ce_name, rev, timebuf,
                    664:            newent->ce_opts ? newent->ce_opts : "", sticky, 0,
                    665:            newent->ce_status == CVS_ENT_REMOVED ? 1 : 0,
                    666:            entry, CVS_ENT_MAXLINELEN);
1.9       joris     667:
                    668:        cvs_ent_free(newent);
                    669:        cvs_ent_add(entlist, entry);
                    670:        cvs_ent_close(entlist, ENT_SYNC);
1.102     xsa       671:
                    672:        xfree(entry);
1.1       jfb       673: }
                    674:
1.9       joris     675: void
                    676: cvs_client_updated(char *data)
                    677: {
1.57      xsa       678:        int fd;
1.9       joris     679:        time_t now;
1.51      otto      680:        mode_t fmode, mask;
1.9       joris     681:        size_t flen;
                    682:        CVSENTRIES *ent;
                    683:        struct cvs_ent *e;
                    684:        const char *errstr;
1.103     joris     685:        struct tm *datetm;
1.9       joris     686:        struct timeval tv[2];
1.102     xsa       687:        char repo[MAXPATHLEN], *entry;
1.67      xsa       688:        char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
1.98      joris     689:        char *en, *mode, *len, *rpath;
                    690:        char sticky[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN];
1.9       joris     691:
1.62      ray       692:        if (data == NULL)
                    693:                fatal("Missing argument for Updated");
                    694:
1.9       joris     695:        rpath = cvs_remote_input();
1.54      otto      696:        en = cvs_remote_input();
1.9       joris     697:        mode = cvs_remote_input();
                    698:        len = cvs_remote_input();
                    699:
1.98      joris     700:        client_check_directory(data, rpath);
1.9       joris     701:        cvs_get_repository_path(".", repo, MAXPATHLEN);
1.48      joris     702:
                    703:        STRIP_SLASH(repo);
1.9       joris     704:
                    705:        if (strlen(repo) + 1 > strlen(rpath))
                    706:                fatal("received a repository path that is too short");
                    707:
1.98      joris     708:        (void)xsnprintf(fpath, sizeof(fpath), "%s/%s", data,
                    709:            strrchr(rpath, '/'));
1.9       joris     710:
                    711:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    712:        if (errstr != NULL)
                    713:                fatal("cvs_client_updated: %s: %s", len, errstr);
                    714:        xfree(len);
                    715:
                    716:        cvs_strtomode(mode, &fmode);
                    717:        xfree(mode);
1.51      otto      718:        mask = umask(0);
                    719:        umask(mask);
                    720:        fmode &= ~mask;
1.9       joris     721:
                    722:        time(&now);
1.52      joris     723:        asctime_r(gmtime(&now), timebuf);
1.88      tobias    724:        timebuf[strcspn(timebuf, "\n")] = '\0';
1.9       joris     725:
1.54      otto      726:        e = cvs_ent_parse(en);
                    727:        xfree(en);
1.69      joris     728:
                    729:        sticky[0] = '\0';
1.103     joris     730:        if (e->ce_tag != NULL) {
1.69      joris     731:                (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
1.103     joris     732:        } else if (e->ce_date != -1) {
                    733:                datetm = gmtime(&(e->ce_date));
1.104     xsa       734:                (void)strftime(sticky, sizeof(sticky),
                    735:                    "D"CVS_DATE_FMT, datetm);
1.103     joris     736:        }
1.69      joris     737:
1.9       joris     738:        rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
1.102     xsa       739:
                    740:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    741:        cvs_ent_line_str(e->ce_name, revbuf, timebuf,
                    742:            e->ce_opts ? e->ce_opts : "", sticky, 0, 0,
                    743:            entry, CVS_ENT_MAXLINELEN);
1.9       joris     744:
                    745:        cvs_ent_free(e);
1.90      tobias    746:
                    747:        if (cvs_cmdop != CVS_OP_EXPORT) {
1.98      joris     748:                ent = cvs_ent_open(data);
1.90      tobias    749:                cvs_ent_add(ent, entry);
                    750:                cvs_ent_close(ent, ENT_SYNC);
                    751:        }
1.102     xsa       752:
                    753:        xfree(entry);
1.9       joris     754:
                    755:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    756:                fatal("cvs_client_updated: open: %s: %s",
                    757:                    fpath, strerror(errno));
1.1       jfb       758:
1.46      joris     759:        cvs_remote_receive_file(fd, flen);
1.1       jfb       760:
1.9       joris     761:        tv[0].tv_sec = now;
                    762:        tv[0].tv_usec = 0;
                    763:        tv[1] = tv[0];
1.1       jfb       764:
1.9       joris     765:        if (futimes(fd, tv) == -1)
                    766:                fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1       jfb       767:
1.9       joris     768:        if (fchmod(fd, fmode) == -1)
                    769:                fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1       jfb       770:
1.9       joris     771:        (void)close(fd);
1.1       jfb       772:
1.9       joris     773:        xfree(rpath);
1.1       jfb       774: }
                    775:
1.9       joris     776: void
                    777: cvs_client_merged(char *data)
                    778: {
1.56      joris     779:        int fd;
                    780:        time_t now;
1.83      tobias    781:        mode_t fmode, mask;
1.56      joris     782:        size_t flen;
                    783:        CVSENTRIES *ent;
                    784:        const char *errstr;
                    785:        struct timeval tv[2];
1.67      xsa       786:        char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
1.56      joris     787:        char *len, *fpath, *wdir;
                    788:
1.62      ray       789:        if (data == NULL)
                    790:                fatal("Missing argument for Merged");
                    791:
1.56      joris     792:        rpath = cvs_remote_input();
                    793:        entry = cvs_remote_input();
                    794:        mode = cvs_remote_input();
                    795:        len = cvs_remote_input();
                    796:
1.98      joris     797:        client_check_directory(data, rpath);
                    798:
1.56      joris     799:        repo = xmalloc(MAXPATHLEN);
                    800:        cvs_get_repository_path(".", repo, MAXPATHLEN);
                    801:
                    802:        STRIP_SLASH(repo);
                    803:
                    804:        if (strlen(repo) + 1 > strlen(rpath))
                    805:                fatal("received a repository path that is too short");
                    806:
                    807:        fpath = rpath + strlen(repo) + 1;
                    808:        if ((wdir = dirname(fpath)) == NULL)
                    809:                fatal("cvs_client_merged: dirname: %s", strerror(errno));
                    810:        xfree(repo);
                    811:
                    812:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    813:        if (errstr != NULL)
                    814:                fatal("cvs_client_merged: %s: %s", len, errstr);
                    815:        xfree(len);
                    816:
                    817:        cvs_strtomode(mode, &fmode);
                    818:        xfree(mode);
1.83      tobias    819:        mask = umask(0);
                    820:        umask(mask);
                    821:        fmode &= ~mask;
1.56      joris     822:
                    823:        time(&now);
                    824:        asctime_r(gmtime(&now), timebuf);
1.88      tobias    825:        timebuf[strcspn(timebuf, "\n")] = '\0';
1.56      joris     826:
                    827:        ent = cvs_ent_open(wdir);
                    828:        cvs_ent_add(ent, entry);
                    829:        cvs_ent_close(ent, ENT_SYNC);
1.86      tobias    830:        xfree(entry);
1.56      joris     831:
                    832:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    833:                fatal("cvs_client_merged: open: %s: %s",
                    834:                    fpath, strerror(errno));
                    835:
                    836:        cvs_remote_receive_file(fd, flen);
                    837:
                    838:        tv[0].tv_sec = now;
                    839:        tv[0].tv_usec = 0;
                    840:        tv[1] = tv[0];
                    841:
                    842:        if (futimes(fd, tv) == -1)
                    843:                fatal("cvs_client_merged: futimes: %s", strerror(errno));
                    844:
                    845:        if (fchmod(fd, fmode) == -1)
                    846:                fatal("cvs_client_merged: fchmod: %s", strerror(errno));
                    847:
                    848:        (void)close(fd);
                    849:
                    850:        xfree(rpath);
1.9       joris     851: }
1.1       jfb       852:
1.9       joris     853: void
                    854: cvs_client_removed(char *data)
                    855: {
1.65      joris     856:        CVSENTRIES *entlist;
                    857:        char *rpath, *filename, fpath[MAXPATHLEN];
1.91      tobias    858:
                    859:        if (data == NULL)
                    860:                fatal("Missing argument for Removed");
1.65      joris     861:
                    862:        rpath = cvs_remote_input();
                    863:        if ((filename = strrchr(rpath, '/')) == NULL)
                    864:                fatal("bad rpath in cvs_client_removed: %s", rpath);
                    865:        filename++;
                    866:
                    867:        entlist = cvs_ent_open(data);
                    868:        cvs_ent_remove(entlist, filename);
                    869:        cvs_ent_close(entlist, ENT_SYNC);
                    870:
                    871:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
                    872:        (void)unlink(fpath);
1.13      joris     873:
1.65      joris     874:        xfree(rpath);
1.9       joris     875: }
1.1       jfb       876:
1.9       joris     877: void
                    878: cvs_client_remove_entry(char *data)
1.1       jfb       879: {
1.38      xsa       880:        CVSENTRIES *entlist;
1.44      joris     881:        char *filename, *rpath;
1.41      joris     882:
1.62      ray       883:        if (data == NULL)
                    884:                fatal("Missing argument for Remove-entry");
                    885:
1.41      joris     886:        rpath = cvs_remote_input();
                    887:        if ((filename = strrchr(rpath, '/')) == NULL)
                    888:                fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
1.61      ray       889:        filename++;
1.1       jfb       890:
1.38      xsa       891:        entlist = cvs_ent_open(data);
1.44      joris     892:        cvs_ent_remove(entlist, filename);
1.38      xsa       893:        cvs_ent_close(entlist, ENT_SYNC);
                    894:
1.44      joris     895:        xfree(rpath);
1.36      xsa       896: }
                    897:
                    898: void
                    899: cvs_client_set_static_directory(char *data)
                    900: {
                    901:        FILE *fp;
1.50      otto      902:        char *dir, fpath[MAXPATHLEN];
1.36      xsa       903:
1.62      ray       904:        if (data == NULL)
                    905:                fatal("Missing argument for Set-static-directory");
                    906:
1.36      xsa       907:        STRIP_SLASH(data);
                    908:
                    909:        dir = cvs_remote_input();
                    910:        xfree(dir);
                    911:
1.90      tobias    912:        if (cvs_cmdop == CVS_OP_EXPORT)
                    913:                return;
                    914:
1.58      xsa       915:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    916:            data, CVS_PATH_STATICENTRIES);
1.36      xsa       917:
                    918:        if ((fp = fopen(fpath, "w+")) == NULL) {
                    919:                cvs_log(LP_ERRNO, "%s", fpath);
1.50      otto      920:                return;
1.36      xsa       921:        }
                    922:        (void)fclose(fp);
1.16      xsa       923: }
1.35      xsa       924:
                    925: void
                    926: cvs_client_clear_static_directory(char *data)
                    927: {
1.50      otto      928:        char *dir, fpath[MAXPATHLEN];
1.35      xsa       929:
1.62      ray       930:        if (data == NULL)
                    931:                fatal("Missing argument for Clear-static-directory");
                    932:
1.35      xsa       933:        STRIP_SLASH(data);
                    934:
                    935:        dir = cvs_remote_input();
                    936:        xfree(dir);
                    937:
1.90      tobias    938:        if (cvs_cmdop == CVS_OP_EXPORT)
                    939:                return;
                    940:
1.58      xsa       941:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    942:            data, CVS_PATH_STATICENTRIES);
1.35      xsa       943:
                    944:        (void)cvs_unlink(fpath);
                    945: }
                    946:
                    947: void
                    948: cvs_client_set_sticky(char *data)
                    949: {
                    950:        FILE *fp;
1.50      otto      951:        char *dir, *tag, tagpath[MAXPATHLEN];
1.35      xsa       952:
1.62      ray       953:        if (data == NULL)
                    954:                fatal("Missing argument for Set-sticky");
                    955:
1.35      xsa       956:        STRIP_SLASH(data);
                    957:
                    958:        dir = cvs_remote_input();
                    959:        tag = cvs_remote_input();
                    960:
1.90      tobias    961:        if (cvs_cmdop == CVS_OP_EXPORT)
                    962:                goto out;
                    963:
1.98      joris     964:        client_check_directory(data, dir);
1.69      joris     965:
1.58      xsa       966:        (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.35      xsa       967:
                    968:        if ((fp = fopen(tagpath, "w+")) == NULL) {
                    969:                cvs_log(LP_ERRNO, "%s", tagpath);
                    970:                goto out;
                    971:        }
                    972:
                    973:        (void)fprintf(fp, "%s\n", tag);
                    974:        (void)fclose(fp);
                    975: out:
                    976:        xfree(tag);
1.98      joris     977:        xfree(dir);
1.35      xsa       978: }
                    979:
                    980: void
                    981: cvs_client_clear_sticky(char *data)
                    982: {
1.50      otto      983:        char *dir, tagpath[MAXPATHLEN];
1.47      xsa       984:
1.62      ray       985:        if (data == NULL)
                    986:                fatal("Missing argument for Clear-sticky");
1.35      xsa       987:
                    988:        STRIP_SLASH(data);
                    989:
                    990:        dir = cvs_remote_input();
1.90      tobias    991:
1.98      joris     992:        if (cvs_cmdop == CVS_OP_EXPORT) {
                    993:                xfree(dir);
1.90      tobias    994:                return;
1.98      joris     995:        }
1.35      xsa       996:
1.98      joris     997:        client_check_directory(data, dir);
1.69      joris     998:
1.58      xsa       999:        (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.69      joris    1000:        (void)unlink(tagpath);
1.98      joris    1001:
                   1002:        xfree(dir);
1.35      xsa      1003: }
                   1004:
                   1005:
1.16      xsa      1006: /*
                   1007:  * cvs_client_initlog()
                   1008:  *
                   1009:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                   1010:  * set.  In this case, the variable's value is used as a path to which the
                   1011:  * appropriate suffix is added (".in" for client input and ".out" for server
                   1012:  * output).
                   1013:  */
                   1014: static void
                   1015: cvs_client_initlog(void)
                   1016: {
                   1017:        u_int i;
                   1018:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                   1019:        char rpath[MAXPATHLEN], *s;
                   1020:        struct stat st;
                   1021:        time_t now;
                   1022:        struct passwd *pwd;
                   1023:
                   1024:        /* avoid doing it more than once */
                   1025:        if (cvs_client_logon)
                   1026:                return;
                   1027:
                   1028:        if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
                   1029:                return;
                   1030:
                   1031:        envdup = xstrdup(env);
                   1032:        if ((s = strchr(envdup, '%')) != NULL)
                   1033:                *s = '\0';
                   1034:
                   1035:        if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
                   1036:                fatal("cvs_client_initlog: truncation");
                   1037:
                   1038:        if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
                   1039:                fatal("cvs_client_initlog: truncation");
                   1040:
                   1041:        xfree(envdup);
                   1042:
                   1043:        s = buf;
                   1044:        while ((s = strchr(s, '%')) != NULL) {
                   1045:                s++;
                   1046:                switch (*s) {
                   1047:                case 'c':
1.87      tobias   1048:                        if (strlcpy(fpath, cmdp->cmd_name, sizeof(fpath)) >=
1.16      xsa      1049:                            sizeof(fpath))
                   1050:                                fatal("cvs_client_initlog: truncation");
                   1051:                        break;
                   1052:                case 'd':
                   1053:                        time(&now);
                   1054:                        if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
                   1055:                            sizeof(fpath))
                   1056:                                fatal("cvs_client_initlog: truncation");
                   1057:                        break;
                   1058:                case 'p':
1.57      xsa      1059:                        (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1.16      xsa      1060:                        break;
                   1061:                case 'u':
                   1062:                        if ((pwd = getpwuid(getuid())) != NULL) {
                   1063:                                if (strlcpy(fpath, pwd->pw_name,
                   1064:                                    sizeof(fpath)) >= sizeof(fpath))
                   1065:                                        fatal("cvs_client_initlog: truncation");
                   1066:                        } else {
                   1067:                                fpath[0] = '\0';
                   1068:                        }
                   1069:                        endpwent();
                   1070:                        break;
                   1071:                default:
                   1072:                        fpath[0] = '\0';
                   1073:                        break;
                   1074:                }
                   1075:
                   1076:                if (fpath[0] != '\0') {
                   1077:                        if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
                   1078:                                fatal("cvs_client_initlog: truncation");
                   1079:
                   1080:                        if (strlcat(rpath, fpath, sizeof(rpath))
                   1081:                            >= sizeof(rpath))
                   1082:                                fatal("cvs_client_initlog: truncation");
                   1083:                }
                   1084:        }
                   1085:
                   1086:        for (i = 0; i < UINT_MAX; i++) {
1.57      xsa      1087:                (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.16      xsa      1088:
                   1089:                if (stat(fpath, &st) != -1)
                   1090:                        continue;
                   1091:
                   1092:                if (errno != ENOENT)
                   1093:                        fatal("cvs_client_initlog() stat failed '%s'",
                   1094:                            strerror(errno));
                   1095:
                   1096:                break;
                   1097:        }
                   1098:
                   1099:        if ((cvs_client_inlog_fd = open(fpath,
1.60      xsa      1100:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16      xsa      1101:                fatal("cvs_client_initlog: open `%s': %s",
                   1102:                    fpath, strerror(errno));
                   1103:        }
                   1104:
                   1105:        for (i = 0; i < UINT_MAX; i++) {
1.57      xsa      1106:                (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.16      xsa      1107:
                   1108:                if (stat(fpath, &st) != -1)
                   1109:                        continue;
                   1110:
                   1111:                if (errno != ENOENT)
                   1112:                        fatal("cvs_client_initlog() stat failed '%s'",
                   1113:                            strerror(errno));
                   1114:
                   1115:                break;
                   1116:        }
                   1117:
1.93      xsa      1118:        if ((cvs_client_outlog_fd = open(fpath,
1.60      xsa      1119:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16      xsa      1120:                fatal("cvs_client_initlog: open `%s': %s",
                   1121:                    fpath, strerror(errno));
                   1122:        }
                   1123:
                   1124:        cvs_client_logon = 1;
1.1       jfb      1125: }