master
 1#ifndef __wasi_libc_find_relpath_h
 2#define __wasi_libc_find_relpath_h
 3
 4#ifdef __cplusplus
 5extern "C" {
 6#endif
 7
 8/**
 9 * Look up the given `path`, relative to the cwd, in the preopened directory
10 * map. If a suitable entry is found, then the file descriptor for that entry
11 * is returned. Additionally the absolute path of the directory's file
12 * descriptor is returned in `abs_prefix` and the relative portion that needs
13 * to be opened is stored in `*relative_path`.
14 *
15 * The `relative_path` argument must be a pointer to a buffer valid  for
16 * `relative_path_len` bytes, and this may be used as storage for the relative
17 * portion of the path being returned through `*relative_path`.
18 *
19 * See documentation on `__wasilibc_find_abspath` for more info about what the
20 * paths look like.
21 *
22 * Returns -1 on failure. Errno is set to either:
23 *
24 *  * ENOMEM - failed to allocate memory for internal routines.
25 *  * ENOENT - the `path` could not be found relative to any preopened dir.
26 *  * ERANGE - the `relative_path` buffer is too small to hold the relative path.
27 */
28int __wasilibc_find_relpath(const char *path,
29                            const char **__restrict__ abs_prefix,
30                            char **relative_path,
31                            size_t relative_path_len);
32
33/**
34 * Look up the given `path`, which is interpreted as absolute, in the preopened
35 * directory map. If a suitable entry is found, then the file descriptor for
36 * that entry is returned. Additionally the relative portion of the path to
37 * where the fd is opened is returned through `relative_path`, the absolute
38 * prefix which was matched is stored to `abs_prefix`, and `relative_path` may
39 * be an interior pointer to the `abspath` string.
40 *
41 * The `abs_prefix` returned string will not contain a leading `/`. Note that
42 * this may be the empty string. Additionally the returned `relative_path` will
43 * not contain a leading `/`. The `relative_path` return will not return an
44 * empty string, it will return `"."` instead if it would otherwise do so.
45 *
46 * Returns -1 on failure. Errno is set to either:
47 *
48 *  * ENOMEM - failed to allocate memory for internal routines.
49 *  * ENOENT - the `path` could not be found relative to any preopened dir.
50 */
51int __wasilibc_find_abspath(const char *abspath,
52                            const char **__restrict__ abs_prefix,
53                            const char **__restrict__ relative_path);
54
55/**
56 * Same as `__wasilibc_find_relpath`, except that this function will interpret
57 * `relative` as a malloc'd buffer that will be `realloc`'d to the appropriate
58 * size to contain the relative path.
59 *
60 * Note that this is a weak symbol and if it's not defined you can use
61 * `__wasilibc_find_relpath`. The weak-nature of this symbols means that if it's
62 * not otherwise included in the compilation then `chdir` wasn't used an there's
63 * no need for this symbol.
64 *
65 * See documentation on `__wasilibc_find_relpath` for more information.
66 */
67int __wasilibc_find_relpath_alloc(
68    const char *path,
69    const char **abs,
70    char **relative,
71    size_t *relative_len,
72    int can_realloc
73) __attribute__((__weak__));
74
75#ifdef __cplusplus
76}
77#endif
78
79#endif