-/*!
- * @brief symlink safe chdir replacement
- *
- * Only chdirs to dir if it doesn't contain symlinks.
- *
- * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
- */
-static int lchdir(const char *dir)
-{
- int ret = 0;
- char buf[MAXPATHLEN+1];
-#ifdef REALPATH_TAKES_NULL
- char *rpath = NULL;
-#else
- char rpath[MAXPATHLEN+1];
-#endif
-
- /* dir might be an relative or an absolute path */
- if (dir[0] == '/') {
- /* absolute path, just make sure buf is prepared for strlcat */
- buf[0] = 0;
- } else {
- /* relative path, push cwd int buf */
- if (getcwd(buf, MAXPATHLEN) == NULL)
- return -1;
- if (strlcat(buf, "/", MAXPATHLEN) >= MAXPATHLEN)
- return -1;
- }
-
- if (strlcat(buf, dir, MAXPATHLEN) >= MAXPATHLEN)
- return -1;
-
-#ifdef REALPATH_TAKES_NULL
- if ((rpath = realpath(dir, NULL)) == NULL) {
-#else
- if (realpath(dir, rpath) == NULL) {
-#endif
- ret = -1;
- goto exit;
- }
-
- /*
- * Cases:
- * chdir request | realpath result | ret
- * (after getwcwd) | |
- * =======================================
- * /a/b/. | /a/b | 0
- * /a/b/. | /c | 1
- * /a/b/. | /c/d/e/f | 1
- */
- ret = 0;
- for (int i = 0; rpath[i]; i++) {
- if (buf[i] != rpath[i]) {
- ret = 1;
- goto exit;
- }
- }
-
- if (chdir(dir) != 0) {
- ret = -1;
- goto exit;
- }
-
-exit:
-#ifdef REALPATH_TAKES_NULL
- free(rpath);
-#endif
- return ret;
-}
-