xref: /onnv-gate/usr/src/lib/libc/port/gen/mkdtemp.c (revision 6812:febeba71273d)
11475Scasper /*
21475Scasper  * CDDL HEADER START
31475Scasper  *
41475Scasper  * The contents of this file are subject to the terms of the
51475Scasper  * Common Development and Distribution License (the "License").
61475Scasper  * You may not use this file except in compliance with the License.
71475Scasper  *
81475Scasper  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91475Scasper  * or http://www.opensolaris.org/os/licensing.
101475Scasper  * See the License for the specific language governing permissions
111475Scasper  * and limitations under the License.
121475Scasper  *
131475Scasper  * When distributing Covered Code, include this CDDL HEADER in each
141475Scasper  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151475Scasper  * If applicable, add the following below this CDDL HEADER, with the
161475Scasper  * fields enclosed by brackets "[]" replaced with your own identifying
171475Scasper  * information: Portions Copyright [yyyy] [name of copyright owner]
181475Scasper  *
191475Scasper  * CDDL HEADER END
201475Scasper  */
211475Scasper 
221475Scasper /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
241475Scasper  * Use is subject to license terms.
251475Scasper  */
261475Scasper 
271475Scasper #pragma ident	"%Z%%M%	%I%	%E% SMI"
281475Scasper 
291475Scasper /*
301475Scasper  * mkdtemp(3C) - create a directory with a unique name.
311475Scasper  */
321475Scasper 
33*6812Sraf #include "lint.h"
341475Scasper #include <alloca.h>
351475Scasper #include <errno.h>
361475Scasper #include <stdlib.h>
371475Scasper #include <string.h>
381475Scasper #include <sys/stat.h>
391475Scasper 
401475Scasper char *
411475Scasper mkdtemp(char *template)
421475Scasper {
431475Scasper 	char *t = alloca(strlen(template) + 1);
441475Scasper 	char *r;
451475Scasper 
461475Scasper 	/* Save template */
471475Scasper 	(void) strcpy(t, template);
48*6812Sraf 	for (;;) {
491475Scasper 		r = mktemp(template);
501475Scasper 
511475Scasper 		if (*r == '\0')
521475Scasper 			return (NULL);
531475Scasper 
541475Scasper 		if (mkdir(template, 0700) == 0)
551475Scasper 			return (r);
561475Scasper 
571475Scasper 		/* Other errors indicate persistent conditions. */
581475Scasper 		if (errno != EEXIST)
591475Scasper 			return (NULL);
601475Scasper 
611475Scasper 		/* Reset template */
621475Scasper 		(void) strcpy(template, t);
631475Scasper 	}
641475Scasper }
65