comparison audacious/mkdtemp.c @ 0:cb178e5ad177 trunk

[svn] Import audacious source.
author nenolod
date Mon, 24 Oct 2005 03:06:47 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cb178e5ad177
1 /*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /*
35 * This has been derived from the implementation in the FreeBSD libc.
36 *
37 * 2000-12-28 Håvard Kvålen <havardk@xmms.org>:
38 * Stripped down to only mkdtemp() and made more portable
39 *
40 */
41
42 #ifndef HAVE_MKDTEMP
43
44 #if 0
45 static const char rcsid[] =
46 "$FreeBSD: /c/ncvs/src/lib/libc/stdio/mktemp.c,v 1.20 2000/11/10 23:27:55 kris Exp $";
47 #endif
48
49 #include <sys/stat.h>
50 #include <sys/time.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include <glib.h>
58
59 static const char padchar[] =
60 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
61
62 char *
63 mkdtemp(char *path)
64 {
65 register char *start, *trv, *suffp;
66 char *pad;
67 struct stat sbuf;
68 int rval;
69
70 for (trv = path; *trv; ++trv);
71 suffp = trv;
72 --trv;
73 if (trv < path) {
74 errno = EINVAL;
75 return NULL;
76 }
77
78 /* Fill space with random characters */
79 /*
80 * I hope this is random enough. The orginal implementation
81 * uses arc4random(3) which is not available everywhere.
82 */
83 while (*trv == 'X') {
84 int randv = g_random_int_range(0, sizeof(padchar) - 1);
85 *trv-- = padchar[randv];
86 }
87 start = trv + 1;
88
89 /*
90 * check the target directory.
91 */
92 for (;; --trv) {
93 if (trv <= path)
94 break;
95 if (*trv == '/') {
96 *trv = '\0';
97 rval = stat(path, &sbuf);
98 *trv = '/';
99 if (rval != 0)
100 return NULL;
101 if (!S_ISDIR(sbuf.st_mode)) {
102 errno = ENOTDIR;
103 return NULL;
104 }
105 break;
106 }
107 }
108
109 for (;;) {
110 if (mkdir(path, 0700) == 0)
111 return path;
112 if (errno != EEXIST)
113 return NULL;
114
115 /* If we have a collision, cycle through the space of filenames */
116 for (trv = start;;) {
117 if (*trv == '\0' || trv == suffp)
118 return NULL;
119 pad = strchr(padchar, *trv);
120 if (pad == NULL || !*++pad)
121 *trv++ = padchar[0];
122 else {
123 *trv++ = *pad;
124 break;
125 }
126 }
127 }
128 /*NOTREACHED*/}
129
130 #endif /* HAVE_MKDTEMP */