• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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.
0
tiger0516
Asked:
tiger0516
  • 3
  • 2
  • 2
2 Solutions
 
fridomCommented:
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
0
 
tiger0516Author Commented:
I am supposed to use chdir, I think. I tired, but did not have good luck.

Any idea about using chdir?
0
 
fridomCommented:
chdir is fine, but you have to tell it where to change. For that you have to "normalize" the path. So you have to decide how to handle ., .., ~ etc.  You could e.g. use glob to get an array of path elements you then can step through that array and run chdir on each entry.
glob would take care of ../.. and . but not of ~

you could use e.g part of libapr for the given job:
http://apr.apache.org/docs/apr/group__apr__filepath.html
http://apr.apache.org/docs/apr/group__apr__fnmatch.html

Or as written before you ahve to build the path yourself. In the end you need fully or relative qualfied parth to hand that over to chdir.

That's the whole deal

Regards
Friedrich

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tiger0516Author Commented:
Hi Friedrich, I am asking C question, not apache question.

anyway, thanks.

0
 
fridomCommented:
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
0
 
srinimsCommented:

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.
0
 
srinimsCommented:
#include<unistd.h>
#include <stdio.h>
#define _MAX_PATH 1024
int main()
{

        char buffer[_MAX_PATH];
        getcwd(buffer,_MAX_PATH);
        chdir("..");
        char buffer1[_MAX_PATH];
        getcwd(buffer1,_MAX_PATH);
        printf("%s\n",buffer1);
        printf("%s\n",buffer);
        chdir(buffer);

        return 0;
}


this one works fine
it's giving previous directory as first output and current directory as second output
you have to handle the return value of the chdir. before proceeding

srinims
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now