Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a path is absolute or relative

Tags:

c

linux

path

How do you check whether a path is absolute or relative, using C on Linux?

like image 819
rwallace Avatar asked Sep 27 '11 08:09

rwallace


People also ask

How do you know if a path is absolute or relative?

This just checks if the path string has either "/./" or "/../". and returns true on any and false on neither. If any of the two tests is true then the path string is relative.

How do you know if a path is absolute or relative in Python?

path. isabs() method in Python is used to check whether the specified path is an absolute path or not. On Unix platforms, an absolute path begins with a forward slash ('/') and on Windows it begins with a backward slash ('\') after removing any potential drive letter.

What makes a path absolute '?

An absolute path refers to the complete details needed to locate a file or folder, starting from the root element and ending with the other subdirectories. Absolute paths are used in websites and operating systems for locating files and folders. An absolute path is also known as an absolute pathname or full path.


2 Answers

On Unix like systems (incl. Linux, macOS)

If it starts with a slash it's absolute, otherwise it's relative. That is because everything is part of a single tree starting at the root (/) and file systems are mounted somewhere in this tree.

On Windows

Windows uses backslashes (though slashes are also supported these days) and drive letters. But it's bit more complex:

  • A path starting with a drive letter followed by a colon not followed by a backslash (or slash) can be relative to that drive's current directory.
  • A path starting with a backslash (or slash) is absolute but on the current drive, so in that sense it is in fact relative (to the top of the current drive).
  • A path starting with 2 backslashes is an UNC path (pointing to a network location) which is always absolute, except when the path starts with \\?\ which is a special case to support longer paths.

So on Windows it's best to use the PathIsRelative() (PathIsRelativeA/PathIsRelativeW) function to determine if the path is relative or not.

like image 74
Brecht Sanders Avatar answered Oct 03 '22 21:10

Brecht Sanders


It's absolute if it begins with a /, otherwise relative.

like image 20
David Heffernan Avatar answered Oct 03 '22 22:10

David Heffernan