OSDN Git Service

* Makefile.in (local-distclean): Remove leftover built files.
[pf3gnuchains/gcc-fork.git] / libiberty / getcwd.c
1 /* Emulate getcwd using getwd.
2    This function is in the public domain. */
3
4 /*
5 NAME
6         getcwd -- get absolute pathname for current working directory
7
8 SYNOPSIS
9         char *getcwd (char pathname[len], len)
10
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.
16
17         If pathname is a null pointer, getcwd() will obtain size bytes of
18         space using malloc.
19
20 BUGS
21         Emulated via the getwd() call, which is reasonable for most
22         systems that do not have getcwd().
23
24 */
25
26 #include "config.h"
27
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
38
39 extern char *getwd ();
40 extern int errno;
41
42 #ifndef MAXPATHLEN
43 #define MAXPATHLEN 1024
44 #endif
45
46 char *
47 getcwd (buf, len)
48   char *buf;
49   int len;
50 {
51   char ourbuf[MAXPATHLEN];
52   char *result;
53
54   result = getwd (ourbuf);
55   if (result) {
56     if (strlen (ourbuf) >= len) {
57       errno = ERANGE;
58       return 0;
59     }
60     if (!buf) {
61        buf = (char*)malloc(len);
62        if (!buf) {
63            errno = ENOMEM;
64            return 0;
65        }
66     }
67     strcpy (buf, ourbuf);
68   }
69   return buf;
70 }