xref: /onnv-gate/usr/src/lib/libc/port/gen/mkdtemp.c (revision 1475:0c7070c5774f)
1*1475Scasper /*
2*1475Scasper  * CDDL HEADER START
3*1475Scasper  *
4*1475Scasper  * 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.
7*1475Scasper  *
8*1475Scasper  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1475Scasper  * or http://www.opensolaris.org/os/licensing.
10*1475Scasper  * See the License for the specific language governing permissions
11*1475Scasper  * and limitations under the License.
12*1475Scasper  *
13*1475Scasper  * When distributing Covered Code, include this CDDL HEADER in each
14*1475Scasper  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1475Scasper  * If applicable, add the following below this CDDL HEADER, with the
16*1475Scasper  * fields enclosed by brackets "[]" replaced with your own identifying
17*1475Scasper  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1475Scasper  *
19*1475Scasper  * CDDL HEADER END
20*1475Scasper  */
21*1475Scasper 
22*1475Scasper /*
23*1475Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*1475Scasper  * Use is subject to license terms.
25*1475Scasper  */
26*1475Scasper 
27*1475Scasper #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*1475Scasper 
29*1475Scasper /*
30*1475Scasper  * mkdtemp(3C) - create a directory with a unique name.
31*1475Scasper  */
32*1475Scasper 
33*1475Scasper #pragma weak mkdtemp = _mkdtemp
34*1475Scasper 
35*1475Scasper #include "synonyms.h"
36*1475Scasper 
37*1475Scasper #include <alloca.h>
38*1475Scasper #include <errno.h>
39*1475Scasper #include <stdlib.h>
40*1475Scasper #include <string.h>
41*1475Scasper #include <sys/stat.h>
42*1475Scasper 
43*1475Scasper char *
44*1475Scasper mkdtemp(char *template)
45*1475Scasper {
46*1475Scasper 	char *t = alloca(strlen(template) + 1);
47*1475Scasper 	char *r;
48*1475Scasper 
49*1475Scasper 	/* Save template */
50*1475Scasper 	(void) strcpy(t, template);
51*1475Scasper 	for (; ; ) {
52*1475Scasper 		r = mktemp(template);
53*1475Scasper 
54*1475Scasper 		if (*r == '\0')
55*1475Scasper 			return (NULL);
56*1475Scasper 
57*1475Scasper 		if (mkdir(template, 0700) == 0)
58*1475Scasper 			return (r);
59*1475Scasper 
60*1475Scasper 		/* Other errors indicate persistent conditions. */
61*1475Scasper 		if (errno != EEXIST)
62*1475Scasper 			return (NULL);
63*1475Scasper 
64*1475Scasper 		/* Reset template */
65*1475Scasper 		(void) strcpy(template, t);
66*1475Scasper 	}
67*1475Scasper }
68