am e8bd8c2e: Merge "Upgrade mktemp.c to the current upstream version."

* commit 'e8bd8c2ed9c71c73682f29020456934c55f260e3':
  Upgrade mktemp.c to the current upstream version.
This commit is contained in:
Elliott Hughes 2013-07-19 16:37:52 -07:00 committed by Android Git Automerger
commit 178c418573
3 changed files with 78 additions and 69 deletions

View File

@ -25,7 +25,6 @@ libc_common_src_files := \
stdio/ftell.c \ stdio/ftell.c \
stdio/fvwrite.c \ stdio/fvwrite.c \
stdio/gets.c \ stdio/gets.c \
stdio/mktemp.c \
stdio/printf.c \ stdio/printf.c \
stdio/refill.c \ stdio/refill.c \
stdio/rewind.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/getc.c \
upstream-freebsd/lib/libc/stdio/getchar.c \ upstream-freebsd/lib/libc/stdio/getchar.c \
upstream-freebsd/lib/libc/stdio/makebuf.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/putc.c \
upstream-freebsd/lib/libc/stdio/putchar.c \ upstream-freebsd/lib/libc/stdio/putchar.c \
upstream-freebsd/lib/libc/stdio/puts.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 * Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -28,18 +27,30 @@
* SUCH DAMAGE. * 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 <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <ctype.h> #include <ctype.h>
#include <unistd.h> #include <unistd.h>
#include "un-namespace.h"
char *_mktemp(char *);
static int _gettemp(char *, int *, int, int); static int _gettemp(char *, int *, int, int);
extern uint32_t arc4random(); static const unsigned char padchar[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int int
mkstemps(char *path, int slen) mkstemps(char *path, int slen)
@ -63,9 +74,7 @@ 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 *); char *
__LIBC_HIDDEN__ char *
_mktemp(char *path) _mktemp(char *path)
{ {
return (_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL); return (_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
@ -80,54 +89,50 @@ mktemp(char *path)
return (_mktemp(path)); return (_mktemp(path));
} }
static int static int
_gettemp(char *path, int *doopen, int domkdir, int slen) _gettemp(char *path, int *doopen, int domkdir, int slen)
{ {
char *start, *trv, *suffp; char *start, *trv, *suffp, *carryp;
char *pad;
struct stat sbuf; struct stat sbuf;
int rval; int rval;
pid_t pid; uint32_t rand;
char carrybuf[MAXPATHLEN];
if (doopen && domkdir) { if ((doopen != NULL && domkdir) || slen < 0) {
errno = EINVAL; errno = EINVAL;
return (0); return (0);
} }
for (trv = path; *trv; ++trv) for (trv = path; *trv != '\0'; ++trv)
; ;
if (trv - path >= MAXPATHLEN) {
errno = ENAMETOOLONG;
return (0);
}
trv -= slen; trv -= slen;
suffp = trv; suffp = trv;
--trv; --trv;
if (trv < path) { if (trv < path || NULL != strchr(suffp, '/')) {
errno = EINVAL; errno = EINVAL;
return (0); 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); /* Fill space with random characters */
if (pid < 26) while (trv >= path && *trv == 'X') {
c = pid + 'A'; rand = arc4random_uniform(sizeof(padchar) - 1);
else *trv-- = padchar[rand];
c = (pid - 26) + 'a';
*trv-- = c;
} }
start = trv + 1; 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 * check the target directory.
* doesn't exist this runs for a *very* long time.
*/ */
if (doopen || domkdir) { if (doopen != NULL || domkdir) {
for (;; --trv) { for (; trv > path; --trv) {
if (trv <= path)
break;
if (*trv == '/') { if (*trv == '/') {
*trv = '\0'; *trv = '\0';
rval = stat(path, &sbuf); rval = stat(path, &sbuf);
@ -146,7 +151,7 @@ _gettemp(char *path, int *doopen, int domkdir, int slen)
for (;;) { for (;;) {
if (doopen) { if (doopen) {
if ((*doopen = if ((*doopen =
open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) _open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
return (1); return (1);
if (errno != EEXIST) if (errno != EEXIST)
return (0); return (0);
@ -156,26 +161,28 @@ _gettemp(char *path, int *doopen, int domkdir, int slen)
if (errno != EEXIST) if (errno != EEXIST)
return (0); return (0);
} else if (lstat(path, &sbuf)) } else if (lstat(path, &sbuf))
return(errno == ENOENT ? 1 : 0); return (errno == ENOENT);
/* tricky little algorithm for backward compatibility */ /* If we have a collision, cycle through the space of filenames */
for (trv = start;;) { for (trv = start, carryp = carrybuf;;) {
if (!*trv) /* have we tried all possible permutations? */
return (0);
if (*trv == 'Z') {
if (trv == suffp) if (trv == suffp)
return (0); /* yes - exit with EEXIST */
pad = strchr(padchar, *trv);
if (pad == NULL) {
/* this should never happen */
errno = EIO;
return (0); return (0);
*trv++ = 'a';
} else {
if (isdigit(*trv))
*trv = 'a';
else if (*trv == 'z') /* inc from z to A */
*trv = 'A';
else {
if (trv == suffp)
return (0);
++*trv;
} }
/* 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; break;
} }
} }

View File

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