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.