aboutsummaryrefslogtreecommitdiffstats
path: root/main/strcompat.c
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2010-09-28 18:20:20 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2010-09-28 18:20:20 +0000
commitfbff1f94909d66a96e1308afa3e7ef3d8f99669c (patch)
tree80312d731b5d271b32891813eb236cb07975c542 /main/strcompat.c
parente2cfdefadbfd7789c6fa38cc053554a46f1e26ae (diff)
Merged revisions 289104 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.8 ........ r289104 | tilghman | 2010-09-28 13:18:43 -0500 (Tue, 28 Sep 2010) | 4 lines Solaris compatibility fixes Review: https://reviewboard.asterisk.org/r/942/ ........ git-svn-id: http://svn.digium.com/svn/asterisk/trunk@289112 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/strcompat.c')
-rw-r--r--main/strcompat.c90
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