#include <stdio.h>
#include <mntent.h>
FILE *setmntent(const char *path, const char *type);
struct mntent *getmntent(FILE *stream);
int addmntent(FILE *restrict stream,
const struct mntent *restrict mnt);
int endmntent(FILE *streamp);
char *hasmntopt(const struct mntent *mnt, const char *opt);
/* GNU による拡張 */
#include <mntent.h>
struct mntent *getmntent_r(int size;
FILE *restrict streamp,
struct mntent *restrict mntbuf,
char buf[restrict size], int size);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
getmntent_r():
Since glibc 2.19:
_DEFAULT_SOURCE
glibc 2.19 and earlier:
_BSD_SOURCE || _SVID_SOURCE
The setmntent() function opens the filesystem description file path and returns a file pointer which can be used by getmntent(). The argument type is the type of access required and can take the same values as the mode argument of fopen(3). The returned stream should be closed using endmntent() rather than fclose(3).
getmntent() 関数は stream からファイルシステムの記述ファイルの次の行を読み込み、 読み込んだ行をフィールドに分割した内容を収めた構造体へのポインターを返す。 ポインターはメモリーの静的な領域を指しており、この領域は getmntent() を次に呼び出したときに上書きされてしまう。
addmntent() 関数は mntent 構造体 mnt の内容を、オープンされている stream の最後に追加する。
endmntent() 関数はファイルシステムの記述ファイルに関連付けられている stream を閉じる。
hasmntopt() 関数は mntent 構造体 mnt の mnt_opts フィールド (下記 参照) をスキャンし、 opt に一致する部分文字列があるかを調べる。 有効なマウントオプションについては <mntent.h> と mount(8) を参照のこと。
The reentrant getmntent_r() function is similar to getmntent(), but stores the mntent structure in the provided *mntbuf, and stores the strings pointed to by the entries in that structure in the provided array buf of size size.
mntent 構造体は <mntent.h> で以下のように定義されている。
struct mntent {
char *mnt_fsname; /* name of mounted file system */
char *mnt_dir; /* file system path prefix */
char *mnt_type; /* mount type (see mntent.h) */
char *mnt_opts; /* mount options (see mntent.h) */
int mnt_freq; /* dump frequency in days */
int mnt_passno; /* pass number on parallel fsck */
};
Since fields in the mtab and fstab files are separated by whitespace, octal escapes are used to represent the characters space (\040), tab (\011), newline (\012), and backslash (\\) in those files when they occur in one of the four strings in a mntent structure. The routines addmntent() and getmntent() will convert from string representation to escaped representation and back. When converting from escaped representation, the sequence \134 is also converted to a backslash.
addmntent() 関数は成功したら 0 を返し、失敗したら 1 を返す。
endmntent() 関数はつねに 1 を返す。
hasmntopt() 関数は、マッチした場合は部分文字列へのアドレスを返し、 マッチしなければ NULL を返す。
| インターフェース | 属性 | 値 |
| setmntent(), endmntent(), hasmntopt() | Thread safety | MT-Safe |
| getmntent() | Thread safety |
MT-Unsafe race:mntentbuf locale
|
| addmntent() | Thread safety |
MT-Safe race:stream locale
|
| getmntent_r() | Thread safety | MT-Safe locale |
System V also has a getmntent() function but the calling sequence differs, and the returned structure is different. Under System V /etc/mnttab is used. 4.4BSD and Digital UNIX have a routine getmntinfo(), a wrapper around the system call getfsstat().