diff options
author | William Joye <wjoye@cfa.harvard.edu> | 2016-12-21 22:56:22 (GMT) |
---|---|---|
committer | William Joye <wjoye@cfa.harvard.edu> | 2016-12-21 22:56:22 (GMT) |
commit | d1a6de55efc90f190dee42ab8c4fa9070834e77d (patch) | |
tree | ec633f5608ef498bee52a5f42c12c49493ec8bf8 /tcl8.6/compat/mkstemp.c | |
parent | 5514e37335c012cc70f5b9aee3cedfe3d57f583f (diff) | |
parent | 98acd3f494b28ddd8c345a2bb9311e41e2d56ddd (diff) | |
download | blt-d1a6de55efc90f190dee42ab8c4fa9070834e77d.zip blt-d1a6de55efc90f190dee42ab8c4fa9070834e77d.tar.gz blt-d1a6de55efc90f190dee42ab8c4fa9070834e77d.tar.bz2 |
Merge commit '98acd3f494b28ddd8c345a2bb9311e41e2d56ddd' as 'tcl8.6'
Diffstat (limited to 'tcl8.6/compat/mkstemp.c')
-rw-r--r-- | tcl8.6/compat/mkstemp.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tcl8.6/compat/mkstemp.c b/tcl8.6/compat/mkstemp.c new file mode 100644 index 0000000..1a44dfa --- /dev/null +++ b/tcl8.6/compat/mkstemp.c @@ -0,0 +1,78 @@ +/* + * mkstemp.c -- + * + * Source code for the "mkstemp" library routine. + * + * Copyright (c) 2009 Donal K. Fellows + * + * See the file "license.terms" for information on usage and redistribution of + * this file, and for a DISCLAIMER OF ALL WARRANTIES. + */ + +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + +/* + *---------------------------------------------------------------------- + * + * mkstemp -- + * + * Create an open temporary file from a template. + * + * Results: + * A file descriptor, or -1 (with errno set) in the case of an error. + * + * Side effects: + * The template is updated to contain the real filename. + * + *---------------------------------------------------------------------- + */ + +int +mkstemp( + char *template) /* Template for filename. */ +{ + static const char alphanumerics[] = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + register char *a, *b; + int fd, count, alphanumericsLen = strlen(alphanumerics); /* == 62 */ + + a = template + strlen(template); + while (a > template && *(a-1) == 'X') { + a--; + } + + if (a == template) { + errno = ENOENT; + return -1; + } + + /* + * We'll only try up to 10 times; after that, we're suffering from enemy + * action and should let the caller know. + */ + + count = 10; + do { + /* + * Replace the X's in the original template with random alphanumeric + * digits. + */ + + for (b=a ; *b ; b++) { + float r = rand() / ((float) RAND_MAX); + + *b = alphanumerics[(int)(r * alphanumericsLen)]; + } + + /* + * Template is now realized; try to open (with correct options). + */ + + fd = open(template, O_RDWR|O_CREAT|O_EXCL, 0600); + } while (fd == -1 && errno == EEXIST && --count > 0); + + return fd; +} |