By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,475 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,475 IT Pros & Developers. It's quick & easy.

"Problem" with navigating in dirs with chdir...

P: 22
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/../..
Nov 19 '08 #1
Share this Question
Share on Google+
3 Replies


P: 63
You can use realpath if it is available on your OS.

It takes 2 parameters, one the path you calculated (with, possible, some /.././) and the second is the output where it will store the best path that it can resolve.

Example:
char buf[] = "/local/home/directory/../../bin/";
char output[500];

realpath(buf, output);

printf("%s\n", output);

this will print out:
/local/bin/
Nov 19 '08 #2

P: 22
You can use realpath if it is available on your OS.
Found the realpath function a little before i got the notification about your reply, but thanks a bunch anyway :)

BTW: Is there a reference on which operating system the different functions/libs is available?
Nov 19 '08 #3

P: 63
Hmm... I was under the impression that the man pages will provide a section specifying which standard the function comes from, but I can't find one...

In my experience, if a function is in the 3rd section of man pages, then it is standard (C89/C99). If it is in the 2nd section, then it is POSIX... I'm more than certain that there are exceptions though.

Anyhoo, if it's in C89/C99, then it is available on all compilers (some don't support all C99 functionality well though). If it's POSIX, then you can expect to find it on most *nix systems (linux, unix), but you might have a harder time on windows.

I know this is a hand-wavey explanation, so hopefully someone has a better one.
Nov 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.