libsir 2.2.5
Standard Incident Reporter
Loading...
Searching...
No Matches
filesystem.h
1/*
2 * filesystem.h
3 *
4 * Version: 2.2.5
5 *
6 * -----------------------------------------------------------------------------
7 *
8 * SPDX-License-Identifier: MIT
9 *
10 * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
11 * Copyright (c) 2018-2024 Jeffrey H. Johnson <trnsz@pobox.com>
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy of
14 * this software and associated documentation files (the "Software"), to deal in
15 * the Software without restriction, including without limitation the rights to
16 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
17 * the Software, and to permit persons to whom the Software is furnished to do so,
18 * subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in all
21 * copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
26 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
27 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 *
30 * -----------------------------------------------------------------------------
31 */
32
33#ifndef _SIR_FILESYSTEM_H_INCLUDED
34# define _SIR_FILESYSTEM_H_INCLUDED
35
36# include "sir/platform.h"
37
38# if defined(__cplusplus)
39extern "C" {
40# endif
41
44# define SIR_PATH_BUFFER_GROW_BY 32
45
48# define SIR_STAT_NONEXISTENT ((off_t)0xffffff02)
49
52typedef enum {
53 SIR_PATH_REL_TO_CWD = 0x0001,
54 SIR_PATH_REL_TO_APP = 0x0002
55} sir_rel_to;
56
57bool _sir_pathgetstat(const char* restrict path, struct stat* restrict st, sir_rel_to rel_to);
58bool _sir_pathexists(const char* restrict path, bool* restrict exists, sir_rel_to rel_to);
59bool _sir_openfile(FILE* restrict* restrict f, const char* restrict path,
60 const char* restrict mode, sir_rel_to rel_to);
61
62char* _sir_getcwd(void);
63
64char* _sir_getappfilename(void);
65char* _sir_getappbasename(void);
66char* _sir_getappdir(void);
67
68char* _sir_getbasename(char* restrict path);
69char* _sir_getdirname(char* restrict path);
70
71bool _sir_ispathrelative(const char* restrict path, bool* restrict relative);
72bool _sir_getrelbasepath(const char* restrict path, bool* restrict relative,
73 const char* restrict* restrict base_path, sir_rel_to rel_to);
74
75bool _sir_deletefile(const char* restrict path);
76
77# if !defined(__WIN__)
78/* suppress unconditional warning from Flawfinder about the use of
79 * the readlink function, which is prone to pathname race conditions. */
80static inline
81ssize_t _sir_readlink(const char* restrict path, char* restrict buf, size_t bufsize)
82{
83 return readlink(path, buf, bufsize); /* flawfinder: ignore */
84}
85# endif
86
87# if defined(_AIX)
88int _sir_aixself(char* buffer, size_t* size);
89# endif
90
91# if defined(__OpenBSD__)
92int _sir_openbsdself(char* buffer, int size);
93# endif
94
95# if defined(__OpenBSD__) || (defined(_AIX) && defined(__PASE__))
96int _sir_resolvepath(const char* restrict path, char* restrict buffer, size_t size);
97# endif
98
99# if defined(__cplusplus)
100}
101# endif
102
103#endif /* !_SIR_FILESYSTEM_H_INCLUDED */