NeoMutt  2025-09-05-43-g177ed6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
tmp.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <errno.h>
31#include <inttypes.h>
32#include <limits.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37#include "mutt/lib.h"
38#include "config/lib.h"
39#include "tmp.h"
40#include "globals.h"
41#include "neomutt.h"
42
51void buf_mktemp_full(struct Buffer *buf, const char *prefix, const char *suffix,
52 const char *src, int line)
53{
54 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
55 buf_printf(buf, "%s/%s-%s-%d-%d-%" PRIu64 "%s%s", NONULL(c_tmp_dir),
56 NONULL(prefix), NONULL(ShortHostname), (int) getuid(),
57 (int) getpid(), mutt_rand64(), suffix ? "." : "", NONULL(suffix));
58
59 mutt_debug(LL_DEBUG3, "%s:%d: buf_mktemp returns \"%s\"\n", src, line, buf_string(buf));
60 if (unlink(buf_string(buf)) && (errno != ENOENT))
61 {
62 mutt_debug(LL_DEBUG1, "%s:%d: ERROR: unlink(\"%s\"): %s (errno %d)\n", src,
63 line, buf_string(buf), strerror(errno), errno);
64 }
65}
66
77FILE *mutt_file_mkstemp_full(const char *file, int line, const char *func)
78{
79 char name[PATH_MAX] = { 0 };
80
81 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
82 int n = snprintf(name, sizeof(name), "%s/neomutt-XXXXXX", NONULL(c_tmp_dir));
83 if (n < 0)
84 return NULL;
85
86 int fd = mkstemp(name);
87 if (fd == -1)
88 return NULL;
89
90 FILE *fp = fdopen(fd, "w+");
91
92 if ((unlink(name) != 0) && (errno != ENOENT))
93 {
95 return NULL;
96 }
97
98 MuttLogger(0, file, line, func, 1, "created temp file '%s'\n", name);
99 return fp;
100}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition helpers.c:168
Convenience wrapper for the config headers.
#define mutt_file_fclose(FP)
Definition file.h:139
char * ShortHostname
Short version of the hostname.
Definition globals.c:37
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
int log_dispatcher_t MuttLogger
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:46
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:44
Convenience wrapper for the library headers.
#define PATH_MAX
Definition mutt.h:42
Container for Accounts, Notifications.
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition random.c:123
#define NONULL(x)
Definition string2.h:43
String manipulation buffer.
Definition buffer.h:36
Container for Accounts, Notifications.
Definition neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:47
FILE * mutt_file_mkstemp_full(const char *file, int line, const char *func)
Create temporary file safely.
Definition tmp.c:77
void buf_mktemp_full(struct Buffer *buf, const char *prefix, const char *suffix, const char *src, int line)
Create a temporary file.
Definition tmp.c:51
Create Temporary Files.