diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-11 08:21:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-11 08:21:29 +0000 |
commit | 29cd838eab01ed7110f3ccb2e8c6a35c8a31dbcc (patch) | |
tree | 63ef546b10a81d461e5cf5ed9e98a68cd7dee1aa /src/sed/lib/mkstemp.c | |
parent | Initial commit. (diff) | |
download | kbuild-e48fe8ce9d7780c2e25428a70815d2c87c580412.tar.xz kbuild-e48fe8ce9d7780c2e25428a70815d2c87c580412.zip |
Adding upstream version 1:0.1.9998svn3589+dfsg.upstream/1%0.1.9998svn3589+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/sed/lib/mkstemp.c')
-rw-r--r-- | src/sed/lib/mkstemp.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/sed/lib/mkstemp.c b/src/sed/lib/mkstemp.c new file mode 100644 index 0000000..5b00205 --- /dev/null +++ b/src/sed/lib/mkstemp.c @@ -0,0 +1,70 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STRINGS_H +# include <strings.h> +#else +# include <string.h> +#endif /* HAVE_STRINGS_H */ + +#ifdef HAVE_STDLIB_H +# include <stdlib.h> +#endif /* HAVE_STDLIB_H */ + +#ifdef HAVE_SYS_FILE_H +# include <sys/file.h> +#endif /* HAVE_SYS_FILE_H */ + +#ifdef HAVE_IO_H +# include <io.h> +#endif /* HAVE_IO_H */ + +#ifdef HAVE_UNISTD_H +# include <unistd.h> +#endif /* HAVE_UNISTD_H */ + +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif /* HAVE_FCNTL_H */ + +#include <limits.h> +#include <errno.h> + +/* Generate a unique temporary file name from template. The last six characters of + template must be XXXXXX and these are replaced with a string that makes the + filename unique. */ + +int +mkstemp (template) + char *template; +{ + int i, j, n, fd; + char *data = template + strlen(template) - 6; + + if (data < template) { + errno = EINVAL; + return -1; + } + + for (n = 0; n <= 5; n++) + if (data[n] != 'X') { + errno = EINVAL; + return -1; + } + + for (i = 0; i < INT_MAX; i++) { + j = i ^ 827714841; /* Base 36 DOSSUX :-) */ + for (n = 5; n >= 0; n--) { + data[n] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [j % 36]; + j /= 36; + } + + fd = open (template, O_CREAT|O_EXCL|O_RDWR, 0600); + if (fd != -1) + return fd; + } + + errno = EEXIST; + return -1; +} |