ptg10805159
Section 4.3 File Types 97
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);
}
Figure 4.3 Print type of file for each command-line argument
Sample output from Figure4.3 is
$./a.out /etc/passwd /etc /dev/log /dev/tty \
>/var/lib/oprofile/opd_pipe /dev/sr0 /dev/cdrom
/etc/passwd: regular
/etc: directory
/dev/log: socket
/dev/tty: character special
/var/lib/oprofile/opd_pipe: fifo
/dev/sr0: 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 prompted us with its secondary prompt,>, on the next line.) We have
specifically used thelstatfunction instead of thestatfunction to detect symbolic
links. If we used thestatfunction, we would never see symbolic links.
Historically,early versions of the UNIX System didn’t provide the S_ISxxx
macros. Instead, we had to logically AND thest_modevalue with the maskS_IFMT
and then comparethe result with the constants whose names areS_IFxxx.Most
systems define this mask and the related constants in the file<sys/stat.h>.Ifwe
examine this file, we’ll find theS_ISDIRmacrodefined something like
#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
We’ve said that regular files arepredominant, but it is interesting to see what
percentage of the files on a given system are of each file type. Figure4.4 shows the
counts and percentages for a Linux system that is used as a single-user workstation.
This data was obtained from the program shown in Section 4.22.