//在学UNIX环境高级编程时把下面两个头文件与源文件放在同一个文件下就可以正常编译了,我的是在ubuntu 12.04环境下,第一个程序编译和运行成功了,希望对大家有帮助(我已经根据网上的资料修改好两个头文件)
/* Our own header, to be included *after* all standard system headers */
//ourhdr.h
#ifndef__ourhdr_h
#define__ourhdr_h
#include<sys/types.h>/* required for some of our prototypes */
#include<stdio.h>/* for convenience */
#include<stdlib.h>/* for convenience */
#include<string.h>/* for convenience */
#include<unistd.h>/* for convenience */
#defineMAXLINE4096/* max line length */
#defineFILE_MODE(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/* default file access permissions for new files */
#defineDIR_MODE(FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
/* default permissions for new directories */
typedefvoid Sigfunc(int);/* for signal handlers */
/* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */
#ifdefined(SIG_IGN) && !defined(SIG_ERR)
#defineSIG_ERR((Sigfunc *)-1)
#endif
#definemin(a,b)((a) < (b) ? (a) : (b))
#definemax(a,b)((a) > (b) ? (a) : (b))
/* prototypes for our own functions */
char*path_alloc(int *);/* {Prog pathalloc} */
intopen_max(void);/* {Prog openmax} */
voidclr_fl(int, int);/* {Prog setfl} */
voidset_fl(int, int);/* {Prog setfl} */
voidpr_exit(int);/* {Prog prexit} */
voidpr_mask(const char *);/* {Prog prmask} */
Sigfunc*signal_intr(int, Sigfunc *);/* {Prog signal_intr_function} */
inttty_cbreak(int);/* {Prog raw} */
inttty_raw(int);/* {Prog raw} */
inttty_reset(int);/* {Prog raw} */
voidtty_atexit(void);/* {Prog raw} */
#ifdefECHO /* only if <termios.h> has been included */
struct termios*tty_termios(void);/* {Prog raw} */
#endif
voidsleep_us(unsigned int);/* {Ex sleepus} */
ssize_treadn(int, void *, size_t);/* {Prog readn} */
ssize_twriten(int, const void *, size_t);/* {Prog writen} */
intdaemon_init(void);/* {Prog daemoninit} */
ints_pipe(int *);/* {Progs svr4_spipe bsd_spipe} */
intrecv_fd(int, ssize_t (*func)(int, const void *, size_t));
/* {Progs recvfd_svr4 recvfd_43bsd} */
intsend_fd(int, int);/* {Progs sendfd_svr4 sendfd_43bsd} */
intsend_err(int, int, const char *);/* {Prog senderr} */
intserv_listen(const char *);/* {Progs servlisten_svr4 servlisten_44bsd} */
intserv_accept(int, uid_t *);/* {Progs servaccept_svr4 servaccept_44bsd} */
intcli_conn(const char *);/* {Progs cliconn_svr4 cliconn_44bsd} */
intbuf_args(char *, int (*func)(int, char **));
/* {Prog bufargs} */
intptym_open(char *);/* {Progs ptyopen_svr4 ptyopen_44bsd} */
intptys_open(int, char *);/* {Progs ptyopen_svr4 ptyopen_44bsd} */
#ifdefTIOCGWINSZ
pid_tpty_fork(int *, char *, const struct termios *,
const struct winsize *);/* {Prog ptyfork} */
#endif
intlock_reg(int, int, int, off_t, int, off_t);
/* {Prog lockreg} */
#defineread_lock(fd, offset, whence, len)
lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len)
#definereadw_lock(fd, offset, whence, len)
lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len)
#definewrite_lock(fd, offset, whence, len)
lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)
#definewritew_lock(fd, offset, whence, len)
lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len)
#defineun_lock(fd, offset, whence, len)
lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len)
pid_tlock_test(int, int, off_t, int, off_t);
/* {Prog locktest} */
#defineis_readlock(fd, offset, whence, len)
lock_test(fd, F_RDLCK, offset, whence, len)
#defineis_writelock(fd, offset, whence, len)
lock_test(fd, F_WRLCK, offset, whence, len)
voiderr_dump(const char *, ...);/* {App misc_source} */
voiderr_msg(const char *, ...);
voiderr_quit(const char *, ...);
voiderr_ret(const char *, ...);
voiderr_sys(const char *, ...);
voidlog_msg(const char *, ...);/* {App misc_source} */
voidlog_open(const char *, int, int);
voidlog_quit(const char *, ...);
voidlog_ret(const char *, ...);
voidlog_sys(const char *, ...);
voidTELL_WAIT(void);/* parent/child from {Sec race_conditions} */
voidTELL_PARENT(pid_t);
voidTELL_CHILD(pid_t);
voidWAIT_PARENT(void);
voidWAIT_CHILD(void);
#endif/* __ourhdr_h */
//myerr.h
#include "ourhdr.h"
#include <errno.h>/* for definition of errno */
#include <stdarg.h>/* ISO C variable aruments */
static voiderr_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)
{
charbuf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
strerror(error));
strcat(buf, "
");
fflush(stdout);/* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}