*** 3000test.c 2021-02-24 16:49:19.000000000 -0500 --- 3000test-dirent.c 2025-03-25 09:23:28.105713863 -0400 *************** *** 13,18 **** --- 13,20 ---- #include #include #include + #include + #include void report_error(char *error) { *************** *** 23,30 **** int main(int argc, char *argv[]) { ! struct stat statbuf; ! char *fn; int fd; size_t len, i, count; --- 25,31 ---- int main(int argc, char *argv[]) { ! char *fn, fna[256], fnb[256]; int fd; size_t len, i, count; *************** *** 42,57 **** fn = argv[1]; ! if (stat(fn, &statbuf)) { ! report_error(strerror(errno)); ! } ! ! len = statbuf.st_size; printf("File %s: \n", fn); ! printf(" inode %ld\n", statbuf.st_ino); ! printf(" length %ld\n", len); ! if (S_ISREG(statbuf.st_mode)) { fd = open(fn, O_RDONLY); if (fd == -1) { report_error(strerror(errno)); --- 43,67 ---- fn = argv[1]; ! DIR *dir; ! struct dirent *e; ! realpath(fn, fna); ! strcpy(fnb, fna); //keep in mind, dirname() and basename() will modify your input ! dir = opendir(dirname(fna)); ! if (dir == NULL) { ! fprintf(stderr, "ERROR: Couldn't open directory.\n"); ! return 1; ! } ! ! for (e = readdir(dir); e != NULL; e = readdir(dir)) { ! if (!strcmp(basename(fnb), e->d_name)) { ! ! printf("File %s: \n", fn); ! printf(" inode %ld\n", e->d_fileno); ! if (e->d_type == DT_REG) { ! #if 0 fd = open(fn, O_RDONLY); if (fd == -1) { report_error(strerror(errno)); *************** *** 75,80 **** --- 85,98 ---- report_error(strerror(errno)); } close(fd); + #endif + } + break; + } + } + + if (closedir(dir)) { + fprintf(stderr, "ERROR: Couldn't close directory.\n"); } return 0;