32 |
#include <netdb.h> |
#include <netdb.h> |
33 |
#include <errno.h> |
#include <errno.h> |
34 |
|
|
35 |
|
#include <mailutils/error.h> |
36 |
#include <mailutils/mutil.h> |
#include <mailutils/mutil.h> |
37 |
#include <mailutils/iterator.h> |
#include <mailutils/iterator.h> |
38 |
|
|
530 |
} |
} |
531 |
} |
} |
532 |
|
|
533 |
|
/* Create and open a temporary file. Be vary careful about it, since we |
534 |
|
may be running with extra privilege i.e setgid(). |
535 |
|
Returns file descriptor of the open file. |
536 |
|
If namep is not NULL, the pointer to the malloced file name will |
537 |
|
be stored there. Otherwise, the file is unlinked right after open, |
538 |
|
i.e. it will disappear after close(fd). */ |
539 |
|
|
540 |
|
#ifndef P_tmpdir |
541 |
|
# define P_tmpdir "/tmp" |
542 |
|
#endif |
543 |
|
|
544 |
|
int |
545 |
|
mu_tempfile (const char *tmpdir, char **namep) |
546 |
|
{ |
547 |
|
char *filename; |
548 |
|
int fd; |
549 |
|
|
550 |
|
if (!tmpdir) |
551 |
|
tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : P_tmpdir; |
552 |
|
|
553 |
|
filename = malloc (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1); |
554 |
|
if (!filename) |
555 |
|
return -1; |
556 |
|
sprintf (filename, "%s/muXXXXXX", tmpdir); |
557 |
|
|
558 |
|
#ifdef HAVE_MKSTEMP |
559 |
|
{ |
560 |
|
int save_mask = umask (077); |
561 |
|
fd = mkstemp (filename); |
562 |
|
umask (save_mask); |
563 |
|
} |
564 |
|
#else |
565 |
|
if (mktemp (filename)) |
566 |
|
fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600); |
567 |
|
else |
568 |
|
fd = -1; |
569 |
|
#endif |
570 |
|
|
571 |
|
if (fd == -1) |
572 |
|
{ |
573 |
|
mu_error ("Can not open temporary file: %s", strerror(errno)); |
574 |
|
free (filename); |
575 |
|
return -1; |
576 |
|
} |
577 |
|
|
578 |
|
if (namep) |
579 |
|
*namep = filename; |
580 |
|
else |
581 |
|
{ |
582 |
|
unlink (filename); |
583 |
|
free (filename); |
584 |
|
} |
585 |
|
|
586 |
|
return fd; |
587 |
|
} |