- 论坛徽章:
- 0
|
按照书上 抄了两个函数:
#include <errno.h>
#include <stdarg.h>
#include "ourhdr.h"
static void err_diot(int, const char *, va_list )
char *pname = NULL;
/*caller can set this from argv[0]*/
/*
nonfatal error related to a system call
print a message and return
*/
void err_ret(const char *fmt, ...)
{
va_lsit ap;
va_start(ap, fmt);
err_doit(1, fmt, ap);
va_end(ap);
return;
}
/*
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);
va_doit(1, fmt, ap);
va_end (ap)
exit(1);
}
/*
fatal error related to a system call
print a messsage and dump core ,terminate
*/
void err_dump(const char * fmt)
{
va_list ap;
va_start(ap, fmt);
va_doit(1, fmt, ap);
va_end(ap);
abort();
exit(1);
}
/*
fatal error related to a system call
print a message and return
*/
void err_msg(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
va_doit(0, fmt, ap);
va_end(ap);
retrun;
}
/*
fatal error related to a system call
print a message and terminate
*/
void err_quit(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
va_doit(0, fmt, ap);
va_end(ap);
exit(1);
}
/*
print a message and return to caller
caller specifies "errnoflag"
*/
static void va_doit(int errnoflag, const char *fmt, va_lst ap)
{
int errno_save;
char buf[MAXLINE];
errno_save = errno;/*value caller may want printed*/
vsprintf(buf, fmt, ap);
if (errnoflag)
{
sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
}
strcat(buf, "\n");
fflush(stdout); /*in case stdout and stderr are the same*/
fputs(buf, stderr);
fflush(NULL); /*flushes all stdio output stream*/
return;
}
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include "myerr.c"
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
{
err_quit("a single argument(the dir name )is required!!");
}
if (dp = opendir(argv[1]) == NULL)
{
err_sys("can not open the dir %s" ,argv[1]);
}
while (dirp = readdir(dp) != NULL)
{
printf("%s\n" , dirp->d_name);
}
closedir(dp);
exit(0);
}
在linux下编译出错如下: 请大家帮助看看
linux:/opt/test # gcc opendir.c
In file included from opendir.c:4:
myerr.h: In function `err_doit':
myerr.h:6: error: parameter `pname' is initialized
myerr.h:14: error: parse error before '{' token
myerr.h:6: error: parm types given both in parmlist and separately
myerr.h:5: error: parameter name omitted
myerr.h:5: error: parameter name omitted
myerr.h:5: error: parameter name omitted
myerr.h:15: error: `va_lsit' undeclared (first use in this function)
myerr.h:15: error: (Each undeclared identifier is reported only once
myerr.h:15: error: for each function it appears in.)
myerr.h:16: error: `ap' undeclared (first use in this function)
myerr.h:16: error: `fmt' undeclared (first use in this function)
myerr.h:16: error: `va_start' used in function with fixed args
myerr.h: In function `err_sys':
myerr.h:33: error: parse error before "exit"
myerr.h: At top level:
myerr.h:41: error: conflicting types for `err_dump'
ourhdr.h:90: error: previous declaration of `err_dump'
myerr.h: In function `err_dump':
myerr.h:43: error: `va_start' used in function with fixed args
myerr.h: At top level:
myerr.h:56: error: conflicting types for `err_msg'
ourhdr.h:91: error: previous declaration of `err_msg'
myerr.h: In function `err_msg':
myerr.h:61: error: `retrun' undeclared (first use in this function)
myerr.h: At top level:
myerr.h:69: error: conflicting types for `err_quit'
ourhdr.h:92: error: previous declaration of `err_quit'
myerr.h:81: error: parse error before "va_lst"
myerr.h:82: warning: `va_doit' was declared implicitly `extern' and later `static'
myerr.h:72: warning: previous declaration of `va_doit'
myerr.h:82: warning: type mismatch with previous implicit declaration
myerr.h:72: warning: previous implicit declaration of `va_doit'
myerr.h:82: warning: `va_doit' was previously implicitly declared to return `int'
myerr.h: In function `va_doit':
myerr.h:87: error: `fmt' undeclared (first use in this function)
myerr.h:87: error: `ap' undeclared (first use in this function)
myerr.h:88: error: `errnoflag' undeclared (first use in this function)
opendir.c: In function `main':
opendir.c:17: warning: assignment makes pointer from integer without a cast
opendir.c:22: warning: assignment makes pointer from integer without a cast
[ 本帖最后由 yinian9826 于 2006-9-12 17:51 编辑 ] |
|