Merge "Upgrade mktemp.c to the current upstream version."

This commit is contained in:
Elliott Hughes 2013-07-19 23:35:29 +00:00 committed by Gerrit Code Review
commit e8bd8c2ed9
3 changed files with 78 additions and 69 deletions

View file

@ -25,7 +25,6 @@ libc_common_src_files := \
stdio/ftell.c \
stdio/fvwrite.c \
stdio/gets.c \
stdio/mktemp.c \
stdio/printf.c \
stdio/refill.c \
stdio/rewind.c \
@ -270,6 +269,7 @@ libc_upstream_freebsd_src_files := \
upstream-freebsd/lib/libc/stdio/getc.c \
upstream-freebsd/lib/libc/stdio/getchar.c \
upstream-freebsd/lib/libc/stdio/makebuf.c \
upstream-freebsd/lib/libc/stdio/mktemp.c \
upstream-freebsd/lib/libc/stdio/putc.c \
upstream-freebsd/lib/libc/stdio/putchar.c \
upstream-freebsd/lib/libc/stdio/puts.c \

View file

@ -1,4 +1,3 @@
/* $OpenBSD: mktemp.c,v 1.19 2005/08/08 08:05:36 espie Exp $ */
/*
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
@ -28,18 +27,30 @@
* SUCH DAMAGE.
*/
#include <sys/types.h>
#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);
extern uint32_t arc4random();
static const unsigned char padchar[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int
mkstemps(char *path, int slen)
@ -60,15 +71,13 @@ mkstemp(char *path)
char *
mkdtemp(char *path)
{
return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
return (_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
}
char *_mktemp(char *);
__LIBC_HIDDEN__ char *
char *
_mktemp(char *path)
{
return(_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
return (_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
}
__warn_references(mktemp,
@ -77,66 +86,62 @@ __warn_references(mktemp,
char *
mktemp(char *path)
{
return(_mktemp(path));
return (_mktemp(path));
}
static int
_gettemp(char *path, int *doopen, int domkdir, int slen)
{
char *start, *trv, *suffp;
char *start, *trv, *suffp, *carryp;
char *pad;
struct stat sbuf;
int rval;
pid_t pid;
uint32_t rand;
char carrybuf[MAXPATHLEN];
if (doopen && domkdir) {
errno = EINVAL;
return(0);
}
for (trv = path; *trv; ++trv)
;
trv -= slen;
suffp = trv;
--trv;
if (trv < path) {
if ((doopen != NULL && domkdir) || slen < 0) {
errno = EINVAL;
return (0);
}
pid = getpid();
while (trv >= path && *trv == 'X' && pid != 0) {
*trv-- = (pid % 10) + '0';
pid /= 10;
}
while (trv >= path && *trv == 'X') {
char c;
pid = (arc4random() & 0xffff) % (26+26);
if (pid < 26)
c = pid + 'A';
else
c = (pid - 26) + 'a';
*trv-- = c;
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 you have six X's and it
* doesn't exist this runs for a *very* long time.
* check the target directory.
*/
if (doopen || domkdir) {
for (;; --trv) {
if (trv <= path)
break;
if (doopen != NULL || domkdir) {
for (; trv > path; --trv) {
if (*trv == '/') {
*trv = '\0';
rval = stat(path, &sbuf);
*trv = '/';
if (rval != 0)
return(0);
return (0);
if (!S_ISDIR(sbuf.st_mode)) {
errno = ENOTDIR;
return(0);
return (0);
}
break;
}
@ -146,36 +151,38 @@ _gettemp(char *path, int *doopen, int domkdir, int slen)
for (;;) {
if (doopen) {
if ((*doopen =
open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
return(1);
_open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
return (1);
if (errno != EEXIST)
return(0);
return (0);
} else if (domkdir) {
if (mkdir(path, 0700) == 0)
return(1);
return (1);
if (errno != EEXIST)
return(0);
} else if (lstat(path, &sbuf))
return(errno == ENOENT ? 1 : 0);
/* tricky little algorithm for backward compatibility */
for (trv = start;;) {
if (!*trv)
return (0);
if (*trv == 'Z') {
if (trv == suffp)
return (0);
*trv++ = 'a';
} 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 {
if (isdigit(*trv))
*trv = 'a';
else if (*trv == 'z') /* inc from z to A */
*trv = 'A';
else {
if (trv == suffp)
return (0);
++*trv;
}
/* try with new name */
break;
}
}

View file

@ -17,4 +17,6 @@
#ifndef _BIONIC_FREEBSD_NAMESPACE_H_included
#define _BIONIC_FREEBSD_NAMESPACE_H_included
__attribute__((visibility("hidden"))) char* _mktemp(char*);
#endif