27

How to get the absolute path for a given relative path programmatically in Linux?

Incase of Windows we have the _fullpath() API. In other words, I mean what is analogous API to _fullpath of Windows in Linux?

1

7 Answers 7

25

As Paul mentioned, use realpath(). Please note though, that since many file systems in Linux support hard links, any given directory can have a number of different absolute paths.

Sign up to request clarification or add additional context in comments.

8 Comments

Any given file can, for sure. Hard-links to directories isn't necessarily supported. Symlinks also cause some confusion when it come sto determining the "real" path.
@unwind, Thanks for the Info. If due to hard links, if a given directory resolves to multiple different absolute paths, what will be the behaviour of realpath API?
Hardlinks to directories are considered evil and forbidden by most filesystems.
I am a little confused. Is it possible that a given directory resolves to many different absolute paths? If yes, then what will be the behaviour realpath?
@Jay it doesn't affect resolving a relative path to an absolute path.
|
20

Check out the realpath function.

#include <stdlib.h> 
#include <stdio.h> 
#include <linux/limits.h>
int main() 
{ 
        char resolved_path[PATH_MAX]; 
        realpath("../../", resolved_path); 
        printf("\n%s\n",resolved_path); 
        return 0; 
} 

3 Comments

Please use PATH_MAX instead of 100
This code is insecure and buggy, as pointed out above. Do not give such a small buffer to realpath which will most likely write beyond the buffer size (since it requires a length of PATH_MAX). Even if the program doesn't crash, this code could lead to security vulnerabilities depending on the variable layout, if an adversary can control the original path to be resolved. The manual recommends passing NULL as a second parameter and letting realpath allocate memory to ensure no issues with PATH_MAX definitions, starting with POSIX 2008.
I think it is a good think to always remember that PATH_MAX simply isn't: insanecoding.blogspot.com.br/2007/11/pathmax-simply-isnt.html
6

Try realpath:

$ man realpath

This is also available in BSD, OS X, et al.

Comments

3

There is the realpath from stdlib.h

2 Comments

I immediately thought of realpath, too, but I was stunned -- stunned I say -- when I saw your answer showing that realpath is in stdlib.h. Surely that can't be true, considering that realpath is not part of the C library. Lo and behold, it's true. I'm dumbfounded. What's a well-formed program that defines its own function named realpath to do? Those POSIX guys have run amok! Amok I say!
Dan: So long as they invoke their compiler in "strictly conforming" mode and don't define any macros that invoke undefined behaviour (like _XOPEN_SOURCE), they should be OK.
2

Running on RedHat 5.3, realpath doesn't exist but readlink is installed. You can use it on relative paths and symlinks, plus it will resolve symlinks recursively for you. It's thus a better option that realpath in my opinion

readlink -f .

Comments

0

The is also another useful way, like "readlink -m $filename"

First of all, it works without requirement for target file to exist. Secondly, it will handle symlinks and get really real path.

Comments

-3
// For C++ with Gnome Gtkmm3 libraries
#include <glibmm.h>
#include <giomm.h>

  string PathRel2Abs(string relpath) {
  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(relpath);
  return file->get_path();
}

2 Comments

Usually it is a good idea to add some explanation to your post on how the code works. This helps newer developers know what the code does.
And it's not even C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.