* reached by a symlink. The old string will be freed. */
for (;;) {
- char *target, *k, *name;
+ char *target, *name;
if (c++ >= FOLLOW_MAX)
return -ELOOP;
return -errno;
/* Hmm, so this is a symlink. Let's read the name, and follow it manually */
- if ((r = readlink_malloc(*filename, &target)) < 0)
+ if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
return r;
- k = file_in_same_dir(*filename, target);
- free(target);
-
- if (!k)
- return -ENOMEM;
-
free(*filename);
- *filename = k;
+ *filename = target;
}
if (!(f = fdopen(fd, "r"))) {
}
}
+int readlink_and_make_absolute(const char *p, char **r) {
+ char *target, *k;
+ int j;
+
+ assert(p);
+ assert(r);
+
+ if ((j = readlink_malloc(p, &target)) < 0)
+ return j;
+
+ k = file_in_same_dir(p, target);
+ free(target);
+
+ if (!k)
+ return -ENOMEM;
+
+ *r = k;
+ return 0;
+}
+
char *file_name_from_path(const char *p) {
char *r;
char *strappend(const char *s, const char *suffix);
int readlink_malloc(const char *p, char **r);
+int readlink_and_make_absolute(const char *p, char **r);
char *file_name_from_path(const char *p);
bool is_path(const char *p);