Mercurial > pidgin
comparison src/util.c @ 3230:d05dc05bde8a
[gaim-migrate @ 3247]
Added gaim_mkstemp() for secure tempfile creation.
committer: Tailor Script <tailor@pidgin.im>
| author | Jim Seymour <jseymour> |
|---|---|
| date | Tue, 07 May 2002 23:12:14 +0000 |
| parents | fb4393b3da5f |
| children | cf460a8c859c |
comparison
equal
deleted
inserted
replaced
| 3229:20612da83d8c | 3230:d05dc05bde8a |
|---|---|
| 1264 tm.tm_hour = hour; | 1264 tm.tm_hour = hour; |
| 1265 tm.tm_min = min; | 1265 tm.tm_min = min; |
| 1266 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; | 1266 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; |
| 1267 return mktime(&tm); | 1267 return mktime(&tm); |
| 1268 } | 1268 } |
| 1269 | |
| 1270 /* | |
| 1271 * Like mkstemp() but returns a file pointer, uses a pre-set template, | |
| 1272 * uses the semantics of tempnam() for the directory to use and allocates | |
| 1273 * the space for the filepath. | |
| 1274 * | |
| 1275 * Caller is responsible for closing the file and removing it when done, | |
| 1276 * as well as freeing the space pointed-to by "path" with g_free(). | |
| 1277 * | |
| 1278 * Returns NULL on failure and cleans up after itself if so. | |
| 1279 */ | |
| 1280 static const char *gaim_mkstemp_templ = {"gaimXXXXXX"}; | |
| 1281 | |
| 1282 FILE *gaim_mkstemp(gchar **fpath) | |
| 1283 { | |
| 1284 static char *tmpdir = NULL; | |
| 1285 int fd; | |
| 1286 FILE *fp = NULL; | |
| 1287 | |
| 1288 if(!tmpdir) { | |
| 1289 if((tmpdir = tempnam(NULL, NULL)) == NULL) { | |
| 1290 fprintf(stderr, "tempnam() failed, error: %d\n", errno); | |
| 1291 } else { | |
| 1292 char *t = strrchr(tmpdir, '/'); | |
| 1293 *t = '\0'; | |
| 1294 } | |
| 1295 } | |
| 1296 | |
| 1297 if(tmpdir) { | |
| 1298 if((*fpath = g_strdup_printf("%s/%s", tmpdir, gaim_mkstemp_templ)) != NULL) { | |
| 1299 if((fd = mkstemp(*fpath)) == -1) { | |
| 1300 fprintf(stderr, "Couldn't make \"%s\", error: %d\n", *fpath, errno); | |
| 1301 } else { | |
| 1302 if((fp = fdopen(fd, "r+")) == NULL) { | |
| 1303 close(fd); | |
| 1304 fprintf(stderr, "Couldn't fdopen(), error: %d\n", errno); | |
| 1305 } | |
| 1306 } | |
| 1307 if(!fp) { | |
| 1308 g_free(*fpath); | |
| 1309 *fpath = NULL; | |
| 1310 } | |
| 1311 } | |
| 1312 } | |
| 1313 | |
| 1314 return fp; | |
| 1315 } |
