源码:
#include "apue.h"
int main(int argc, char argv[])
{
int i;
struct stat buff;
char *ptr;
#for(i = 1; i < argc; i++)Segmentation fault (core dumped) memmory overflow
for(i = 1; i < argc; i++)
{
printf("%s:",argv[i]);
if(lstat(argv[i], &buff) < 0)
{
err_ret("lstat error");
continue;
}
if(S_ISREG(buff.st_mode))
ptr = "regular";
else if(S_ISDIR(buff.st_mode))
ptr = "directory";
else if(S_ISBLK(buff.st_mode))
ptr = "block apecial";
else if(S_ISCHR(buff.st_mode))
ptr = "char special";
else if(S_ISFIFO(buff.st_mode))
ptr = "FIFO";
else if(S_ISLNK(buff.st_mode))
ptr = "symbolic link";
else if(S_ISSOCK(buff.st_mode))
ptr = "socket";
else
ptr = "unknown mode";
printf("%s file\n", ptr);
}
exit(0);
}
运行之后,出现错误:
/usr/include/sys/stat.h:264:12: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
extern int lstat (const char *__restrict __file,)
查看stat.h 文件后,发现:
extern int lstat (const char *__restrict __file,
struct stat *__restrict __buf) __THROW __nonnull ((1, 2));
# else
# ifdef __REDIRECT_NTH
extern int __REDIRECT_NTH (lstat,
(const char *__restrict __file,
struct stat *__restrict __buf), lstat64)
__nonnull ((1, 2));
应该是因为本书是在Linux3.20下做的实验,而我是在3.10下做的,所以出现了问题,所以在做示例程序时,最好避免环境的不一致性,会省去很多的问题(不过不出错,可能就不会学习到额外的东西吧,各有利弊吧)。