LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-07-2004, 08:30 AM   #1
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Rep: Reputation: 30
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.
 
Old 04-07-2004, 09:46 AM   #2
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
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
 
Old 04-08-2004, 11:17 PM   #3
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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.
 
Old 04-09-2004, 06:12 AM   #4
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
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>><??..
 
Old 04-09-2004, 08:23 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 04-09-2004, 02:55 PM   #6
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
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().. ;(
 
Old 04-10-2004, 11:52 AM   #7
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
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
          }
 
Old 04-17-2007, 05:42 PM   #8
atamer
LQ Newbie
 
Registered: Apr 2007
Posts: 1

Rep: Reputation: 0
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;

}
 
Old 04-19-2007, 10:21 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bandwidth control not working as expected. ALMAM Linux - Networking 7 09-28-2005 06:41 AM
Crontab not working as expected nro Linux - Newbie 7 08-29-2005 12:56 PM
LinkSys EZXS55W not working as expected spaaarky21 Linux - Networking 2 09-05-2004 11:44 PM
chmod u+s not working as expected clinton Linux - Newbie 3 03-19-2004 07:49 PM
up2date not working as expected johnny13 Linux - Newbie 7 07-21-2003 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration