annotate audacious/mkdtemp.c @ 2:6efb9e514224 trunk

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