NeoMutt  2023-05-17-16-g61469c
Teaching an old dog new tricks
DOXYGEN
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 mutt_mktemp(buf, buflen)   mutt_mktemp_pfx_sfx(buf, buflen, "neomutt", NULL)
 
#define mutt_mktemp_pfx_sfx(buf, buflen, prefix, suffix)   mutt_mktemp_full(buf, buflen, prefix, suffix, __FILE__, __LINE__)
 
#define buf_mktemp(buf)   buf_mktemp_pfx_sfx(buf, "neomutt", NULL)
 
#define buf_mktemp_pfx_sfx(buf, prefix, suffix)   buf_mktemp_full(buf, prefix, suffix, __FILE__, __LINE__)
 
#define mutt_file_mkstemp()   mutt_file_mkstemp_full(__FILE__, __LINE__, __func__)
 

Functions

void buf_mktemp_full (struct Buffer *buf, const char *prefix, const char *suffix, const char *src, int line)
 Create a temporary file. More...
 
FILE * mutt_file_mkstemp_full (const char *file, int line, const char *func)
 Create temporary file safely. More...
 
void mutt_mktemp_full (char *s, size_t slen, const char *prefix, const char *suffix, const char *src, int line)
 Create a temporary filename. More...
 

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

◆ mutt_mktemp

#define mutt_mktemp (   buf,
  buflen 
)    mutt_mktemp_pfx_sfx(buf, buflen, "neomutt", NULL)

Definition at line 34 of file tmp.h.

◆ mutt_mktemp_pfx_sfx

#define mutt_mktemp_pfx_sfx (   buf,
  buflen,
  prefix,
  suffix 
)    mutt_mktemp_full(buf, buflen, prefix, suffix, __FILE__, __LINE__)

Definition at line 35 of file tmp.h.

◆ buf_mktemp

#define buf_mktemp (   buf)    buf_mktemp_pfx_sfx(buf, "neomutt", NULL)

Definition at line 37 of file tmp.h.

◆ buf_mktemp_pfx_sfx

#define buf_mktemp_pfx_sfx (   buf,
  prefix,
  suffix 
)    buf_mktemp_full(buf, prefix, suffix, __FILE__, __LINE__)

Definition at line 38 of file tmp.h.

◆ mutt_file_mkstemp

#define mutt_file_mkstemp ( )    mutt_file_mkstemp_full(__FILE__, __LINE__, __func__)

Definition at line 40 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 
)

Create a temporary file.

Parameters
bufBuffer for result
prefixPrefix for filename
suffixSuffix for filename
srcSource file of caller
lineSource line number of caller

Definition at line 50 of file tmp.c.

52{
53 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
54 buf_printf(buf, "%s/%s-%s-%d-%d-%" PRIu64 "%s%s", NONULL(c_tmp_dir),
55 NONULL(prefix), NONULL(ShortHostname), (int) getuid(),
56 (int) getpid(), mutt_rand64(), suffix ? "." : "", NONULL(suffix));
57
58 mutt_debug(LL_DEBUG3, "%s:%d: mutt_mktemp returns \"%s\"\n", src, line, buf_string(buf));
59 if (unlink(buf_string(buf)) && (errno != ENOENT))
60 {
61 mutt_debug(LL_DEBUG1, "%s:%d: ERROR: unlink(\"%s\"): %s (errno %d)\n", src,
62 line, buf_string(buf), strerror(errno), errno);
63 }
64}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:171
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:78
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
char * ShortHostname
Short version of the hostname.
Definition: globals.c:40
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:42
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:40
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition: random.c:131
#define NONULL(x)
Definition: string2.h:37
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call 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 76 of file tmp.c.

77{
78 char name[PATH_MAX] = { 0 };
79
80 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
81 int n = snprintf(name, sizeof(name), "%s/neomutt-XXXXXX", NONULL(c_tmp_dir));
82 if (n < 0)
83 return NULL;
84
85 int fd = mkstemp(name);
86 if (fd == -1)
87 return NULL;
88
89 FILE *fp = fdopen(fd, "w+");
90
91 if ((unlink(name) != 0) && (errno != ENOENT))
92 {
94 return NULL;
95 }
96
97 MuttLogger(0, file, line, func, 1, "created temp file '%s'\n", name);
98 return fp;
99}
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:150
log_dispatcher_t MuttLogger
The log dispatcher -.
Definition: logging.c:52
#define PATH_MAX
Definition: mutt.h:41
+ Here is the call graph for this function:

◆ mutt_mktemp_full()

void mutt_mktemp_full ( char *  buf,
size_t  buflen,
const char *  prefix,
const char *  suffix,
const char *  src,
int  line 
)

Create a temporary filename.

Parameters
bufBuffer for result
buflenLength of buffer
prefixPrefix for filename
suffixSuffix for filename
srcSource file of caller
lineSource line number of caller
Note
This doesn't create the file, only the name

Definition at line 112 of file tmp.c.

114{
115 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
116 size_t n = snprintf(buf, buflen, "%s/%s-%s-%d-%d-%" PRIu64 "%s%s",
117 NONULL(c_tmp_dir), NONULL(prefix), NONULL(ShortHostname),
118 (int) getuid(), (int) getpid(), mutt_rand64(),
119 suffix ? "." : "", NONULL(suffix));
120 if (n >= buflen)
121 {
122 mutt_debug(LL_DEBUG1, "%s:%d: ERROR: insufficient buffer space to hold temporary filename! buflen=%zu but need %zu\n",
123 src, line, buflen, n);
124 }
125 mutt_debug(LL_DEBUG3, "%s:%d: mutt_mktemp returns \"%s\"\n", src, line, buf);
126 if ((unlink(buf) != 0) && (errno != ENOENT))
127 {
128 mutt_debug(LL_DEBUG1, "%s:%d: ERROR: unlink(\"%s\"): %s (errno %d)\n", src,
129 line, buf, strerror(errno), errno);
130 }
131}
+ Here is the call graph for this function: