Back to index

courier  0.68.2
tempname.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001-2003 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 #include "config.h"
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <errno.h>
00011 #include <unistd.h>
00012 #include <fcntl.h>
00013 #include <time.h>
00014 #if TIME_WITH_SYS_TIME
00015 #include <sys/time.h>
00016 #include <time.h>
00017 #else
00018 #if HAVE_SYS_TIME_H
00019 #include <sys/time.h>
00020 #else
00021 #include <time.h>
00022 #endif
00023 #endif
00024 #include "tempname.h"
00025 
00026 static const char base64[] =
00027 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-,";
00028 
00029 int libmail_tempfile(char *filename_buf)
00030 {
00031        unsigned long seed;
00032        int i;
00033        int fd;
00034 
00035 #if HAVE_GETTIMEOFDAY
00036        struct timeval tv;
00037 
00038        gettimeofday(&tv, NULL);
00039 
00040        seed=tv.tv_sec;
00041        seed ^= tv.tv_usec << 16;
00042 #else
00043        time_t t;
00044 
00045        time(&t);
00046        seed=t;
00047 #endif
00048        seed ^= getpid();
00049 
00050        for (i=0; i<1000; i++, seed += 5000)
00051        {
00052               char *p;
00053               unsigned long n;
00054 
00055               strcpy(filename_buf, "/tmp/mimegpg.");
00056 
00057               p=filename_buf + strlen(filename_buf);
00058 
00059               n=seed;
00060               *p++=base64[ n % 64 ]; n /= 64;
00061               *p++=base64[ n % 64 ]; n /= 64;
00062               *p++=base64[ n % 64 ]; n /= 64;
00063               *p++=base64[ n % 64 ]; n /= 64;
00064               *p++=base64[ n % 64 ]; n /= 64;
00065               *p++=base64[ n % 64 ];
00066               *p=0;
00067 
00068               if ((fd=open(filename_buf, O_RDWR | O_CREAT | O_EXCL, 0600))
00069                   >= 0)
00070                      return (fd);
00071 
00072 
00073               if (errno != EEXIST)
00074                      break;
00075        }
00076        return (-1);
00077 }