]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/utils/utils/path.h
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libstrongswan / utils / utils / path.h
1 /*
2 * Copyright (C) 2008-2014 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 *
5 * Copyright (C) secunet Security Networks AG
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup path_i path
20 * @{ @ingroup utils_i
21 */
22
23 #ifndef PATH_H_
24 #define PATH_H_
25
26 /**
27 * Directory separator character in paths on this platform
28 */
29 #ifdef WIN32
30 # define DIRECTORY_SEPARATOR "\\"
31 /* the Windows API also accepts / as separator */
32 # define DIRECTORY_SEPARATOR_ALT "/"
33 #else
34 # define DIRECTORY_SEPARATOR "/"
35 #endif
36
37 /**
38 * Check if the given character is a directory separator.
39 *
40 * @param c character
41 * @return TRUE if the character is a directory separator
42 */
43 static inline bool path_is_separator(char c)
44 {
45 #ifdef WIN32
46 return c == DIRECTORY_SEPARATOR[0] || c == DIRECTORY_SEPARATOR_ALT[0];
47 #else
48 return c == DIRECTORY_SEPARATOR[0];
49 #endif
50 }
51
52 /**
53 * Returns a pointer to the first occurrence of a directory separator in the
54 * given string (optionally limited to a given length).
55 *
56 * @param path path to search
57 * @param len optional length to search, -1 for the full string
58 * @return pointer to separator, or NULL if not found
59 */
60 char *path_first_separator(const char *path, int len);
61
62 /**
63 * Returns a pointer to the last occurrence of a directory separator in the
64 * given string (optionally limited to a given length).
65 *
66 * @param path path to search
67 * @param len optional length to search, -1 for the full string
68 * @return pointer to separator, or NULL if not found
69 */
70 char *path_last_separator(const char *path, int len);
71
72 /**
73 * Like dirname(3) returns the directory part of the given null-terminated
74 * pathname, up to but not including the final '/' (or '.' if no '/' is found).
75 * Trailing '/' are not counted as part of the pathname.
76 *
77 * The difference is that it does this in a thread-safe manner (i.e. it does not
78 * use static buffers) and does not modify the original path.
79 *
80 * @param path original pathname
81 * @return allocated directory component
82 */
83 char *path_dirname(const char *path);
84
85 /**
86 * Like basename(3) returns the filename part of the given null-terminated path,
87 * i.e. the part following the final '/' (or '.' if path is empty or NULL).
88 * Trailing '/' are not counted as part of the pathname.
89 *
90 * The difference is that it does this in a thread-safe manner (i.e. it does not
91 * use static buffers) and does not modify the original path.
92 *
93 * @param path original pathname
94 * @return allocated filename component
95 */
96 char *path_basename(const char *path);
97
98 /**
99 * Check if a given path is absolute.
100 *
101 * @param path path to check
102 * @return TRUE if absolute, FALSE if relative
103 */
104 bool path_absolute(const char *path);
105
106 /**
107 * Creates a directory and all required parent directories.
108 *
109 * @param path path to the new directory
110 * @param mode permissions of the new directory/directories
111 * @return TRUE on success
112 */
113 bool mkdir_p(const char *path, mode_t mode);
114
115 #endif /** PATH_H_ @} */