LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 10-01-2004, 03:18 AM   #1
joeyBig
LQ Newbie
 
Registered: Jul 2004
Posts: 23

Rep: Reputation: 15
how to distinguish a file and a directory


hi

I need to know how to distinguish between a file and a directory in a C program,
under Linux and Windows.
Is there any system calls to check if it is a directory?

regards,
joe.
 
Old 10-01-2004, 04:50 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
Try and open it using opendir:

Code:
#include <sys/types.h>
#include <dirent.h>
#incllude <errno.h>

//Returns 0 for directory, 1 for file, -1 for error in errno
//str is the pathname of the unknown file/directory
int isDirectory(char * str) {
  DIR dir;
  errno = 0;
  dir = opendir(str);
  if (dir != NULL) {
     closedir(dir);
     return 0;
  } else if (errno == ENOTDIR)
     return 1;
  } else return -1;
}
 
Old 10-01-2004, 05:21 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
You can check the file type (regular file, dir, pipe, device, etc.) with stat() (see "man 2 stat").
Example:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
     char *filename;
     int result;
     struct stat statinfo;

     if (argc != 2) {
		  fprintf(stderr, "Usage: %s [<file>]\n", argv[0]);
		  return 1;
     }
     filename = argv[1];
     result = lstat(filename, &statinfo);
     if (result < 0) {
		  if (errno == ENOENT) {
			   printf("File %s does not exist\n", filename);
			   return 0;
		  } else {
			   perror(*argv);
			   return 1;
		  }
     }
     printf("File %s exists, and is a ", filename);
     switch (statinfo.st_mode & S_IFMT) {
     case S_IFREG:  printf("regular file.\n"); break;
     case S_IFSOCK: printf("filesystem socket.\n"); break;
     case S_IFLNK:  printf("symbolic link.\n"); break;
     case S_IFBLK:  printf("block device file.\n"); break;
     case S_IFDIR:  printf("directory.\n"); break;
     case S_IFCHR:  printf("character device file.\n"); break;
     case S_IFIFO:  printf("filesystem pipe (fifo) file.\n"); break;
     default:  printf("unknown type of file.\n"); break;
     }
     return 0;
}
Note: Here lstat() is used instead of stat(), in order to be able to detect symbolic links.

Last edited by Hko; 10-01-2004 at 05:24 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Distinguish between crash and reboot tukaaa Linux - General 4 11-26-2005 06:25 PM
distinguish load and store from segmentation fault gaven Programming 1 08-24-2005 04:40 PM
distinguish load and store from segmentation fault gaven Linux - Software 0 08-23-2005 02:36 PM
how to distinguish kernel versions fobius Linux - General 2 06-24-2004 08:28 AM
How can I distinguish a whole mail from TCP packets sunnyriver Linux - Networking 2 03-23-2004 12:08 AM


All times are GMT -5. The time now is 09:25 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration