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

Annotation of src/usr.bin/cvs/commit.c, Revision 1.1.1.1

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@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 conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions 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/types.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <errno.h>
                     31: #include <stdio.h>
                     32: #include <fcntl.h>
                     33: #include <stdlib.h>
                     34: #include <unistd.h>
                     35: #include <string.h>
                     36: #include <sysexits.h>
                     37:
                     38: #include "cvs.h"
                     39: #include "log.h"
                     40:
                     41:
                     42:
                     43:
                     44: static char*  cvs_commit_openmsg   (const char *);
                     45:
                     46:
                     47:
                     48:
                     49: /*
                     50:  * cvs_commit()
                     51:  *
                     52:  * Handler for the `cvs commit' command.
                     53:  */
                     54:
                     55: int
                     56: cvs_commit(int argc, char **argv)
                     57: {
                     58:        int ch, recurse;
                     59:        char *msg, *mfile;
                     60:
                     61:        recurse = 1;
                     62:        mfile = NULL;
                     63:        msg = NULL;
                     64:
                     65:        while ((ch = getopt(argc, argv, "F:flm:R")) != -1) {
                     66:                switch (ch) {
                     67:                case 'F':
                     68:                        mfile = optarg;
                     69:                        break;
                     70:                case 'f':
                     71:                        recurse = 0;
                     72:                        break;
                     73:                case 'l':
                     74:                        recurse = 0;
                     75:                        break;
                     76:                case 'm':
                     77:                        msg = optarg;
                     78:                        break;
                     79:                case 'R':
                     80:                        recurse = 1;
                     81:                        break;
                     82:                default:
                     83:                        return (EX_USAGE);
                     84:                }
                     85:        }
                     86:
                     87:        if ((msg != NULL) && (mfile != NULL)) {
                     88:                cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
                     89:                return (EX_USAGE);
                     90:        }
                     91:
                     92:        if ((mfile != NULL) && (msg = cvs_commit_openmsg(mfile)) == NULL)
                     93:                return (EX_DATAERR);
                     94:
                     95:        argc -= optind;
                     96:        argv += optind;
                     97:
                     98:        return (0);
                     99: }
                    100:
                    101:
                    102: /*
                    103:  * cvs_commit_openmsg()
                    104:  *
                    105:  * Open the file specified by <path> and allocate a buffer large enough to
                    106:  * hold all of the file's contents.  The returned value must later be freed
                    107:  * using the free() function.
                    108:  * Returns a pointer to the allocated buffer on success, or NULL on failure.
                    109:  */
                    110:
                    111: static char*
                    112: cvs_commit_openmsg(const char *path)
                    113: {
                    114:        int fd;
                    115:        size_t sz;
                    116:        char *msg;
                    117:        struct stat st;
                    118:
                    119:        if (stat(path, &st) == -1) {
                    120:                cvs_log(LP_ERRNO, "failed to stat `%s'", path);
                    121:                return (NULL);
                    122:        }
                    123:
                    124:        sz = st.st_size + 1;
                    125:
                    126:        msg = (char *)malloc(sz);
                    127:        if (msg == NULL) {
                    128:                cvs_log(LP_ERRNO, "failed to allocate message buffer");
                    129:                return (NULL);
                    130:        }
                    131:
                    132:        fd = open(path, O_RDONLY, 0);
                    133:        if (fd == -1) {
                    134:                cvs_log(LP_ERRNO, "failed to open message file `%s'", path);
                    135:                return (NULL);
                    136:        }
                    137:
                    138:        if (read(fd, msg, sz - 1) == -1) {
                    139:                cvs_log(LP_ERRNO, "failed to read CVS commit message");
                    140:                return (NULL);
                    141:        }
                    142:        msg[sz - 1] = '\0';
                    143:
                    144:        return (msg);
                    145: }