1 /* $Progeny: mkdtemp.c 3839 2003-11-17 04:25:01Z dsp $ */
2 /* $NetBSD: gettemp.c,v 1.5 1999/09/20 04:39:30 lukem Exp $ */
5 * Copyright (c) 1987, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
38 #include "safesysstat.h"
39 #include "safeunistd.h"
44 do_mkdtemp(char *path
)
50 /* To guarantee multiple calls generate unique names even if
51 the file is not created. 676 different possibilities with 7
52 or more X's, 26 with 6 or less. */
53 static char xtra
[2] = { 'a', 'a' };
58 /* Move to end of path and count trailing X's. */
59 for (trv
= path
; *trv
; ++trv
)
65 /* Use at least one from xtra. Use 2 if more than 6 X's. */
68 if (xcnt
> 6 && *(trv
-1) == 'X')
71 /* Set remaining X's to pid digits with 0's to the left. */
72 while (*--trv
== 'X') {
73 *trv
= (pid
% 10) + '0';
77 /* update xtra for next call. */
89 * check the target directory; if you have six X's and it
90 * doesn't exist this runs for a *very* long time.
92 for (start
= trv
+ 1;; --trv
) {
97 if (stat(path
, &sbuf
))
99 if (!S_ISDIR(sbuf
.st_mode
)) {
109 if (mkdir(path
, 0700) >= 0)
114 /* tricky little algorithm for backward compatibility */
115 for (trv
= start
;;) {
121 if (*trv
>= '0' && *trv
<= '9')
135 return (do_mkdtemp(path
) ? path
: (char *)0);