Need help with expanding a unix path correctly.

Reply

Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Need help with expanding a unix path correctly.

 
0
  #1
Nov 13th, 2007
I am writing client and server programs for a simple FTP.

I am allowed to use chdir, getcwd and getenv.

What would be a correct procedure to expand a user provided path correctly using these functions, assuming of course the user supplied path is stored in a c-string?

Thank you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need help with expanding a unix path correctly.

 
0
  #2
Nov 14th, 2007
User supplied file names must be either absolute or relative to the application. If the file starts with '/' then it must be absolute. Otherwise, it is relative. Just tack it on to the end of the current working directory name, then collapse instances of "./" and "../".

You didn't say whether you are working on Unix or Windows, so you should be able to handle paths that start with a drive letter ("D:/foo/bar" is absolute, but "D:foo/bar" is relative) and that use '\\' and/or '/' as separators.

Personally, I would be inclined to split the path into a deque, manipulate its parts, then join it back together when done.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Re: Need help with expanding a unix path correctly.

 
0
  #3
Nov 14th, 2007
Thanks again, Duoas. Both the server and the client will be on a Solaris (Unix) machine.

The object of this socket/network programming class is to keep things as barebone/simple as possible, so I don't think a deque is an option. Shouldn't the unix chdir function be able to handle both a relative and an absolute path?
Last edited by Ratte; Nov 14th, 2007 at 12:12 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need help with expanding a unix path correctly.

 
0
  #4
Nov 14th, 2007
Sorry, I had just answered some questions in the C++ forum and forgot that this was the C forum, so a deque is unavailable in C...

Yes, the chdir() function can take both relative and absolute paths.

For parsing the paths I recommend you to the strpbrk() or strchr() functions (both defined in <string.h>).

Good luck.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC