APUE_公共库与头文件

// apuesunyj.h
#ifndef APUE_SUNYJ
#define APUE_SUNYJ

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

int64_t const MAXLINE = 4096; // max line length
int64_t const ARG_MAX = 4096;

void err_quit(const char *fmt, ...);
void err_sys(const char *fmt, ...);
void err_ret(const char *fmt, ...);

#endif

 

// error.cpp
#include "apuesunyj.h"

/*
 *  * 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) ;
}

/*
 * Nonfatal error related to a system call.
 * Print a message and return.
 */
void err_ret(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	err_doit(1, errno, fmt, ap);
	va_end(ap);
}

 

# libApueSunyj.a
libApueSunyj.a: error.o
	ar cr libApueSunyj.a error.o
error.o: error.cpp apuesunyj.h
	g++ -c -g error.cpp -o error.o
clean:
	rm error.o libApueSunyj.a
posted @ 2015-02-11 10:34  孙永杰  阅读(290)  评论(0编辑  收藏  举报