Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* Emulate getcwd using getwd. |
2 | This function is in the public domain. */ | |
3 | ||
4 | /* | |
252b5132 | 5 | |
99b58139 | 6 | @deftypefn Supplemental char* getcwd (char *@var{pathname}, int @var{len}) |
252b5132 | 7 | |
39423523 DD |
8 | Copy the absolute pathname for the current working directory into |
9 | @var{pathname}, which is assumed to point to a buffer of at least | |
10 | @var{len} bytes, and return a pointer to the buffer. If the current | |
11 | directory's path doesn't fit in @var{len} characters, the result is | |
99b58139 | 12 | @code{NULL} and @code{errno} is set. If @var{pathname} is a null pointer, |
39423523 DD |
13 | @code{getcwd} will obtain @var{len} bytes of space using |
14 | @code{malloc}. | |
252b5132 | 15 | |
39423523 | 16 | @end deftypefn |
252b5132 RH |
17 | |
18 | */ | |
19 | ||
20 | #include "config.h" | |
21 | ||
22 | #ifdef HAVE_SYS_PARAM_H | |
23 | #include <sys/param.h> | |
24 | #endif | |
25 | #include <errno.h> | |
443519c1 JL |
26 | #ifdef HAVE_STRING_H |
27 | #include <string.h> | |
28 | #endif | |
29 | #ifdef HAVE_STDLIB_H | |
30 | #include <stdlib.h> | |
31 | #endif | |
252b5132 RH |
32 | |
33 | extern char *getwd (); | |
34 | extern int errno; | |
35 | ||
36 | #ifndef MAXPATHLEN | |
37 | #define MAXPATHLEN 1024 | |
38 | #endif | |
39 | ||
40 | char * | |
49b1fae4 | 41 | getcwd (char *buf, size_t len) |
252b5132 RH |
42 | { |
43 | char ourbuf[MAXPATHLEN]; | |
44 | char *result; | |
45 | ||
46 | result = getwd (ourbuf); | |
47 | if (result) { | |
48 | if (strlen (ourbuf) >= len) { | |
49 | errno = ERANGE; | |
50 | return 0; | |
51 | } | |
e2eaf477 ILT |
52 | if (!buf) { |
53 | buf = (char*)malloc(len); | |
54 | if (!buf) { | |
55 | errno = ENOMEM; | |
56 | return 0; | |
57 | } | |
58 | } | |
252b5132 RH |
59 | strcpy (buf, ourbuf); |
60 | } | |
61 | return buf; | |
62 | } |