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