Advanced Programming in the UNIX Environment: Second Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Advanced Programming in the UNIX Environment: Second Edition [Electronic resources] - نسخه متنی

W. Richard Stevens; Stephen A. Rago

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



4.3. File Types


We'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:

  • Regular file. The most common type of file, which contains data of some form. There is no distinction to the UNIX kernel whether this data is text or binary. Any interpretation of the contents of a regular file is left to the application processing the file.

    One notable exception to this is with binary executable files. To execute a program, the kernel must understand its format. All binary executable files conform to a format that allows the kernel to identify where to load a program's text and data.

  • Directory file. A file that contains the names of other files and pointers to information on these files. Any process that has read permission for a directory file can read the contents of the directory, but only the kernel can write directly Section 15.5.

  • Socket. A type of file used for network communication between processes. A socket can also be used for non-network communication between processes on a single host. We use sockets for interprocess communication in Chapter 16.

  • Symbolic link. A type of file that points to another file. We talk more about symbolic links in Section 4.16.

  • The type of a file is encoded in the st_mode member of the stat structure. We can determine the file type with the macros shown 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 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.


      / 369