Use dos.library/CreateNewProc() instead of alib/CreateNewProcTags()
[tangerine.git] / compiler / clib / getcwd.c
blobf17074c183a232be223ea6c1d8fd13784b72c3ad
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 "__upath.h"
14 #include <proto/exec.h>
15 #include <proto/dos.h>
17 /*****************************************************************************
19 NAME */
20 #include <unistd.h>
22 char *getcwd (
24 /* SYNOPSIS */
25 char *buf,
26 size_t size)
28 /* FUNCTION
29 Get the current working directory.
31 INPUTS
32 buf - Pointer of the buffer where the path is to be stored
33 size - The size of the above buffer
35 RESULT
36 Copies the absolute pathname of the current working directory
37 to the buffer. If the pathname is longer than the buffer
38 (with lenght "size") NULL is returned and errno set to ERANGE.
39 Otherwise the pointer to the buffer is returned.
41 NOTES
42 If buf is NULL this function will allocate the buffer itself
43 using malloc() and the specified size "size". If size is
44 0, too, the buffer is allocated to hold the whole path.
45 It is possible and recommended to free() this buffer yourself!
46 The path returned does not have to be literally the same as the
47 one given to chdir. See NOTES from chdir for more explanation.
49 EXAMPLE
51 BUGS
53 SEE ALSO
54 chdir
56 INTERNALS
58 ******************************************************************************/
60 char pathname[FILENAME_MAX];
61 char *tpath;
62 BPTR lock;
64 lock = CurrentDir(NULL);
65 CurrentDir(lock);
66 if (NameFromLock (lock, pathname, FILENAME_MAX) == 0)
68 errno = IoErr2errno (IoErr ());
69 return NULL;
72 tpath = __path_a2u(pathname);
73 strcpy(pathname, tpath);
75 if (buf != NULL)
77 if (strlen(pathname) < size)
79 strcpy (buf, pathname);
81 else
83 errno = ERANGE;
84 return NULL;
87 else
89 int len;
90 char *newbuf;
92 len = strlen(pathname);
94 if (size == 0)
96 size = len+1;
99 if (len < size)
101 newbuf = (char *)malloc (size*sizeof(char));
102 strcpy (newbuf, pathname);
103 return newbuf;
105 else
107 errno = ERANGE;
108 return NULL;
112 return buf;
114 } /* getcwd */