clean up bcm bases and add primecell peripheral defines
[AROS.git] / compiler / clib / getcwd.c
blob68a9c558f67b9a23b8da0320640b747be639d70f
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2008 function getcwd().
6 */
8 #include <stdlib.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "__upath.h"
13 #include <proto/exec.h>
14 #include <proto/dos.h>
16 /*****************************************************************************
18 NAME */
19 #include <unistd.h>
21 char *getcwd (
23 /* SYNOPSIS */
24 char *buf,
25 size_t size)
27 /* FUNCTION
28 Get the current working directory.
30 INPUTS
31 buf - Pointer of the buffer where the path is to be stored
32 size - The size of the above buffer
34 RESULT
35 Copies the absolute pathname of the current working directory
36 to the buffer. If the pathname is longer than the buffer
37 (with lenght "size") NULL is returned and errno set to ERANGE.
38 Otherwise the pointer to the buffer is returned.
40 NOTES
41 If buf is NULL this function will allocate the buffer itself
42 using malloc() and the specified size "size". If size is
43 0, too, the buffer is allocated to hold the whole path.
44 It is possible and recommended to free() this buffer yourself!
45 The path returned does not have to be literally the same as the
46 one given to chdir. See NOTES from chdir for more explanation.
48 EXAMPLE
50 BUGS
52 SEE ALSO
53 chdir()
55 INTERNALS
57 ******************************************************************************/
59 char pathname[FILENAME_MAX];
60 const char *tpath;
61 BPTR lock;
63 lock = CurrentDir(BNULL);
64 CurrentDir(lock);
65 if (NameFromLock (lock, pathname, FILENAME_MAX) == 0)
67 errno = __arosc_ioerr2errno (IoErr ());
68 return NULL;
71 tpath = __path_a2u(pathname);
72 strcpy(pathname, tpath);
74 if (buf != NULL)
76 if (strlen(pathname) < size)
78 strcpy (buf, pathname);
80 else
82 errno = ERANGE;
83 return NULL;
86 else
88 int len;
89 char *newbuf;
91 len = strlen(pathname);
93 if (size == 0)
95 size = len+1;
98 if (len < size)
100 newbuf = (char *)malloc (size*sizeof(char));
101 strcpy (newbuf, pathname);
102 return newbuf;
104 else
106 errno = ERANGE;
107 return NULL;
111 return buf;
113 } /* getcwd */