mlib update: new isnan()/isnanf() implementation
[tangerine.git] / compiler / clib / getcwd.c
blobfa85609ec5b7ba20e920528dbfcb8b91e9e9dea9
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function getcwd().
6 */
8 #include <stdlib.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "__errno.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 BPTR lock;
62 lock = CurrentDir(NULL);
63 CurrentDir(lock);
64 if (NameFromLock (lock, pathname, FILENAME_MAX) == 0)
66 errno = IoErr2errno (IoErr ());
67 return NULL;
70 if (buf != NULL)
72 if (strlen(pathname) < size)
74 strcpy (buf, pathname);
76 else
78 errno = ERANGE;
79 return NULL;
82 else
84 int len;
85 char *newbuf;
87 len = strlen(pathname);
89 if (size == 0)
91 size = len+1;
94 if (len < size)
96 newbuf = (char *)malloc (size*sizeof(char));
97 strcpy (newbuf, pathname);
98 return newbuf;
100 else
102 errno = ERANGE;
103 return NULL;
107 return buf;
109 } /* getcwd */