Check Directory
2001-11-10 18:49:59
Category: c:files
Description: Tests a file to see if it's a directory
Author: detour
Viewed: 8128
Rating: (19 votes)


/* checkfile.c written by detour@metalshell.com
 *
 * simple function to check for a directory
 *
 * http://www.metalshell.com/
 *
*/
 
#include <stdio.h>
#include <sys/stat.h>
 
int isDir(const char *dname);
 
int main(int argc, char *argv[]) {
 
  if (argc != 2) { 
    printf("Usage: %s directory name\n", argv[0]); 
    exit(0);
  }
 
  if(isDir(argv[1])) {
    printf("%s is a directory.\n", argv[1]);
  }
 
return 0;
}
 
int isDir(const char *dname) {
  struct stat sbuf;
 
  if (lstat(dname, &sbuf) == -1) {
    fprintf(stderr, "lstat() Failed.\n");
    return 0;
  }
 
  if(S_ISDIR(sbuf.st_mode)) {
    return 1;
  }
 
return 0;
}