diff options
author | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2010-09-28 18:18:43 +0000 |
---|---|---|
committer | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2010-09-28 18:18:43 +0000 |
commit | 945ccbb69099c3b9a4cd5cfdb679f4eb18f0b3d8 (patch) | |
tree | 297c8a9788fc502b35f3cb29edecfd4f49b924c2 /main | |
parent | c1992316ae7b98e191df106898b0ba26bbe1b56b (diff) |
Solaris compatibility fixes
Review: https://reviewboard.asterisk.org/r/942/
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.8@289104 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r-- | main/strcompat.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/main/strcompat.c b/main/strcompat.c index 831ab0ce9..e718cc628 100644 --- a/main/strcompat.c +++ b/main/strcompat.c @@ -29,6 +29,8 @@ #include <unistd.h> /* for fcntl(2) */ #include <fcntl.h> /* for fcntl(2) */ +#include "asterisk/utils.h" + #ifndef HAVE_STRSEP char *strsep(char **str, const char *delims) { @@ -478,3 +480,91 @@ void closefrom(int n) } #endif +#ifndef HAVE_MKDTEMP +/* $OpenBSD: mktemp.c,v 1.30 2010/03/21 23:09:30 schwarze 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. + */ + +#define MKTEMP_NAME 0 +#define MKTEMP_FILE 1 +#define MKTEMP_DIR 2 + +#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_." +#define NUM_CHARS (sizeof(TEMPCHARS) - 1) + +static int mktemp_internal(char *path, int slen, int mode) +{ + char *start, *cp, *ep; + const char *tempchars = TEMPCHARS; + unsigned int r, tries; + struct stat sb; + size_t len; + int fd; + + len = strlen(path); + if (len == 0 || slen >= len) { + errno = EINVAL; + return(-1); + } + ep = path + len - slen; + + tries = 1; + for (start = ep; start > path && start[-1] == 'X'; start--) { + if (tries < INT_MAX / NUM_CHARS) { + tries *= NUM_CHARS; + } + } + tries *= 2; + + do { + for (cp = start; cp != ep; cp++) { + r = ast_random() % NUM_CHARS; + *cp = tempchars[r]; + } + + 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 *mkdtemp(char *path) +{ + return mktemp_internal(path, 0, MKTEMP_DIR) ? NULL : path; +} +#endif |