[英]How to fix segmentation fault in the use of struct dirent in C
My code prints the files/directory names in a given path(user enters it as a command-line argument). 我的代码在给定路径中打印文件/目录名称(用户将其作为命令行参数输入)。 When executing with a given path in the directory, it just works fine but it is supposed to do the same for the current working directory if user does not provide any command-line argument.
使用目录中的给定路径执行时,它可以正常工作,但是如果用户不提供任何命令行参数,则应该对当前工作目录执行相同的操作。
I am getting seg fault if I just run as: ./a.out
如果我仅以
./a.out
运行, ./a.out
段错误。
It works when I run as: ./a.out /path
当我以
./a.out /path
运行时,它可以工作
Please fix my code by providing the necessary code fragment 请通过提供必要的代码片段来修复我的代码
I have tried to do debugging and found out that it gives the error right after it executes the line following line in the depthFirst function 我尝试进行调试,发现在执行depthFirst函数中的下一行代码后,它立即给出错误
printf("%s\n", sd->d_name);
My faulty code: 我的错误代码:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <limits.h>
void depthFirst(char * path){
struct dirent *sd;
DIR *dir;
//char path[PATH_MAX];
dir = opendir(path);
if(dir == NULL){
printf("Error, unable to open\n");
exit(1);
}
while( (sd = readdir(dir)) != NULL){
if(strcmp(sd->d_name, ".") != 0 && strcmp(sd->d_name, "..") != 0){
printf("%s\n", sd->d_name);
realpath(sd->d_name,path);
if(isdirectory(path)){
printf("\t");
depthFirst(sd->d_name);
}
}
}
closedir(dir);
}
int isdirectory(char *path) {
struct stat statbuf;
if (stat(path, &statbuf) == -1)
return 0;
else
return S_ISDIR(statbuf.st_mode);
}
int main(int argc, char *argv[]){
char * path;
char * currentDirectory;
if(argc<2){
currentDirectory = ".";
depthFirst(currentDirectory);
}
else{
path = argv[1];
depthFirst(path);
}
return 0;
}
The output is shown below: 输出如下所示:
.git
Segmentation fault
Jonathan beat me to it in the comments, but this change prevents the problem. 乔纳森(Jonathan)在评论中击败了我,但此更改可防止出现问题。
@@ -9,7 +9,7 @@
void depthFirst(char * path){
struct dirent *sd;
DIR *dir;
- //char path[PATH_MAX];
+ char rpath[PATH_MAX];
dir = opendir(path);
@@ -22,8 +22,8 @@
while( (sd = readdir(dir)) != NULL){
if(strcmp(sd->d_name, ".") != 0 && strcmp(sd->d_name, "..") != 0){
printf("%s\n", sd->d_name);
- realpath(sd->d_name,path);
- if(isdirectory(path)){
+ realpath(sd->d_name,rpath);
+ if(isdirectory(rpath)){
printf("\t");
depthFirst(sd->d_name);
As another comment pointed out, you cannot reuse the char* path
because it is stored in a page of memory that is not writable by your program. 正如另一条评论指出的那样,您无法重用
char* path
因为它存储在程序无法写的内存页面中。 Therefore, realpath()
will crash upon attempting to write to it. 因此,
realpath()
在尝试写入时会崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.