Question
I am printing this tree and it prints correctly but I am getting a bad file descriptor message. The error message bad file descriptor is
I am printing this tree and it prints correctly but I am getting a "bad file descriptor" message. The error message "bad file descriptor" is indicating that the file descriptor passed to the fdopendir() function is invalid. Can you help me dubug and edit the code below to fix this issue?
static int
tree_print_recurse(struct fileinfo finfo)
{
int dir = -1, sav_dir = cur_dir;
DIR *dirp = NULL;
struct fileinfo *file_list = NULL;
size_t file_count = 0;
errno = 0;
if (opts.dirsonly && !S_ISDIR(finfo.st.st_mode)) {
goto exit;
}
for (int i = 0; i < depth; ++i) {
printf(" ");
}
if (print_path_info(finfo) == -1) {
goto exit;
}
if (!S_ISDIR(finfo.st.st_mode)) {
goto exit;
}
if ((dir = openat(cur_dir, finfo.path, O_RDONLY | O_DIRECTORY)) == -1 ||
(dirp = fdopendir(dir)) == NULL) {
if (errno == EACCES) {
printf(" [could not open directory adapter %s] ", finfo.path);
errno = 0;
printf(" [could not open directory %s] ", finfo.path);
} else {
printf("%s [could not open directory] ", finfo.path);
}
goto exit;
}
cur_dir = dir;
if (putchar(' ') == EOF) goto exit;
if (read_file_list(dirp, &file_list, &file_count) == -1) goto exit;
qsort(file_list, file_count, sizeof *file_list, filecmp);
++depth;
for (size_t i = 0; i < file_count; ++i) {
struct fileinfo finfo = file_list[i]; /* TODO hint: fstatat or realpath */
if (fstatat(cur_dir, finfo.path, &(finfo.st), AT_SYMLINK_NOFOLLOW) == -1) goto exit;
if (tree_print_recurse(finfo) == -1) goto exit;
}
--depth;
exit:;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started