4.3. File TypesWe've talked about two different types of files so far: regular files and directories. Most files on a UNIX system are either regular files or directories, but there are additional types of files. The types are: ExampleThe program in Figure 4.3 prints the type of file for each command-line argument.Sample output from Figure 4.3 is$ ./a.out /etc/passwd /etc /dev/initctl /dev/log /dev/tty \ > /dev/scsi/host0/bus0/target0/lun0/cd /dev/cdrom /etc/passwd: regular /etc: directory /dev/initctl: fifo /dev/log: socket /dev/tty: character special /dev/scsi/host0/bus0/target0/lun0/cd: block special /dev/cdrom: symbolic link (Here, we have explicitly entered a backslash at the end of the first command line, telling the shell that we want to continue entering the command on another line. The shell then prompts us with its secondary prompt, >, on the next line.) We have specifically used the lstat function instead of the stat function to detect symbolic links. If we used the stat function, we would never see symbolic links.To compile this program on a Linux system, we must define _GNU_SOURCE to include the definition of the S_ISSOCK macro. Figure 4.3. Print type of file for each command-line argument#include "apue.h" int main(int argc, char *argv[]) { int i; struct stat buf; char *ptr; for (i = 1; i < argc; i++) { printf("%s: ", argv[i]); if (lstat(argv[i], &buf) < 0) { err_ret("lstat error"); continue; } if (S_ISREG(buf.st_mode)) ptr = "regular"; else if (S_ISDIR(buf.st_mode)) ptr = "directory"; else if (S_ISCHR(buf.st_mode)) ptr = "character special"; else if (S_ISBLK(buf.st_mode)) ptr = "block special"; else if (S_ISFIFO(buf.st_mode)) ptr = "fifo"; else if (S_ISLNK(buf.st_mode)) ptr = "symbolic link"; else if (S_ISSOCK(buf.st_mode)) ptr = "socket"; else ptr = "** unknown mode **"; printf("%s\n", ptr); } exit(0); } Historically, early versions of the UNIX System didn't provide the S_ISxxx macros. Instead, we had to logically AND the st_mode value with the mask S_IFMT and then compare the result with the constants whose names are S_IFxxx. Most systems define this mask and the related constants in the file <sys/stat.h>. If we examine this file, we'll find the S_ISDIR macro defined something like#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) We've said that regular files are predominant, but it is interesting to see what percentage of the files on a given system are of each file type. Section 4.21. ![]() |