[ci] Test Tcl bindings for dragonfly/freebsd
[xapian.git] / xapian-applications / omega / portability / mkdtemp.cc
blob1fb01115f0dda8b3d9f5debec6bcc273cf1a30a0
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 $ */
4 /*
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
10 * are met:
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
30 * SUCH DAMAGE.
33 #include <config.h>
34 #include "mkdtemp.h"
36 #include <sys/types.h>
38 #include "safesysstat.h"
39 #include "safeunistd.h"
41 #include <cerrno>
43 static int
44 do_mkdtemp(char *path)
46 char *start, *trv;
47 struct stat sbuf;
48 pid_t pid;
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' };
54 int xcnt = 0;
56 pid = getpid();
58 /* Move to end of path and count trailing X's. */
59 for (trv = path; *trv; ++trv)
60 if (*trv == 'X')
61 xcnt++;
62 else
63 xcnt = 0;
65 /* Use at least one from xtra. Use 2 if more than 6 X's. */
66 if (*(trv-1) == 'X')
67 *--trv = xtra[0];
68 if (xcnt > 6 && *(trv-1) == 'X')
69 *--trv = xtra[1];
71 /* Set remaining X's to pid digits with 0's to the left. */
72 while (*--trv == 'X') {
73 *trv = (pid % 10) + '0';
74 pid /= 10;
77 /* update xtra for next call. */
78 if (xtra[0] != 'z')
79 xtra[0]++;
80 else {
81 xtra[0] = 'a';
82 if (xtra[1] != 'z')
83 xtra[1]++;
84 else
85 xtra[1] = 'a';
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) {
93 if (trv <= path)
94 break;
95 if (*trv == '/') {
96 *trv = '\0';
97 if (stat(path, &sbuf))
98 return (0);
99 if (!S_ISDIR(sbuf.st_mode)) {
100 errno = ENOTDIR;
101 return (0);
103 *trv = '/';
104 break;
108 for (;;) {
109 if (mkdir(path, 0700) >= 0)
110 return (1);
111 if (errno != EEXIST)
112 return (0);
114 /* tricky little algorithm for backward compatibility */
115 for (trv = start;;) {
116 if (!*trv)
117 return (0);
118 if (*trv == 'z')
119 *trv++ = 'a';
120 else {
121 if (*trv >= '0' && *trv <= '9')
122 *trv = 'a';
123 else
124 ++*trv;
125 break;
129 /*NOTREACHED*/
132 char *
133 mkdtemp(char *path)
135 return (do_mkdtemp(path) ? path : (char *)0);