]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fstab-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Zbigniew Jędrzejewski-Szmek
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "path-util.h"
23 #include "string-util.h"
26 #include "fstab-util.h"
28 bool fstab_is_mount_point(const char *mount
) {
29 _cleanup_endmntent_
FILE *f
= NULL
;
32 f
= setmntent("/etc/fstab", "r");
36 while ((m
= getmntent(f
)))
37 if (path_equal(m
->mnt_dir
, mount
))
43 int fstab_filter_options(const char *opts
, const char *names
,
44 const char **namefound
, char **value
, char **filtered
) {
45 const char *name
, *n
= NULL
, *x
;
46 _cleanup_strv_free_
char **stor
= NULL
;
47 _cleanup_free_
char *v
= NULL
, **strv
= NULL
;
49 assert(names
&& *names
);
54 /* If !value and !filtered, this function is not allowed to fail. */
57 const char *word
, *state
;
60 FOREACH_WORD_SEPARATOR(word
, l
, opts
, ",", state
)
61 NULSTR_FOREACH(name
, names
) {
64 if (!strneq(word
, name
, strlen(name
)))
67 /* we know that the string is NUL
68 * terminated, so *x is valid */
69 x
= word
+ strlen(name
);
70 if (IN_SET(*x
, '\0', '=', ',')) {
74 if (IN_SET(*x
, '\0', ','))
79 v
= strndup(x
, l
- strlen(name
) - 1);
89 stor
= strv_split(opts
, ",");
92 strv
= memdup(stor
, sizeof(char*) * (strv_length(stor
) + 1));
96 for (s
= t
= strv
; *s
; s
++) {
97 NULSTR_FOREACH(name
, names
) {
98 x
= startswith(*s
, name
);
99 if (x
&& IN_SET(*x
, '\0', '='))
107 /* Keep the last occurence found */
131 f
= strv_join(strv
, ",");
145 int fstab_extract_values(const char *opts
, const char *name
, char ***values
) {
146 _cleanup_strv_free_
char **optsv
= NULL
, **res
= NULL
;
153 optsv
= strv_split(opts
, ",");
157 STRV_FOREACH(s
, optsv
) {
161 arg
= startswith(*s
, name
);
162 if (!arg
|| *arg
!= '=')
164 r
= strv_extend(&res
, arg
+ 1);
175 int fstab_find_pri(const char *options
, int *ret
) {
176 _cleanup_free_
char *opt
= NULL
;
182 r
= fstab_filter_options(options
, "pri\0", NULL
, &opt
, NULL
);
188 r
= safe_atou(opt
, &pri
);