xref: /illumos-gate/usr/src/cmd/mktemp/mktemp.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c4dcc55Scasper  * Common Development and Distribution License (the "License").
6*7c4dcc55Scasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*7c4dcc55Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Create unique plain files or directories.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static void
usage(void)357c478bd9Sstevel@tonic-gate usage(void)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
387c478bd9Sstevel@tonic-gate 	    gettext("Usage: mktemp [-dqtu] [-p prefix_dir] [template]\n"));
397c478bd9Sstevel@tonic-gate 	exit(1);
407c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)447c478bd9Sstevel@tonic-gate main(int argc, char **argv)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	int opt;
477c478bd9Sstevel@tonic-gate 	char *prefix = NULL;
487c478bd9Sstevel@tonic-gate 	boolean_t dounlink = B_FALSE;
497c478bd9Sstevel@tonic-gate 	boolean_t domkdir = B_FALSE;
507c478bd9Sstevel@tonic-gate 	boolean_t quiet = B_FALSE;
517c478bd9Sstevel@tonic-gate 	boolean_t usetmpdir = B_FALSE;
527c478bd9Sstevel@tonic-gate 	char template[] = "tmp.XXXXXX";
537c478bd9Sstevel@tonic-gate 	char *tmpl;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
587c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	opterr = 0;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "dqtup:")) != EOF) {
657c478bd9Sstevel@tonic-gate 		switch (opt) {
667c478bd9Sstevel@tonic-gate 		case 'd':
677c478bd9Sstevel@tonic-gate 			domkdir = B_TRUE;
687c478bd9Sstevel@tonic-gate 			break;
697c478bd9Sstevel@tonic-gate 		case 'q':
707c478bd9Sstevel@tonic-gate 			quiet = B_TRUE;
717c478bd9Sstevel@tonic-gate 			break;
727c478bd9Sstevel@tonic-gate 		case 'p':
737c478bd9Sstevel@tonic-gate 			prefix = optarg;
747c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH - -p implies -t */
757c478bd9Sstevel@tonic-gate 		case 't':
767c478bd9Sstevel@tonic-gate 			usetmpdir = B_TRUE;
777c478bd9Sstevel@tonic-gate 			break;
787c478bd9Sstevel@tonic-gate 		case 'u':
797c478bd9Sstevel@tonic-gate 			dounlink = B_TRUE;
807c478bd9Sstevel@tonic-gate 			break;
817c478bd9Sstevel@tonic-gate 		default:
827c478bd9Sstevel@tonic-gate 			usage();
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	argc -= optind;
867c478bd9Sstevel@tonic-gate 	argv += optind;
877c478bd9Sstevel@tonic-gate 	switch (argc) {
887c478bd9Sstevel@tonic-gate 	case 0:
897c478bd9Sstevel@tonic-gate 		tmpl = template;
907c478bd9Sstevel@tonic-gate 		usetmpdir = B_TRUE;
917c478bd9Sstevel@tonic-gate 		break;
927c478bd9Sstevel@tonic-gate 	case 1:
937c478bd9Sstevel@tonic-gate 		tmpl = argv[0];
947c478bd9Sstevel@tonic-gate 		break;
957c478bd9Sstevel@tonic-gate 	default:
967c478bd9Sstevel@tonic-gate 		usage();
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (usetmpdir) {
1007c478bd9Sstevel@tonic-gate 		char *tmp = getenv("TMPDIR");
1017c478bd9Sstevel@tonic-gate 		size_t len;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		if (strchr(tmpl, '/') != NULL) {
1047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1057c478bd9Sstevel@tonic-gate 			    gettext("mktemp: template argument specified "
1067c478bd9Sstevel@tonic-gate 			    "with -t/-p option must not contain '/'"
1077c478bd9Sstevel@tonic-gate 			    "\n"));
1087c478bd9Sstevel@tonic-gate 			return (1);
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		/* TMPDIR overrides -p so that scripts will honor $TMPDIR */
1117c478bd9Sstevel@tonic-gate 		if (tmp != NULL)
1127c478bd9Sstevel@tonic-gate 			prefix = tmp;
1137c478bd9Sstevel@tonic-gate 		else if (prefix == NULL)
1147c478bd9Sstevel@tonic-gate 			prefix = "/tmp";
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		len = snprintf(NULL, 0, "%s/%s", prefix, tmpl) + 1;
1177c478bd9Sstevel@tonic-gate 		tmp = malloc(len);
1187c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
1197c478bd9Sstevel@tonic-gate 			perror("malloc");
1207c478bd9Sstevel@tonic-gate 			return (1);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 		(void) snprintf(tmp, len, "%s/%s", prefix, tmpl);
1237c478bd9Sstevel@tonic-gate 		tmpl = tmp;
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (domkdir) {
127*7c4dcc55Scasper 		if (mkdtemp(tmpl) == NULL) {
1287c478bd9Sstevel@tonic-gate 			if (!quiet) {
1297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1307c478bd9Sstevel@tonic-gate 				    gettext("mktemp: failed to create "
1317c478bd9Sstevel@tonic-gate 				    "directory: %s\n"), tmpl);
1327c478bd9Sstevel@tonic-gate 			}
1337c478bd9Sstevel@tonic-gate 			return (1);
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 		if (dounlink)
1367c478bd9Sstevel@tonic-gate 			(void) rmdir(tmpl);
1377c478bd9Sstevel@tonic-gate 	} else {
1387c478bd9Sstevel@tonic-gate 		if (mkstemp(tmpl) < 0) {
1397c478bd9Sstevel@tonic-gate 			if (!quiet) {
1407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1417c478bd9Sstevel@tonic-gate 				    gettext("mktemp: failed to create file: "
1427c478bd9Sstevel@tonic-gate 				    "%s\n"), tmpl);
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 			return (1);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 		if (dounlink)
1477c478bd9Sstevel@tonic-gate 			(void) unlink(tmpl);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	(void) puts(tmpl);
1507c478bd9Sstevel@tonic-gate 	return (0);
1517c478bd9Sstevel@tonic-gate }
152