Add Book to My BookshelfPurchase This Book Online

Appendix B - Accessing Filesystem Data Structures

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

The Mounted Filesystem Table
The file /etc/mnttab contains a list of the filesystems that are currently mounted, and some information about them. This file is maintained mostly by the mount and umount commands, although other processes such as the automounter and the volume management daemon can also make updates to it.
In SVR4, the /etc/mnttab file is a text file, consisting of one-line entries. In most other versions of UNIX, it is a binary file, with each entry consisting of a structure that contains more or less the same information. The functions provided for reading this file use a structure of type struct mnttab to describe each entry. This structure is declared in the include file sys/mnttab.h:
    struct mnttab {
        char    *mnt_special;
        char    *mnt_mountp;
        char    *mnt_fstype;
        char    *mnt_mntopts;
        char    *mnt_time;
    };
The fields of the structure are:
mnt_special
The name of the block-special device where the filesystem resides.
mnt_mountp
The name of the filesystem mount point, i.e., the directory that it is mounted on.
mnt_fstype
The type of the filesystem (e.g., ufs, nfs, hsfs, or pcfs).
mnt_mntopts
A comma-separated list of the options with which the filesystem was mounted. The legal values vary with the filesystem type, but this includes things such as read-only, no set-user-id, and so forth.
mnt_time
The time the filesystem was mounted. This is a character string containing the time_t value in ASCII; it must be converted to an integer with atoi and then passed to ctime or whatever (see Chapter 7, Time of Day Operations).
Three functions read the /etc/mnttab file:
    #include <stdio.h>
    #include <sys/mnttab.h>
    int getmntent(FILE *fp, struct mnttab *mnt);
    int getmntany(FILE *fp, struct mnttab *mnt, struct mnttab *mntref);
    char *hasmntopt(struct mnttab *mnt, char *option);
The getmntent function reads the next entry from the file referenced by fp, and stores the broken-out fields of the entry in the area pointed to by mnt. The getmntany function searches the file referenced by fp for an entry that matches the non-null fields of mntref, and stores the broken-out fields of the entry in the area pointed to by mnt. Notice that neither of these functions opens, closes, or rewinds the /etc/mnttab file.
Both getmntent and getmntany return 0 if an entry is successfully read, and -1 if end-of-file is encountered. If a formatting error occurs in the file, they return one of the following:
MNT_TOOLONG
A line in the file exceeded the maximum line length.
MNT_TOOMANY
A line in the file contains too many fields.
MNT_TOOFEW
A line in the file does not contain enough fields.
The hasmntopt function scans the mnt_mntopts field of mnt for a substring that matches option. It returns a pointer to the substring if it is present, and NULL if is not.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback