xref: /illumos-gate/usr/src/lib/libc/port/gen/mkdtemp.c (revision 6a634c9d)
17c4dcc55Scasper /*
27c4dcc55Scasper  * CDDL HEADER START
37c4dcc55Scasper  *
47c4dcc55Scasper  * The contents of this file are subject to the terms of the
57c4dcc55Scasper  * Common Development and Distribution License (the "License").
67c4dcc55Scasper  * You may not use this file except in compliance with the License.
77c4dcc55Scasper  *
87c4dcc55Scasper  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c4dcc55Scasper  * or http://www.opensolaris.org/os/licensing.
107c4dcc55Scasper  * See the License for the specific language governing permissions
117c4dcc55Scasper  * and limitations under the License.
127c4dcc55Scasper  *
137c4dcc55Scasper  * When distributing Covered Code, include this CDDL HEADER in each
147c4dcc55Scasper  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c4dcc55Scasper  * If applicable, add the following below this CDDL HEADER, with the
167c4dcc55Scasper  * fields enclosed by brackets "[]" replaced with your own identifying
177c4dcc55Scasper  * information: Portions Copyright [yyyy] [name of copyright owner]
187c4dcc55Scasper  *
197c4dcc55Scasper  * CDDL HEADER END
207c4dcc55Scasper  */
217c4dcc55Scasper 
227c4dcc55Scasper /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
247c4dcc55Scasper  */
257c4dcc55Scasper 
267c4dcc55Scasper /*
277c4dcc55Scasper  * mkdtemp(3C) - create a directory with a unique name.
287c4dcc55Scasper  */
297c4dcc55Scasper 
307257d1b4Sraf #include "lint.h"
317c4dcc55Scasper #include <errno.h>
327c4dcc55Scasper #include <stdlib.h>
337c4dcc55Scasper #include <string.h>
347c4dcc55Scasper #include <sys/stat.h>
357c4dcc55Scasper 
367c4dcc55Scasper char *
mkdtemp(char * template)377c4dcc55Scasper mkdtemp(char *template)
387c4dcc55Scasper {
39*23a1cceaSRoger A. Faulkner 	char *t;
407c4dcc55Scasper 	char *r;
417c4dcc55Scasper 
427c4dcc55Scasper 	/* Save template */
43*23a1cceaSRoger A. Faulkner 	t = strdupa(template);
447257d1b4Sraf 	for (;;) {
457c4dcc55Scasper 		r = mktemp(template);
467c4dcc55Scasper 
477c4dcc55Scasper 		if (*r == '\0')
487c4dcc55Scasper 			return (NULL);
497c4dcc55Scasper 
507c4dcc55Scasper 		if (mkdir(template, 0700) == 0)
517c4dcc55Scasper 			return (r);
527c4dcc55Scasper 
537c4dcc55Scasper 		/* Other errors indicate persistent conditions. */
547c4dcc55Scasper 		if (errno != EEXIST)
557c4dcc55Scasper 			return (NULL);
567c4dcc55Scasper 
577c4dcc55Scasper 		/* Reset template */
587c4dcc55Scasper 		(void) strcpy(template, t);
597c4dcc55Scasper 	}
607c4dcc55Scasper }
61