free the correctly allocated size
[AROS.git] / compiler / posixc / fchdir.c
blobe2c081f179a844bc5b43427f3cca845447083a43
1 /*
2 Copyright © 2008-2013, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "__posixc_intbase.h"
7 #include "__fdesc.h"
9 #include <exec/types.h>
10 #include <dos/dos.h>
11 #include <proto/dos.h>
12 #include <aros/symbolsets.h>
13 #include <errno.h>
14 #include "__upath.h"
15 #include <aros/debug.h>
17 /*****************************************************************************
19 NAME */
20 #include <unistd.h>
22 int fchdir(
24 /* SYNOPSIS */
25 int fd )
27 /* FUNCTION
28 Change the current working directory to the directory given as an open
29 file descriptor.
31 INPUTS
32 fd - File descriptor of the directory to change to.
34 RESULT
35 If the current directory was changed successfully, zero is returned.
36 Otherwise, -1 is returned and errno set apropriately.
38 NOTES
39 At program exit, the current working directory will be changed back
40 to the one that was current when the program first started. If you
41 do not desire this behaviour, use dos.library/CurrentDir() instead.
43 EXAMPLE
45 BUGS
47 SEE ALSO
49 INTERNALS
51 ******************************************************************************/
53 struct PosixCIntBase *PosixCBase =
54 (struct PosixCIntBase *)__aros_getbase_PosixCBase();
55 BPTR oldlock = BNULL;
56 BPTR newlock = BNULL;
57 BPTR handle = BNULL;
59 fdesc *fdc = __getfdesc(fd);
60 if (!(fdc->fcb->privflags & _FCB_ISDIR))
62 errno = ENOTDIR;
63 goto error;
66 if ( __get_default_file(fd, (long*) &handle) != 0 )
68 errno = EBADF;
69 goto error;
72 newlock = DupLock(handle);
74 if( newlock == BNULL )
76 errno = __stdc_ioerr2errno( IoErr() );
77 goto error;
79 oldlock = CurrentDir( newlock );
81 if( PosixCBase->cd_changed )
83 UnLock( oldlock );
85 else
87 PosixCBase->cd_changed = TRUE;
88 PosixCBase->cd_lock = oldlock;
90 return 0;
92 error:
93 if( newlock != BNULL )
94 UnLock( newlock );
96 return -1;