请帮我分析一下为啥stat()时报错了,实在分析不出来
song@song-tp:~/learn_c/practice/uulp$ cat ls3.c
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<errno.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
#include<unistd.h>
#define RWX_LEN 11
#define MAX_RECORD 1024 /* 最大文件记录数,应该用平台提供的限制来设置此值 */
#define MAX_LEN 1024
void do_ls2(char *);
void do_stat(char *, struct stat *);
void perm(mode_t, char *);
char *uid_to_name(uid_t);
char *gid_to_name(gid_t);
char *lastime(time_t);
int main(int argc, char *argv[])
{
char name[MAX_LEN];
int i;
if(argc == 1)
{
do_ls2(".");
}
else
{
for(i = 1; i < argc; i++)
{
++argv;
fprintf(stdout, "path:%s\n", *argv);
do_ls2(*argv);
}
}
return 0;
}
void do_ls2(char *dirname)
{
DIR *dirp;
struct dirent *direntp;
struct stat statbuf[1];
if((dirp = opendir(dirname)) == NULL)
fprintf(stderr, "ls:cannot open %s\n", dirname);
else
{
while((direntp = readdir(dirp)) != NULL)
{
do_stat(direntp->d_name, statbuf);
}
closedir(dirp);
}
}
void do_stat(char *file, struct stat *buf)
{
char rwx[RWX_LEN];
fprintf(stdout, "name:%s\n", file);
file[strlen(file)] = '\0';
if(stat(file, buf) == -1)
{
perror("stat");
exit(EXIT_FAILURE);
}
else
{
perm(buf->st_mode, rwx);
fprintf(stdout, "%s", rwx);
fprintf(stdout, "%4d ", (int)buf->st_nlink);
fprintf(stdout, "%-8s ", uid_to_name(buf->st_uid));
fprintf(stdout, "%-8s ", gid_to_name(buf->st_gid));
fprintf(stdout, "%8ld ", buf->st_size);
fprintf(stdout, "%.12s ", (lastime(buf->st_mtime) +4));
fprintf(stdout, "%s\n", file);
}
}
void perm(mode_t mode, char *str)
{
snprintf(str, RWX_LEN, "%c%c%c%c%c%c%c%c%c%c",
S_ISREG(mode) ? '-':S_ISDIR(mode) ?'d':S_ISLNK(mode) ? 'l' :'-',
(mode & S_IRUSR) ? 'r':'-',
(mode & S_IWUSR) ? 'w':'-',
(mode & S_IXUSR) ? 'x':'-',
(mode & S_IRGRP) ? 'r':'-',
(mode & S_IWGRP) ? 'w':'-',
(mode & S_IXGRP) ? 'x':'-',
(mode & S_IROTH) ? 'r':'-',
(mode & S_IWOTH) ? 'w':'-',
(mode & S_IXOTH) ? 'x':'-');
}
char *uid_to_name(uid_t uid)
{
struct passwd *pwd;
static char numstr[10]; /* 把函数内的局部变量内容传递出去 */
errno = 0;
pwd = getpwuid(uid);
if(pwd == NULL)
{
if(errno == 0)
{
sprintf(numstr, "%d", uid);
return numstr;
}
else
{
perror("getpwuid()");
exit(EXIT_FAILURE);
}
}
return pwd->pw_name; /* getpwuid()函数是内部分配静态空间,故只需要单纯的传递出去即可 */
}
char *gid_to_name(gid_t gid)
{
struct group *grp;
static char numstr[10];
errno = 0;
grp = getgrgid(gid);
if(grp == NULL)
{
if(errno == 0)
{
sprintf(numstr, "%d", gid);
return numstr;
}
else
{
perror("getgrgid()");
exit(EXIT_FAILURE);
}
}
return grp->gr_name;
}
char *lastime(time_t time)
{
char *tp;
tp = ctime(&time);
if(tp == NULL)
{
perror("ctime");
exit(EXIT_FAILURE);
}
return tp;
}
void do_ls2(char *dirname)
{
DIR *dirp;
struct dirent *direntp;
struct stat statbuf[1];
if((dirp = opendir(dirname)) == NULL)
{
if(ENOTDIR == errno) //非目录文件直接进行stat
{
do_stat(dirname, statbuf);
return;
}
fprintf(stderr, "ls:cannot open %s\n", dirname);
}
else
{
if(chdir(dirname) == -1) //你stat中的参数路径是相对路径,所以得先切换过去
{
perror("chdir");
exit(EXIT_FAILURE);
}
while((direntp = readdir(dirp)) != NULL)
{
do_stat(direntp->d_name, statbuf);
}
closedir(dirp);
}
}