Relative path to Absolute path

Relative path to Absolute path

Post by Ross Hayd » Thu, 05 Jun 1997 04:00:00



I'm looking for a C algorithm to convert relative paths to an absolute
path.

For instance, I want to resolve "/usr/bin/X11/../ls" into "/usr/bin/ls."
But I would also like to generally clean up a path as well; eg.
"/usr/bin//ls" to "/usr/bin/ls," and so on.

Years ago I saw an algorithm to do this, posted here, but I have since
lost the darn thing.

Thanks for any ideas,
RH

 
 
 

Relative path to Absolute path

Post by Dr. Bor » Thu, 05 Jun 1997 04:00:00



> I'm looking for a C algorithm to convert relative paths to an absolute
> path.

> For instance, I want to resolve "/usr/bin/X11/../ls" into "/usr/bin/ls."
> But I would also like to generally clean up a path as well; eg.
> "/usr/bin//ls" to "/usr/bin/ls," and so on.

> Years ago I saw an algorithm to do this, posted here, but I have since
> lost the darn thing.

Howdy Ross,

I was investigating the subject the other day and I found a function
that can do that, I think it is a GNU-specific function so you can
find a source for it on any FTP site carrying GNU software.

---------------------------------------------------------------------------
NAME
 realpath - returns the canonicalized absolute pathname.

SYNOPSIS
 #include <sys/param.h>
 #include <unistd.h>

 char *realpath(char *path, char resolved_path[]);

DESCRIPTION
 realpath  expands all symbolic links and resolves references to '/./', '/../'
 and extra '/' characters in the null terminated  string  named  by  path  and
 stores  the  canonicalized absolute pathname in the buffer of size MAXPATHLEN
 named by resolved_path.  The resulting path will have no symbolic link, '/./'
 or '/../' components.
[ THE REST SNIPPED ]
-----------------------------------------------------------------------------

--

#include <disclaimer.h>  |     *Good pings come in small packets*
       Vancouver         |     Windows: for IQs smaller than 95
   British Columbia      | SIGSIG -- signature too long (core dumped)

 
 
 

Relative path to Absolute path

Post by Alan S. Gree » Tue, 10 Jun 1997 04:00:00




> > I'm looking for a C algorithm to convert relative paths to an absolute
> > path.

> > For instance, I want to resolve "/usr/bin/X11/../ls" into "/usr/bin/ls."
> > But I would also like to generally clean up a path as well; eg.
> > "/usr/bin//ls" to "/usr/bin/ls," and so on.

> > Years ago I saw an algorithm to do this, posted here, but I have since
> > lost the darn thing.

> Howdy Ross,

> I was investigating the subject the other day and I found a function
> that can do that, I think it is a GNU-specific function so you can
> find a source for it on any FTP site carrying GNU software.

I don't think its GNU specific. I found it in standard C library on
HP-UX and Unixware.

- Show quoted text -

> ---------------------------------------------------------------------------
> NAME
>  realpath - returns the canonicalized absolute pathname.

> SYNOPSIS
>  #include <sys/param.h>
>  #include <unistd.h>

>  char *realpath(char *path, char resolved_path[]);

> DESCRIPTION
>  realpath  expands all symbolic links and resolves references to '/./', '/../'
>  and extra '/' characters in the null terminated  string  named  by  path  and
>  stores  the  canonicalized absolute pathname in the buffer of size MAXPATHLEN
>  named by resolved_path.  The resulting path will have no symbolic link, '/./'
>  or '/../' components.
> [ THE REST SNIPPED ]
> -----------------------------------------------------------------------------

> --

> #include <disclaimer.h>  |     *Good pings come in small packets*
>        Vancouver         |     Windows: for IQs smaller than 95
>    British Columbia      | SIGSIG -- signature too long (core dumped)

--
Alan S. Greer           /\/\o/\/\


 
 
 

1. Relative path to Absolute Path

Although I got absolutely zero response to this request, I did find an
answer.  With the error checking turned off, what I ended up doing was
approximately this:

        chdir(new_dir);
        getcwd(new_dir, MAX_PATH_LEN);

It works beatifully, on both VMS and unix.  (And probably DOS, but
I wouldn't bet my life on it.)

--
Paul Tomblin, Department of Redundancy Department.       ! My employer does
Two roads diverged in a wood, and I/ I took the one less ! not stand by my
travelled by/ And that is why I'm lost, dammit...        ! opinions....

2. Some problems with TAR

3. Symbolic Links - Absolute Paths to Relative Paths

4. iptables/MASQUERADE packets stuck in PREROUTING

5. C: get absolute path to a file from relative path

6. Limit size of Mail file storage

7. Add an absolute path prefix only to relative paths

8. setting up for linux

9. How to convert a relative path into an absolute one?

10. tarring absolute paths for relative untar

11. Make relative tar path absolute at extraction

12. finding absolute or relative path?

13. Relative / absolute paths and symlinks