Path: blob/main/crypto/krb5/src/util/support/mkstemp.c
34889 views
/* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */1/*2* Copyright (c) 1987, 19933* The Regents of the University of California. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13* 3. All advertising materials mentioning features or use of this software14* must display the following acknowledgement:15* This product includes software developed by the University of16* California, Berkeley and its contributors.17* 4. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#if defined(LIBC_SCCS) && !defined(lint)35static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";36#endif /* LIBC_SCCS and not lint */3738#include "k5-platform.h"3940#include <sys/types.h>41#include <sys/stat.h>42#include <fcntl.h>43#include <errno.h>44#include <stdio.h>45#include <ctype.h>46#ifdef HAVE_UNISTD_H47#include <unistd.h>48#endif4950#ifndef O_BINARY51#define O_BINARY 052#endif5354#if !defined S_ISDIR55#if defined S_IFMT56#define S_ISDIR(MODE) (((MODE) & S_IFMT) == S_IFDIR)57#elif defined _S_IFMT58#define S_ISDIR(MODE) (((MODE) & _S_IFMT) == _S_IFDIR)59#else60/* Hope that there's a S_ISDIR function defined. */61#endif62#endif6364static int _gettemp(char *, int *);6566int mkstemp(path)67char *path;68{69int fd;7071return (_gettemp(path, &fd) ? fd : -1);72}7374static int75_gettemp(path, doopen)76char *path;77int *doopen;78{79char *start, *trv;80struct stat sbuf;81u_int pid;8283pid = getpid();84for (trv = path; *trv; ++trv); /* extra X's get set to 0's */85while (*--trv == 'X') {86*trv = (pid % 10) + '0';87pid /= 10;88}8990/*91* check the target directory; if you have six X's and it92* doesn't exist this runs for a *very* long time.93*/94for (start = trv + 1;; --trv) {95if (trv <= path)96break;97if (*trv == '/') {98*trv = '\0';99if (stat(path, &sbuf))100return(0);101if (!S_ISDIR(sbuf.st_mode)) {102errno = ENOTDIR;103return(0);104}105*trv = '/';106break;107}108}109110for (;;) {111if (doopen) {112if ((*doopen =113open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0)114return(1);115if (errno != EEXIST)116return(0);117}118else if (stat(path, &sbuf))119return(errno == ENOENT ? 1 : 0);120121/* tricky little algorithm for backward compatibility */122for (trv = start;;) {123if (!*trv)124return(0);125if (*trv == 'z')126*trv++ = 'a';127else {128if (isdigit(*trv))129*trv = 'a';130else131++*trv;132break;133}134}135}136/*NOTREACHED*/137}138139140