我的 C 程序有问题,该程序旨在从根目录打印出给定文件的路径。例子:
./pathto
/users/cs/a1066199/
./pathto .
/users/cs/a1066199/
./pathto file.txt
/users/cs/a1066199/file.txt
./pathto ..
/users/cs/
./pathto ../teaching/somefile.c
/users/cs/teaching/somefile.c
正在使用的算法如下,我必须用递归来实现它:
let d be a directory.
open the parent of d (ie d/..)
loop
Get next entry, e, in the parent diretory
get the status of e, using stat, into es
until es.device==d.device and es.inum==d.inum
endloop
我已经编写了程序并运行了程序,但是当第一次工作时,递归函数的任何后续调用都会出错。
我不确定问题出在哪里/哪里。
任何帮助表示赞赏。
代码:
#define _BSD_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
char* compare(char currDir[1024], char newDir[1024], struct stat D){
int found = 0;
struct stat Dnew;
struct stat ES;
struct dirent* direntp;
DIR* dirp;
char* p;
char dirName[1024];
char filePath[1024];
char newDir2[1024];
char rootPath[1024];
dirp = opendir(newDir);//open parent directory
while ((direntp = readdir(dirp)) != NULL){//read parent
//printf("%s\n", direntp->d_name);
strcpy(filePath, newDir);//create path to the file in the parent
strcat(filePath, "/");
strcat(filePath, direntp->d_name);
if(stat(filePath, &ES) == -1){//read into stat
perror("stat");
}
if(ES.st_dev == D.st_dev){//check es.device == d.device
//printf("st_dev are the same\n");
if(ES.st_ino == D.st_ino){//check es.inum == d.inum
//printf("st_ino are the same\n");
printf("The child Dir is %s\n", direntp->d_name);//print this if they are the same
found = 1;
if(ES.st_mode & S_IFDIR){//if its a directory, put / on the end
strcpy(dirName, direntp->d_name);
strcat(dirName, "/");
} else {
strcpy(dirName, direntp->d_name);
}
} else {
found = 0;
}
} else {
found = 0;
}
}
closedir(dirp);
if(D.st_ino == 2){
//strcpy(dirName, "/");//if root, return /
return "/";
} else {
dirp = opendir(newDir);
while ((direntp = readdir(dirp)) != NULL){
if (strcmp(direntp->d_name, "..") == 0){//find ..
strcpy(filePath, newDir);
strcat(filePath, "/");
strcat(filePath, direntp->d_name);
if(stat(filePath, &Dnew) == -1){//read .. into Dnew
//perror("stat");
}
}
}
closedir(dirp);
strcpy(newDir2, newDir);//make new dir
strcat(newDir2, "/..");
printf("%s\n", newDir);
printf("%s\n", newDir2);
strcpy(rootPath, "");
/*strncpy(rootPath, */p = compare(newDir, newDir2, Dnew)/*, 1024)*/;//call recursivly
while(*p != '\0'){
strcat(rootPath, *p);
p++;
}
strcat(rootPath,dirName);
return rootPath;
}
}
int main(int argc, char* argv[]){
struct stat D;
//struct stat E;
//struct stat ES;
//struct dirent* direntp;
//DIR* dirp;
//char filePath[1024];
char* p;
char rootPath[1024];
char currDir[1024];
char newDir[1024];
strcpy(currDir, argv[1]);
if(stat(argv[1], &D) == -1){
//perror("stat");
}
strcpy(newDir, currDir);
strcat(newDir, "/..");
printf("%s\n", currDir);
printf("%s\n", newDir);
/*strncpy(rootPath, */p = compare(currDir, newDir, D)/*, 1024)*/;
while(*p != '\0'){
strcat(rootPath, *p);
p++;
}
printf("%s", rootPath);
/*if(D.st_mode & S_IFDIR){
printf("/\n");
}*/
return 0;
}
终端输出:
/usr
/usr/..
The child Dir is usr
/usr/..
/usr/../..
The child Dir is ..
The child Dir is .
./pathto: line 6: 27236 Segmentation fault (core dumped) ./pathto.o $*