getmntent(3)
β¨ getmntent β bestie edition β¨
okay so this is the manual page for the getmntent family of library functions, living in the Library Functions Manual. if you've ever wanted to peek at what filesystems are mounted or what's in your fstab without hand-parsing a huge file, this is your moment. π
πΈ NAME πΈ
getmntent, setmntent, addmntent, endmntent, hasmntopt, getmntent_r β get filesystem descriptor file entry ποΈβ¨
π LIBRARY π
Standard C library (libc, -lc) π«Ά
π SYNOPSIS π
#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 extension */
#include <mntent.h>
struct mntent *getmntent_r(int size;
FILE *restrict streamp,
struct mntent *restrict mntbuf,
char buf[restrict size], int size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getmntent_r():- Since glibc 2.19:
_DEFAULT_SOURCE - glibc 2.19 and earlier:
_BSD_SOURCE || _SVID_SOURCE
- Since glibc 2.19:
no cap, thatgetmntent_r()prototype above is exactly how it's written here, weirdint size;bit and all. copying it character for character because that's the whole vibe of a manpage. π
π¦ DESCRIPTION π¦
These routines are the little key π to the filesystem description file /etc/fstab and the mounted filesystem description file /etc/mtab.
The setmntent() function opens the filesystem description file path and hands you back a file pointer you can feed to getmntent(). The type argument is the type of access you want, and it takes the same values as the mode argument of fopen(3). Important: the stream it gives you back should be closed with endmntent(), not fclose(3). don't mix those up bestie, it's giving "wrong cleanup function" energy π
The getmntent() function reads the next line of the filesystem description file from stream and returns a pointer to a structure holding the broken-out fields from one line of the file. β οΈ heads up: that pointer points at a static area of memory that gets overwritten by subsequent calls to getmntent(). So if you're keeping the result around, copy it. periodt.
The addmntent() function adds the mntent structure mnt to the end of the open stream.
The endmntent() function closes the stream associated with the filesystem description file.
The hasmntopt() function scans the mnt_opts field (see below π) of the mntent structure mnt looking for a substring that matches opt. Wanna know which mount options are even valid? Check <mntent.h> and mount(8). π§
The reentrant getmntent_r() function is like getmntent() but with main-character energy: it stores the mntent structure in the provided *mntbuf, and it stores the strings that those entries point to inside the provided array buf of size size. (reentrant = thread-friendly, we love to see it π)
the mntent struct π
The mntent structure is defined in <mntent.h> like this:
struct mntent {
char *mnt_fsname; /* name of mounted filesystem */
char *mnt_dir; /* filesystem 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 */
};
the escape thing π
Since the fields in the mtab and fstab files are separated by whitespace, octal escapes get used to represent certain characters when they show up inside one of the four strings in a mntent structure. Those characters are:
- space β
\040 - tab β
\011 - newline β
\012 - backslash β
\\
The routines addmntent() and getmntent() will convert from string representation to escaped representation and back π. And when it's converting from escaped representation, the sequence \134 also gets converted to a backslash. (yes, two ways to write a backslash, iconic of them honestly.)
π RETURN VALUE π
getmntent()andgetmntent_r()return a pointer to themntentstructure, orNULLon failure. πaddmntent()returns0on success and1on failure. πendmntent()always returns1. always. it just does. πhasmntopt()returns the address of the substring if a match is found, andNULLotherwise.
π FILES π
/etc/fstabβ filesystem description file/etc/mtabβ mounted filesystem description file
β ATTRIBUTES β
For an explanation of the terms used in this section, see attributes(7).
setmntent(),endmntent(),hasmntopt()β Thread safety:MT-Safegetmntent()β Thread safety:MT-Unsafe race:mntentbuf localeaddmntent()β Thread safety:MT-Safe race:stream localegetmntent_r()β Thread safety:MT-Safe locale
(yeah that race:mntentbuf one is exactly why the non-reentrant getmntent() gives you that shared static buffer. told you. π)
π°οΈ STANDARDS π°οΈ
None.
(it's giving "no standard will claim us" and honestly? relatable.)
π HISTORY π
The nonreentrant functions come from SunOS 4.1.3. A getmntent_r() routine was introduced in HP-UX 10, but it returns an int. The prototype shown above is glibc-only. π
System V also has a getmntent() function, but the calling sequence is different and the returned structure is different too. Under System V, /etc/mnttab is used instead. Meanwhile 4.4BSD and Digital UNIX have a routine called getmntinfo(), which is a wrapper around the system call getfsstat().
π SEE ALSO π
and that's the whole page bestie π«Ά this one's from Linux man-pages 6.20, dated 2026-02-08, and you just read getmntent(3). go mount something responsibly. π β¨πΈ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms