gettemp.c revision 1.14.4.2 1 1.14.4.2 apb /* $NetBSD: gettemp.c,v 1.14.4.2 2008/10/20 10:28:39 apb Exp $ */
2 1.14.4.2 apb
3 1.14.4.2 apb /*
4 1.14.4.2 apb * Copyright (c) 1987, 1993
5 1.14.4.2 apb * The Regents of the University of California. All rights reserved.
6 1.14.4.2 apb *
7 1.14.4.2 apb * Redistribution and use in source and binary forms, with or without
8 1.14.4.2 apb * modification, are permitted provided that the following conditions
9 1.14.4.2 apb * are met:
10 1.14.4.2 apb * 1. Redistributions of source code must retain the above copyright
11 1.14.4.2 apb * notice, this list of conditions and the following disclaimer.
12 1.14.4.2 apb * 2. Redistributions in binary form must reproduce the above copyright
13 1.14.4.2 apb * notice, this list of conditions and the following disclaimer in the
14 1.14.4.2 apb * documentation and/or other materials provided with the distribution.
15 1.14.4.2 apb * 3. Neither the name of the University nor the names of its contributors
16 1.14.4.2 apb * may be used to endorse or promote products derived from this software
17 1.14.4.2 apb * without specific prior written permission.
18 1.14.4.2 apb *
19 1.14.4.2 apb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.14.4.2 apb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.14.4.2 apb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.14.4.2 apb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.14.4.2 apb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.14.4.2 apb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.14.4.2 apb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.14.4.2 apb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.14.4.2 apb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.14.4.2 apb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.14.4.2 apb * SUCH DAMAGE.
30 1.14.4.2 apb */
31 1.14.4.2 apb
32 1.14.4.2 apb #if HAVE_NBTOOL_CONFIG_H
33 1.14.4.2 apb #include "nbtool_config.h"
34 1.14.4.2 apb #endif
35 1.14.4.2 apb
36 1.14.4.2 apb #if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP
37 1.14.4.2 apb
38 1.14.4.2 apb #include <sys/cdefs.h>
39 1.14.4.2 apb #if defined(LIBC_SCCS) && !defined(lint)
40 1.14.4.2 apb #if 0
41 1.14.4.2 apb static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
42 1.14.4.2 apb #else
43 1.14.4.2 apb __RCSID("$NetBSD: gettemp.c,v 1.14.4.2 2008/10/20 10:28:39 apb Exp $");
44 1.14.4.2 apb #endif
45 1.14.4.2 apb #endif /* LIBC_SCCS and not lint */
46 1.14.4.2 apb
47 1.14.4.2 apb #include <sys/types.h>
48 1.14.4.2 apb #include <sys/stat.h>
49 1.14.4.2 apb
50 1.14.4.2 apb #include <assert.h>
51 1.14.4.2 apb #include <ctype.h>
52 1.14.4.2 apb #include <errno.h>
53 1.14.4.2 apb #include <fcntl.h>
54 1.14.4.2 apb #include <stdio.h>
55 1.14.4.2 apb #include <stdlib.h>
56 1.14.4.2 apb #include <unistd.h>
57 1.14.4.2 apb
58 1.14.4.2 apb #if HAVE_NBTOOL_CONFIG_H
59 1.14.4.2 apb #define GETTEMP __nbcompat_gettemp
60 1.14.4.2 apb #else
61 1.14.4.2 apb #include "reentrant.h"
62 1.14.4.2 apb #include "local.h"
63 1.14.4.2 apb #define GETTEMP __gettemp
64 1.14.4.2 apb #endif
65 1.14.4.2 apb
66 1.14.4.2 apb int
67 1.14.4.2 apb GETTEMP(path, doopen, domkdir)
68 1.14.4.2 apb char *path;
69 1.14.4.2 apb int *doopen;
70 1.14.4.2 apb int domkdir;
71 1.14.4.2 apb {
72 1.14.4.2 apb char *start, *trv;
73 1.14.4.2 apb struct stat sbuf;
74 1.14.4.2 apb u_int pid;
75 1.14.4.2 apb
76 1.14.4.2 apb /* To guarantee multiple calls generate unique names even if
77 1.14.4.2 apb the file is not created. 676 different possibilities with 7
78 1.14.4.2 apb or more X's, 26 with 6 or less. */
79 1.14.4.2 apb static char xtra[2] = "aa";
80 1.14.4.2 apb int xcnt = 0;
81 1.14.4.2 apb
82 1.14.4.2 apb _DIAGASSERT(path != NULL);
83 1.14.4.2 apb /* doopen may be NULL */
84 1.14.4.2 apb
85 1.14.4.2 apb pid = getpid();
86 1.14.4.2 apb
87 1.14.4.2 apb /* Move to end of path and count trailing X's. */
88 1.14.4.2 apb for (trv = path; *trv; ++trv)
89 1.14.4.2 apb if (*trv == 'X')
90 1.14.4.2 apb xcnt++;
91 1.14.4.2 apb else
92 1.14.4.2 apb xcnt = 0;
93 1.14.4.2 apb
94 1.14.4.2 apb /* Use at least one from xtra. Use 2 if more than 6 X's. */
95 1.14.4.2 apb if (*(trv - 1) == 'X')
96 1.14.4.2 apb *--trv = xtra[0];
97 1.14.4.2 apb if (xcnt > 6 && *(trv - 1) == 'X')
98 1.14.4.2 apb *--trv = xtra[1];
99 1.14.4.2 apb
100 1.14.4.2 apb /* Set remaining X's to pid digits with 0's to the left. */
101 1.14.4.2 apb while (*--trv == 'X') {
102 1.14.4.2 apb *trv = (pid % 10) + '0';
103 1.14.4.2 apb pid /= 10;
104 1.14.4.2 apb }
105 1.14.4.2 apb
106 1.14.4.2 apb /* update xtra for next call. */
107 1.14.4.2 apb if (xtra[0] != 'z')
108 1.14.4.2 apb xtra[0]++;
109 1.14.4.2 apb else {
110 1.14.4.2 apb xtra[0] = 'a';
111 1.14.4.2 apb if (xtra[1] != 'z')
112 1.14.4.2 apb xtra[1]++;
113 1.14.4.2 apb else
114 1.14.4.2 apb xtra[1] = 'a';
115 1.14.4.2 apb }
116 1.14.4.2 apb
117 1.14.4.2 apb /*
118 1.14.4.2 apb * check the target directory; if you have six X's and it
119 1.14.4.2 apb * doesn't exist this runs for a *very* long time.
120 1.14.4.2 apb */
121 1.14.4.2 apb for (start = trv + 1;; --trv) {
122 1.14.4.2 apb if (trv <= path)
123 1.14.4.2 apb break;
124 1.14.4.2 apb if (*trv == '/') {
125 1.14.4.2 apb *trv = '\0';
126 1.14.4.2 apb if (stat(path, &sbuf))
127 1.14.4.2 apb return (0);
128 1.14.4.2 apb if (!S_ISDIR(sbuf.st_mode)) {
129 1.14.4.2 apb errno = ENOTDIR;
130 1.14.4.2 apb return (0);
131 1.14.4.2 apb }
132 1.14.4.2 apb *trv = '/';
133 1.14.4.2 apb break;
134 1.14.4.2 apb }
135 1.14.4.2 apb }
136 1.14.4.2 apb
137 1.14.4.2 apb for (;;) {
138 1.14.4.2 apb if (doopen) {
139 1.14.4.2 apb if ((*doopen =
140 1.14.4.2 apb open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0)
141 1.14.4.2 apb return (1);
142 1.14.4.2 apb if (errno != EEXIST)
143 1.14.4.2 apb return (0);
144 1.14.4.2 apb } else if (domkdir) {
145 1.14.4.2 apb if (mkdir(path, 0700) >= 0)
146 1.14.4.2 apb return (1);
147 1.14.4.2 apb if (errno != EEXIST)
148 1.14.4.2 apb return (0);
149 1.14.4.2 apb } else if (lstat(path, &sbuf))
150 1.14.4.2 apb return (errno == ENOENT ? 1 : 0);
151 1.14.4.2 apb
152 1.14.4.2 apb /* tricky little algorithm for backward compatibility */
153 1.14.4.2 apb for (trv = start;;) {
154 1.14.4.2 apb if (!*trv)
155 1.14.4.2 apb return (0);
156 1.14.4.2 apb if (*trv == 'z')
157 1.14.4.2 apb *trv++ = 'a';
158 1.14.4.2 apb else {
159 1.14.4.2 apb if (isdigit((unsigned char)*trv))
160 1.14.4.2 apb *trv = 'a';
161 1.14.4.2 apb else
162 1.14.4.2 apb ++*trv;
163 1.14.4.2 apb break;
164 1.14.4.2 apb }
165 1.14.4.2 apb }
166 1.14.4.2 apb }
167 1.14.4.2 apb /*NOTREACHED*/
168 1.14.4.2 apb }
169 1.14.4.2 apb
170 1.14.4.2 apb #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */
171