---
name: "getmntopts"
section: "3"
platform: "macos"
package: "libutil"
version: "73"
release: "macOS 26.5"
platforms: ["freebsd", "macos"]
url: "https://new.carta.tech/getmntopts"
---

# getmntopts(3)

scan mount options

## NAME

**getmntopts** – scan mount options

## LIBRARY

System Utilities Library (libutil, -lutil)

## SYNOPSIS

**#include \<mntopts.h\>**

*mntoptparse\_t*\

**getmntopts**(*const char \*options*, *const struct mntopt \*mopts*, *int \*flagp*, *int \*altflagp*);

*const* *char* *\**\

**getmntoptstr**(*mntoptparse\_t mp*, *const char \*opt*);

*long*\

**getmntoptnum**(*mntoptparse\_t mp*, *const char \*opt*);

*void*\

**freemntopts**(*mntoptparse\_t mp*);

## DESCRIPTION

The **getmntopts**() function takes a comma separated option list and a list of valid option names, and computes the bitmasks corresponding to the requested set of options.

The string *options* is broken down into a sequence of comma separated tokens. Each token is looked up in the table described by *mopts* and the bits in the word referenced by either *flagp* or *altflagp* (depending on the m\_altloc field of the option's table entry) are updated. The flag words are not initialized by **getmntopts**(). The table, *mopts*, has the following format:

```
struct mntopt {
	const char *m_option;	/* option name */
	int m_inverse;		/* negative option, e.g., "dev" */
	int m_flag;		/* bit to set, e.g., MNT_RDONLY */
	int m_altloc;		/* use altflagp rather than flagp */
};
```

The members of this structure are:

- *m\_option*

  the option name, for example “suid”.

- *m\_inverse*

  tells **getmntopts**() that the name has the inverse meaning of the bit. For example, “suid” is the string, whereas the mount flag is MNT\_NOSUID. In this case, the sense of the string and the flag are inverted, so the *m\_inverse* flag should be set.

- *m\_flag*

  the value of the bit to be set or cleared in the flag word when the option is recognized. The bit is set when the option is discovered, but cleared if the option name was preceded by the letters “no”. The *m\_inverse* flag causes these two operations to be reversed.

- *m\_altloc*

  the bit should be set or cleared in *altflagp* rather than *flagp*.

Each of the user visible MNT\_ flags has a corresponding MOPT\_ macro which defines an appropriate struct mntopt entry. To simplify the program interface and ensure consistency across all programs, a general purpose macro, MOPT\_STDOPTS, is defined which contains an entry for all the generic VFS options:

```
MOPT_USERQUOTA,
MOPT_GROUPQUOTA,
MOPT_FSTAB_COMPAT,
MOPT_NODEV,
MOPT_NOEXEC,
MOPT_NOSUID,
MOPT_NOFOLLOW,
MOPT_RDONLY,
MOPT_UNION,
MOPT_BROWSE,
MOPT_AUTOMOUNTED,
MOPT_DEFWRITE,
MOPT_NOATIME,
MOPT_PERMISSIONS,
MOPT_IGNORE_OWNERSHIP,
MOPT_QUARANTINE,
MOPT_CPROTECT
```

In addition, the macros MOPT\_FORCE and MOPT\_UPDATE exist to enable the MNT\_FORCE and MNT\_UPDATE flags to be set. Finally, the table must be terminated by an entry with a NULL first element.

**getmntopts**() returns a mntoptparse\_t handle that can be used in subsequent **getmntoptstr**() and **getmntoptnum**() calls to fetch a value for an option and that must be freed with a call to **freemntopts**(). If an error occurred, then if the external integer value *getmnt\_silent* is zero then **getmntopts**() prints an error message and exits; if *getmnt\_silent* is non-zero then **getmntopts**() returns NULL.

The **getmntoptstr**() function returns the string value of the named option, if such a value was set in the option string. If the value was not set, then if the external integer value *getmnt\_silent* is zero then **getmntoptstr**() prints an error message and exits; if *getmnt\_silent* is non-zero then **getmntoptstr**() returns NULL.

The **getmntoptnum**() returns the long value of the named option, if such a value was set in the option string. If the value was not set, or could not be converted from a string to a long, then if the external integer value *getmnt\_silent* is zero then **getmntoptnum**() prints an error message and exits; if *getmnt\_silent* is non-zero then **getmntoptnum**() returns -1.

The **freemntopts**() frees the storage used by **getmntopts**().

## RETURN VALUES

**getmntopts**() returns NULL if an error occurred. Note that some bits may already have been set in *flagp* and *altflagp* even if NULL is returned. **getmntoptstr**() returns NULL if an error occurred. **getmntoptnum**() returns -1 if an error occurred.

## EXAMPLES

Most commands will use the standard option set. Local filesystems which support the MNT\_UPDATE flag, would also have an MOPT\_UPDATE entry. This can be declared and used as follows:

```
#include mntopts.h
static const struct mntopt mopts[] = {
	MOPT_STDOPTS,
	MOPT_UPDATE,
	{ NULL }
};
...
long val;
mntoptparse_t mp;
mntflags = mntaltflags = 0;
...
mp = getmntopts(options, mopts, mntflags, mntaltflags);
if (mp == NULL)
	err(EXIT_FAILURE, "getmntopts");
...
val = getmntoptnum(mp, "rsize");
freemntopts(mp);
```

## DIAGNOSTICS

If the external integer variable *getmnt\_silent* is zero then the **getmntopts**(), **getmntoptstr**(), and **getmntoptnum**() functions display an error message and exit if an error occurred. By default *getmnt\_silent* is zero.

## SEE ALSO

[err(3)](/err/3.md), [mount(8)](/mount/8.md)

## HISTORY

The **getmntopts**() function appeared in 4.4BSD. It was moved to the utilities library and enhanced to retrieve option values in NetBSD 2.0.
