diff options
| author | Sam Varshavchik | 2013-08-19 16:39:41 -0400 |
|---|---|---|
| committer | Sam Varshavchik | 2013-08-25 14:43:51 -0400 |
| commit | 9c45d9ad13fdf439d44d7443ae75da15ea0223ed (patch) | |
| tree | 7a81a04cb51efb078ee350859a64be2ebc6b8813 /gpglib/tempname.c | |
| parent | a9520698b770168d1f33d6301463bb70a19655ec (diff) | |
| download | courier-libs-9c45d9ad13fdf439d44d7443ae75da15ea0223ed.tar.bz2 | |
Initial checkin
Imported from subversion report, converted to git. Updated all paths in
scripts and makefiles, reflecting the new directory hierarchy.
Diffstat (limited to 'gpglib/tempname.c')
| -rw-r--r-- | gpglib/tempname.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/gpglib/tempname.c b/gpglib/tempname.c new file mode 100644 index 0000000..30a6faf --- /dev/null +++ b/gpglib/tempname.c @@ -0,0 +1,77 @@ +/* +** Copyright 2001-2003 Double Precision, Inc. See COPYING for +** distribution information. +*/ + + +#include "config.h" +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> +#include <time.h> +#if TIME_WITH_SYS_TIME +#include <sys/time.h> +#include <time.h> +#else +#if HAVE_SYS_TIME_H +#include <sys/time.h> +#else +#include <time.h> +#endif +#endif +#include "tempname.h" + +static const char base64[] = +"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-,"; + +int libmail_tempfile(char *filename_buf) +{ + unsigned long seed; + int i; + int fd; + +#if HAVE_GETTIMEOFDAY + struct timeval tv; + + gettimeofday(&tv, NULL); + + seed=tv.tv_sec; + seed ^= tv.tv_usec << 16; +#else + time_t t; + + time(&t); + seed=t; +#endif + seed ^= getpid(); + + for (i=0; i<1000; i++, seed += 5000) + { + char *p; + unsigned long n; + + strcpy(filename_buf, "/tmp/mimegpg."); + + p=filename_buf + strlen(filename_buf); + + n=seed; + *p++=base64[ n % 64 ]; n /= 64; + *p++=base64[ n % 64 ]; n /= 64; + *p++=base64[ n % 64 ]; n /= 64; + *p++=base64[ n % 64 ]; n /= 64; + *p++=base64[ n % 64 ]; n /= 64; + *p++=base64[ n % 64 ]; + *p=0; + + if ((fd=open(filename_buf, O_RDWR | O_CREAT | O_EXCL, 0600)) + >= 0) + return (fd); + + + if (errno != EEXIST) + break; + } + return (-1); +} |
