arosc.library: Compiler delint
[AROS.git] / compiler / clib / chdir.c
blobd1e9363c37d274e6c483c84256ec841dd1966e0f
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2008 function chdir().
6 */
8 #include "__arosc_privdata.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 aroscbase *aroscbase = __aros_getbase_aroscbase();
58 BPTR oldlock;
59 BPTR newlock;
61 path = __path_u2a(path);
63 if (path == NULL)
64 return -1;
66 newlock = Lock( path, SHARED_LOCK );
68 if( newlock == BNULL )
70 errno = __arosc_ioerr2errno( IoErr() );
71 goto error;
74 oldlock = CurrentDir( newlock );
76 if( aroscbase->acb_cd_changed )
78 UnLock( oldlock );
80 else
82 aroscbase->acb_cd_changed = TRUE;
83 aroscbase->acb_cd_lock = oldlock;
86 return 0;
88 error:
89 if( newlock != BNULL ) UnLock( newlock );
91 return -1;
94 int __init_chdir(struct aroscbase *aroscbase)
96 aroscbase->acb_cd_changed = FALSE;
98 return 1;
101 void __exit_chdir(struct aroscbase *aroscbase)
103 if( aroscbase->acb_cd_changed )
105 BPTR lock = CurrentDir( aroscbase->acb_cd_lock );
107 UnLock( lock );
111 ADD2OPENLIB(__init_chdir, -100);
112 ADD2CLOSELIB(__exit_chdir, -100);