關(guān)于unix高級(jí)環(huán)境編程 編譯時(shí)的err_sys和err_quit錯(cuò)誤

err_sys以及err_quit等函數(shù)不是C語(yǔ)言自帶函數(shù),是作者自己編寫的函數(shù)。所以,想要運(yùn)行書中的源代碼,就必須自建一個(gè)頭文件my_err.h把作者的代碼拷貝進(jìn)去,把這個(gè)文件跟目標(biāo)程序放于同一個(gè)文件夾中。然后在目標(biāo)中加上#include"my_err.h"就可以了
下面是error.h的內(nèi)容。

#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */

static void err_doit(int, int, const char *, va_list);

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


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


/*
 * Fatal error unrelated to a system call.
 * Error code passed as explict parameter.
 * Print a message and terminate.
 */
void
err_exit(int error, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    err_doit(1, error, fmt, ap);
    va_end(ap);
    exit(1);
}


/*
 * Fatal error related to a system call.
 * Print a message, dump core, and terminate.
 */
void
err_dump(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    err_doit(1, errno, fmt, ap);
    va_end(ap);
    abort(); /* dump core and terminate */
    exit(1); /* shouldn't get here */
}


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

    va_start(ap, fmt);
    err_doit(0, 0, fmt, ap);
    va_end(ap);
}


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


/*
 * 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 */
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 動(dòng)態(tài)鏈接,在可執(zhí)行文件裝載時(shí)或運(yùn)行時(shí),由操作系統(tǒng)的裝載程序加載庫(kù)。大多數(shù)操作系統(tǒng)將解析外部引用(比如庫(kù))作為加載過(guò)...
    小5筒閱讀 5,778評(píng)論 0 3
  • 【MySQL】Linux下MySQL 5.5、5.6和5.7的RPM、二進(jìn)制和源碼安裝 1.1BLOG文檔結(jié)構(gòu)圖 ...
    小麥苗DB寶閱讀 10,878評(píng)論 0 31
  • make menuconfig過(guò)程解析作者 codercjg 在 28 九月 2015, 5:27 下午 make...
    codercjg閱讀 1,227評(píng)論 0 1
  • 1.團(tuán)隊(duì)意識(shí)。這是個(gè)共贏的時(shí)代,幫助身邊每個(gè)人,就是幫助你自己。 2.細(xì)節(jié)意識(shí)。給每個(gè)人都給予關(guān)心幫助,關(guān)懷上...
    Stan森閱讀 303評(píng)論 0 0
  • 多年的思念日益劇增,多么渴望能夠陪伴在你們身邊!
    琦琦格閱讀 469評(píng)論 2 2

友情鏈接更多精彩內(nèi)容