NeoMutt  2025-12-11-769-g906513
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
tmp.h File Reference

Create Temporary Files. More...

#include <stdio.h>
+ Include dependency graph for tmp.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define buf_mktemp(buf)
 
#define buf_mktemp_draft(buf)
 
#define mutt_file_mkstemp()
 

Functions

void buf_mktemp_full (struct Buffer *buf, const char *prefix, const char *suffix, const char *src, int line, const char *cfg)
 Create a temporary file.
 
FILE * mutt_file_mkstemp_full (const char *file, int line, const char *func)
 Create temporary file safely.
 

Detailed Description

Create Temporary Files.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file tmp.h.

Macro Definition Documentation

◆ buf_mktemp

#define buf_mktemp ( buf)
Value:
buf_mktemp_full(buf, "neomutt", NULL, __FILE__, __LINE__, "tmp_dir")
void buf_mktemp_full(struct Buffer *buf, const char *prefix, const char *suffix, const char *src, int line, const char *cfg)
Create a temporary file.
Definition tmp.c:52

Definition at line 33 of file tmp.h.

◆ buf_mktemp_draft

#define buf_mktemp_draft ( buf)
Value:
buf_mktemp_full(buf, "neomutt", NULL, __FILE__, __LINE__, "tmp_draft_dir")

Definition at line 34 of file tmp.h.

◆ mutt_file_mkstemp

#define mutt_file_mkstemp ( )
Value:
mutt_file_mkstemp_full(__FILE__, __LINE__, __func__)
FILE * mutt_file_mkstemp_full(const char *file, int line, const char *func)
Create temporary file safely.
Definition tmp.c:78

Definition at line 36 of file tmp.h.

Function Documentation

◆ buf_mktemp_full()

void buf_mktemp_full ( struct Buffer * buf,
const char * prefix,
const char * suffix,
const char * src,
int line,
const char * cfg )

Create a temporary file.

Parameters
bufBuffer for result
prefixPrefix for filename
suffixSuffix for filename
srcSource file of caller
lineSource line number of caller
cfgConfig option for the temp directory

Definition at line 52 of file tmp.c.

54{
55 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, cfg);
56 buf_printf(buf, "%s/%s-%s-%d-%d-%" PRIu64 "%s%s", NONULL(c_tmp_dir),
57 NONULL(prefix), NONULL(ShortHostname), (int) getuid(),
58 (int) getpid(), mutt_rand64(), suffix ? "." : "", NONULL(suffix));
59
60 mutt_debug(LL_DEBUG3, "%s:%d: buf_mktemp returns \"%s\"\n", src, line, buf_string(buf));
61 if (unlink(buf_string(buf)) && (errno != ENOENT))
62 {
63 mutt_debug(LL_DEBUG1, "%s:%d: ERROR: unlink(\"%s\"): %s (errno %d)\n", src,
64 line, buf_string(buf), strerror(errno), errno);
65 }
66}
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
char * ShortHostname
Short version of the hostname.
Definition globals.c:36
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition random.c:123
#define NONULL(x)
Definition string2.h:44
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_file_mkstemp_full()

FILE * mutt_file_mkstemp_full ( const char * file,
int line,
const char * func )

Create temporary file safely.

Parameters
fileSource file of caller
lineSource line number of caller
funcFunction name of caller
Return values
ptrFILE handle
NULLError, see errno

Create and immediately unlink a temp file using mkstemp().

Definition at line 78 of file tmp.c.

79{
80 char name[PATH_MAX] = { 0 };
81
82 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
83 int n = snprintf(name, sizeof(name), "%s/neomutt-XXXXXX", NONULL(c_tmp_dir));
84 if ((n < 0) || ((size_t) n >= sizeof(name)))
85 return NULL;
86
87 int fd = mkstemp(name);
88 if (fd == -1)
89 return NULL;
90
91 FILE *fp = fdopen(fd, "w+");
92 if (!fp)
93 {
94 close(fd);
95 return NULL;
96 }
97
98 if ((unlink(name) != 0) && (errno != ENOENT))
99 {
100 mutt_file_fclose(&fp);
101 return NULL;
102 }
103
104 MuttLogger(0, file, line, func, 1, "created temp file '%s'\n", name);
105 return fp;
106}
#define mutt_file_fclose(FP)
Definition file.h:139
int log_dispatcher_t MuttLogger
#define PATH_MAX
Definition mutt.h:49
+ Here is the call graph for this function: