Skip to content

Instantly share code, notes, and snippets.

@alexpreynolds
Created March 7, 2015 00:04
Show Gist options
  • Save alexpreynolds/11c2cd7eb77ad204a48c to your computer and use it in GitHub Desktop.
Save alexpreynolds/11c2cd7eb77ad204a48c to your computer and use it in GitHub Desktop.
Test input file's POSIX type
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
struct stat st;
if (stat(argv[1], &st) == -1) {
fprintf(stderr, "Error: stat() failed\n");
return EXIT_FAILURE;
}
switch (st.st_mode & S_IFMT)
{
case S_IFBLK: fprintf(stdout, "block device\n"); break;
case S_IFCHR: fprintf(stdout, "character device\n"); break;
case S_IFDIR: fprintf(stdout, "directory\n"); break;
case S_IFIFO: fprintf(stdout, "FIFO/pipe\n"); break;
case S_IFLNK: fprintf(stdout, "symlink\n"); break;
case S_IFREG: fprintf(stdout, "regular file\n"); break;
case S_IFSOCK: fprintf(stdout, "socket\n"); break;
default: fprintf(stdout, "unknown?\n"); break;
}
return EXIT_SUCCESS;
}
@alexpreynolds
Copy link
Author

Compiles with gcc -Wall type_test.c -o type_test

Sample run:

$ ./type_test <(cat makefile)
FIFO/pipe
$ ./type_test makefile
regular file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment