Advanced Programming in the UNIX Environment (third edition) is a well-known programming book for Unix systems.
However, the code example in the book, in order to correctly compile the run, you need to prepare the work:
1. Download the source code
Portal: http://apuebook.com/code3e.html
2. Extracting source code
Tar XF src.3e.tar.gz
3. Install Libbsd-devel, otherwise compile the wrong return
Yum Install Libbsd-devel-y
4. Compiling
CD apue.3e/MAKECP. /include/apue.h/usr/include/ CP. /lib/libapue.a/usr/local/lib/CP. /lib/libapue.a/usr/lib/
5. Custom Error Header File
Vim/usr/include/myerr.h#include<errno.h>/*For definition of errno*/#include<stdarg.h>/*ISO C variable aruments*/StaticvoidErr_doit (int,int, the constChar*, va_list);/** Nonfatal error related to a system call. * Print a message and return.*/voidErr_ret (constChar*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.*/voidErr_sys (constChar*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. */voidErr_exit (intError, constChar*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.*/voiderr_dump (constChar*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.*/voiderr_msg (constChar*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.*/voiderr_quit (constChar*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".*/StaticvoidErr_doit (intErrnoflag,intError, constChar*FMT, va_list AP) { CharBuf[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 is the same*/fputs (buf, stderr); Fflush (NULL); /*flushes all stdio output streams*/}
Test
Write a program
Vim Myls.c#include"Apue.h"#include"Myerr.h"#include<dirent.h>intMain (intargcChar*argv[]) {DIR*DP; struct Dirent*Dirp; if(ARGC! = 2) Err_quit ("Usage:ls Directory_name"); if(DP = Opendir (argv[1)) = =NULL) Err_sys ("Can ' t open%s", argv[1]); while((DIRP = Readdir (DP))! =NULL) printf ("%s\n", dirp->d_name); Closedir (DP); Exit (0);}
Compile
Perform
./a.out/root
about installing the "Advanced Programming for UNIX environment" source code under Unix/linux