chiark / gitweb /
Get rid of some more unused defines and dirs
[elogind.git] / src / shared / fstab-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2015 Zbigniew JÄ™drzejewski-Szmek
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include "fstab-util.h"
23 #include "strv.h"
24 #include "util.h"
25
26 int fstab_filter_options(const char *opts, const char *names,
27                          const char **namefound, char **value, char **filtered) {
28         const char *name, *n = NULL, *x;
29         _cleanup_strv_free_ char **stor = NULL;
30         _cleanup_free_ char *v = NULL, **strv = NULL;
31
32         assert(names && *names);
33
34         if (!opts)
35                 goto answer;
36
37         /* If !value and !filtered, this function is not allowed to fail. */
38
39         if (!filtered) {
40                 const char *word, *state;
41                 size_t l;
42
43                 FOREACH_WORD_SEPARATOR(word, l, opts, ",", state)
44                         NULSTR_FOREACH(name, names) {
45                                 if (l < strlen(name))
46                                         continue;
47                                 if (!strneq(word, name, strlen(name)))
48                                         continue;
49
50                                 /* we know that the string is NUL
51                                  * terminated, so *x is valid */
52                                 x = word + strlen(name);
53                                 if (IN_SET(*x, '\0', '=', ',')) {
54                                         n = name;
55                                         if (value) {
56                                                 free(v);
57                                                 if (IN_SET(*x, '\0', ','))
58                                                         v = NULL;
59                                                 else {
60                                                         assert(*x == '=');
61                                                         x++;
62                                                         v = strndup(x, l - strlen(name) - 1);
63                                                         if (!v)
64                                                                 return -ENOMEM;
65                                                 }
66                                         }
67                                 }
68                         }
69         } else {
70                 char **t, **s;
71
72                 stor = strv_split(opts, ",");
73                 if (!stor)
74                         return -ENOMEM;
75                 strv = memdup(stor, sizeof(char*) * (strv_length(stor) + 1));
76                 if (!strv)
77                         return -ENOMEM;
78
79                 for (s = t = strv; *s; s++) {
80                         NULSTR_FOREACH(name, names) {
81                                 x = startswith(*s, name);
82                                 if (x && IN_SET(*x, '\0', '='))
83                                         goto found;
84                         }
85
86                         *t = *s;
87                         t++;
88                         continue;
89                 found:
90                         /* Keep the last occurence found */
91                         n = name;
92                         if (value) {
93                                 free(v);
94                                 if (*x == '\0')
95                                         v = NULL;
96                                 else {
97                                         assert(*x == '=');
98                                         x++;
99                                         v = strdup(x);
100                                         if (!v)
101                                                 return -ENOMEM;
102                                 }
103                         }
104                 }
105                 *t = NULL;
106         }
107
108 answer:
109         if (namefound)
110                 *namefound = n;
111         if (filtered) {
112                 char *f;
113
114                 f = strv_join(strv, ",");
115                 if (!f)
116                         return -ENOMEM;
117
118                 *filtered = f;
119         }
120         if (value) {
121                 *value = v;
122                 v = NULL;
123         }
124
125         return !!n;
126 }
127
128 int fstab_find_pri(const char *options, int *ret) {
129         _cleanup_free_ char *opt = NULL;
130         int r;
131         unsigned pri;
132
133         assert(ret);
134
135         r = fstab_filter_options(options, "pri\0", NULL, &opt, NULL);
136         if (r < 0)
137                 return r;
138         if (r == 0 || !opt)
139                 return 0;
140
141         r = safe_atou(opt, &pri);
142         if (r < 0)
143                 return r;
144
145         if ((int) pri < 0)
146                 return -ERANGE;
147
148         *ret = (int) pri;
149         return 1;
150 }