#include #include #include #include #include #include #include #include int main() { struct stat stat_buf; struct dirent *file_info; ino_t itself_ino; /* holds current directory inode */ ino_t parent_ino; /* holds parent directory inode */ char Current[PATH_MAX]; /* directory name */ char Path[PATH_MAX]; /* holds the full path */ char Slash[PATH_MAX]; /* add / before the directory name */ DIR *dir; while (1) { dir = opendir("."); if(dir == NULL) { fprintf(stderr, "cannot get current directory.\n"); exit(-1); } /* read the information about the current directory */ file_info = readdir(dir); lstat(file_info->d_name, &stat_buf); itself_ino = stat_buf.st_ino; closedir(dir); chdir(".."); /* go to parent directory */ dir = opendir("."); // printf("DEBUG1*:\tci=%d,\tpi=%d,\tnm=%s,\tsl=%s\n\n", itself_ino, parent_ino, file_info->d_name, Slash); while (file_info = readdir(dir)) { lstat(file_info->d_name, &stat_buf); parent_ino = stat_buf.st_ino; // printf("DEBUG2**:\tci=%d,\tpi=%d,\tnm=%s,\tsl=%s\n", itself_ino, parent_ino, file_info->d_name, Slash); if (itself_ino == parent_ino && itself_ino != 2) { strcpy(Slash, "/"); strcpy(Current, file_info->d_name); strcat(Slash, Current); /* add "/" as the first */ strcat(Slash, Path); /* charcter of the directory */ /* check the length of the pathname */ if(strlen(Slash) >= PATH_MAX) { fprintf(stderr, "Error! Path too long!\n"); exit(0); } /* save the full pathname */ strcpy(Path, Slash); // printf("\nDEBUG3***:\tci=%d,\tpi=%d,\tnm=%s,\tsl=%s\n\n", itself_ino, parent_ino, file_info->d_name, Slash); break; } } closedir(dir); if (itself_ino == 2) { break; } } /* print the full path of the current working directory */ if (strlen(Path) == 0) { printf("/\n"); } else { printf("%s\n", Path); } return 0; }