purge remaining gpl code from clib, and make clib build again
[tangerine.git] / compiler / clib / chdir.c
blob270e926f7299e298b6f20564366e6831d713a6cc
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C 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 "__errno.h"
16 #include "__upath.h"
18 /*****************************************************************************
20 NAME */
21 #include <unistd.h>
23 int chdir(
25 /* SYNOPSIS */
26 const char *path )
28 /* FUNCTION
29 Change the current working directory to the one specified by path.
31 INPUTS
32 path - Path 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.
42 The path given to chdir can be translated so that getcwd gives back
43 a string that is not the same but points to th same directory. For
44 example, assigns are replaced by the path where the assign points to
45 and device names (like DH0:) are replaced with the volume name
46 (e.g. Workbench:).
48 EXAMPLE
50 BUGS
52 SEE ALSO
54 INTERNALS
56 ******************************************************************************/
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 == NULL )
70 errno = IoErr2errno( IoErr() );
71 goto error;
74 oldlock = CurrentDir( newlock );
76 if( __startup_cd_changed )
78 UnLock( oldlock );
80 else
82 __startup_cd_changed = TRUE;
83 __startup_cd_lock = oldlock;
86 return 0;
88 error:
89 if( newlock != NULL ) UnLock( newlock );
91 return -1;
94 int __init_chdir(void)
96 __startup_cd_changed = FALSE;
98 return 1;
101 void __exit_chdir(void)
103 if( __startup_cd_changed )
105 BPTR lock = CurrentDir( __startup_cd_lock );
107 UnLock( lock );
111 ADD2INIT(__init_chdir, 110);
112 ADD2EXIT(__exit_chdir, 110);