Safe cross-platform function to normalize the path

I would like to have a standard function that will convert relative paths to absolute, and, if possible, I would like to make it as cross-platform as possible (so I would like to avoid calling external library functions), This is intended to prevent the use of paths.

I know that such a function will not be able to detect symbolic links, but I'm fine with this for my application.

I could roll my own code, but some problems might arise, for example. how the platform handles the coding or changes to the "../" template.

Is there something similar already implemented?

+5
source share
2 answers

There is no universal function that you can call, since there is no such function in standard C or C ++ libraries. On Windows, you can use GetFullPathName . On Linux, Mac OS X, and other * Unix systems, you can use the realpath(3) function, which also allows symbolic links along this path as a bonus.

Beware: any solution for this is reliable only in a single-threaded program. If you use multiple threads, another may exit the system and unexpectedly change the working directory because of you, changing the path name resolution.

+9
source

I think that the closest thing to platform independence is the POSIX library . In particular, you will want to check out unistd.h , which, unfortunately, I do not believe, has the concept of a "normalized" path. If I remember correctly, the standard itself does not even know about directories much less relative.

To get better, I think you need to quit your own path.

+1
source

All Articles