file make-gas.com was initially added on branch binutils-2_10-branch.
[binutils.git] / libiberty / getcwd.c
blob47b1c1eec31e1c680bbb5deb42efb07e84f6a3f6
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>
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 int 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;