cwk_path_get_root.md (2434B)
1 --- 2 title: cwk_path_get_root 3 description: Determines the root of a path. 4 --- 5 6 _(since v1.0.0)_ 7 Determines the root of a path. 8 9 ## Description 10 ```c 11 void cwk_path_get_root(const char *path, size_t *length); 12 ``` 13 This function determines the root of a path by finding it's length. The root comes before the first segment of the path. For example, ``C:\`` is the root of ``C:\folder\file.txt``. It always starts at the submitted path. If the path has no root, the length will be set to zero. 14 15 ## Parameters 16 * **path**: The path which will be inspected. 17 * **length**: The output of the root length. 18 19 ## Outcomes 20 21 | Style | Path | Root | 22 |---------|--------------------------|----------------------| 23 | UNIX | ``/test/`` | ``/`` | 24 | UNIX | ``test.txt`` | `` `` | 25 | UNIX | ``C:\test.txt`` | `` `` | 26 | UNIX | ``\folder\`` | `` `` | 27 | WINDOWS | ``/test.txt`` | ``/`` | 28 | WINDOWS | ``\test.txt`` | ``\`` | 29 | WINDOWS | ``C:\test.txt`` | ``C:\`` | 30 | WINDOWS | ``\\server\folder\data`` | ``\\server\folder\`` | 31 | WINDOWS | ``\\.\folder\data`` | ``\\.\`` | 32 | WINDOWS | ``\\?\folder\data`` | ``\\?\`` | 33 | WINDOWS | ``C:test.txt`` | ``C:`` | 34 | WINDOWS | ``..\hello\world.txt`` | `` `` | 35 36 ### Note 37 The style is automatically chosen during compile time, which is 38 UNIX for macOS and linux and WINDOWS for windows. You can change the style 39 using [cwk_path_set_style]({{ site.baseurl }}{% link reference/cwk_path_set_style.md %}). 40 41 ## Example 42 ```c 43 #include <cwalk.h> 44 #include <stdio.h> 45 #include <stddef.h> 46 #include <stdlib.h> 47 48 int main(int argc, char *argv[]) 49 { 50 const char *path; 51 size_t length; 52 53 path = "/my/path.txt"; 54 cwk_path_get_root(path, &length); 55 printf("The root is: '%.*s'", length, path); 56 57 return EXIT_SUCCESS; 58 } 59 ``` 60 61 Ouput: 62 ``` 63 The root is: '/' 64 ``` 65 66 ## Changelog 67 68 | Version | Description | 69 |------------|--------------------------------------------------------| 70 | **v1.1.0** | The UNC path root now includes the shared folder name. | 71 | **v1.1.0** | Fixed false root detection on relative windows paths. | 72 | **v1.0.0** | The function is introduced. |