Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

96 Files and Directories Chapter 4


MacroType of file
S_ISREG() regular file
S_ISDIR() directory file
S_ISCHR() character special file
S_ISBLK() block special file
S_ISFIFO() pipe or FIFO
S_ISLNK() symbolic link
S_ISSOCK() socket

Figure 4.1 File type macros in<sys/stat.h>

POSIX.1 allows implementations to represent interprocess communication(IPC)
objects, such as message queues and semaphores, as files. The macros shown in
Figure4.2 allow us to determine the type of IPC object from thestatstructure. Instead
of taking thest_modemember as an argument, these macros differ from those in
Figure4.1 in that their argument is a pointer to thestatstructure.

MacroType of object
S_TYPEISMQ() message queue
S_TYPEISSEM() semaphore
S_TYPEISSHM() shared memory object

Figure 4.2 IPC type macros in<sys/stat.h>

Message queues, semaphores, and shared memory objects arediscussed in Chapter 15.
However,none of the various implementations of the UNIX System discussed in this
book represent these objects as files.

Example


The program in Figure4.3 prints the 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))
Free download pdf