Debian Codegraph @

texk/kpathsea/win32lib.c

raw
1 /* win32lib.c: bits and pieces for win32 and msvc.
2
3 Copyright 2006, 2011-2017 Akira Kakuto.
4 Copyright 1996, 1997, 1998, 1999 Fabrice Popineau.
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/concatn.h>
21 #include <kpathsea/variable.h>
22 #include <kpathsea/c-stat.h>
23
24 static int is_include_space(const char *s)
25 {
26 char *p;
27 p = strchr(s, ' ');
28 if(p) return 1;
29 p = strchr(s, '\t');
30 if(p) return 1;
31 return 0;
32 }
33
34 double win32_floor (double x)
35 {
36 return floor (x);
37 }
38
39 FILE * win32_popen (const char *cmd, const char *fmode)
40 {
41 char mode[3];
42 char *p, *q;
43 const char *cmd2;
44 FILE *ret;
45
46 mode[0] = fmode[0];
47 mode[1] = 'b';
48 mode[2] = '\0';
49
50 if (is_include_space (cmd)) {
51 cmd2 = xmalloc (strlen (cmd) + 3);
52 q = (char *)cmd2;
53 p = (char *)cmd;
54 *q++= '\"';
55 while(*p)
56 *q++ = *p++;
57 *q++ = '\"';
58 *q = '\0';
59 ret = _popen (cmd2, mode);
60 free ((char *)cmd2);
61 return ret;
62 } else {
63 return _popen (cmd, mode);
64 }
65 }
66
67 int win32_pclose (FILE *f)
68 {
69 return _pclose (f);
70 }
71
72 /* large file support */
73
74 __int64
75 xftell64 (FILE *f, const char *filename)
76 {
77 #if _MSC_VER > 1200
78 __int64 where;
79 where = _ftelli64(f);
80 if (where < (__int64)0)
81 FATAL_PERROR(filename);
82 #else
83 __int64 where, filepos;
84 int fd;
85
86 fd = fileno(f);
87 if(f->_cnt < 0)
88 f->_cnt = 0;
89 if((filepos = _lseeki64(fd, (__int64)0, SEEK_CUR)) < (__int64)0) {
90 FATAL_PERROR(filename);
91 return (__int64)(-1);
92 }
93 if(filepos == (__int64)0)
94 where = (__int64)(f->_ptr - f->_base);
95 else
96 where = filepos - f->_cnt;
97 #endif
98 return where;
99 }
100
101 void
102 xfseek64 (FILE *f, __int64 offset, int wherefrom, const char *filename)
103 {
104 #if _MSC_VER > 1200
105 if (_fseeki64(f, offset, wherefrom) < (__int64)0)
106 FATAL_PERROR(filename);
107 #else
108 if(wherefrom == SEEK_CUR) {
109 offset += xftell64(f, filename);
110 wherefrom = SEEK_SET;
111 }
112 fflush(f);
113 if (_lseeki64(fileno(f), offset, wherefrom) < (__int64)0)
114 FATAL_PERROR(filename);
115 #endif
116 }
117
118
119 /* special TeXLive Ghostscript */
120
121 static int is_dir (char *buff)
122 {
123 struct stat stats;
124
125 return stat (buff, &stats) == 0 && S_ISDIR (stats.st_mode);
126 }
127
128 /*
129 TeXlive uses its own gs in
130 $SELFAUTOPARENT/tlpkg/tlgs
131 */
132 void texlive_gs_init(void)
133 {
134 char *nptr, *path;
135 char tlgsbindir[512];
136 char tlgslibdir[512];
137 char resourcedir[512];
138 nptr = kpse_var_value("TEXLIVE_WINDOWS_EXTERNAL_GS");
139 if (nptr == NULL || *nptr == '0' || *nptr == 'n' || *nptr == 'f') {
140 if (nptr)
141 free (nptr);
142 nptr = kpse_var_value("SELFAUTOPARENT");
143 if (nptr) {
144 strcpy(tlgsbindir, nptr);
145 strcat(tlgsbindir,"/tlpkg/tlgs");
146 strcpy(resourcedir, tlgsbindir);
147 strcat(resourcedir, "/Resource");
148 if(is_dir(tlgsbindir)) {
149 strcpy(tlgslibdir, tlgsbindir);
150 strcat(tlgslibdir, "/lib;");
151 if(is_dir(resourcedir)) {
152 strcat(tlgslibdir, tlgsbindir);
153 strcat(tlgslibdir, "/fonts;");
154 strcat(tlgslibdir, tlgsbindir);
155 strcat(tlgslibdir, "/Resource/Init;");
156 strcat(tlgslibdir, tlgsbindir);
157 strcat(tlgslibdir, "/Resource;");
158 strcat(tlgslibdir, tlgsbindir);
159 strcat(tlgslibdir, "/kanji");
160 } else {
161 strcat(tlgslibdir, tlgsbindir);
162 strcat(tlgslibdir, "/fonts");
163 }
164 strcat(tlgsbindir, "/bin;");
165 free(nptr);
166 for(nptr = tlgsbindir; *nptr; nptr++) {
167 if(*nptr == '/') *nptr = '\\';
168 }
169 nptr = getenv("PATH");
170 path = (char *)malloc(strlen(nptr) + strlen(tlgsbindir) + 6);
171 strcpy(path, tlgsbindir);
172 strcat(path, nptr);
173 xputenv("PATH", path);
174 xputenv("GS_LIB", tlgslibdir);
175 }
176 }
177 } else {
178 free (nptr);
179 }
180 }
181
182 /*
183 path name in char *input is changed into the long
184 name format and returned in char *buff.
185 return value: 0 if failed
186 1 if succeeded
187 */
188
189 int kpathsea_getlongpath(kpathsea kpse, char *buff, char *input, int len)
190 {
191 HANDLE hnd;
192 WIN32_FIND_DATA ffd;
193 int cnt = 0;
194 char *p, *q, *r;
195
196 buff[0] = '\0';
197 /*
198 temporarily change directory separators into back slashs
199 */
200 for(p = input; *p; p++) {
201 if(*p == '/')
202 *p = '\\';
203 }
204
205 p = q = input;
206 r = buff;
207
208 /*
209 UNC name
210 */
211 if(q[0] == '\\' && q[1] == '\\') {
212 cnt += 2;
213 if(cnt > len) return 0;
214 buff[0] = '/';
215 buff[1] = '/';
216 p += 2;
217 r += 2;
218 while(*p != '\\' && *p) {
219 if (kpathsea_IS_KANJI(kpse, p)) {
220 cnt++;
221 if(cnt > len) return 0;
222 *r++ = *p++;
223 }
224 cnt++;
225 if(cnt > len) return 0;
226 *r++ = *p++;
227 }
228 cnt++;
229 if(cnt > len) return 0;
230 *r++ = '/';
231 if(*p) p++;
232 while(*p != '\\' && *p) {
233 if (kpathsea_IS_KANJI(kpse, p)) {
234 cnt++;
235 if(cnt > len) return 0;
236 *r++ = *p++;
237 }
238 cnt++;
239 if(cnt > len) return 0;
240 *r++ = *p++;
241 }
242 cnt++;
243 if(cnt > len) return 0;
244 *r++ = '/';
245 *r= '\0';
246 if(*p) p++;
247 /*
248 drive name
249 */
250 } else if(isalpha(q[0]) && q[1] == ':' && q[2] == '\\') {
251 *r++ = q[0];
252 *r++ = ':';
253 *r++ = '/';
254 *r = '\0';
255 p += 3;
256 cnt += 3;
257 if(cnt > len) return 0;
258 }
259
260 for( ; *p; p++) {
261 if(kpathsea_IS_KANJI(kpse, p)) {
262 p++;
263 continue;
264 }
265 if(*p == '\\') {
266 *p = '\0';
267 if((*(p-2) == '\\' || p-1 == q) && *(p-1) == '.') {
268 cnt += 2;
269 if(cnt > len) return 0;
270 strcat(buff, "./");
271 } else if((*(p-3) == '\\' || p-2 == q) && *(p-2) == '.' && *(p-1) == '.') {
272 cnt += 3;
273 if(cnt > len) return 0;
274 strcat(buff, "../");
275 } else {
276 if((hnd = FindFirstFile(q, &ffd)) == INVALID_HANDLE_VALUE) {
277 return 0;
278 }
279 FindClose(hnd);
280 cnt += strlen(ffd.cFileName);
281 cnt++;
282 if(cnt > len) return 0;
283 strcat(buff, ffd.cFileName);
284 strcat(buff, "/");
285 }
286 *p = '\\';
287 }
288 }
289
290 /*
291 file itself
292 */
293 if((hnd = FindFirstFile(q, &ffd)) == INVALID_HANDLE_VALUE) {
294 return 0;
295 }
296 FindClose(hnd);
297 cnt += strlen(ffd.cFileName);
298 if(cnt > len) return 0;
299 strcat(buff, ffd.cFileName);
300 return 1;
301 }
302
303 /* user info */
304
305 /* Adapted for XEmacs by David Hobley <[email protected]> */
306 /* Sync'ed with Emacs 19.34.6 by Marc Paquette <[email protected]> */
307 /* Adapted to fpTeX 0.4 by Fabrice Popineau <[email protected]> */
308
309 char * kpathsea_get_home_directory(kpathsea kpse)
310 {
311 char *p;
312 char *home = getenv("HOME");
313 if(!home)
314 home = getenv("USERPROFILE");
315 if(home) {
316 home = xstrdup(home);
317 for(p = home; *p; p++) {
318 if(kpathsea_IS_KANJI(kpse, p)) {
319 p++;
320 continue;
321 }
322 if(*p == '\\')
323 *p = '/';
324 }
325 }
326 return home;
327 }
328
329 int
330 kpathsea_getuid (kpathsea kpse)
331 {
332 return kpse->the_passwd.pw_uid;
333 }
334
335 int
336 kpathsea_getgid (kpathsea kpse)
337 {
338 return kpse->the_passwd.pw_gid;
339 }
340
341 struct passwd *
342 kpathsea_getpwuid (kpathsea kpse, int uid)
343 {
344 if (uid == kpse->the_passwd.pw_uid)
345 return &kpse->the_passwd;
346 return NULL;
347 }
348
349 struct passwd *
350 kpathsea_getpwnam (kpathsea kpse, char *name)
351 {
352 struct passwd *pw;
353
354 pw = kpathsea_getpwuid (kpse, kpathsea_getuid (kpse));
355 if (!pw)
356 return pw;
357
358 if (stricmp (name, pw->pw_name))
359 return NULL;
360
361 return pw;
362 }
363
364 void
365 kpathsea_init_user_info (kpathsea kpse)
366 {
367 char *home;
368 DWORD nSize = 256;
369
370 if (!GetUserName (kpse->the_passwd.pw_name, &nSize))
371 strcpy (kpse->the_passwd.pw_name, "unknown");
372 kpse->the_passwd.pw_uid = 123;
373 kpse->the_passwd.pw_gid = 123;
374
375 /* Ensure HOME and SHELL are defined. */
376
377 home = kpathsea_get_home_directory(kpse);
378 if (home) {
379 putenv(concat("HOME=", home));
380 }
381 else {
382 putenv ("HOME=c:/");
383 }
384
385 if (getenv ("SHELL") == NULL)
386 putenv ((GetVersion () & 0x80000000) ? "SHELL=command" : "SHELL=cmd");
387
388 {
389 /* If TEXMFTEMP is defined, then use it as the TEMP and TMP variables. */
390 char *p;
391 if ((p = getenv("TEXMFTEMP")) != NULL) {
392 putenv(concat("TEMP=", p));
393 putenv(concat("TMP=", p));
394 }
395 }
396
397 /* Set dir and shell from environment variables. */
398 strcpy (kpse->the_passwd.pw_dir, kpathsea_get_home_directory(kpse));
399 strcpy (kpse->the_passwd.pw_shell, getenv ("SHELL"));
400 }
401
402 /* win32_system */
403 int win32_system(const char *cmd)
404 {
405 const char *p;
406 char *q;
407 char *av[4];
408 int len, ret;
409 int spacep = 0;
410
411 if(cmd == NULL)
412 return 1;
413
414 av[0] = xstrdup("cmd.exe");
415 av[1] = xstrdup("/c");
416
417 len = strlen(cmd) + 3;
418 spacep = is_include_space(cmd);
419 av[2] = malloc(len);
420 q = av[2];
421 if(spacep)
422 *q++ = '"';
423 for(p = cmd; *p; p++, q++) {
424 if(*p == '\'')
425 *q = '"';
426 else
427 *q = *p;
428 }
429 if(spacep)
430 *q++ = '"';
431 *q = '\0';
432 av[3] = NULL;
433 ret = _spawnvp(_P_WAIT, av[0], av);
434 free(av[0]);
435 free(av[1]);
436 free(av[2]);
437 return ret;
438 }
439
440 #if defined (KPSE_COMPAT_API)
441 int getlongpath(char *buff, char *input, int len)
442 {
443 return kpathsea_getlongpath(kpse_def, buff, input, len);
444 }
445
446 char * get_home_directory(void)
447 {
448 return kpathsea_get_home_directory(kpse_def);
449 }
450
451 int
452 getuid (void)
453 {
454 return kpathsea_getuid(kpse_def);
455 }
456
457 int
458 geteuid (void)
459 {
460 /* I could imagine arguing for checking to see whether the user is
461 in the Administrators group and returning a UID of 0 for that
462 case, but I don't know how wise that would be in the long run. */
463 return getuid ();
464 }
465
466 int
467 getgid (void)
468 {
469 return kpathsea_getgid (kpse_def);
470 }
471
472 int
473 getegid (void)
474 {
475 return getgid ();
476 }
477
478 struct passwd *
479 getpwuid (int uid)
480 {
481 return kpathsea_getpwuid (kpse_def, uid);
482 }
483
484 struct passwd *
485 getpwnam (char *name)
486 {
487 return kpathsea_getpwnam (kpse_def, name);
488 }
489
490 #endif /* KPSE_COMPAT_API */