Debian Codegraph @

texk/kpathsea/xgetcwd.c

raw
1 /* xgetcwd.c: a from-scratch version of getwd, returning result in new
2 memory. Never uses a system getwd. Ideas from tcsh 5.20 source.
3
4 Copyright 1992, 1994, 1996, 2008, 2011, 2016, 2026 Karl Berry.
5 Copyright 2005 Olaf Weber.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this library; if not, see <http://www.gnu.org/licenses/>. */
19
20 #include <kpathsea/config.h>
21
22 #if defined (HAVE_GETCWD) && !defined (GETCWD_FORKS)
23 #include <kpathsea/c-pathmx.h>
24
25 #else /* not HAVE_GETCWD || GETCWD_FORKS */
26 #include <kpathsea/c-dir.h>
27 #include <kpathsea/xopendir.h>
28 #include <kpathsea/xstat.h>
29
30 static void
31 xchdir (string dirname)
32 {
33 if (chdir (dirname) != 0) {
34 FATAL_PERROR (dirname);
35 }
36 }
37 #endif /* not HAVE_GETCWD || GETCWD_FORKS */
38
39
40 /* Return the pathname of the current directory in new memory, or give a
41 fatal error. */
42
43 string
44 xgetcwd (void)
45 {
46 /* If the system provides getcwd, and it doesn't fork, use it.
47 This should be the case on just about all systems nowadays.
48 But just in case, if getcwd does fork, provide a fallback
49 implemented from scratch. Don't ever use getwd, since some
50 versions of GNU ld (as of 2025 or so) give an unconditional
51 warning that it's dangerous. */
52 #if defined (HAVE_GETCWD) && !defined (GETCWD_FORKS)
53 char path[PATH_MAX + 1];
54 #if defined(WIN32)
55 string pp;
56 #endif
57 if (getcwd (path, PATH_MAX + 1) == NULL) {
58 FATAL_PERROR ("getcwd");
59 }
60
61 #if defined(WIN32)
62 for (pp = path; *pp; pp++) {
63 if (*pp == '\\')
64 *pp = '/';
65 #if defined (KPSE_COMPAT_API)
66 else if (IS_KANJI(pp))
67 pp++;
68 #endif
69 }
70 #endif
71
72 return xstrdup (path);
73
74 #else /* not HAVE_GETCWD || GETCWD_FORKS */
75 /* Our own implementation, written by Olaf. */
76 struct stat root_stat, cwd_stat;
77 string cwd_path = (string)xmalloc(2); /* In case we assign "/" below. */
78
79 *cwd_path = 0;
80
81 /* Find the inodes of the root and current directories. */
82 root_stat = xstat("/");
83 cwd_stat = xstat(".");
84
85 /* Go up the directory hierarchy until we get to root, prepending each
86 directory we pass through to `cwd_path'. */
87 while (!SAME_FILE_P(root_stat, cwd_stat)) {
88 struct dirent *e;
89 DIR *parent_dir;
90 boolean found = false;
91
92 xchdir("..");
93 parent_dir = xopendir(".");
94
95 /* Look through the parent directory for the entry with the same
96 inode, so we can get its name. */
97 while ((e = readdir (parent_dir)) != NULL && !found) {
98 struct stat test_stat;
99 test_stat = xlstat(e->d_name);
100
101 if (SAME_FILE_P(test_stat, cwd_stat)) {
102 /* We've found it. Prepend the pathname. */
103 string temp = cwd_path;
104 cwd_path = concat3("/", e->d_name, cwd_path);
105 free(temp);
106
107 /* Set up to test the next parent. */
108 cwd_stat = xstat(".");
109
110 /* Stop reading this directory. */
111 found = true;
112 }
113 }
114 if (!found)
115 FATAL2("No inode %d/device %d in parent directory",
116 cwd_stat.st_ino, cwd_stat.st_dev);
117
118 xclosedir(parent_dir);
119 }
120
121 /* If the current directory is the root, cwd_path will be the empty
122 string, and we will have not gone through the loop. */
123 if (*cwd_path == 0)
124 strcpy(cwd_path, "/");
125 else
126 /* Go back to where we were. */
127 xchdir(cwd_path);
128
129 #ifdef DOSISH
130 /* Prepend the drive letter to CWD_PATH, since this technique
131 never tells us what the drive is.
132
133 Note that on MS-DOS/MS-Windows, the branch that works around
134 missing `getwd' will probably only work for DJGPP (which does
135 have `getwd'), because only DJGPP reports meaningful
136 st_ino numbers. But someday, somebody might need this... */
137 {
138 char drive[3];
139 string temp = cwd_path;
140
141 /* Make the drive letter lower-case, unless it is beyond Z: (yes,
142 there ARE such drives, in case of Novell Netware on MS-DOS). */
143 drive[0] = root_stat.st_dev + (root_stat.st_dev < 26 ? 'a' : 'A');
144 drive[1] = ':';
145 drive[2] = '\0';
146
147 cwd_path = concat(drive, cwd_path);
148 free(temp);
149 }
150 #endif
151
152 return cwd_path;
153 #endif /* not HAVE_GETCWD || GETCWD_FORKS */
154 }