Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
04-07-2004, 08:30 AM
|
#1
|
Member
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182
Rep:
|
S_ISDIR not working as expected. what is going on?
hello. I'm not sure what is going on with the way I use S_ISDIR() macro.
Maybe you can help.
tree says
Quote:
[root@Freud DIR_TEST]# tree
.
|-- Anotherfile
|-- a_DIR
| `-- file_in_a_DIR
`-- a_file
1 directory, 3 files
|
but my compiled code says:
Quote:
$./foo DIR_TEST
. is a directory
.. is a directory
a_DIR is not a directory
a_file is not a directory
Anotherfile is not a directory
|
a_DIR is not detected as a DIRECTORY. what am I doing wrong?
this is my source code
Code:
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
int scan_dir( char *_pathname );
int isdirectory(char *path);
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
return 1;
}
scan_dir( argv[1] );
return 0;
}
int scan_dir( char *path )
{
struct dirent *direntp;
DIR *dirp;
if ((dirp = opendir(path)) == NULL)
{
perror ("Failed to open directory");
return 1;
}
while ((direntp = readdir(dirp)) != NULL)
{
if (isdirectory(direntp->d_name))
{
fprintf(stderr,"%s is a directory\n", direntp->d_name);
}
else
fprintf(stderr,"%s is not a directory\n", direntp->d_name);
//printf("%s\n", direntp->d_name);
}
while ((closedir(dirp) == -1) && (errno == EINTR)) ;
return 0;
}
int isdirectory(char *path) {
struct stat statbuf;
if (stat(path, &statbuf) == -1)
return 0;
else
return S_ISDIR(statbuf.st_mode);
}
thank you very much
Last edited by zeppelin; 04-07-2004 at 09:35 AM.
|
|
|
04-07-2004, 09:46 AM
|
#2
|
Member
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182
Original Poster
Rep:
|
SaTaN and others are welcomed to help :P
I mentioned SaTaN cause I did a lot of search in this forum before posting, and I saw that SaTaN {and not only} replies to similar to mine posts
|
|
|
04-08-2004, 11:17 PM
|
#3
|
Member
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419
Rep:
|
odd, compiled and ran just fine on my system. Could you post a `ls -l` of that directoryż?
Last edited by jinksys; 04-08-2004 at 11:18 PM.
|
|
|
04-09-2004, 06:12 AM
|
#4
|
Member
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182
Original Poster
Rep:
|
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>><??..
|
|
|
04-09-2004, 08:23 AM
|
#5
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
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.
|
|
|
04-09-2004, 02:55 PM
|
#6
|
Member
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182
Original Poster
Rep:
|
hmm. the same relative_path thing [starting with ./] is also necessary for opendir() ??
chdir() needs it also?
I 'm more a basher where I just type cd NAME_OF_DIR and not cd ./NAME_OF_DIR
ps. the only way I debug is via printf().. ;(
|
|
|
04-10-2004, 11:52 AM
|
#7
|
Member
Registered: Aug 2002
Distribution: Debian
Posts: 540
Rep:
|
Use lstat instead:
Code:
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
}
|
|
|
04-17-2007, 05:42 PM
|
#8
|
LQ Newbie
Registered: Apr 2007
Posts: 1
Rep:
|
relative and absolute address
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
tree_pointer traversaPath(char filename[]){
struct stat buf;
DIR *dirp;
struct dirent *dp;
if( (dirp = opendir(filename) ) == NULL ) {
printf("couldnt open %s\n",filename);
return;
}
do{
if(( dp = readdir(dirp)) != NULL){
lstat(dp->d_name, &buf);
if(S_ISDIR(buf.st_mode)){
if ( strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0){
printf("directoryName %s\n",dp->d_name);
chdir(dp->d_name);
traversaPath(".");
chdir("../");
//printf("Dir\n");
printf("File Name :%s\n",dp->d_name);
}
}
else{
printf("File Name :%s\n",dp->d_name);
}
}while( dp != NULL );
closedir(dirp);
return NULL;
}
|
|
|
04-19-2007, 10:21 AM
|
#9
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Quote:
Originally Posted by zeppelin
Code:
int isdirectory(char *path) {
struct stat statbuf;
if (stat(path, &statbuf) == -1)
return 0;
else
return S_ISDIR(statbuf.st_mode);
}
|
If this function returns 0 it is pretty unclear whether an error ocuurred in stat(), or the path provided is not a directory!
One (simple) way to fix:
Code:
int isdirectory(char *path) {
struct stat statbuf;
if (stat(path, &statbuf) == -1) {
perror("while calling stat()");
return -1;
} else {
return S_ISDIR(statbuf.st_mode);
}
}
also fix the calling code to check the return value for -1 (error).
|
|
|
All times are GMT -5. The time now is 11:29 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|