Link to home
Start Free TrialLog in
Avatar of tiger0516
tiger0516

asked on

Relatiev path question

Hi folks,

I am writing a ls-like program in unix. I got some problem when dealing with relative path.

My program runs correctly in I give absolute path or do not give path (I use "." instead"). However, when provided with relative path, either in ../ format or in ~/ format, I got problems.

i.e.

MYCODE -l ../../foo

reports an error  but ls -l ../../foo can go thought

or

MYCODE ~/ list all files in the ROOT directory instead of my home directory

I think it is sth due to chdir. Any help?

Pls provide an example. Many thanks.

Pls tell me in which step shall I use chdir (chdir(argv[1] or chdir(entry->d_name), where entry is pointer to struct dirent )

Thanks

PS:

My code can display correct file attributions (drwx..., inode, etc) if provided with absolute paths. But with relative paths, only file name displays correctly.
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

you C code does not have any idea of the expansion which are done by the Shell, for you.
E.g ~ is expanded to the home directory of you user. You have to use something like glob
to mimix that behaviour or you have to write that on your own.

Regards
Friedrich
Avatar of tiger0516
tiger0516

ASKER

I am supposed to use chdir, I think. I tired, but did not have good luck.

Any idea about using chdir?
SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Friedrich, I am asking C question, not apache question.

anyway, thanks.

I understand libapr is a C library so I answered your question. The other point is that otherwise you have to handle the .., . and ~ expansions yourself.

Friedrich

what error you are getting.

try to print the current directory after changing the directory using _chdir.

and one more thing is you have to store the currentworking directory in buffer.

and then you have to write your ls functionalities, which uses _chdir

after then you have to change back to orginal directory. (hope you have done all these things.)

check the _chdir with single . or using .. seperately with out other directory information. check it's wotking correctly or not.

if it works, then problem is in other part.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial