test6_7.txt

来自「Linux下的C语言编程」· 文本 代码 · 共 85 行

TXT
85
字号
/*无主函数,只是应用四个子函数*/
#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);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?