#ifndef EWRAP_H
#define EWRAP_H
#include <errno.h>
#include <stdarg.h>
#include <string.h>
static char *name = NULL;//プログラム名
char *progname( void )
/* progname : プログラム名を返す */
{
return name;
}
void eprintf( char *fmt, ... )
/* eprintf : エラーメッセージを出力する */
{
va_list args;
fflush( stdout );
if( progname() != NULL ){
fprintf( stderr, "%s: ", progname() );
}
va_start( args,fmt );
vfprintf( stderr, fmt, args );
va_end( args );
if( fmt[0] != '\0' && fmt[ strlen( fmt )-1 ] == ':' ){
fprintf( stderr,"%s", strerror( errno ) );
}
fprintf( stderr, "\n" );
exit(2);
}
char *estrdup( char *s )
/* estrdup : 文字列のコピー.エラー時にはそれを知らせる */
{
char *t;
t = (char *)malloc( strlen(s) + 1 );
if( t == NULL ){
eprintf( "estrdup(\"%.20s\")failed:", s );
}
strcpy( t, s );
return t;
}
void setprogname( char *str )
/* setprogname : プログラム名を設定 */
{
name = estrdup(str);
}
void escanf( char *instr,char *outstr )
/* escanf : エラー無く標準入力から文字列を取る */
{
char buffer[8];
fgets( buffer, sizeof( buffer ), stdin );
int error_check = sscanf( buffer, instr, outstr );
if( error_check != 1 ){
eprintf( "Error:input" );
}
}
void *emalloc( size_t n )
/* emalloc : mallocで、エラー時にはそれを知らせる */
{
void *p;
p = malloc(n);
if( p == NULL ){
eprintf( "malloc of %u bytes failed:", n );
}
return p;
}
#endif
最終更新:2011年03月04日 14:10