From: Jim S. <jse...@us...> - 2002-05-07 23:12:17
|
Update of /cvsroot/gaim/gaim/src In directory usw-pr-cvs1:/tmp/cvs-serv30651/src Modified Files: util.c gaim.h Log Message: Added gaim_mkstemp() for secure tempfile creation. Index: util.c =================================================================== RCS file: /cvsroot/gaim/gaim/src/util.c,v retrieving revision 1.117 retrieving revision 1.118 diff -u -d -r1.117 -r1.118 --- util.c 16 Apr 2002 15:04:28 -0000 1.117 +++ util.c 7 May 2002 23:12:14 -0000 1.118 @@ -1266,3 +1266,50 @@ tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; return mktime(&tm); } + +/* + * Like mkstemp() but returns a file pointer, uses a pre-set template, + * uses the semantics of tempnam() for the directory to use and allocates + * the space for the filepath. + * + * Caller is responsible for closing the file and removing it when done, + * as well as freeing the space pointed-to by "path" with g_free(). + * + * Returns NULL on failure and cleans up after itself if so. + */ +static const char *gaim_mkstemp_templ = {"gaimXXXXXX"}; + +FILE *gaim_mkstemp(gchar **fpath) +{ + static char *tmpdir = NULL; + int fd; + FILE *fp = NULL; + + if(!tmpdir) { + if((tmpdir = tempnam(NULL, NULL)) == NULL) { + fprintf(stderr, "tempnam() failed, error: %d\n", errno); + } else { + char *t = strrchr(tmpdir, '/'); + *t = '\0'; + } + } + + if(tmpdir) { + if((*fpath = g_strdup_printf("%s/%s", tmpdir, gaim_mkstemp_templ)) != NULL) { + if((fd = mkstemp(*fpath)) == -1) { + fprintf(stderr, "Couldn't make \"%s\", error: %d\n", *fpath, errno); + } else { + if((fp = fdopen(fd, "r+")) == NULL) { + close(fd); + fprintf(stderr, "Couldn't fdopen(), error: %d\n", errno); + } + } + if(!fp) { + g_free(*fpath); + *fpath = NULL; + } + } + } + + return fp; +} Index: gaim.h =================================================================== RCS file: /cvsroot/gaim/gaim/src/gaim.h,v retrieving revision 1.323 retrieving revision 1.324 diff -u -d -r1.323 -r1.324 --- gaim.h 6 May 2002 18:25:14 -0000 1.323 +++ gaim.h 7 May 2002 23:12:14 -0000 1.324 @@ -432,6 +432,7 @@ extern char *add_cr(char *); extern void strip_linefeed(char *); extern time_t get_time(int, int, int, int, int, int); +extern FILE *gaim_mkstemp(gchar **); /*------------------------------------------------------------------------*/ /* Multi-Entry dialog and vCard dialog support */ |