源码下载:http://apuebook.com/code3e.html
apue.3e/systype.h :判断操作系统类别
apue.3e/Make.defines.linux:平台相关的定义
apue.3e/Make.libapue.inc:执行 “apue/lib/”中Makefile
apue.3e/include/apue.h:宏定义和函数声明
apue.3e/lib/:函数的具体实现
父文件夹
systype.sh
判断操作系统类别
# (leading space required for Xenix /bin/sh)
#
# Determine the type of *ix operating system that we're
# running on, and echo an appropriate value.
# This script is intended to be used in Makefiles.
# (This is a kludge. Gotta be a better way.)
#
# 判断操作系统类别
case "`uname -s`" in
"FreeBSD")
PLATFORM="freebsd"
;;
"Linux")
PLATFORM="linux"
;;
"Darwin")
PLATFORM="macos"
;;
"SunOS")
PLATFORM="solaris"
;;
*)
echo "Unknown platform" >&2
exit 1
esac
echo $PLATFORM
exit 0
Make.defines.linux
# Common make definitions, customized for each platform
# Definitions required in all program directories to compile and link
# C programs using gcc.
# C语言编译器参数。默认命令是“cc”
CC = gcc
# CPPFLAGS: C++语言编译器参数
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
# LDFLAGS: 连接器参数(如“ld”)
LDFLAGS =
LDDIR = -L$(ROOT)/lib
LDLIBS = $(LDDIR) -lapue $(EXTRALIBS)
# -ansi ansi C标准
# -I将后面表示的目录作为第一个寻找头文件的目录,顺序为:(ROOT)/include->/usr/include->/usr/local/include
CFLAGS = -ansi -I$(ROOT)/include -Wall -DLINUX -D_GNU_SOURCE $(EXTRA)
RANLIB = echo
# 函数库打开包程序。默认命令是“ar”
AR = ar
AWK = awk
LIBAPUE = $(ROOT)/lib/libapue.a
# Common temp files to delete from each directory.
TEMPFILES = core core.* *.o temp.* *.out
Make.libapue.inc
$(LIBAPUE):
(cd $(ROOT)/lib && $(MAKE))
子文件夹 include
apue.h
/*
* Our own header, to be included before all standard system headers.
*/
#ifndef _APUE_H
#define _APUE_H
#define _POSIX_C_SOURCE 200809L
#if defined(SOLARIS) /* Solaris 10 */
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 700
#endif
#include <sys/types.h> /* some systems still require this */
#include <sys/stat.h>
#include <sys/termios.h> /* for winsize */
#if defined(MACOS) || !defined(TIOCGWINSZ)
#include <sys/ioctl.h>
#endif
#include <stdio.h> /* for convenience */
#include <stdlib.h> /* for convenience */
#include <stddef.h> /* for offsetof */
#include <string.h> /* for convenience */
#include <unistd.h> /* for convenience */
#include <signal.h> /* for SIG_ERR */
#define MAXLINE 4096 /* max line length */
/*
* Default file access permissions for new files.
用户读 | 用户写 | 用户组读 | 其他用户读
*/
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/*
* Default permissions for new directories.
FILE_MODE | 用户执行 | 用户组执行 | 其他用户执行
*/
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
typedef void Sigfunc(int); /* for signal handlers */
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
/*
* Prototypes for our own functions.
*/
char *path_alloc(size_t *); /* {Prog pathalloc} */
long open_max(void); /* {Prog openmax} */
int set_cloexec(int); /* {Prog setfd} */
void clr_fl(int, int);
void set_fl(int, int); /* {Prog setfl} */
void pr_exit(int); /* {Prog prexit} */
void pr_mask(const char *); /* {Prog prmask} */
Sigfunc *signal_intr(int, Sigfunc *); /* {Prog signal_intr_function} */
void daemonize(const char *); /* {Prog daemoninit} */
void sleep_us(unsigned int); /* {Ex sleepus} */
ssize_t readn(int, void *, size_t); /* {Prog readn_writen} */
ssize_t writen(int, const void *, size_t); /* {Prog readn_writen} */
int fd_pipe(int *); /* {Prog sock_fdpipe} */
int recv_fd(int, ssize_t (*func)(int,
const void *, size_t)); /* {Prog recvfd_sockets} */
int send_fd(int, int); /* {Prog sendfd_sockets} */
int send_err(int, int,
const char *); /* {Prog senderr} */
int serv_listen(const char *); /* {Prog servlisten_sockets} */
int serv_accept(int, uid_t *); /* {Prog servaccept_sockets} */
int cli_conn(const char *); /* {Prog cliconn_sockets} */
int buf_args(char *, int (*func)(int,
char **)); /* {Prog bufargs} */
int tty_cbreak(int); /* {Prog raw} */
int tty_raw(int); /* {Prog raw} */
int tty_reset(int); /* {Prog raw} */
void tty_atexit(void); /* {Prog raw} */
struct termios *tty_termios(void); /* {Prog raw} */
int ptym_open(char *, int); /* {Prog ptyopen} */
int ptys_open(char *); /* {Prog ptyopen} */
#ifdef TIOCGWINSZ
pid_t pty_fork(int *, char *, int, const struct termios *,
const struct winsize *); /* {Prog ptyfork} */
#endif
int lock_reg(int, int, int, off_t, int, off_t); /* {Prog lockreg} */
#define read_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
pid_t lock_test(int, int, off_t, int, off_t); /* {Prog locktest} */
#define is_read_lockable(fd, offset, whence, len) \
(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) \
(lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)
void err_msg(const char *, ...); /* {App misc_source} */
void err_dump(const char *, ...) __attribute__((noreturn));
void err_quit(const char *, ...) __attribute__((noreturn));
void err_cont(int, const char *, ...);
void err_exit(int, const char *, ...) __attribute__((noreturn));
void err_ret(const char *, ...);
void err_sys(const char *, ...) __attribute__((noreturn));
void log_msg(const char *, ...); /* {App misc_source} */
void log_open(const char *, int, int);
void log_quit(const char *, ...) __attribute__((noreturn));
void log_ret(const char *, ...);
void log_sys(const char *, ...) __attribute__((noreturn));
void log_exit(int, const char *, ...) __attribute__((noreturn));
void TELL_WAIT(void); /* parent/child from {Sec race_conditions} */
void TELL_PARENT(pid_t);
void TELL_CHILD(pid_t);
void WAIT_PARENT(void);
void WAIT_CHILD(void);
#endif /* _APUE_H */
子文件夹 lib
Makefile
#
# Makefile for misc library.
#
ROOT=..
PLATFORM=$(shell $(ROOT)/systype.sh)
include $(ROOT)/Make.defines.$(PLATFORM)
LIBMISC = libapue.a
OBJS = bufargs.o cliconn.o clrfl.o \
daemonize.o error.o errorlog.o lockreg.o locktest.o \
openmax.o pathalloc.o popen.o prexit.o prmask.o \
ptyfork.o ptyopen.o readn.o recvfd.o senderr.o sendfd.o \
servaccept.o servlisten.o setfd.o setfl.o signal.o signalintr.o \
sleepus.o spipe.o tellwait.o ttymodes.o writen.o
all: $(LIBMISC) sleep.o
$(LIBMISC): $(OBJS)
$(AR) rv $(LIBMISC) $?
$(RANLIB) $(LIBMISC)
clean:
rm -f *.o a.out core temp.* $(LIBMISC)
include $(ROOT)/Make.libapue.inc
error.c
输出至标准出错文件的出错处理函数
#include "apue.h"
#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;
/*ap指向传入的第一个可选参数,fmt是最后一个确定的参数, 即...前面的参数*/
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);
}
/*
* Nonfatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and return.
*/
void
err_cont(int error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
}
/*
* 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];
/* int vsnprintf(char* str, size_t size, const char* format, va_list ap);
* char *str [out],把生成的格式化的字符串存放在这里.
* size_t size [in], str可接受的最大字符数
* const char *format [in], 指定输出格式的字符串,它决定了你需要提供的可变参数的类型、个数和顺序
* va_list ap [in], va_list变量. va:variable-argument:可变参数
*/
vsnprintf(buf, MAXLINE-1, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf)-1, ": %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 */
}
errlog.c
用于守护进程的出错处理函数
/*
* Error routines for programs that can run as a daemon.
*/
#include "apue.h"
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ISO C variable arguments */
#include <syslog.h>
static void log_doit(int, int, int, const char *, va_list ap);
/*
* Caller must define and set this: nonzero if
* interactive, zero if daemon
*/
extern int log_to_stderr;
/*
* Initialize syslog(), if running as daemon.
*/
void
log_open(const char *ident, int option, int facility)
{
if (log_to_stderr == 0)
/* void openlog(const char *ident, int logopt, int facility);
* ident: 是一个字符串指针,它所指向的字符串会放在每个消息的前面,
* 通常的应用都是设置为程序名,用来指示是哪个程序创建里这个消息
* facility: 指定是什么程序类型在记录日志---这在后来使用 syslog() 里会被用到。
*/
openlog(ident, option, facility);
}
/*
* Nonfatal error related to a system call.
* Print a message with the system's errno value and return.
*/
void
log_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(1, errno, LOG_ERR, fmt, ap);
va_end(ap);
}
/*
* Fatal error related to a system call.
* Print a message and terminate.
*/
void
log_sys(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(1, errno, LOG_ERR, fmt, ap);
va_end(ap);
exit(2);
}
/*
* Nonfatal error unrelated to a system call.
* Print a message and return.
*/
void
log_msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(0, 0, LOG_ERR, fmt, ap);
va_end(ap);
}
/*
* Fatal error unrelated to a system call.
* Print a message and terminate.
*/
void
log_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(0, 0, LOG_ERR, fmt, ap);
va_end(ap);
exit(2);
}
/*
* Fatal error related to a system call.
* Error number passed as an explicit parameter.
* Print a message and terminate.
*/
void
log_exit(int error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(1, error, LOG_ERR, fmt, ap);
va_end(ap);
exit(2);
}
/*
* Print a message and return to caller.
* Caller specifies "errnoflag" and "priority".
*/
static void
log_doit(int errnoflag, int error, int priority, const char *fmt,
va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE-1, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf)-1, ": %s",
strerror(error));
strcat(buf, "\n");
if (log_to_stderr) {
fflush(stdout);
fputs(buf, stderr);
fflush(stderr);
} else {
/* void syslog(int priority, const char *message, arguments...);
* 向日志设备(日志工具 facility)发送日志消息。每一个消息都有一个 priority(优先级) 参数,
* 这个参数由一个“危险系数”(severity level)和一个程序标识码(facility value)相或(OR)得来。
*/
syslog(priority, "%s", buf);
}
}
其他的c文件未列出
执行make,可在lib文件下生成.o文件,libapue.a,
参考
http://www.bubuko.com/infodetail-1193969.html
https://www.cnblogs.com/xbf9xbf/p/4755458.html
https://www.cnblogs.com/yyangblog/p/4159778.html
https://blog.youkuaiyun.com/ixidof/article/details/5961621