compiler/clib: Rename IoErr2errno() to __arosc_ioerr2errno(); function is now defined...
[AROS.git] / compiler / clib / fchdir.c
blob2656768fd2aa899fee9be5c0d977bc229648cc09
1 /*
2 Copyright © 2008-2012, 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 "__upath.h"
14 #include <aros/debug.h>
16 /*****************************************************************************
18 NAME */
19 #include <unistd.h>
21 int fchdir(
23 /* SYNOPSIS */
24 int fd )
26 /* FUNCTION
27 Change the current working directory to the directory given as an open
28 file descriptor.
30 INPUTS
31 fd - File descriptor of the directory to change to.
33 RESULT
34 If the current directory was changed successfully, zero is returned.
35 Otherwise, -1 is returned and errno set apropriately.
37 NOTES
38 At program exit, the current working directory will be changed back
39 to the one that was current when the program first started. If you
40 do not desire this behaviour, use dos.library/CurrentDir() instead.
42 EXAMPLE
44 BUGS
46 SEE ALSO
48 INTERNALS
50 ******************************************************************************/
52 struct aroscbase *aroscbase = __GM_GetBase();
53 BPTR oldlock = BNULL;
54 BPTR newlock = BNULL;
55 BPTR handle = BNULL;
57 if ( __get_default_file(fd, (long*) &handle) != 0 )
59 errno = EBADF;
60 goto error;
63 newlock = DupLockFromFH(handle);
65 if( newlock == BNULL )
67 errno = __arosc_ioerr2errno( IoErr() );
68 goto error;
70 oldlock = CurrentDir( newlock );
72 if( aroscbase->acb_cd_changed )
74 UnLock( oldlock );
76 else
78 aroscbase->acb_cd_changed = TRUE;
79 aroscbase->acb_cd_lock = oldlock;
81 return 0;
83 error:
84 if( newlock != BNULL )
85 UnLock( newlock );
87 return -1;