10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1475Scasper * Common Development and Distribution License (the "License").
6*1475Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*1475Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * Create unique plain files or directories.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <locale.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate static void
usage(void)370Sstevel@tonic-gate usage(void)
380Sstevel@tonic-gate {
390Sstevel@tonic-gate (void) fprintf(stderr,
400Sstevel@tonic-gate gettext("Usage: mktemp [-dqtu] [-p prefix_dir] [template]\n"));
410Sstevel@tonic-gate exit(1);
420Sstevel@tonic-gate /* NOTREACHED */
430Sstevel@tonic-gate }
440Sstevel@tonic-gate
450Sstevel@tonic-gate int
main(int argc,char ** argv)460Sstevel@tonic-gate main(int argc, char **argv)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate int opt;
490Sstevel@tonic-gate char *prefix = NULL;
500Sstevel@tonic-gate boolean_t dounlink = B_FALSE;
510Sstevel@tonic-gate boolean_t domkdir = B_FALSE;
520Sstevel@tonic-gate boolean_t quiet = B_FALSE;
530Sstevel@tonic-gate boolean_t usetmpdir = B_FALSE;
540Sstevel@tonic-gate char template[] = "tmp.XXXXXX";
550Sstevel@tonic-gate char *tmpl;
560Sstevel@tonic-gate
570Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
580Sstevel@tonic-gate
590Sstevel@tonic-gate #ifndef TEXT_DOMAIN
600Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
610Sstevel@tonic-gate #endif
620Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
630Sstevel@tonic-gate
640Sstevel@tonic-gate opterr = 0;
650Sstevel@tonic-gate
660Sstevel@tonic-gate while ((opt = getopt(argc, argv, "dqtup:")) != EOF) {
670Sstevel@tonic-gate switch (opt) {
680Sstevel@tonic-gate case 'd':
690Sstevel@tonic-gate domkdir = B_TRUE;
700Sstevel@tonic-gate break;
710Sstevel@tonic-gate case 'q':
720Sstevel@tonic-gate quiet = B_TRUE;
730Sstevel@tonic-gate break;
740Sstevel@tonic-gate case 'p':
750Sstevel@tonic-gate prefix = optarg;
760Sstevel@tonic-gate /* FALLTHROUGH - -p implies -t */
770Sstevel@tonic-gate case 't':
780Sstevel@tonic-gate usetmpdir = B_TRUE;
790Sstevel@tonic-gate break;
800Sstevel@tonic-gate case 'u':
810Sstevel@tonic-gate dounlink = B_TRUE;
820Sstevel@tonic-gate break;
830Sstevel@tonic-gate default:
840Sstevel@tonic-gate usage();
850Sstevel@tonic-gate }
860Sstevel@tonic-gate }
870Sstevel@tonic-gate argc -= optind;
880Sstevel@tonic-gate argv += optind;
890Sstevel@tonic-gate switch (argc) {
900Sstevel@tonic-gate case 0:
910Sstevel@tonic-gate tmpl = template;
920Sstevel@tonic-gate usetmpdir = B_TRUE;
930Sstevel@tonic-gate break;
940Sstevel@tonic-gate case 1:
950Sstevel@tonic-gate tmpl = argv[0];
960Sstevel@tonic-gate break;
970Sstevel@tonic-gate default:
980Sstevel@tonic-gate usage();
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if (usetmpdir) {
1020Sstevel@tonic-gate char *tmp = getenv("TMPDIR");
1030Sstevel@tonic-gate size_t len;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if (strchr(tmpl, '/') != NULL) {
1060Sstevel@tonic-gate (void) fprintf(stderr,
1070Sstevel@tonic-gate gettext("mktemp: template argument specified "
1080Sstevel@tonic-gate "with -t/-p option must not contain '/'"
1090Sstevel@tonic-gate "\n"));
1100Sstevel@tonic-gate return (1);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate /* TMPDIR overrides -p so that scripts will honor $TMPDIR */
1130Sstevel@tonic-gate if (tmp != NULL)
1140Sstevel@tonic-gate prefix = tmp;
1150Sstevel@tonic-gate else if (prefix == NULL)
1160Sstevel@tonic-gate prefix = "/tmp";
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate len = snprintf(NULL, 0, "%s/%s", prefix, tmpl) + 1;
1190Sstevel@tonic-gate tmp = malloc(len);
1200Sstevel@tonic-gate if (tmp == NULL) {
1210Sstevel@tonic-gate perror("malloc");
1220Sstevel@tonic-gate return (1);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate (void) snprintf(tmp, len, "%s/%s", prefix, tmpl);
1250Sstevel@tonic-gate tmpl = tmp;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if (domkdir) {
129*1475Scasper if (mkdtemp(tmpl) == NULL) {
1300Sstevel@tonic-gate if (!quiet) {
1310Sstevel@tonic-gate (void) fprintf(stderr,
1320Sstevel@tonic-gate gettext("mktemp: failed to create "
1330Sstevel@tonic-gate "directory: %s\n"), tmpl);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate return (1);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate if (dounlink)
1380Sstevel@tonic-gate (void) rmdir(tmpl);
1390Sstevel@tonic-gate } else {
1400Sstevel@tonic-gate if (mkstemp(tmpl) < 0) {
1410Sstevel@tonic-gate if (!quiet) {
1420Sstevel@tonic-gate (void) fprintf(stderr,
1430Sstevel@tonic-gate gettext("mktemp: failed to create file: "
1440Sstevel@tonic-gate "%s\n"), tmpl);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate return (1);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate if (dounlink)
1490Sstevel@tonic-gate (void) unlink(tmpl);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate (void) puts(tmpl);
1520Sstevel@tonic-gate return (0);
1530Sstevel@tonic-gate }
154