C – file type

Takes one or more files as arguments on the command line and returns the type of the file(s) (normal file, directory, etc.)

/* reports the type of a file or of multiple files
 * that are given as arguments on the command line 
 * it works too */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
int main (int argc, char *argv[])
/* In ISO C you can define `main' either to take no arguments, or to
 * take two arguments that represent the command line arguments to the
 * program as shown above */
 int i;
 struct stat buf;
 /* struct stat {
  * dev_t         st_dev;     ( device )
  * ino_t         st_ino;     ( inode )
  * mode_t        st_mode;    ( protection )
  * nlink_t       st_nlink;   ( number of hard links )
  * uid_t         st_uid;     ( user ID of owner )
  * gid_t         st_gid;     ( group ID of owner )
  * dev_t         st_rdev;    ( device type (if inode device) )
  * off_t         st_size;    ( total size, in bytes )
  * blksize_t     st_blksize; ( blocksize for filesystem I/O )
  * blkcnt_t      st_blocks;  ( number of blocks allocated )
  * time_t        st_atime;   ( time of last access )
  * time_t        st_mtime;   ( time of last modification )
  * time_t        st_ctime;   ( time of last change )
  * }; */
  char *ptr;
  for (i=1; i<argc; i++)
    printf ("%s is a ", argv[i]);
    if (lstat(argv[i], &buf)<0)
	/* lstat() stats the file pointed to and fills in buf. In the case of a symbolic link, 
	 * the link itself is stat-ed, not the file that it refers to as oppsed to stat() */
		printf("... i don't know what it is, lstat returned an error\n");
	/* The following predicate macros test the type of a file, given the
	 * `st_mode' field returned by `lstat' on that file:
	else if (S_ISREG(buf.st_mode))  ptr ="regular file\n";
	else if (S_ISDIR(buf.st_mode))  ptr ="directory\n";
	else if (S_ISCHR(buf.st_mode))  ptr ="character special file (a device like a terminal)\n";
	else if (S_ISBLK(buf.st_mode))  ptr ="block special file (a device like a disk)\n";
	else if (S_ISFIFO(buf.st_mode)) ptr ="FIFO special file, or a pipe\n";
	//#ifdef S_ISLNK
	else if (S_ISLNK(buf.st_mode))  ptr ="symbolic link\n";
	//#ifdef S_ISSOCK
    else if (S_ISSOCK(buf.st_mode)) ptr ="socket\n";
    else ptr="... wait... it's a... a... WHAT THE HELL IS THAT ?\n";
return 0;