Check File
2001-11-10 18:51:36
Category: c:files
Description: Checks the given filename to see if it is a normal file.
Author: detour
Viewed: 3481
Rating: (8 votes)


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