Question: int createPath( int index, ino_t iNodeRoot, char* path){ 30 struct stat buff; 31 int i; 32 DIR * dirp; 33 struct dirent *dp; 34 i
int createPath( int index, ino_t iNodeRoot, char* path){ 30 struct stat buff; 31 int i; 32 DIR * dirp; 33 struct dirent *dp; 34 i = stat(".", &buff); 35 if(i != 0){ 36 perror("cwd error"); 37 exit(0); 38 } 39 if(iNodeRoot == buff.st_ino){ 40 /* printf("%s ", path); */ 41 return index; 42 } 43 if ((dirp = opendir("..")) == NULL) { 44 perror("couldn't open '.'"); 45 exit(0); 46 } 47 do { 48 errno = 0; 49 if ((dp = readdir(dirp)) != NULL) { 50 /* (void) printf("found %s ", dp->d_name); */ 51 if(buff.st_ino == dp->d_ino){ 52 index = prepend(index, path, dp->d_name); 53 chdir(".."); 54 index = createPath(index, iNodeRoot, path); 55 } 56 } 57 } while (dp != NULL); 58 (void) closedir(dirp); 59 return index; 60 } 61 62 int prepend(int index, char* path, char* dir){ 63 int len, plen, c; 64 len = strlen(dir); 65 if(len > index + 1){ 66 perror("path too long"); 67 return 0; 68 } 69 plen = path + strlen(path) - len; 70 71 if(plen + len + 1 > PATH_MAX){ 72 perror("path too long"); 73 return 0; 74 } 75 memmove(path + len + 1, path, plen + 1); 76 memcpy(path, dir, len); 77 path[len] = '/'; 78 /* dont copy null terminator*/ 79 /*think about / in paths*/ 80 index += len + 1; 81 return index; 82 }
I am trying to print out a path to my root directory, similar to what /bin/pwd would produce, without using the getcwd(3) command. I believe I have an issue with line 69, finding the path length, as we dont care about the first part of the string, which has been initialized to char path[PATH_MAX], we only care about the later portion where we are prepending the path string. Any help would be appreciated. all main does is call create path, and prints path at index.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
