C: get absolute path to a file from relative path

C: get absolute path to a file from relative path

Post by Robert Mark Bra » Thu, 06 Jun 2002 07:45:44



Howdy all!

In C, if I can fopen a file by name and relative path, is there anything I
can do to obtain the absolute path for that file?

Any help would be much appreciated!

Rob

:)
:-}
;->

 
 
 

C: get absolute path to a file from relative path

Post by Fletcher Glen » Thu, 06 Jun 2002 08:43:37


You can chdir() to the relative directory, then getcwd().  There's
no guarantee that this is the only path to the file.

--
Fletcher Glenn
to reply remove NOSPAM from my reply address



Quote:> Howdy all!

> In C, if I can fopen a file by name and relative path, is there anything I
> can do to obtain the absolute path for that file?

> Any help would be much appreciated!

> Rob

> :)
> :-}
> ;->


 
 
 

C: get absolute path to a file from relative path

Post by Robert Mark Bra » Thu, 06 Jun 2002 09:11:11


Hi Fletcher!

Quote:> You can chdir() to the relative directory, then getcwd().  There's
> no guarantee that this is the only path to the file.

I had not thought of this. The program will use the shell's working
directory as a starting point, so at least that indicates where the absolute
path is.. well, relative to.

mm..

Rob

:)
:-}
;->


> You can chdir() to the relative directory, then getcwd().  There's
> no guarantee that this is the only path to the file.

> --
> Fletcher Glenn
> to reply remove NOSPAM from my reply address



> > Howdy all!

> > In C, if I can fopen a file by name and relative path, is there anything
I
> > can do to obtain the absolute path for that file?

> > Any help would be much appreciated!

> > Rob

> > :)
> > :-}
> > ;->

 
 
 

C: get absolute path to a file from relative path

Post by those who know me have no need of my nam » Thu, 06 Jun 2002 11:45:22


in comp.unix.programmer i read:

Quote:>In C, if I can fopen a file by name and relative path, is there anything I
>can do to obtain the absolute path for that file?

not in ansi/iso c, but your unspecified system might have realpath().

--
bringing you boring signatures for 17 years

 
 
 

C: get absolute path to a file from relative path

Post by Andreas K?h? » Thu, 06 Jun 2002 12:01:28


Submitted by "those who know me have no need of my name" to
comp.unix.programmer:

Quote:> in comp.unix.programmer i read:

>>In C, if I can fopen a file by name and relative path, is there anything I
>>can do to obtain the absolute path for that file?

> not in ansi/iso c, but your unspecified system might have realpath().

...which is a X/Open System Interfaces Extension (XSI).

The getcwd function might be of use if you don't have
realpath, it's in POSIX Base.

--
Andreas K?h?ri
--------------------------------------------------------------
Feed your daemons.              www.netbsd.org

 
 
 

C: get absolute path to a file from relative path

Post by David Schwart » Thu, 06 Jun 2002 12:07:05



Quote:

> in comp.unix.programmer i read:

> >In C, if I can fopen a file by name and relative path, is there anything I
> >can do to obtain the absolute path for that file?

> not in ansi/iso c, but your unspecified system might have realpath().

        This is comp.unix.programmer and realpath is part of SUS. Unfortuntely,
it's not quite 100% reliable on many implementations. For one thing, a
file may not have a single absolute path.

        DS

 
 
 

C: get absolute path to a file from relative path

Post by Walter Brisco » Thu, 06 Jun 2002 16:43:11



02:45:22 in comp.unix.programmer, those who know me have no need of my

Quote:>in comp.unix.programmer i read:

>>In C, if I can fopen a file by name and relative path, is there anything I
>>can do to obtain the absolute path for that file?

>not in ansi/iso c, but your unspecified system might have realpath().

And, judging by IEEE Std 1003.1-2001, your system probably does so if it
supports POSIX.
--
Walter Briscoe
 
 
 

C: get absolute path to a file from relative path

Post by Donald McLachl » Thu, 06 Jun 2002 20:08:57



Quote:> Howdy all!

> In C, if I can fopen a file by name and relative path, is there anything I
> can do to obtain the absolute path for that file?

> Any help would be much appreciated!

> Rob

Gee, sounds like you you read before you post.  The thread "Full path name" has
the answer you need, ie. man realpath.

--

Communications Research Centre / RNS    Tel     (613) 998-2845
3701 Carling Ave.,                      Fax     (613) 998-9648
Ottawa, Ontario
K2H 8S2
Canada

 
 
 

C: get absolute path to a file from relative path

Post by those who know me have no need of my nam » Thu, 06 Jun 2002 23:08:38


in comp.unix.programmer i read:


>> in comp.unix.programmer i read:
>> >In C, if I can fopen a file by name and relative path, is there
>> >anything I can do to obtain the absolute path for that file?

>> not in ansi/iso c, but your unspecified system might have realpath().

>    This is comp.unix.programmer and realpath is part of SUS.

and hence posix.  i didn't mention that, though i'm not sure it matters.

Quote:>Unfortuntely, it's not quite 100% reliable on many implementations. For
>one thing, a file may not have a single absolute path.

i didn't say it was.  on a system with /proc/self/fd one might just open
the file then see if /proc/self/fd/<fd> is a symlink to the absolute-ified
path, but that's not certain either.

--
bringing you boring signatures for 17 years

 
 
 

C: get absolute path to a file from relative path

Post by those who know me have no need of my nam » Thu, 06 Jun 2002 23:11:11


in comp.unix.programmer i read:

Quote:>Submitted by "those who know me have no need of my name" to
>comp.unix.programmer:
>> your unspecified system might have realpath().
>...which is a X/Open System Interfaces Extension (XSI).

CHANGE HISTORY

    First released in Issue 4, Version 2.

Issue 5

    Moved from X/OPEN UNIX extension to BASE.

--
bringing you boring signatures for 17 years

 
 
 

C: get absolute path to a file from relative path

Post by Andreas K?h? » Fri, 07 Jun 2002 07:00:52


Submitted by "those who know me have no need of my name" to
comp.unix.programmer:

Quote:> in comp.unix.programmer i read:
>>Submitted by "those who know me have no need of my name" to
>>comp.unix.programmer:

>>> your unspecified system might have realpath().

>>...which is a X/Open System Interfaces Extension (XSI).

> CHANGE HISTORY

>     First released in Issue 4, Version 2.

> Issue 5

>     Moved from X/OPEN UNIX extension to BASE.

I have to admit that I didn't read that far...
Hmmm, so why do it still have the XSI tag in the synopsis?

--
Andreas K?h?ri
Bioinformatics and Unix System Support, Dunedin, New Zealand
--------------------------------------------------------------
Feed your daemons.              www.netbsd.org

 
 
 

1. Relative path to Absolute path

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

2. Diamond Stealth 64 DRAM - Final Word!!

3. Relative path to Absolute Path

4. Which Databases are available for Linux

5. Symbolic Links - Absolute Paths to Relative Paths

6. Large Integer ??

7. Add an absolute path prefix only to relative paths

8. How to set up Redhat linux without a CD-ROM

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