Small cleanup of extensions code
[AROS.git] / compiler / clib / fchdir.c
blob495ce7720e3beb184ae99e3d88ae3afffef43760
1 /*
2 Copyright © 2008, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "__arosc_privdata.h"
8 #include <exec/types.h>
9 #include <dos/dos.h>
10 #include <proto/dos.h>
11 #include <aros/symbolsets.h>
12 #include <errno.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 BPTR oldlock = NULL;
54 BPTR newlock = NULL;
55 BPTR handle = NULL;
57 if ( __get_default_file(fd, (long*) &handle) != 0 )
59 errno = EBADF;
60 goto error;
63 newlock = DupLockFromFH(handle);
65 if( newlock == NULL )
67 errno = IoErr2errno( IoErr() );
68 goto error;
70 oldlock = CurrentDir( newlock );
72 if( __startup_cd_changed )
74 UnLock( oldlock );
76 else
78 __startup_cd_changed = TRUE;
79 __startup_cd_lock = oldlock;
81 return 0;
83 error:
84 if( newlock != NULL )
85 UnLock( newlock );
87 return -1;