aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEli Zaretskii2005-09-10 11:29:15 +0000
committerEli Zaretskii2005-09-10 11:29:15 +0000
commitf78f1a83df3ed97fd59ec98143f156af3cfd4491 (patch)
treee8d9c15e42058630811f9f9ab4e95e33fad985c1 /src
parent63e31cc915a599711f2abbd544456cf41e7237a0 (diff)
downloademacs-f78f1a83df3ed97fd59ec98143f156af3cfd4491.tar.gz
emacs-f78f1a83df3ed97fd59ec98143f156af3cfd4491.zip
(get_current_dir_name) [!HAVE_CURRENT_DIR_NAME]: New function.
Diffstat (limited to 'src')
-rw-r--r--src/sysdep.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/sysdep.c b/src/sysdep.c
index e63ee904f14..0fbf37e537e 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -258,6 +258,81 @@ void hft_reset ();
258 258
259SIGMASKTYPE sigprocmask_set; 259SIGMASKTYPE sigprocmask_set;
260 260
261
262#ifndef HAVE_CURRENT_DIR_NAME
263
264/* Return the current working directory. Returns NULL on errors.
265 Any other returned value must be freed with free. This is used
266 only when get_current_dir_name is not defined on the system. */
267char*
268get_current_dir_name ()
269{
270 char *buf;
271 char *pwd;
272 struct stat dotstat, pwdstat;
273 /* If PWD is accurate, use it instead of calling getwd. PWD is
274 sometimes a nicer name, and using it may avoid a fatal error if a
275 parent directory is searchable but not readable. */
276 if ((pwd = getenv ("PWD")) != 0
277 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
278 && stat (pwd, &pwdstat) == 0
279 && stat (".", &dotstat) == 0
280 && dotstat.st_ino == pwdstat.st_ino
281 && dotstat.st_dev == pwdstat.st_dev
282#ifdef MAXPATHLEN
283 && strlen (pwd) < MAXPATHLEN
284#endif
285 )
286 {
287 buf = (char *) malloc (strlen (pwd) + 1);
288 if (!buf)
289 return NULL;
290 strcpy (buf, pwd);
291 }
292#ifdef HAVE_GETCWD
293 else
294 {
295 size_t buf_size = 1024;
296 buf = (char *) malloc (buf_size);
297 if (!buf)
298 return NULL;
299 for (;;)
300 {
301 if (getcwd (buf, buf_size) == buf)
302 break;
303 if (errno != ERANGE)
304 {
305 int tmp_errno = errno;
306 free (buf);
307 errno = tmp_errno;
308 return NULL;
309 }
310 buf_size *= 2;
311 buf = (char *) realloc (buf, buf_size);
312 if (!buf)
313 return NULL;
314 }
315 }
316#else
317 else
318 {
319 /* We need MAXPATHLEN here. */
320 buf = (char *) malloc (MAXPATHLEN + 1);
321 if (!buf)
322 return NULL;
323 if (getwd (buf) == NULL)
324 {
325 int tmp_errno = errno;
326 free (buf);
327 errno = tmp_errno;
328 return NULL;
329 }
330 }
331#endif
332 return buf;
333}
334#endif
335
261 336
262/* Specify a different file descriptor for further input operations. */ 337/* Specify a different file descriptor for further input operations. */
263 338