The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get Absolute path from file descriptors vinp UNIX for Advanced & Expert Users 2 11-15-2007 10:46 PM
absolute path Kirichiko UNIX for Dummies Questions & Answers 2 10-03-2007 03:30 AM
$PWD shows absolute path vs path w/symbolic links kornshellmaven Shell Programming and Scripting 3 06-13-2007 09:15 AM
vi - replacing a relative path with absolute path in a file Yinzer955i UNIX for Dummies Questions & Answers 2 09-07-2006 08:47 AM
HOW to make absolute path???? HELP youngvet High Level Programming 1 11-01-2003 01:58 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-05-2007
Registered User
 

Join Date: Apr 2007
Posts: 4
absolute path

How to find out the absolute path of a file in C/C++?
Thanks
Reply With Quote
Forum Sponsor
  #2  
Old 06-05-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
You mean that you're given a filename like "foo.txt" and you want to find it in the filesystem to see that it's in a given path, example: /somepath/to/myfiles/foo.txt?

stat the file, get the inode number of the file, call popen with
ls -i /somepath/to/myfiles/foo.txt
and check the inode you get back. It has to match the inode number you started with.

The other case is nasty - when you have no idea where the file is located.
The reason is that the one unique identifier for a file in a given filesystem is the inode.
It can be duplicated in all of the other filesystems mounted on the machine.
So you could have several foo.txt files with the same inode number.

You can try using ftw() or nftw(), or call find from a popen() call.
It is not efficient to use either of these from the root directory /, plus it is possible to find more than one matching filename/inode.

The find syntax is:
Code:
find / -type f  -name foo.txt -inode <inode number>
Edit: note that st_dev plus st_inode give a unique identifier for a file.
You have to call ftw(), as there is no way to identify an st_dev value for find to use.

Last edited by jim mcnamara; 06-05-2007 at 01:17 PM.
Reply With Quote
  #3  
Old 06-05-2007
Registered User
 

Join Date: Apr 2007
Posts: 4
it is a bit more complicated than that.
to be more specific, when given such a root folder that has foldera and folderb under it.
root---foldera
|-folderb
there is a file called afile in foldera.
right now, i am in folderb and I am trying to open afile by calling a function. the parameter of this function can be either "../foldera/afile" or "/root/foldera/afile". Now, in this function, I want to find out the absolute path of "afile" if the parameter is not absolute yet.
any help is appreciated
thanks

Last edited by filedeliver; 06-05-2007 at 12:57 PM.
Reply With Quote
  #4  
Old 06-05-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
You do realize there can be more than one hard link for a given file, plus there may be multiple symlinks as well. When you call fopen ("/path/to/foo", "r"), foo can easily be a link. So you may not have the real path to start with.

So going "up" the file tree is not going to give you a perfect answer all the time.

I'm not giving you a hard time, it's just that working out whre a file is in all the filesystems on one box is a daunting task. find / -name <> -inode <> is about as good as it gets, even if it is really slow.

In your example, if you're postive foldera or folderb or folderc are under /root then your task is:

get st_dev and st_ino for the file you do have
with popen(), "find /root -name afile -inode st_ino", then stat all of the files returned.
check the st_dev values, when you have a match you have the file.
Reply With Quote
  #5  
Old 06-05-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,648
If you are in, ahem, /directoryb (sorry, but I hate calling directories "folders"), you can call getcwd() to obtain "/directoryb". Since "../directorya/filea" does not start with a /, it must be a relative path. So prepend your current directory to it to get "/directoryb/../directorya/filea" which is an absolute path to the file in question. You could parse this to remove "/.." and the word preceding "/.." if you really need to simplify the path. But no matter what you do, it is possible for files to have multiple absolute paths that do not involve ".." due to symbolic links and loopback mounts.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0