return 0;
}
-static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
+static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
{
- const char *s=search, *z;
- int l, fd;
+ size_t l;
+ int fd;
for (;;) {
- while (*s==':') s++;
- if (!*s) return -1;
- z = strchr(s, ':');
- l = z ? z-s : strlen(s);
- snprintf(buf, buf_size, "%.*s/%s", l, s, name);
+ s += strspn(s, ":\n");
+ l = strcspn(s, ":\n");
+ if (l-1 >= INT_MAX) return -1;
+ if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) >= buf_size)
+ continue;
if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
s += l;
}