2001-02-26 Philip Blundell <philb@gnu.org>
[official-gcc.git] / libiberty / getcwd.c
blob344556392b31c0fab62f2c859392b0211d48581e
1 /* Emulate getcwd using getwd.
2 This function is in the public domain. */
4 /*
5 NAME
6 getcwd -- get absolute pathname for current working directory
8 SYNOPSIS
9 char *getcwd (char pathname[len], len)
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.
17 If pathname is a null pointer, getcwd() will obtain size bytes of
18 space using malloc.
20 BUGS
21 Emulated via the getwd() call, which is reasonable for most
22 systems that do not have getcwd().
26 #include "config.h"
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
30 #endif
31 #include <errno.h>
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
39 extern char *getwd ();
40 extern int errno;
42 #ifndef MAXPATHLEN
43 #define MAXPATHLEN 1024
44 #endif
46 char *
47 getcwd (buf, len)
48 char *buf;
49 int len;
51 char ourbuf[MAXPATHLEN];
52 char *result;
54 result = getwd (ourbuf);
55 if (result) {
56 if (strlen (ourbuf) >= len) {
57 errno = ERANGE;
58 return 0;
60 if (!buf) {
61 buf = (char*)malloc(len);
62 if (!buf) {
63 errno = ENOMEM;
64 return 0;
67 strcpy (buf, ourbuf);
69 return buf;