1 |
commit: 7b4879cb72e907414b70553663bd9b6fda8d4408 |
2 |
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com> |
3 |
AuthorDate: Wed Feb 14 22:56:19 2018 +0000 |
4 |
Commit: William Hubbs <williamh <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Feb 14 22:56:19 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=7b4879cb |
7 |
|
8 |
mountinfo: create strings with xasprintf |
9 |
|
10 |
src/rc/mountinfo.c | 21 +++++++++++---------- |
11 |
1 file changed, 11 insertions(+), 10 deletions(-) |
12 |
|
13 |
diff --git a/src/rc/mountinfo.c b/src/rc/mountinfo.c |
14 |
index d9c25a38..5bf97386 100644 |
15 |
--- a/src/rc/mountinfo.c |
16 |
+++ b/src/rc/mountinfo.c |
17 |
@@ -248,7 +248,6 @@ find_mounts(struct args *args) |
18 |
struct opt *o; |
19 |
int netdev; |
20 |
char *tmp; |
21 |
- size_t l; |
22 |
|
23 |
if ((nmnts = getmntinfo(&mnts, MNT_NOWAIT)) == 0) |
24 |
eerrorx("getmntinfo: %s", strerror (errno)); |
25 |
@@ -264,11 +263,7 @@ find_mounts(struct args *args) |
26 |
if (! options) |
27 |
options = xstrdup(o->o_name); |
28 |
else { |
29 |
- l = strlen(options) + |
30 |
- strlen(o->o_name) + 2; |
31 |
- tmp = xmalloc(sizeof (char) * l); |
32 |
- snprintf(tmp, l, "%s,%s", options, |
33 |
- o->o_name); |
34 |
+ xasprintf(&tmp, "%s,%s", options, o->o_name); |
35 |
free(options); |
36 |
options = tmp; |
37 |
} |
38 |
@@ -315,6 +310,7 @@ find_mounts(struct args *args) |
39 |
{ |
40 |
FILE *fp; |
41 |
char *buffer; |
42 |
+ size_t size; |
43 |
char *p; |
44 |
char *from; |
45 |
char *to; |
46 |
@@ -329,8 +325,8 @@ find_mounts(struct args *args) |
47 |
|
48 |
list = rc_stringlist_new(); |
49 |
|
50 |
- buffer = xmalloc(sizeof(char) * PATH_MAX * 3); |
51 |
- while (fgets(buffer, PATH_MAX * 3, fp)) { |
52 |
+ buffer = NULL; |
53 |
+ while (getline(&buffer, &size, fp) != -1) { |
54 |
netdev = -1; |
55 |
p = buffer; |
56 |
from = strsep(&p, " "); |
57 |
@@ -346,6 +342,8 @@ find_mounts(struct args *args) |
58 |
} |
59 |
|
60 |
process_mount(list, args, from, to, fst, opts, netdev); |
61 |
+ free(buffer); |
62 |
+ buffer = NULL; |
63 |
} |
64 |
free(buffer); |
65 |
fclose(fp); |
66 |
@@ -380,7 +378,7 @@ int main(int argc, char **argv) |
67 |
regex_t *skip_point_regex = NULL; |
68 |
RC_STRINGLIST *nodes; |
69 |
RC_STRING *s; |
70 |
- char real_path[PATH_MAX + 1]; |
71 |
+ char *real_path = NULL; |
72 |
int opt; |
73 |
int result; |
74 |
char *this_path; |
75 |
@@ -450,9 +448,12 @@ int main(int argc, char **argv) |
76 |
eerrorx("%s: `%s' is not a mount point", |
77 |
argv[0], argv[optind]); |
78 |
this_path = argv[optind++]; |
79 |
- if (realpath(this_path, real_path)) |
80 |
+ real_path = realpath(this_path, NULL); |
81 |
+ if (real_path) |
82 |
this_path = real_path; |
83 |
rc_stringlist_add(args.mounts, this_path); |
84 |
+ free(real_path); |
85 |
+ real_path = NULL; |
86 |
} |
87 |
nodes = find_mounts(&args); |
88 |
rc_stringlist_free(args.mounts); |