Solved

Details of a file.

Posted on 2000-03-06
8
182 Views
Last Modified: 2010-04-02
Hi, In an environ, gcc running under solaris, i need to check whether a file is a directory or a simple file. Could anyone let me know how to do this.

-gsbabu
0
Comment
Question by:gsbabu
  • 5
  • 3
8 Comments
 

Expert Comment

by:ori_b
ID: 2587702
Try the following :

#include <sys/types.h>
#include <sys/stat.h>

struct stat buf;
         
if (stat("file_name", &buf) == 0) {
 if ((buf.st_mode & S_IFMT) == _IFDIR)  {
      // it is a directory
}
0
 

Author Comment

by:gsbabu
ID: 2587954
ori_b
>> f ((buf.st_mode & S_IFMT) == _IFDIR)  {

on compiling, i get _IFDIR undeclared, i don't know more about, where it could be defined. help.

-gsbabu
0
 

Accepted Solution

by:
ori_b earned 15 total points
ID: 2587992
try S_IFDIR instead, it is defined
in <sys/stat.h>
0
 

Author Comment

by:gsbabu
ID: 2588047
thanks, it is ok, also could i know how to make this in a loop, that is if in a directory there are some entries, which could be plain file or directory, i need to loop for the number of entries in the directory. could you help me. thanks for your previous help, let me know if you want me to post this as a new question.

--gsbabu.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:ori_b
ID: 2588257
You can try this (if I understood your question correctly) :

int     DirLoop(const char* path) {
 
  DIR *dirp;
  struct dirent *direntp;

  char file_name[256];
 
  printf("opening directory %s\n",path);
 
  dirp = opendir( path );
  if (dirp == NULL) {
      printf("Error %s : %s\n", path,strerror(errno));
      return -1;
  }
 
  while ( (direntp = readdir( dirp )) != NULL ) {
      if (direntp == NULL) {
        printf("Erorr %s : %s \n", path,strerror(errno));
        return -1;
      }


    // skip the ./ and ../ files        
    if (direntp->d_name[0] != '.') {

        // build the full path
        strcpy (file_name,path);
        strcat (file_name,"/");
        strcat (file_name,direntp->d_name);

        struct stat buf;
        
        // Find file's type
        if (stat(file_name, &buf) == 0) {
            if ((buf.st_mode & S_IFMT) == S_IFDIR) {
              // it is a directory
              DirLoop(file_name);
            }

            else {
              // A regular file - Do What you want to ...
              printf("%s\n",file_name);
            }
        }

        else {
            printf("Fail to stat file %s : %s\n",file_name, strerror(errno ));
            return -1;
        }
      
      }
  }

 
  if (closedir( dirp ) == -1) {
      printf("Erorr closedir of %s : %s \n", path,strerror(errno));
      return -1;
  }

  return 0;
}

0
 

Expert Comment

by:ori_b
ID: 2588268
You will need these #include  :

#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

0
 

Author Comment

by:gsbabu
ID: 2591166

Thanks very much Master Ori, It really worked pretty well.

 -gsbabu
 ugnasu01@thiru.vetri.com
0
 

Expert Comment

by:ori_b
ID: 2591624
glad to help
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now