Browse Source

Add die-on-error version of asprintf

tags/fmail-0.1.1
Damien Goutte-Gattat 9 years ago
parent
commit
c5bd68602e
3 changed files with 53 additions and 8 deletions
  1. +36
    -0
      lib/xmem.c
  2. +13
    -4
      lib/xmem.h
  3. +4
    -4
      src/fmail.c

+ 36
- 0
lib/xmem.c View File

@@ -21,6 +21,7 @@
#endif

#include <xmem.h>
#include <stdio.h>
#include <string.h>
#include <err.h>

@@ -76,3 +77,38 @@ do_strdup(const char *s, const char *file, unsigned int line)

return dup;
}

int
do_asprintf(char **s,
const char *fmt,
const char *file,
unsigned int line,
...)
{
int ret;
va_list ap;

va_start(ap, line);
ret = vasprintf(s, fmt, ap);
va_end(ap);

if ( ret == -1 )
mem_error(file, line);

return ret;
}

int
do_vasprintf(char **s,
const char *fmt,
const char *file,
unsigned int line,
va_list ap)
{
int ret;

if ( (ret = vasprintf(s, fmt, ap)) == -1 )
mem_error(file, line);

return ret;
}

+ 13
- 4
lib/xmem.h View File

@@ -20,6 +20,7 @@
#define ICP20110203_XMEM_H

#include <stdlib.h>
#include <stdarg.h>

#ifdef __cpluscplus
extern "C" {
@@ -39,10 +40,18 @@ do_realloc(void *, size_t, const char *, unsigned int);
char *
do_strdup(const char *, const char *, unsigned int);

#define xmalloc(s) do_malloc(s, __FILE__, __LINE__)
#define xcalloc(n,s) do_calloc(n, s, __FILE__, __LINE__)
#define xrealloc(p,s) do_realloc(p, s, __FILE__, __LINE__)
#define xstrdup(s) do_strdup(s, __FILE__, __LINE__)
int
do_asprintf(char **, const char *, const char *, unsigned int, ...);

int
do_vasprintf(char **, const char *, const char *, unsigned int, va_list);

#define xmalloc(s) do_malloc(s, __FILE__, __LINE__)
#define xcalloc(n,s) do_calloc(n, s, __FILE__, __LINE__)
#define xrealloc(p,s) do_realloc(p, s, __FILE__, __LINE__)
#define xstrdup(s) do_strdup(s, __FILE__, __LINE__)
#define xasprintf(s,f,...) do_asprintf(s, f, __FILE__, __LINE__, __VA_ARGS__)
#define xvasprintf(s,f,v) do_vasprintf(s, f, __FILE__, __LINE__, v)

#ifdef __cplusplus
}


+ 4
- 4
src/fmail.c View File

@@ -465,12 +465,12 @@ read_input_from_editor(void)
int fd;
FILE *f;

if ( asprintf(&filename, "%s/fmailXXXXXX", get_tmp_dir()) == -1
|| (fd = mkstemp(filename)) == -1 )
xasprintf(&filename, "%s/fmailXXXXXX", get_tmp_dir());
if ( (fd = mkstemp(filename)) == -1 )
err(EXIT_FAILURE, "cannot create temporary file");

if ( asprintf(&command, "%s %s", get_editor(), filename) == -1
|| system(command) == -1 )
xasprintf(&command, "%s %s", get_editor(), filename);
if ( system(command) == -1 )
err(EXIT_FAILURE, "cannot execute editor");

if ( (f = fdopen(fd, "r")) == NULL )


Loading…
Cancel
Save