Include alloca-conf.h
[deliverable/binutils-gdb.git] / libiberty / getcwd.c
CommitLineData
252b5132
RH
1/* Emulate getcwd using getwd.
2 This function is in the public domain. */
3
4/*
5NAME
6 getcwd -- get absolute pathname for current working directory
7
8SYNOPSIS
9 char *getcwd (char pathname[len], len)
10
11DESCRIPTION
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
e2eaf477
ILT
17 If pathname is a null pointer, getcwd() will obtain size bytes of
18 space using malloc.
19
252b5132
RH
20BUGS
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>
443519c1
JL
32#ifdef HAVE_STRING_H
33#include <string.h>
34#endif
35#ifdef HAVE_STDLIB_H
36#include <stdlib.h>
37#endif
252b5132
RH
38
39extern char *getwd ();
40extern int errno;
41
42#ifndef MAXPATHLEN
43#define MAXPATHLEN 1024
44#endif
45
46char *
47getcwd (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 }
e2eaf477
ILT
60 if (!buf) {
61 buf = (char*)malloc(len);
62 if (!buf) {
63 errno = ENOMEM;
64 return 0;
65 }
66 }
252b5132
RH
67 strcpy (buf, ourbuf);
68 }
69 return buf;
70}
This page took 0.04246 seconds and 4 git commands to generate.