【发布时间】:2016-11-21 19:26:56
【问题描述】:
我正在尝试列出有关指定目录中文件的信息,但我收到了 Memory Fault Core Dumped 错误。 列出目录方法:
int listdir(char *path) {
struct dirent **dirlist;
int n;
char *fullpath;
n = scandir(path, &dirlist, NULL, alphasort);
while (n--) {
strcpy(fullpath, path);
strcat(fullpath, dirlist[n]->d_name);
(void)printfinf(fullpath);
free(dirlist[n]);
}
free(dirlist);
return 0;
}
Printfinf 方法 - 此方法工作正常,打印出有关文件的信息
int printfinf(char *path) {
struct stat info;
struct passwd *pswd;
lstat(path,&info);
pswd = getpwuid(info.st_uid);
char *modestr = mode2str(info.st_mode, getuid(), getgid());
char *timestr = time2str(info.st_mtime);
printf("%s %s %10lld %s %s\n", modestr, pswd->pw_name, info.st_size, timestr, path);
char c = modestr[0];
if(c == 'd')
return FTYPE_DIR;
else if(c == 'f')
return FTYPE_REG;
else if(c == 'e')
return FTYPE_EXE;
else if(c == 'l')
return FTYPE_LNK;
else return FTYPE_OTH;
}
【问题讨论】:
-
您好!你为什么从你的问题中删除代码?现在完全不清楚了。
-
不要从您的问题中删除代码。欢迎来到堆栈溢出。请注意,在这里说“谢谢”的首选方式是投票赞成好的问题和有用的答案(一旦你有足够的声誉这样做),并接受对你提出的任何问题最有帮助的答案(这也给出了你的声誉小幅提升)。请查看About 页面以及How do I ask questions here? 和What do I do when someone answers my question?