标签:
// apue.h #ifndef APUE_SUNYJ #define APUE_SUNYJ void err_quit(const char *fmt, ...); void err_sys(const char *fmt, ...); #endif
// error.cpp
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
int64_t const MAXLINE = 4096; // max line length
/*
 *  * Print a message and return to caller.
 *   * Caller specifies "errnoflag".
 *    */
static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
    char buf[MAXLINE];
    vsnprintf(buf, MAXLINE, fmt, ap);
    if (errnoflag)
        snprintf(buf + strlen(buf), MAXLINE - strlen(buf), ": %s", strerror(
                    error));
    strcat(buf, "\n");
    fflush(stdout);/* in case stdout and stderr are the same */
    fputs(buf, stderr);
    fflush(NULL);/* flushes all stdio output streams */
}
/*
 *  * Fatal error unrelated to a system call.
 *   * Print a message and terminate.
 *    */
void err_quit(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    err_doit(0, 0, fmt, ap);
    va_end(ap);
    exit(1) ; // process terminate, not just like return, totally different
}
/*
 *  * Fatal error related to a system call.
 *   * Print a message and terminate.
 *    */
void err_sys(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    err_doit(1, errno, fmt, ap);
    va_end(ap);
    exit(1) ;
}
主要目的,在centos6.3 64位机器中,使用eclipse学习APUE第三版,且这次学习不使用书中自带的apue.h,与lib下的一些库,或者说,是上一次学习的时候,碰到的库是直接用的,这次我全部提出来,一个一个的改编,细心的一个一个函数的学习,且使用g++编译器
UNIX环境高级编程eclipse环境配置加上error.cc
标签:
原文地址:http://www.cnblogs.com/sunyongjie1984/p/4284479.html