红联Linux门户
Linux帮助

linux 编程 lstat函数 出错

发布时间:2010-05-26 09:26:30来源:红联作者:suowenair
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define oops(ch) {perror(ch);exit(0);}

void do_ls(char *);
void mode_info(struct stat);
void other_info(struct stat);

int main(int argc,char *argv[])
{
struct drent *dir;

//struct stat stats;
if(argc==1)
do_ls(".");
else
while(--argc)
{
do_ls(*(++argv));
}
return 1;
}

void do_ls(char *path)
{
DIR *fd;
struct stat stats,info;
struct dirent *dir;
if(lstat(path,&stats)==-1)
oops("lstat_1");
if(!S_ISDIR(stats.st_mode))
{
lstat(path,&stats);
mode_info(stats);
other_info(stats);
printf(" %s\n",path);
}
else
{
fd=opendir(path);
while((dir=readdir(fd))!=NULL)
{
if(lstat(dir->d_name,&info)==-1)
oops("lstat_2");
if(strcmp(dir->d_name,".")||strcmp(dir->d_name,".."))
{
mode_info(stats);
other_info(stats);
char *s;
printf(" %s\n",dir->d_name);
}
}
closedir(fd);

}

}

void mode_info(struct stat s)
{
char str[11];
strcpy(str,"----------");
if(S_ISDIR(s.st_mode))
str[0]='d';
else if(S_ISCHR(s.st_mode))
str[0]='c';
else if(S_ISBLK(s.st_mode))
str[0]='b';
else if(S_ISLNK(s.st_mode))
str[0]='l';
else if(S_ISFIFO(s.st_mode))
str[0]='f';
else if(S_ISSOCK(s.st_mode))
str[0]='s';
if(s.st_mode&S_IRUSR)
str[1]='r';
if(s.st_mode&S_IWUSR)
str[2]='w';
if(s.st_mode&S_IXUSR)
str[3]='x';

if(s.st_mode&S_IRGRP)
str[4]='r';
if(s.st_mode&S_IWGRP)
str[5]='w';
if(s.st_mode&S_IXGRP)
str[6]='x';
if(s.st_mode&S_IROTH)
str[7]='r';
if(s.st_mode&S_IWOTH)
str[8]='w';
if(s.st_mode&S_IXOTH)
str[9]='x';
printf("%s",str);
}

void other_info(struct stat s)
{
char *ctime();
//time_t tim;
//time(&tim);
printf("%3d",s.st_nlink);
printf("%5s",getpwuid(s.st_uid)->pw_name);
printf("%5s",getgrgid(s.st_gid)->gr_name);
printf("%5d",s.st_size);
printf(" %s",ctime(&s.st_mtime));

}
===============================
if(lstat(dir->d_name,&info)==-1)
oops("lstat_2");
请问运行的时候这个地方怎么老是出错呢?
文章评论

共有 2 条评论

  1. suowenair 于 2010-06-05 19:25:32发表:

    2# deepwhite


    呵呵
    懂了 lstat的第一个参数为绝对路径!
    谢谢!

  2. deepwhite 于 2010-05-28 12:11:28发表:

    出错的描述呢??