Relatiev path question

Posted on 2006-06-07
Last Modified: 2010-04-15
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.


MYCODE -l ../../foo

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


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 )



My code can display correct file attributions (drwx..., inode, etc) if provided with absolute paths. But with relative paths, only file name displays correctly.
Question by:tiger0516
    LVL 24

    Expert Comment

    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.

    LVL 1

    Author Comment

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

    Any idea about using chdir?
    LVL 24

    Assisted Solution

    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:

    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


    LVL 1

    Author Comment

    Hi Friedrich, I am asking C question, not apache question.

    anyway, thanks.

    LVL 24

    Expert Comment

    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.

    LVL 3

    Expert Comment


    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.
    LVL 3

    Accepted Solution

    #include <stdio.h>
    #define _MAX_PATH 1024
    int main()

            char buffer[_MAX_PATH];
            char buffer1[_MAX_PATH];

            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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now