| 1 | /* Emulate getcwd using getwd. |
| 2 | This function is in the public domain. */ |
| 3 | |
| 4 | /* |
| 5 | NAME |
| 6 | getcwd -- get absolute pathname for current working directory |
| 7 | |
| 8 | SYNOPSIS |
| 9 | char *getcwd (char pathname[len], len) |
| 10 | |
| 11 | DESCRIPTION |
| 12 | Copy the absolute pathname for the current working directory into |
| 13 | the supplied buffer and return a pointer to the buffer. If the |
| 14 | current directory's path doesn't fit in LEN characters, the result |
| 15 | is NULL and errno is set. |
| 16 | |
| 17 | If pathname is a null pointer, getcwd() will obtain size bytes of |
| 18 | space using malloc. |
| 19 | |
| 20 | BUGS |
| 21 | Emulated via the getwd() call, which is reasonable for most |
| 22 | systems that do not have getcwd(). |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #ifdef HAVE_SYS_PARAM_H |
| 29 | #include <sys/param.h> |
| 30 | #endif |
| 31 | #include <errno.h> |
| 32 | |
| 33 | extern char *getwd (); |
| 34 | extern int errno; |
| 35 | |
| 36 | #ifndef MAXPATHLEN |
| 37 | #define MAXPATHLEN 1024 |
| 38 | #endif |
| 39 | |
| 40 | char * |
| 41 | getcwd (buf, len) |
| 42 | char *buf; |
| 43 | int len; |
| 44 | { |
| 45 | char ourbuf[MAXPATHLEN]; |
| 46 | char *result; |
| 47 | |
| 48 | result = getwd (ourbuf); |
| 49 | if (result) { |
| 50 | if (strlen (ourbuf) >= len) { |
| 51 | errno = ERANGE; |
| 52 | return 0; |
| 53 | } |
| 54 | if (!buf) { |
| 55 | buf = (char*)malloc(len); |
| 56 | if (!buf) { |
| 57 | errno = ENOMEM; |
| 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | strcpy (buf, ourbuf); |
| 62 | } |
| 63 | return buf; |
| 64 | } |