c - Finding directory in another directory -
i need find specific directory in different directory, reason code finds directory in current directory when start search specific named directory in parent directory, can not find there
#include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <string.h> void print_dir(char *dir_n, char *file) { dir *dir = opendir(dir_n); struct dirent *dirent; struct stat stats; while(1) { dirent = readdir( dir ); if (dirent == null) { break; } stat( dirent->d_name, &stats ); if ( s_isdir( stats.st_mode )) { if(strcmp(file ,dirent->d_name) == 0 && s_isdir( stats.st_mode ) ) { printf("found\n"); break; } } } closedir(dir); } int main(int argc, const char * argv[]) { print_dir("..", "dirtest"); return 0; }
you need check return status of system calls, , stat()
.
what's happening read name found in ..
directory, when invoke stat()
, doing on ./name
, not ../name
.
this code should demonstrate point:
#include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include <errno.h> void print_dir(char *dir_n, char *file) { dir *dir = opendir(dir_n); if (dir == 0) { int errnum = errno; fprintf(stderr, "error: opendir(\"%s\") failed (%d: %s)\n", dir_n, errnum, strerror(errnum)); exit(1); } struct dirent *dirent; while ((dirent = readdir(dir)) != 0) { struct stat stats; if (stat(dirent->d_name, &stats) < 0) { int errnum = errno; fprintf(stderr, "error: failed stat(\"%s\") (%d: %s)\n", dirent->d_name, errnum, strerror(errnum)); } else if (s_isdir(stats.st_mode)) { if (strcmp(file, dirent->d_name) == 0) { printf("found directory %s (inode = %ld)\n", dirent->d_name, (long)stats.st_ino); break; } else printf("found directory %s - not match %s\n", dirent->d_name, file); } else { printf("%s not directory\n", dirent->d_name); } } closedir(dir); } int main(void) { print_dir("..", "dirtest"); return 0; }
and trivial variant should find directory ../dirtest
if exists:
#include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include <errno.h> void print_dir(char *dir_n, char *file) { dir *dir = opendir(dir_n); if (dir == 0) { int errnum = errno; fprintf(stderr, "error: opendir(\"%s\") failed (%d: %s)\n", dir_n, errnum, strerror(errnum)); exit(1); } struct dirent *dirent; while ((dirent = readdir(dir)) != 0) { char fullname[1024]; snprintf(fullname, sizeof(fullname), "%s/%s", dir_n, dirent->d_name); struct stat stats; if (stat(fullname, &stats) < 0) { int errnum = errno; fprintf(stderr, "error: failed stat(\"%s\") (%d: %s)\n", fullname, errnum, strerror(errnum)); } else if (s_isdir(stats.st_mode)) { if (strcmp(file, dirent->d_name) == 0) { printf("found directory %s (%s) (inode = %ld)\n", dirent->d_name, fullname, (long)stats.st_ino); break; } else printf("found directory %s - not match %s\n", fullname, file); } else { printf("%s not directory\n", fullname); } } closedir(dir); } int main(void) { print_dir("..", "dirtest"); return 0; }
Comments
Post a Comment