diff options
Diffstat (limited to 'src/sysdep.c')
| -rw-r--r-- | src/sysdep.c | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/src/sysdep.c b/src/sysdep.c index e63ee904f14..c0ff47e3072 100644 --- a/src/sysdep.c +++ b/src/sysdep.c | |||
| @@ -47,7 +47,6 @@ extern void srandom P_ ((unsigned int)); | |||
| 47 | #endif | 47 | #endif |
| 48 | 48 | ||
| 49 | #include "blockinput.h" | 49 | #include "blockinput.h" |
| 50 | #undef NULL | ||
| 51 | 50 | ||
| 52 | #ifdef MAC_OS8 | 51 | #ifdef MAC_OS8 |
| 53 | /* It is essential to include stdlib.h so that this file picks up | 52 | /* It is essential to include stdlib.h so that this file picks up |
| @@ -187,6 +186,7 @@ extern int quit_char; | |||
| 187 | #define _P_WAIT 0 | 186 | #define _P_WAIT 0 |
| 188 | int _CRTAPI1 _spawnlp (int, const char *, const char *, ...); | 187 | int _CRTAPI1 _spawnlp (int, const char *, const char *, ...); |
| 189 | int _CRTAPI1 _getpid (void); | 188 | int _CRTAPI1 _getpid (void); |
| 189 | extern char *getwd (char *); | ||
| 190 | #endif | 190 | #endif |
| 191 | 191 | ||
| 192 | #ifdef NONSYSTEM_DIR_LIBRARY | 192 | #ifdef NONSYSTEM_DIR_LIBRARY |
| @@ -258,6 +258,81 @@ void hft_reset (); | |||
| 258 | 258 | ||
| 259 | SIGMASKTYPE sigprocmask_set; | 259 | SIGMASKTYPE sigprocmask_set; |
| 260 | 260 | ||
| 261 | |||
| 262 | #ifndef HAVE_GET_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. */ | ||
| 267 | char* | ||
| 268 | get_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 | ||