cleanup usage and wrapper

This commit is contained in:
Renaud 2013-04-30 17:24:46 +08:00 committed by Renaud Fivet
parent 128354e657
commit 68a79430e6
4 changed files with 42 additions and 28 deletions

29
usage.c
View File

@ -1,22 +1,27 @@
/* usage.c -- implements usage.h */
/* TODO: align exit return code */
#include "usage.h" #include "usage.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
static void report(const char* prefix, const char *err, va_list params) static void report( const char *prefix, const char *err, va_list params) {
{ char msg[ 4096] ;
char msg[4096];
vsnprintf(msg, sizeof(msg), err, params); vsnprintf( msg, sizeof msg, err, params) ;
fprintf(stderr, "%s%s\n", prefix, msg); fprintf( stderr, "%s%s\n", prefix, msg) ;
} }
void die(const char* err, ...) void die( const char *err, ...) {
{ va_list params ;
va_list params;
va_start(params, err); va_start( params, err) ;
report("fatal: ", err, params); report( "fatal: ", err, params) ;
va_end(params); va_end( params) ;
exit(128); exit( 128) ;
} }
/* end of usage.c */

View File

@ -1,6 +1,6 @@
#ifndef USAGE_H_ #ifndef USAGE_H_
#define USAGE_H_ #define USAGE_H_
extern void die(const char* err, ...); void die( const char *err, ...) ;
#endif /* USAGE_H_ */ #endif /* USAGE_H_ */

View File

@ -1,22 +1,29 @@
/* wrapper.c -- implements wrapper.h */
#include "wrapper.h"
#include "usage.h" #include "usage.h"
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
/* Function copyright: git */ /* Function copyright: git */
int xmkstemp(char *template) void xmkstemp( char *template) {
{ int fd ;
int fd;
fd = mkstemp(template); fd = mkstemp( template) ;
if (fd < 0) if( fd < 0)
die("Unable to create temporary file"); die( "Unable to create temporary file") ;
return fd;
close( fd) ;
} }
void *xmalloc(size_t size) void *xmalloc( size_t size) {
{ void *ret = malloc( size) ;
void *ret = malloc(size); if( !ret)
if (!ret) die( "Out of memory") ;
die("Out of memory");
return ret; return ret ;
} }
/* end of wrapper.c */

6
wrapper.h Normal file → Executable file
View File

@ -1,8 +1,10 @@
#ifndef WRAPPER_H_ #ifndef WRAPPER_H_
#define WRAPPER_H_ #define WRAPPER_H_
extern int xmkstemp(char *template); #include <stdlib.h>
extern void *xmalloc(size_t size); void xmkstemp( char *template) ;
void *xmalloc( size_t size) ;
#endif /* WRAPPER_H_ */ #endif /* WRAPPER_H_ */