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

Annotation of src/usr.bin/mktemp/mktemp.c, Revision 1.8

1.8     ! millert     1: /*     $OpenBSD: mktemp.c,v 1.7 2001/10/11 00:05:55 millert Exp $      */
1.1       millert     2:
                      3: /*
1.6       millert     4:  * Copyright (c) 1996, 1997, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.5       millert    15:  * 3. The name of the author may not be used to endorse or promote products
1.1       millert    16:  *    derived from this software without specific prior written permission.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     19:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     20:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
                     21:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     22:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     23:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     24:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     25:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     26:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     27:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     28:  */
                     29:
                     30: #ifndef lint
1.8     ! millert    31: static const char rcsid[] = "$OpenBSD: mktemp.c,v 1.7 2001/10/11 00:05:55 millert Exp $";
1.1       millert    32: #endif /* not lint */
                     33:
1.6       millert    34: #include <paths.h>
1.1       millert    35: #include <stdio.h>
                     36: #include <stdlib.h>
                     37: #include <string.h>
                     38: #include <unistd.h>
                     39: #include <err.h>
                     40:
1.8     ! millert    41: __dead void usage(void);
1.1       millert    42:
                     43: int
                     44: main(argc, argv)
                     45:        int argc;
                     46:        char **argv;
                     47: {
1.7       millert    48:        int ch, fd, uflag = 0, quiet = 0, tflag = 0, makedir = 0;
                     49:        char *cp, *template, *tempfile, *prefix = _PATH_TMP;
1.6       millert    50:        size_t plen;
1.1       millert    51:
1.6       millert    52:        while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
                     53:                switch(ch) {
1.3       millert    54:                case 'd':
                     55:                        makedir = 1;
1.1       millert    56:                        break;
1.6       millert    57:                case 'p':
                     58:                        prefix = optarg;
                     59:                        tflag = 1;
                     60:                        break;
1.1       millert    61:                case 'q':
1.7       millert    62:                        quiet = 1;
1.1       millert    63:                        break;
1.6       millert    64:                case 't':
                     65:                        tflag = 1;
                     66:                        break;
1.3       millert    67:                case 'u':
                     68:                        uflag = 1;
                     69:                        break;
1.1       millert    70:                default:
                     71:                        usage();
                     72:        }
                     73:
1.7       millert    74:        /* If no template specified use a default one (implies -t mode) */
                     75:        switch (argc - optind) {
                     76:        case 1:
                     77:                template = argv[optind];
                     78:                break;
                     79:        case 0:
                     80:                template = "tmp.XXXXXXXXXX";
                     81:                tflag = 1;
                     82:                break;
                     83:        default:
1.1       millert    84:                usage();
1.7       millert    85:        }
1.1       millert    86:
1.6       millert    87:        if (tflag) {
1.7       millert    88:                if (strchr(template, '/')) {
                     89:                        if (!quiet)
                     90:                                warnx("template must not contain directory separators in -t mode");
                     91:                        exit(1);
1.6       millert    92:                }
                     93:
                     94:                cp = getenv("TMPDIR");
                     95:                if (cp != NULL && *cp != '\0')
                     96:                        prefix = cp;
                     97:                plen = strlen(prefix);
                     98:                while (plen != 0 && prefix[plen - 1] == '/')
                     99:                        plen--;
                    100:
1.7       millert   101:                tempfile = (char *)malloc(plen + 1 + strlen(template) + 1);
                    102:                if (tempfile == NULL) {
                    103:                        if (!quiet)
                    104:                                warnx("cannot allocate memory");
                    105:                        exit(1);
1.6       millert   106:                }
1.7       millert   107:                (void)memcpy(tempfile, prefix, plen);
                    108:                tempfile[plen] = '/';
                    109:                (void)strcpy(tempfile + plen + 1, template);    /* SAFE */
1.6       millert   110:        } else {
1.7       millert   111:                if ((tempfile = strdup(template)) == NULL) {
                    112:                        if (!quiet)
                    113:                                warnx("cannot allocate memory");
                    114:                        exit(1);
1.6       millert   115:                }
1.1       millert   116:        }
                    117:
1.3       millert   118:        if (makedir) {
1.7       millert   119:                if (mkdtemp(tempfile) == NULL) {
                    120:                        if (!quiet)
                    121:                                warn("cannot make temp dir %s", tempfile);
                    122:                        exit(1);
1.3       millert   123:                }
                    124:
                    125:                if (uflag)
1.7       millert   126:                        (void)rmdir(tempfile);
1.3       millert   127:        } else {
1.7       millert   128:                if ((fd = mkstemp(tempfile)) < 0) {
                    129:                        if (!quiet)
                    130:                                warn("cannot make temp file %s", tempfile);
                    131:                        exit(1);
1.3       millert   132:                }
1.7       millert   133:                (void)close(fd);
1.3       millert   134:
                    135:                if (uflag)
1.7       millert   136:                        (void)unlink(tempfile);
1.1       millert   137:        }
                    138:
1.7       millert   139:        (void)puts(tempfile);
                    140:        free(tempfile);
1.1       millert   141:
                    142:        exit(0);
1.7       millert   143: }
                    144:
                    145: __dead void
                    146: usage()
                    147: {
                    148:        extern char *__progname;
                    149:
                    150:        (void)fprintf(stderr,
                    151:            "Usage: %s [-dqtu] [-p prefix] [template]\n", __progname);
                    152:        exit(1);
1.1       millert   153: }