PR bootstrap/12276
[official-gcc.git] / libiberty / getcwd.c
bloba19d26796d5715c74c3a7b95b1e3570ab8b22a09
1 /* Emulate getcwd using getwd.
2 This function is in the public domain. */
4 /*
6 @deftypefn Supplemental char* getcwd (char *@var{pathname}, int @var{len})
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
12 @code{NULL} and @code{errno} is set. If @var{pathname} is a null pointer,
13 @code{getcwd} will obtain @var{len} bytes of space using
14 @code{malloc}.
16 @end deftypefn
20 #include "config.h"
22 #ifdef HAVE_SYS_PARAM_H
23 #include <sys/param.h>
24 #endif
25 #include <errno.h>
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
33 extern char *getwd ();
34 extern int errno;
36 #ifndef MAXPATHLEN
37 #define MAXPATHLEN 1024
38 #endif
40 char *
41 getcwd (buf, len)
42 char *buf;
43 size_t len;
45 char ourbuf[MAXPATHLEN];
46 char *result;
48 result = getwd (ourbuf);
49 if (result) {
50 if (strlen (ourbuf) >= len) {
51 errno = ERANGE;
52 return 0;
54 if (!buf) {
55 buf = (char*)malloc(len);
56 if (!buf) {
57 errno = ENOMEM;
58 return 0;
61 strcpy (buf, ourbuf);
63 return buf;