Debian Codegraph @

texk/kpathsea/readable.c

raw
1 /* readable.c: check if a filename is a readable non-directory file.
2
3 Copyright 1993, 1995, 1996, 2008, 2011, 2012, 2016, 2018 Karl Berry.
4 Copyright 1998, 1999, 2000, 2001, 2005 Olaf Weber.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
18
19 #include <kpathsea/config.h>
20 #include <kpathsea/c-namemx.h>
21 #include <kpathsea/c-pathch.h>
22 #include <kpathsea/c-pathmx.h>
23 #include <kpathsea/c-stat.h>
24 #include <kpathsea/pathsearch.h>
25 #include <kpathsea/readable.h>
26 #include <kpathsea/tex-hush.h>
27
28
29 /* If access can read FN, run stat (assigning to stat buffer ST) and
30 check that fn is not a directory. Don't check for just being a
31 regular file, as it is potentially useful to read fifo's or some
32 kinds of devices. */
33
34 #ifdef __DJGPP__
35 /* `stat' is way too expensive for such a simple job. */
36 #define READABLE(kpse, fn, st) \
37 (access ((fn), R_OK) == 0 && access ((fn), D_OK) == -1)
38 #elif defined (WIN32)
39 /* st must be an unsigned int under Windows */
40 static boolean
41 READABLE(kpathsea kpse, const_string fn, unsigned int st)
42 {
43 wchar_t *fnw;
44 unsigned char *fnn;
45 unsigned char *p = NULL;
46 size_t len = strlen(fn);
47
48 fnn = xmalloc(len + 10);
49 /*
50 Support very long input path name, longer than _MAX_PATH for
51 Windows, if it really exists and input name is given in
52 full-absolute path in a command line.
53 /../, /./, \..\, \.\ should be excluded (2020/06/06)
54 More than one adjacent directory separators should be
55 excluded. (2020/10/24)
56 The "nul" device should be excluded. (2021/04/07).
57 */
58 if (stricmp(fn + len - 3, "nul") == 0)
59 p = (unsigned char *)fn;
60 else if (stricmp(fn + len - 4, "nul:") == 0)
61 p = (unsigned char *)fn;
62 else if (stricmp(fn + len - 7, "nul.tex") == 0)
63 p = (unsigned char *)fn;
64 else if (stricmp(fn + len - 8, "nul:.tex") == 0)
65 p = (unsigned char *)fn;
66
67 if (!p) {
68 p = strstr(fn, ".\\");
69 }
70 if (!p) {
71 p = strstr(fn, "./");
72 }
73 if (!p && len > 2) {
74 p = strstr(fn + 2, "//");
75 }
76 if (!p && len > 2) {
77 p = strstr(fn + 2, "\\\\");
78 }
79 if (!p && len > 2) {
80 p = strstr(fn + 2, "\\/");
81 }
82 if (!p && len > 2) {
83 p = strstr(fn + 2, "/\\");
84 }
85 if (!p && len > 2 && ((fn[0] == '/' && fn[1] == '/') ||
86 (fn[0] == '\\' && fn[1] == '\\' && fn[2] != '?'))) {
87 fn += 2;
88 strcpy (fnn, "\\\\?\\UNC\\");
89 strcat (fnn, fn);
90 } else if (!p && len > 2 && fn[1] == ':') {
91 strcpy (fnn, "\\\\?\\");
92 strcat (fnn, fn);
93 } else {
94 strcpy (fnn, fn);
95 }
96
97 for (p = fnn; *p; p++) {
98 if (*p == '/')
99 *p = '\\';
100 }
101
102 fnw = get_wstring_from_mbstring(kpse->File_system_codepage, fnn, fnw=NULL);
103 if ((st = GetFileAttributesW(fnw)) != 0xFFFFFFFF) {
104 /* succeeded */
105 errno = 0;
106 } else {
107 switch(GetLastError()) {
108 case ERROR_BUFFER_OVERFLOW:
109 errno = ENAMETOOLONG;
110 break;
111 case ERROR_ACCESS_DENIED:
112 errno = EACCES;
113 break;
114 default :
115 errno = EIO; /* meaningless, will make ret=NULL later */
116 break;
117 }
118 }
119 free (fnn);
120 if (fnw)
121 free (fnw);
122 return ((st != 0xFFFFFFFF) && !(st & FILE_ATTRIBUTE_DIRECTORY));
123 }
124 #else /* not __DJGPP__ and not WIN32 */
125 #define READABLE(kpse, fn, st) \
126 (access((fn), R_OK) == 0 && stat((fn), &(st)) == 0 && !S_ISDIR ((st).st_mode))
127 #endif
128
129
130 /* POSIX invented the brain-damage of not necessarily truncating
131 filename components; the system's behavior is defined by the value of
132 the symbol _POSIX_NO_TRUNC, but it can't be changed. */
133
134 string
135 kpathsea_readable_file (kpathsea kpse, string name)
136 {
137 #ifdef WIN32
138 unsigned int st = 0;
139 #else /* ! WIN32 */
140 struct stat st;
141 #endif
142
143 kpathsea_normalize_path (kpse, name);
144 if (READABLE (kpse, name, st)) {
145 return name;
146
147 #ifdef ENAMETOOLONG
148 } else if (errno == ENAMETOOLONG) {
149 /* Truncate any too-long components in NAME. */
150 unsigned c_len = 0; /* Length of current component. */
151 char *s = name; /* Position in current component. */
152 char *t = name; /* End of allowed length. */
153
154 for (; *s; s++) {
155 if (c_len <= NAME_MAX) {
156 t = s;
157 }
158 #if defined(WIN32)
159 if (kpathsea_IS_KANJI (kpse, s)) {
160 s++;
161 c_len += 2;
162 continue;
163 }
164 #endif /* WIN32 */
165 if (IS_DIR_SEP (*s) || IS_DEVICE_SEP (*s)) {
166 if (c_len > NAME_MAX) {
167 /* Truncate if past the max for a component. */
168 memmove (t, s, strlen (s) + 1);
169 s = t;
170 }
171 /* At a directory delimiter, reset component length. */
172 c_len = 0;
173 } else {
174 c_len++;
175 }
176 }
177 if (c_len > NAME_MAX) {
178 /* Truncate if past the max for last component. */
179 *t = 0;
180 }
181
182 /* Perhaps some other error will occur with the truncated name, so
183 let's call access again. */
184 if (READABLE (kpse, name, st)) /* Success. */
185 return name;
186 #endif /* ENAMETOOLONG */
187
188 } else { /* Some other error. */
189 if (errno == EACCES) { /* Maybe warn them if permissions are bad. */
190 if (!kpathsea_tex_hush (kpse, "readable")) {
191 perror (name);
192 }
193 }
194 }
195
196 return NULL;
197 }
198
199
200 #if defined (KPSE_COMPAT_API)
201 string
202 kpse_readable_file (string name)
203 {
204 return kpathsea_readable_file (kpse_def, name);
205 }
206 #endif