annotate nss/lib/dbm/src/mktemp.c @ 4:b513267f632f tip

Build DBM module
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 05 Aug 2014 18:58:03 +0200
parents 150b72113545
children
rev   line source
3
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2 * Copyright (c) 1987, 1993
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
3 * The Regents of the University of California. All rights reserved.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
4 *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
7 * are met:
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
13 * 3. ***REMOVED*** - see
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
14 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
15 * 4. Neither the name of the University nor the names of its contributors
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
16 * may be used to endorse or promote products derived from this software
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
17 * without specific prior written permission.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
18 *
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
29 * SUCH DAMAGE.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
30 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
31
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
32 #if defined(LIBC_SCCS) && !defined(lint)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
33 static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
34 #endif /* LIBC_SCCS and not lint */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
35
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
36 #ifdef macintosh
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
37 #include <unix.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
38 #else
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
39 #include <sys/types.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
40 #include <sys/stat.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
41 #endif
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
42 #include <fcntl.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
43 #include <errno.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
44 #include <stdio.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
45 #include <ctype.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
46 #include "mcom_db.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
47
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
48 #ifndef _WINDOWS
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
49 #include <unistd.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
50 #endif
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
51
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
52 #ifdef _WINDOWS
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
53 #include <process.h>
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
54 #include "winfile.h"
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
55 #endif
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
56
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
57 static int _gettemp(char *path, register int *doopen, int extraFlags);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
58
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
59 int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
60 mkstemp(char *path)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
61 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
62 #ifdef XP_OS2
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
63 FILE *temp = tmpfile();
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
64
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
65 return (temp ? fileno(temp) : -1);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
66 #else
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
67 int fd;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
68
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
69 return (_gettemp(path, &fd, 0) ? fd : -1);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
70 #endif
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
71 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
72
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
73 int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
74 mkstempflags(char *path, int extraFlags)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
75 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
76 int fd;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
77
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
78 return (_gettemp(path, &fd, extraFlags) ? fd : -1);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
79 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
80
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
81 /* NB: This routine modifies its input string, and does not always restore it.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
82 ** returns 1 on success, 0 on failure.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
83 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
84 static int
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
85 _gettemp(char *path, register int *doopen, int extraFlags)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
86 {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
87 register char *start, *trv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
88 struct stat sbuf;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
89 unsigned int pid;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
90
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
91 pid = getpid();
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
92 for (trv = path; *trv; ++trv); /* extra X's get set to 0's */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
93 while (*--trv == 'X') {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
94 *trv = (pid % 10) + '0';
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
95 pid /= 10;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
96 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
97
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
98 /*
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
99 * check the target directory; if you have six X's and it
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
100 * doesn't exist this runs for a *very* long time.
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
101 */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
102 for (start = trv + 1;; --trv) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
103 char saved;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
104 if (trv <= path)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
105 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
106 saved = *trv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
107 if (saved == '/' || saved == '\\') {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
108 int rv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
109 *trv = '\0';
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
110 rv = stat(path, &sbuf);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
111 *trv = saved;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
112 if (rv)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
113 return(0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
114 if (!S_ISDIR(sbuf.st_mode)) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
115 errno = ENOTDIR;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
116 return(0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
117 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
118 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
119 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
120 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
121
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
122 for (;;) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
123 if (doopen) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
124 if ((*doopen =
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
125 open(path, O_CREAT|O_EXCL|O_RDWR|extraFlags, 0600)) >= 0)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
126 return(1);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
127 if (errno != EEXIST)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
128 return(0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
129 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
130 else if (stat(path, &sbuf))
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
131 return(errno == ENOENT ? 1 : 0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
132
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
133 /* tricky little algorithm for backward compatibility */
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
134 for (trv = start;;) {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
135 if (!*trv)
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
136 return(0);
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
137 if (*trv == 'z')
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
138 *trv++ = 'a';
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
139 else {
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
140 if (isdigit(*trv))
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
141 *trv = 'a';
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
142 else
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
143 ++*trv;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
144 break;
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
145 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
146 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
147 }
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
148 /*NOTREACHED*/
150b72113545 Add DBM and legacydb support
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
149 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)