1169695Skan/* Emulate getcwd using getwd.
2169695Skan   This function is in the public domain. */
3169695Skan
4169695Skan/*
5169695Skan
6169695Skan@deftypefn Supplemental char* getcwd (char *@var{pathname}, int @var{len})
7169695Skan
8169695SkanCopy the absolute pathname for the current working directory into
9169695Skan@var{pathname}, which is assumed to point to a buffer of at least
10169695Skan@var{len} bytes, and return a pointer to the buffer.  If the current
11169695Skandirectory's path doesn't fit in @var{len} characters, the result is
12169695Skan@code{NULL} and @code{errno} is set.  If @var{pathname} is a null pointer,
13169695Skan@code{getcwd} will obtain @var{len} bytes of space using
14169695Skan@code{malloc}.
15169695Skan
16169695Skan@end deftypefn
17169695Skan
18169695Skan*/
19169695Skan
20169695Skan#include "config.h"
21169695Skan
22169695Skan#ifdef HAVE_SYS_PARAM_H
23169695Skan#include <sys/param.h>
24169695Skan#endif
25169695Skan#include <errno.h>
26169695Skan#ifdef HAVE_STRING_H
27169695Skan#include <string.h>
28169695Skan#endif
29169695Skan#ifdef HAVE_STDLIB_H
30169695Skan#include <stdlib.h>
31169695Skan#endif
32169695Skan
33169695Skanextern char *getwd ();
34169695Skanextern int errno;
35169695Skan
36169695Skan#ifndef MAXPATHLEN
37169695Skan#define MAXPATHLEN 1024
38169695Skan#endif
39169695Skan
40169695Skanchar *
41169695Skangetcwd (char *buf, size_t len)
42169695Skan{
43169695Skan  char ourbuf[MAXPATHLEN];
44169695Skan  char *result;
45169695Skan
46169695Skan  result = getwd (ourbuf);
47169695Skan  if (result) {
48169695Skan    if (strlen (ourbuf) >= len) {
49169695Skan      errno = ERANGE;
50169695Skan      return 0;
51169695Skan    }
52169695Skan    if (!buf) {
53169695Skan       buf = (char*)malloc(len);
54169695Skan       if (!buf) {
55169695Skan           errno = ENOMEM;
56169695Skan	   return 0;
57169695Skan       }
58169695Skan    }
59169695Skan    strcpy (buf, ourbuf);
60169695Skan  }
61169695Skan  return buf;
62169695Skan}
63