mirror of
https://git.sr.ht/~sircmpwn/gmni
synced 2024-11-23 04:51:59 +01:00
Simplify posix_dirname logic
dirname has two main problems: 1. It could change in place the string that is passed to it. 2. It uses a static string for its return value, so one should copy it somewhere else as soon as possible to avoid subsequent calls to dirname to corrupt his data (see #48). We avoid 1. passing a copy of `path` to dirname and 2. copying it's return value into `dname`.
This commit is contained in:
parent
4a6172f1bf
commit
ab66dd2be9
@ -19,13 +19,8 @@ posix_dirname(char *path, char *dname)
|
||||
assert(strlen(path) <= PATH_MAX);
|
||||
|
||||
strcpy(p, path);
|
||||
t = dirname(path);
|
||||
t = dirname(p);
|
||||
memmove(dname, t, strlen(t) + 1);
|
||||
|
||||
/* restore the path if dirname worked in-place */
|
||||
if (t == path && path != dname) {
|
||||
strcpy(path, p);
|
||||
}
|
||||
}
|
||||
|
||||
/** Make directory and all of its parents */
|
||||
|
Loading…
Reference in New Issue
Block a user