Chinaunix
标题:
请帮我分析一下为啥stat()时报错了,实在分析不出来
[打印本页]
作者:
lylesong
时间:
2013-03-12 23:23
标题:
请帮我分析一下为啥stat()时报错了,实在分析不出来
大家好,我最近在学习linux下的编程,照着书上的例子练习写ls 命令,
这个版本无法支持目录参数,会莫名其妙的让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;
}
复制代码
作者:
lylesong
时间:
2013-03-12 23:25
本帖最后由 lylesong 于 2013-03-12 23:26 编辑
比如报如下的错误
./ls test/
path:test/
name:..
drwxrwxr-x 10 song song 4096 Mar 4 18:01 ..
name:test
drwxrwxr-x 3 song song 4096 Mar 12 22:44 test
name:cp
-rwxrwxr-x 1 song song 11453 Mar 5 16:02 cp
name:.
drwxrwxr-x 5 song song 4096 Mar 12 23:22 .
name:cp2
复制代码
stat: No such file or directory
作者:
lxyscls_cu
时间:
2013-03-13 09:56
看看是哪个file
gdb
作者:
sunceenjoy
时间:
2013-03-13 10:23
调用 do_stat的时候,写相对路径或全路径,把dirname弄在前面,否则stat怎么知道你要看哪里
作者:
happy_fish100
时间:
2013-03-15 17:40
LS正解。stat文件的时候,没有带上目录名称。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2