tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / compiler / posixc / chdir.c
blob26eff29bfd116c5343c9c5a8c5da790315e17f73
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2008 function chdir().
6 */
8 #include "__posixc_intbase.h"
10 #include <exec/types.h>
11 #include <dos/dos.h>
12 #include <proto/dos.h>
13 #include <aros/symbolsets.h>
14 #include <errno.h>
15 #include "__upath.h"
17 /*****************************************************************************
19 NAME */
20 #include <unistd.h>
22 int chdir(
24 /* SYNOPSIS */
25 const char *path )
27 /* FUNCTION
28 Change the current working directory to the one specified by path.
30 INPUTS
31 path - Path 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.
41 The path given to chdir can be translated so that getcwd gives back
42 a string that is not the same but points to th same directory. For
43 example, assigns are replaced by the path where the assign points to
44 and device names (like DH0:) are replaced with the volume name
45 (e.g. Workbench:).
47 EXAMPLE
49 BUGS
51 SEE ALSO
53 INTERNALS
55 ******************************************************************************/
57 struct PosixCIntBase *PosixCBase =
58 (struct PosixCIntBase *)__aros_getbase_PosixCBase();
59 BPTR oldlock;
60 BPTR newlock;
62 path = __path_u2a(path);
64 if (path == NULL)
65 return -1;
67 newlock = Lock( path, SHARED_LOCK );
69 if( newlock == BNULL )
71 errno = __stdc_ioerr2errno( IoErr() );
72 goto error;
75 oldlock = CurrentDir( newlock );
77 if( PosixCBase->cd_changed )
79 UnLock( oldlock );
81 else
83 PosixCBase->cd_changed = TRUE;
84 PosixCBase->cd_lock = oldlock;
87 return 0;
89 error:
90 if( newlock != BNULL ) UnLock( newlock );
92 return -1;
95 int __init_chdir(struct PosixCIntBase *PosixCBase)
97 PosixCBase->cd_changed = FALSE;
99 return 1;
102 void __exit_chdir(struct PosixCIntBase *PosixCBase)
104 if( PosixCBase->cd_changed )
106 BPTR lock = CurrentDir( PosixCBase->cd_lock );
108 UnLock( lock );
112 ADD2OPENLIB(__init_chdir, -100);
113 ADD2CLOSELIB(__exit_chdir, -100);