am 99e2a2f0: Merge "Switch to upstream OpenBSD mktemp.c."
* commit '99e2a2f04ca3bc4c66ecb91bc5abb378ec7c10ef': Switch to upstream OpenBSD mktemp.c.
This commit is contained in:
commit
9ff95c4763
libc
@ -234,7 +234,6 @@ libc_upstream_freebsd_src_files := \
|
|||||||
upstream-freebsd/lib/libc/stdio/fclose.c \
|
upstream-freebsd/lib/libc/stdio/fclose.c \
|
||||||
upstream-freebsd/lib/libc/stdio/flags.c \
|
upstream-freebsd/lib/libc/stdio/flags.c \
|
||||||
upstream-freebsd/lib/libc/stdio/fopen.c \
|
upstream-freebsd/lib/libc/stdio/fopen.c \
|
||||||
upstream-freebsd/lib/libc/stdio/mktemp.c \
|
|
||||||
upstream-freebsd/lib/libc/stdlib/abs.c \
|
upstream-freebsd/lib/libc/stdlib/abs.c \
|
||||||
upstream-freebsd/lib/libc/stdlib/getopt_long.c \
|
upstream-freebsd/lib/libc/stdlib/getopt_long.c \
|
||||||
upstream-freebsd/lib/libc/stdlib/imaxabs.c \
|
upstream-freebsd/lib/libc/stdlib/imaxabs.c \
|
||||||
@ -414,6 +413,7 @@ libc_upstream_openbsd_src_files := \
|
|||||||
upstream-openbsd/lib/libc/stdio/getwc.c \
|
upstream-openbsd/lib/libc/stdio/getwc.c \
|
||||||
upstream-openbsd/lib/libc/stdio/getwchar.c \
|
upstream-openbsd/lib/libc/stdio/getwchar.c \
|
||||||
upstream-openbsd/lib/libc/stdio/makebuf.c \
|
upstream-openbsd/lib/libc/stdio/makebuf.c \
|
||||||
|
upstream-openbsd/lib/libc/stdio/mktemp.c \
|
||||||
upstream-openbsd/lib/libc/stdio/perror.c \
|
upstream-openbsd/lib/libc/stdio/perror.c \
|
||||||
upstream-openbsd/lib/libc/stdio/printf.c \
|
upstream-openbsd/lib/libc/stdio/printf.c \
|
||||||
upstream-openbsd/lib/libc/stdio/putc.c \
|
upstream-openbsd/lib/libc/stdio/putc.c \
|
||||||
@ -496,6 +496,7 @@ libc_common_cflags += \
|
|||||||
-Werror=pointer-to-int-cast \
|
-Werror=pointer-to-int-cast \
|
||||||
-Werror=int-to-pointer-cast \
|
-Werror=int-to-pointer-cast \
|
||||||
-Werror=type-limits \
|
-Werror=type-limits \
|
||||||
|
-Werror \
|
||||||
|
|
||||||
ifeq ($(strip $(DEBUG_BIONIC_LIBC)),true)
|
ifeq ($(strip $(DEBUG_BIONIC_LIBC)),true)
|
||||||
libc_common_cflags += -DDEBUG
|
libc_common_cflags += -DDEBUG
|
||||||
@ -565,7 +566,7 @@ endef
|
|||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
LOCAL_SRC_FILES := bionic/__stack_chk_fail.cpp
|
LOCAL_SRC_FILES := bionic/__stack_chk_fail.cpp
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) -fno-stack-protector -Werror
|
LOCAL_CFLAGS := $(libc_common_cflags) -fno-stack-protector
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
||||||
@ -589,7 +590,6 @@ LOCAL_SRC_FILES += upstream-openbsd/lib/libc/time/wcsftime.c
|
|||||||
|
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) \
|
LOCAL_CFLAGS := $(libc_common_cflags) \
|
||||||
-fvisibility=hidden \
|
-fvisibility=hidden \
|
||||||
-Werror \
|
|
||||||
|
|
||||||
# Don't use ridiculous amounts of stack.
|
# Don't use ridiculous amounts of stack.
|
||||||
LOCAL_CFLAGS += -DALL_STATE
|
LOCAL_CFLAGS += -DALL_STATE
|
||||||
@ -631,12 +631,13 @@ LOCAL_CFLAGS := \
|
|||||||
-DANDROID_CHANGES \
|
-DANDROID_CHANGES \
|
||||||
-DINET6 \
|
-DINET6 \
|
||||||
-fvisibility=hidden \
|
-fvisibility=hidden \
|
||||||
|
-Wno-unused-parameter \
|
||||||
-I$(LOCAL_PATH)/dns/include \
|
-I$(LOCAL_PATH)/dns/include \
|
||||||
-I$(LOCAL_PATH)/private \
|
-I$(LOCAL_PATH)/private \
|
||||||
-I$(LOCAL_PATH)/upstream-netbsd/lib/libc/include \
|
-I$(LOCAL_PATH)/upstream-netbsd/lib/libc/include \
|
||||||
-I$(LOCAL_PATH)/upstream-netbsd/android/include \
|
-I$(LOCAL_PATH)/upstream-netbsd/android/include \
|
||||||
-include netbsd-compat.h \
|
-include netbsd-compat.h \
|
||||||
# -Werror \
|
-Wno-error \
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
@ -665,7 +666,6 @@ LOCAL_CFLAGS := \
|
|||||||
-I$(LOCAL_PATH)/upstream-freebsd/android/include \
|
-I$(LOCAL_PATH)/upstream-freebsd/android/include \
|
||||||
-I$(LOCAL_PATH)/upstream-freebsd/lib/libc/include \
|
-I$(LOCAL_PATH)/upstream-freebsd/lib/libc/include \
|
||||||
-include freebsd-compat.h \
|
-include freebsd-compat.h \
|
||||||
# -Werror \
|
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
@ -691,7 +691,6 @@ LOCAL_SRC_FILES := $(libc_upstream_netbsd_src_files)
|
|||||||
LOCAL_CFLAGS := \
|
LOCAL_CFLAGS := \
|
||||||
$(libc_common_cflags) \
|
$(libc_common_cflags) \
|
||||||
-Wno-sign-compare -Wno-uninitialized \
|
-Wno-sign-compare -Wno-uninitialized \
|
||||||
-Werror \
|
|
||||||
-DPOSIX_MISTAKE \
|
-DPOSIX_MISTAKE \
|
||||||
-I$(LOCAL_PATH)/upstream-netbsd/android/include \
|
-I$(LOCAL_PATH)/upstream-netbsd/android/include \
|
||||||
-I$(LOCAL_PATH)/upstream-netbsd/lib/libc/include \
|
-I$(LOCAL_PATH)/upstream-netbsd/lib/libc/include \
|
||||||
@ -722,7 +721,6 @@ LOCAL_SRC_FILES := $(libc_upstream_openbsd_src_files)
|
|||||||
LOCAL_CFLAGS := \
|
LOCAL_CFLAGS := \
|
||||||
$(libc_common_cflags) \
|
$(libc_common_cflags) \
|
||||||
-Wno-sign-compare -Wno-uninitialized -Wno-unused-parameter \
|
-Wno-sign-compare -Wno-uninitialized -Wno-unused-parameter \
|
||||||
-Werror \
|
|
||||||
-I$(LOCAL_PATH)/upstream-openbsd/android/include \
|
-I$(LOCAL_PATH)/upstream-openbsd/android/include \
|
||||||
-I$(LOCAL_PATH)/upstream-openbsd/lib/libc/include \
|
-I$(LOCAL_PATH)/upstream-openbsd/lib/libc/include \
|
||||||
-I$(LOCAL_PATH)/upstream-openbsd/lib/libc/gdtoa/ \
|
-I$(LOCAL_PATH)/upstream-openbsd/lib/libc/gdtoa/ \
|
||||||
@ -753,7 +751,6 @@ LOCAL_SRC_FILES_64 := $(libc_upstream_openbsd_gdtoa_src_files_64)
|
|||||||
LOCAL_CFLAGS := \
|
LOCAL_CFLAGS := \
|
||||||
$(libc_common_cflags) \
|
$(libc_common_cflags) \
|
||||||
-Wno-sign-compare -Wno-uninitialized \
|
-Wno-sign-compare -Wno-uninitialized \
|
||||||
-Werror \
|
|
||||||
-fvisibility=hidden \
|
-fvisibility=hidden \
|
||||||
-I$(LOCAL_PATH)/upstream-openbsd/android/include \
|
-I$(LOCAL_PATH)/upstream-openbsd/android/include \
|
||||||
-I$(LOCAL_PATH)/upstream-openbsd/lib/libc/include \
|
-I$(LOCAL_PATH)/upstream-openbsd/lib/libc/include \
|
||||||
@ -778,7 +775,6 @@ include $(CLEAR_VARS)
|
|||||||
|
|
||||||
LOCAL_SRC_FILES := $(libc_bionic_src_files)
|
LOCAL_SRC_FILES := $(libc_bionic_src_files)
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) \
|
LOCAL_CFLAGS := $(libc_common_cflags) \
|
||||||
-Werror \
|
|
||||||
-Wframe-larger-than=2048 \
|
-Wframe-larger-than=2048 \
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
@ -874,7 +870,6 @@ LOCAL_SRC_FILES := \
|
|||||||
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) \
|
LOCAL_CFLAGS := $(libc_common_cflags) \
|
||||||
-DLIBC_STATIC \
|
-DLIBC_STATIC \
|
||||||
-Werror \
|
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
@ -896,7 +891,6 @@ include $(BUILD_STATIC_LIBRARY)
|
|||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
LOCAL_SRC_FILES := $(libc_malloc_src)
|
LOCAL_SRC_FILES := $(libc_malloc_src)
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) \
|
LOCAL_CFLAGS := $(libc_common_cflags) \
|
||||||
-Werror \
|
|
||||||
-fvisibility=hidden \
|
-fvisibility=hidden \
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
@ -920,7 +914,6 @@ LOCAL_SRC_FILES := \
|
|||||||
|
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) \
|
LOCAL_CFLAGS := $(libc_common_cflags) \
|
||||||
-DLIBC_STATIC \
|
-DLIBC_STATIC \
|
||||||
-Werror \
|
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
@ -940,7 +933,7 @@ include $(BUILD_STATIC_LIBRARY)
|
|||||||
# ========================================================
|
# ========================================================
|
||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) -Werror
|
LOCAL_CFLAGS := $(libc_common_cflags)
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
||||||
@ -1010,7 +1003,6 @@ include $(CLEAR_VARS)
|
|||||||
LOCAL_CFLAGS := \
|
LOCAL_CFLAGS := \
|
||||||
$(libc_common_cflags) \
|
$(libc_common_cflags) \
|
||||||
-DMALLOC_LEAK_CHECK \
|
-DMALLOC_LEAK_CHECK \
|
||||||
-Werror \
|
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
@ -1046,7 +1038,6 @@ include $(CLEAR_VARS)
|
|||||||
LOCAL_CFLAGS := \
|
LOCAL_CFLAGS := \
|
||||||
$(libc_common_cflags) \
|
$(libc_common_cflags) \
|
||||||
-DMALLOC_QEMU_INSTRUMENT \
|
-DMALLOC_QEMU_INSTRUMENT \
|
||||||
-Werror \
|
|
||||||
|
|
||||||
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
|
||||||
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
LOCAL_CPPFLAGS := $(libc_common_cppflags)
|
||||||
|
@ -1,191 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1987, 1993
|
|
||||||
* The Regents of the University of California. All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. Neither the name of the University nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(LIBC_SCCS) && !defined(lint)
|
|
||||||
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
|
||||||
#endif /* LIBC_SCCS and not lint */
|
|
||||||
#include <sys/cdefs.h>
|
|
||||||
__FBSDID("$FreeBSD$");
|
|
||||||
|
|
||||||
#include "namespace.h"
|
|
||||||
#include <sys/param.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "un-namespace.h"
|
|
||||||
|
|
||||||
char *_mktemp(char *);
|
|
||||||
|
|
||||||
static int _gettemp(char *, int *, int, int);
|
|
||||||
|
|
||||||
static const unsigned char padchar[] =
|
|
||||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
||||||
|
|
||||||
int
|
|
||||||
mkstemps(char *path, int slen)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
return (_gettemp(path, &fd, 0, slen) ? fd : -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
mkstemp(char *path)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
return (_gettemp(path, &fd, 0, 0) ? fd : -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
mkdtemp(char *path)
|
|
||||||
{
|
|
||||||
return (_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
_mktemp(char *path)
|
|
||||||
{
|
|
||||||
return (_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
__warn_references(mktemp,
|
|
||||||
"warning: mktemp() possibly used unsafely; consider using mkstemp()");
|
|
||||||
|
|
||||||
char *
|
|
||||||
mktemp(char *path)
|
|
||||||
{
|
|
||||||
return (_mktemp(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
_gettemp(char *path, int *doopen, int domkdir, int slen)
|
|
||||||
{
|
|
||||||
char *start, *trv, *suffp, *carryp;
|
|
||||||
char *pad;
|
|
||||||
struct stat sbuf;
|
|
||||||
int rval;
|
|
||||||
uint32_t rand;
|
|
||||||
char carrybuf[MAXPATHLEN];
|
|
||||||
|
|
||||||
if ((doopen != NULL && domkdir) || slen < 0) {
|
|
||||||
errno = EINVAL;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (trv = path; *trv != '\0'; ++trv)
|
|
||||||
;
|
|
||||||
if (trv - path >= MAXPATHLEN) {
|
|
||||||
errno = ENAMETOOLONG;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
trv -= slen;
|
|
||||||
suffp = trv;
|
|
||||||
--trv;
|
|
||||||
if (trv < path || NULL != strchr(suffp, '/')) {
|
|
||||||
errno = EINVAL;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fill space with random characters */
|
|
||||||
while (trv >= path && *trv == 'X') {
|
|
||||||
rand = arc4random_uniform(sizeof(padchar) - 1);
|
|
||||||
*trv-- = padchar[rand];
|
|
||||||
}
|
|
||||||
start = trv + 1;
|
|
||||||
|
|
||||||
/* save first combination of random characters */
|
|
||||||
memcpy(carrybuf, start, suffp - start);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* check the target directory.
|
|
||||||
*/
|
|
||||||
if (doopen != NULL || domkdir) {
|
|
||||||
for (; trv > path; --trv) {
|
|
||||||
if (*trv == '/') {
|
|
||||||
*trv = '\0';
|
|
||||||
rval = stat(path, &sbuf);
|
|
||||||
*trv = '/';
|
|
||||||
if (rval != 0)
|
|
||||||
return (0);
|
|
||||||
if (!S_ISDIR(sbuf.st_mode)) {
|
|
||||||
errno = ENOTDIR;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
if (doopen) {
|
|
||||||
if ((*doopen =
|
|
||||||
_open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
|
|
||||||
return (1);
|
|
||||||
if (errno != EEXIST)
|
|
||||||
return (0);
|
|
||||||
} else if (domkdir) {
|
|
||||||
if (mkdir(path, 0700) == 0)
|
|
||||||
return (1);
|
|
||||||
if (errno != EEXIST)
|
|
||||||
return (0);
|
|
||||||
} else if (lstat(path, &sbuf))
|
|
||||||
return (errno == ENOENT);
|
|
||||||
|
|
||||||
/* If we have a collision, cycle through the space of filenames */
|
|
||||||
for (trv = start, carryp = carrybuf;;) {
|
|
||||||
/* have we tried all possible permutations? */
|
|
||||||
if (trv == suffp)
|
|
||||||
return (0); /* yes - exit with EEXIST */
|
|
||||||
pad = strchr(padchar, *trv);
|
|
||||||
if (pad == NULL) {
|
|
||||||
/* this should never happen */
|
|
||||||
errno = EIO;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
/* increment character */
|
|
||||||
*trv = (*++pad == '\0') ? padchar[0] : *pad;
|
|
||||||
/* carry to next position? */
|
|
||||||
if (*trv == *carryp) {
|
|
||||||
/* increment position and loop */
|
|
||||||
++trv;
|
|
||||||
++carryp;
|
|
||||||
} else {
|
|
||||||
/* try with new name */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*NOTREACHED*/
|
|
||||||
}
|
|
143
libc/upstream-openbsd/lib/libc/stdio/mktemp.c
Normal file
143
libc/upstream-openbsd/lib/libc/stdio/mktemp.c
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
/* $OpenBSD: mktemp.c,v 1.33 2014/05/06 22:55:27 millert Exp $ */
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1996-1998, 2008 Theo de Raadt
|
||||||
|
* Copyright (c) 1997, 2008-2009 Todd C. Miller
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define MKTEMP_NAME 0
|
||||||
|
#define MKTEMP_FILE 1
|
||||||
|
#define MKTEMP_DIR 2
|
||||||
|
|
||||||
|
#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
|
||||||
|
#define MIN_X 6
|
||||||
|
|
||||||
|
#ifndef nitems
|
||||||
|
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int
|
||||||
|
mktemp_internal(char *path, int slen, int mode)
|
||||||
|
{
|
||||||
|
char *start, *cp, *ep;
|
||||||
|
const char *tempchars = TEMPCHARS;
|
||||||
|
unsigned int tries;
|
||||||
|
struct stat sb;
|
||||||
|
size_t len;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
len = strlen(path);
|
||||||
|
if (len < MIN_X || slen < 0 || (size_t)slen > len - MIN_X) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
ep = path + len - slen;
|
||||||
|
|
||||||
|
for (start = ep; start > path && start[-1] == 'X'; start--)
|
||||||
|
;
|
||||||
|
if (ep - start < MIN_X) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
tries = INT_MAX;
|
||||||
|
do {
|
||||||
|
cp = start;
|
||||||
|
do {
|
||||||
|
unsigned short rbuf[16];
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Avoid lots of arc4random() calls by using
|
||||||
|
* a buffer sized for up to 16 Xs at a time.
|
||||||
|
*/
|
||||||
|
arc4random_buf(rbuf, sizeof(rbuf));
|
||||||
|
for (i = 0; i < nitems(rbuf) && cp != ep; i++)
|
||||||
|
*cp++ = tempchars[rbuf[i] % NUM_CHARS];
|
||||||
|
} while (cp != ep);
|
||||||
|
|
||||||
|
switch (mode) {
|
||||||
|
case MKTEMP_NAME:
|
||||||
|
if (lstat(path, &sb) != 0)
|
||||||
|
return(errno == ENOENT ? 0 : -1);
|
||||||
|
break;
|
||||||
|
case MKTEMP_FILE:
|
||||||
|
fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
|
||||||
|
if (fd != -1 || errno != EEXIST)
|
||||||
|
return(fd);
|
||||||
|
break;
|
||||||
|
case MKTEMP_DIR:
|
||||||
|
if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR) == 0)
|
||||||
|
return(0);
|
||||||
|
if (errno != EEXIST)
|
||||||
|
return(-1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (--tries);
|
||||||
|
|
||||||
|
errno = EEXIST;
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *_mktemp(char *);
|
||||||
|
|
||||||
|
char *
|
||||||
|
_mktemp(char *path)
|
||||||
|
{
|
||||||
|
if (mktemp_internal(path, 0, MKTEMP_NAME) == -1)
|
||||||
|
return(NULL);
|
||||||
|
return(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
__warn_references(mktemp,
|
||||||
|
"warning: mktemp() possibly used unsafely; consider using mkstemp()");
|
||||||
|
|
||||||
|
char *
|
||||||
|
mktemp(char *path)
|
||||||
|
{
|
||||||
|
return(_mktemp(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mkstemp(char *path)
|
||||||
|
{
|
||||||
|
return(mktemp_internal(path, 0, MKTEMP_FILE));
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mkstemps(char *path, int slen)
|
||||||
|
{
|
||||||
|
return(mktemp_internal(path, slen, MKTEMP_FILE));
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
mkdtemp(char *path)
|
||||||
|
{
|
||||||
|
int error;
|
||||||
|
|
||||||
|
error = mktemp_internal(path, 0, MKTEMP_DIR);
|
||||||
|
return(error ? NULL : path);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user