我不知道我错过了什么,但我已经盯着这个看了好一阵子了。在扫描看起来有效的目录时,似乎出现了分段错误。我只是想打印所有dir条目和subdir条目。GDB运行和回溯:
(gdb) run
Starting program: /home/p/C_projects/recursive_ls/a.out
Directory is now /home/p/C_projects/recursive_ls
In /home/p/C_projects/recursive_ls: a.out
In /home/p/C_projects/recursive_ls: recursive_ls.c
In /home/p/C_projects/recursive_ls: test
In /home/p/C_projects/recursive_ls: test_1
In /home/p/C_projects/recursive_ls: test_2
a.out,inode=265490,owner=1000,size=10328
recursive_ls.c,inode=265577,owner=1000,size=1296
test,inode=265508,owner=1000,size=4096
Directory is now /home/p/C_projects/recursive_ls/test
Program received signal SIGSEGV, Segmentation fault.
0x00000003 in ?? ()
(gdb) backtrace
#0 0x00000003 in ?? ()
#1 0xb7eb5d5c in __alloc_dir (fd=3, close_fd=close_fd@entry=true,
flags=flags@entry=0, statp=0x0) at ../sysdeps/posix/opendir.c:207
#2 0xb7eb5e6a in __opendirat (dfd=dfd@entry=-100,
name=name@entry=0x804b938 "/home/p/C_projects/recursive_ls/test")
at ../sysdeps/posix/opendir.c:152
#3 0xb7eb6c53 in __GI_scandirat (dfd=-100,
dir=0x804b938 "/home/p/C_projects/recursive_ls/test", namelist=0xbffff02c,
select=0x804862b <isSelect>, cmp=0x8048520 <alphasort@plt>)
at scandirat.c:62
#4 0xb7eb62ca in scandir (
dir=0x804b938 "/home/p/C_projects/recursive_ls/test", namelist=0xbffff02c,
select=0x804862b <isSelect>, cmp=0x8048520 <alphasort@plt>) at scandir.c:43
#5 0x080486c5 in getFiles (
path=0x804b938 "/home/p/C_projects/recursive_ls/test") at recursive_ls.c:23
#6 0x0804886c in getFiles (path=0x804b008 "/home/p/C_projects/recursive_ls")
at recursive_ls.c:40
#7 0x080488dd in main () at recursive_ls.c:48
(gdb)
现在,这个绝对路径/home/p/c_projects/recursive_ls/test似乎是合法的:
p@p-virtualbox:~/c_项目/递归ls/test$pwd
/home/p/c_projects/recursive_ls/测试
代码在下面,你能给我指出什么吗?:
#include <stdio.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
const int MAX_PATH = 255;
int isSelect(const struct dirent *file){
if(strcmp(file->d_name,".") == 0 || strcmp(file->d_name,"..") == 0)
return 0;
else
return 1;
}
void getFiles(char* path){
printf("\nDirectory is now %s", path);
printf("\n");
struct dirent** files;
struct stat *stat_;
int count = scandir(path, &files, isSelect, alphasort);
for(int i = 0; i < count; i++){
printf("\nIn %s: %s", path, files[i]->d_name);
}
for(int i = 0; i < count; i++){
char* tmp = malloc(MAX_PATH*2), *tmp1 = malloc(MAX_PATH);
strcat(strcpy(tmp1, path), "/");
strcat(strcat(strcpy(tmp,path), "/"), files[i]->d_name);
int statResult = stat(tmp, stat_);
if(statResult != 0){
printf("\nOh noes! I couldn't get stats on the file %s", files[i]->d_name);
}
else
printf("\n%s,inode=%lu,owner=%d,size=%lu"
,files[i]->d_name,stat_->st_ino,stat_->st_uid,stat_->st_size);
if(S_ISDIR(stat_->st_mode)){
strcat(tmp1, files[i]->d_name);
getFiles(tmp1);
}
}
}
int main(){
char* path = malloc(MAX_PATH);
getcwd(path, MAX_PATH);
getFiles(path);
}
最佳答案
这里没有错误,但使用stat
时有问题。因为这会覆盖堆栈,我敢打赌这就是原因。您正在传递一个未初始化的指针变量。您应该将指针传递给变量,例如:
struct stat stat_;
...
int statResult = stat (tmp, &stat_);
if (statResult != 0)
{
printf ("\nOh noes! I couldn't get stats on the file %s",
files[i]->d_name);
}
else
printf ("\n%s,inode=%lu,owner=%d,size=%lu", files[i]->d_name,
stat_.st_ino, stat_.st_uid, stat_.st_size);
if (S_ISDIR (stat_.st_mode))
...
这样,稍微移动
stat()
循环中的int i
声明和for
结尾处的exit(0)
声明,它就用main()
编译,并且看起来工作正常(即,下降目录)。关于c - scandir()导致segfault,即使具有有效的绝对路径?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33835132/