andre@3: /* andre@3: * Copyright (c) 1987, 1993 andre@3: * The Regents of the University of California. All rights reserved. andre@3: * andre@3: * Redistribution and use in source and binary forms, with or without andre@3: * modification, are permitted provided that the following conditions andre@3: * are met: andre@3: * 1. Redistributions of source code must retain the above copyright andre@3: * notice, this list of conditions and the following disclaimer. andre@3: * 2. Redistributions in binary form must reproduce the above copyright andre@3: * notice, this list of conditions and the following disclaimer in the andre@3: * documentation and/or other materials provided with the distribution. andre@3: * 3. ***REMOVED*** - see andre@3: * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change andre@3: * 4. Neither the name of the University nor the names of its contributors andre@3: * may be used to endorse or promote products derived from this software andre@3: * without specific prior written permission. andre@3: * andre@3: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND andre@3: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE andre@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE andre@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE andre@3: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL andre@3: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS andre@3: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) andre@3: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT andre@3: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY andre@3: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF andre@3: * SUCH DAMAGE. andre@3: */ andre@3: andre@3: #if defined(LIBC_SCCS) && !defined(lint) andre@3: static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93"; andre@3: #endif /* LIBC_SCCS and not lint */ andre@3: andre@3: #ifdef macintosh andre@3: #include andre@3: #else andre@3: #include andre@3: #include andre@3: #endif andre@3: #include andre@3: #include andre@3: #include andre@3: #include andre@3: #include "mcom_db.h" andre@3: andre@3: #ifndef _WINDOWS andre@3: #include andre@3: #endif andre@3: andre@3: #ifdef _WINDOWS andre@3: #include andre@3: #include "winfile.h" andre@3: #endif andre@3: andre@3: static int _gettemp(char *path, register int *doopen, int extraFlags); andre@3: andre@3: int andre@3: mkstemp(char *path) andre@3: { andre@3: #ifdef XP_OS2 andre@3: FILE *temp = tmpfile(); andre@3: andre@3: return (temp ? fileno(temp) : -1); andre@3: #else andre@3: int fd; andre@3: andre@3: return (_gettemp(path, &fd, 0) ? fd : -1); andre@3: #endif andre@3: } andre@3: andre@3: int andre@3: mkstempflags(char *path, int extraFlags) andre@3: { andre@3: int fd; andre@3: andre@3: return (_gettemp(path, &fd, extraFlags) ? fd : -1); andre@3: } andre@3: andre@3: /* NB: This routine modifies its input string, and does not always restore it. andre@3: ** returns 1 on success, 0 on failure. andre@3: */ andre@3: static int andre@3: _gettemp(char *path, register int *doopen, int extraFlags) andre@3: { andre@3: register char *start, *trv; andre@3: struct stat sbuf; andre@3: unsigned int pid; andre@3: andre@3: pid = getpid(); andre@3: for (trv = path; *trv; ++trv); /* extra X's get set to 0's */ andre@3: while (*--trv == 'X') { andre@3: *trv = (pid % 10) + '0'; andre@3: pid /= 10; andre@3: } andre@3: andre@3: /* andre@3: * check the target directory; if you have six X's and it andre@3: * doesn't exist this runs for a *very* long time. andre@3: */ andre@3: for (start = trv + 1;; --trv) { andre@3: char saved; andre@3: if (trv <= path) andre@3: break; andre@3: saved = *trv; andre@3: if (saved == '/' || saved == '\\') { andre@3: int rv; andre@3: *trv = '\0'; andre@3: rv = stat(path, &sbuf); andre@3: *trv = saved; andre@3: if (rv) andre@3: return(0); andre@3: if (!S_ISDIR(sbuf.st_mode)) { andre@3: errno = ENOTDIR; andre@3: return(0); andre@3: } andre@3: break; andre@3: } andre@3: } andre@3: andre@3: for (;;) { andre@3: if (doopen) { andre@3: if ((*doopen = andre@3: open(path, O_CREAT|O_EXCL|O_RDWR|extraFlags, 0600)) >= 0) andre@3: return(1); andre@3: if (errno != EEXIST) andre@3: return(0); andre@3: } andre@3: else if (stat(path, &sbuf)) andre@3: return(errno == ENOENT ? 1 : 0); andre@3: andre@3: /* tricky little algorithm for backward compatibility */ andre@3: for (trv = start;;) { andre@3: if (!*trv) andre@3: return(0); andre@3: if (*trv == 'z') andre@3: *trv++ = 'a'; andre@3: else { andre@3: if (isdigit(*trv)) andre@3: *trv = 'a'; andre@3: else andre@3: ++*trv; andre@3: break; andre@3: } andre@3: } andre@3: } andre@3: /*NOTREACHED*/ andre@3: }