I'm developing a very simple file server and client at school and have the following case.
The server maintains a current directory string and also holds a initial dir (which is a record of where the script was started from). The user is able to navigate in files and folders on the server, but should not be able to navigate outside the initDir.
My first idea was to keep the current dir in a temporary variable, change directory and check with strstr that the initDir is contained in the new current dir.
An example:
1. init=/home/user/test curr=/home/user/test => OK
2. inti=/home/user/test curr=/home/user/test/1 => OK
3. /home/user/test curr=/home/user/ => NOT OK
3. /home/user/test curr=/home/user/test/.. => NOT OK
3. /home/user/test curr=/home/user/test/../test => OK
The problem is the '..' which I thought would be gode when changing the dir, so that when you change currentdir with chdir(/home/user/test/../..) the value returned from getcwd would be /home/user/test/../..