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