当前位置: 技术问答>linux和unix
怎么解决这一个warning
来源: 互联网 发布时间:2016-01-12
本文导语: 刚学Unix编程,系统是freebsd5.4, 编译器是gcc V3.4.2,在编译下面程序时总是有warning: assignment makes pointer from integer without a cast google和baidu了大半天都没能找到,请高手指点下,怎样才能去掉warning? 出warning的地方加...
刚学Unix编程,系统是freebsd5.4, 编译器是gcc V3.4.2,在编译下面程序时总是有warning: assignment makes pointer from integer without a cast
google和baidu了大半天都没能找到,请高手指点下,怎样才能去掉warning?
出warning的地方加上中文释了
#define _POSIX_SOURCE 199506L /*有人说加上这一句可以去掉warning ,但我加上了也没用*/
#include "apue.h"
#include
#include /*for definition of errno*/
#include /*for c variable arguments*/
#include
int log_to_stderr;
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
err_quit("usage: ls directory_name");
if(dp = opendir(argv[1]) == NULL) /*出了warning*/
err_sys("can't open %s", argv[1]);
while(dirp = readdir(dp) != NULL) /*出了warning*/
printf("%sn", dirp ->d_name);
closedir(dp);
exit(0);
}
//#include "apue.h"
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 explicit parameter.
* Print error 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 a 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 cast stdout and stderr are the same*/
fputs(buf, stderr);
fflush(NULL); /*flushes all stdio output streams*/
}
/*
* Error routines for programs that can run as a daemon
*/
//#include "apue.h"
static void log_doit(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)
openlog(ident, option, facility);
}
/*
* Nonfatal error related to a system call.
* Print a message wit a system's errno value and return.
*/
void log_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(1, 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, 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, LOG_ERR, fmt, ap);
va_end(ap);
}
/*
* Fatal error unrelated to a system call
* Print a message and teminate
*/
void log_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(0, LOG_ERR, fmt, ap);
va_end(ap);
exit(2);
}
/*
* Print a message and return to a caller.
* Caller specifies "errnoflag" and "priority"
*/
static void log_doit(int errnoflag, int priority, const char *fmt,
va_list ap)
{
int errno_save;
char buf[MAXLINE];
errno_save = errno; /*value caller might want printed*/
vsnprintf(buf, MAXLINE, fmt, ap);
if(errnoflag)
snprintf(buf + strlen(buf), MAXLINE - strlen(buf), ": %s",
strerror(errno_save));
strcat(buf, "n");
if(log_to_stderr){
fflush(stdout);
fputs(buf, stderr);
fflush(stderr);
}
else{
syslog(priority, buf);
}
}
google和baidu了大半天都没能找到,请高手指点下,怎样才能去掉warning?
出warning的地方加上中文释了
#define _POSIX_SOURCE 199506L /*有人说加上这一句可以去掉warning ,但我加上了也没用*/
#include "apue.h"
#include
#include /*for definition of errno*/
#include /*for c variable arguments*/
#include
int log_to_stderr;
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
err_quit("usage: ls directory_name");
if(dp = opendir(argv[1]) == NULL) /*出了warning*/
err_sys("can't open %s", argv[1]);
while(dirp = readdir(dp) != NULL) /*出了warning*/
printf("%sn", dirp ->d_name);
closedir(dp);
exit(0);
}
//#include "apue.h"
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 explicit parameter.
* Print error 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 a 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 cast stdout and stderr are the same*/
fputs(buf, stderr);
fflush(NULL); /*flushes all stdio output streams*/
}
/*
* Error routines for programs that can run as a daemon
*/
//#include "apue.h"
static void log_doit(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)
openlog(ident, option, facility);
}
/*
* Nonfatal error related to a system call.
* Print a message wit a system's errno value and return.
*/
void log_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(1, 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, 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, LOG_ERR, fmt, ap);
va_end(ap);
}
/*
* Fatal error unrelated to a system call
* Print a message and teminate
*/
void log_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_doit(0, LOG_ERR, fmt, ap);
va_end(ap);
exit(2);
}
/*
* Print a message and return to a caller.
* Caller specifies "errnoflag" and "priority"
*/
static void log_doit(int errnoflag, int priority, const char *fmt,
va_list ap)
{
int errno_save;
char buf[MAXLINE];
errno_save = errno; /*value caller might want printed*/
vsnprintf(buf, MAXLINE, fmt, ap);
if(errnoflag)
snprintf(buf + strlen(buf), MAXLINE - strlen(buf), ": %s",
strerror(errno_save));
strcat(buf, "n");
if(log_to_stderr){
fflush(stdout);
fputs(buf, stderr);
fflush(stderr);
}
else{
syslog(priority, buf);
}
}
|
if(dp = opendir(argv[1]) == NULL)
>>
if( (dp = opendir(argv[1])) == NULL)
>>
if( (dp = opendir(argv[1])) == NULL)