|
楼主 |
发表于 2004-11-22 10:04:07
|
显示全部楼层
/*无主函数,只是应用四个子函数*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <malloc.h>
#include <malloc.h>
#include <signal.h>
#include <errno.h>
#include <stdarg.h>
int safeopen(const char *pathname, int flags, mode_t mode)
{
int retval;
if(retval=open(pathname, flags, mode)==-1)
HandleError(errno, "open", "open %s failed", pathname);
return retval;
}
int safewrite(int fd, const void *buf, size_t count)
{
int retval;
if(retval=write(fd, buf, count)==-1)
HandleError(errno, "write", "write %d byets to fd %d failed", (int)count, fd);
return retval;
}
int saferead(int fd, void *buf, size_t count)
{
int retval;
if(retval=read(fd, buf, count)==-1)
HandleError(errno, "read", "read %d bytes to fd %d failed", (int)count,fd);
return retval;
}
int safeclose(int fd)
{
int retval;
if(retval=close(fd)==-1)
HandleError(errno, "close", "Possible serious problem: colse fd %d failed", fd);
return retval;
}
void HandleError(int ecode, const char *const caller, const char *fmt, ...)
{
va_list fmtargs;
struct sigaction sastruct;
FILE *of=(SafeLibErrorDest)?SafeLibErrorDest:stderr;
SafeLibErrorLoc=caller;
SafeLibErrorno=ecode;
va_start(fmtargs, fmt);
fprintf(of, "***Error in %s:", caller);
vfprintf(of, fmt, fmtargs);
va_end(fmtargs);
fprintf(of, "\n");
if(ecode)
{
fprintf(of, "*** Error cause:%s \n", strerror(ecode));
}
sigaction(SIGUSR1, NULL, &sastruct);
if(sastruct.sa_handler!=SIG_DFL)
{
raise(SIGUSR1);
}
else
{
exit(254);
}
} |
|