所以只是为了澄清。这是一个学校作业。我们正在编写一个简化的查找程序(sfind),并且我遇到了一个问题。编写我自己的查找程序时出现Seg错误
基本上,在任何情况下,-print标志都可以在任何情况下都可以完美地工作,因为这种情况下没有太多的数据可以查看。但是,当我尝试从我的基目录(其中有大量文件)运行它时,我最终遇到了seg故障。我觉得这可能是由于许多原因。
- 我的用户进程限制太低
- 我的最大文件大小太低
- 我得到堆栈溢出了太深的递归
- 我俯瞰
我在ubuntu上运行它,它将事件彻底地在Unix服务器上运行。
这是我目前的递归代码。
int printHelper(struct dirent *entry, DIR *dir, char* path){
struct stat fileStat;
DIR *tempDir;
char tempPath[1000];
char const* name = entry->d_name;
strcpy(tempPath, path);
strcat(tempPath, name);
lstat(tempPath, &fileStat);
if(strcmp(name, ".") != 0 && strcmp(name, "..") != 0){
printf("%s%s\n", path, name);
}
if((S_ISDIR(fileStat.st_mode)) && (strcmp(name, ".") != 0) && (strcmp(name, "..") != 0)){
struct dirent *tempEntr;
char newTempPath[1000];
char newPathName[1000];
strcpy(newPathName, name);
strcpy(newTempPath, path);
strcat(newTempPath, newPathName);
strcat(newTempPath, slashPath);
tempDir = opendir(newTempPath);
tempEntr = readdir(tempDir);
printHelper(tempEntr, tempDir, newTempPath);
closedir(tempDir);
}
if(!(entry = readdir(dir))){
return 0;
}
printHelper(entry, dir, path);
return 0;
}
这里是文件
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include "myPrint.h"
char slashPath[3] = "/\0";
int myPrint(char const* myFile){
DIR *dir;
struct dirent *entry;
int isDir;
isDir = 1;
if (!(dir = opendir(myFile))){
isDir = 0;
}
else if (!(entry = readdir(dir))){
return -1;
}
if(isDir == 0){
dir = opendir(".");
while((entry = readdir(dir))){
if(strcmp(myFile, entry->d_name) == 0){
printf("%s\n", myFile);
return 0;
}
}
printf("find: ‘%s’: No such file or directory\n", myFile);
return 0;
}
else{
char path[2000];
strcpy(path, myFile);
strcat(path, slashPath);
printf("%s\n", myFile);
printHelper(entry, dir, path);
return 0;
}
return 0;
}
因为你没有基础案例 –
你能澄清一点吗?因为代码更多。我将在 – Joe
中编辑它使用调试器的时间。 – Olaf