S_ISDIR not working as expected. what is going on?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ls -l DIR_TEST/
total 9
-rw-r--r-- 1 nk users 10 Apr 7 14:42 Anotherfile
drwxr-xr-x 2 nk users 48 Apr 8 22:47 aSecond_DIR
drwxr-xr-x 3 nk users 120 Apr 8 22:34 a_DIR
-rw-r--r-- 1 nk users 16 Apr 7 14:42 a_file
I found some stuff but I'm not sure.
it has to do with absolute path vs relative path.
can I use to all those functions [stat, readdir, opendir, chdir] the abs_path..?
I think that for some reason you have to chdir (new_dir) and then do that stat, readdir, opendir
I'm not sure, and man doesn't help. Anyone that is a C wizard can help me>><??..
theres nothing wrong with S_ISDIR, because its working on . and .., im 99% sure the stat in is_directory is failing because of an invalid path(remember . and .. are valid paths, hence the stat works) - you should check this with a debugger or printf.
if im right then either pass an absolute path to stat or pass a "proper" relative path ie one that begins ./ or ../ if im not right then please post back with the return value from stat and the contents of statbuf.
DIR *tp;
struct dirent* pt;
struct stat st;
while((pt=readdir(tp))!=NULL)
{
if (!strcmp(pt->d_name, ".")||!strcmp(pt->d_name, "..")) continue;
strcpy(path, dir);
if (path[strlen(path)-1]!='/') strcat(path, "/");
strcat(path, pt->d_name);
if (!(lstat(path, &st)>=0)) continue;
if (st.st_mode&S_IFDIR)
//Is a folder
else if (st.st_mode&S_IFREG)
//File
}
You are pretty right to make a confusion point between relative and absolute addresses
here is the working code for traversing a folder and print all sub - files/folders. Watch out ı used chdir. with out this system call stat S_ISFOLDER() make wrong decisions
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.