Cthulhu  0.2.10
Cthulhu compiler collection
os.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #pragma once
4 
5 #include "os/core.h"
6 #include "os/impl/impl.h" // IWYU pragma: export
7 
8 #include <stdbool.h>
9 
11 
14 
17 
20 
23 
27 typedef struct os_library_t
28 {
29  // used by os_common
30  const char *name;
31 
32  // used by os_native
34 } os_library_t;
35 
39 typedef struct os_file_t
40 {
41  // used by os_common
42  const char *path;
43 
44  // used by os_native
46 } os_file_t;
47 
51 typedef struct os_inode_t
52 {
53  // used by os_common
56 } os_inode_t;
57 
61 typedef struct os_iter_t
62 {
63  // used by os_common
64  os_error_t error;
66 
67  // used by os_native
70 } os_iter_t;
71 
73 typedef os_exitcode_t (*os_thread_fn_t)(void *arg);
74 
78 typedef struct os_thread_t
79 {
80  // used by os_common
81  const char *name;
82 
84  void *arg;
85 
86  // used by os_native
89 } os_thread_t;
90 
94 typedef struct os_mutex_t
95 {
96  // used by os_common
97  const char *name;
98 
99  // used by os_native
101 } os_mutex_t;
102 
105 
113 CT_OS_API os_error_t os_library_open(
114  IN_STRING const char *path,
115  OUT_NOTNULL os_library_t *library);
116 
121 CT_OS_API os_error_t os_library_close(
122  STA_RELEASE os_library_t *library);
123 
132 CT_OS_API os_error_t os_library_symbol(
133  IN_NOTNULL os_library_t *library,
134  OUT_NOTNULL void **symbol,
135  IN_STRING const char *name);
136 
143 CT_OS_API const char *os_library_name(IN_NOTNULL const os_library_t *library);
144 
146 
149 
157 CT_OS_API os_error_t os_file_copy(
158  IN_STRING const char *dst,
159  IN_STRING const char *src);
160 
169 CT_OS_API os_error_t os_file_exists(IN_STRING const char *path);
170 
177 CT_OS_API os_error_t os_file_create(IN_STRING const char *path);
178 
185 CT_OS_API os_error_t os_file_delete(IN_STRING const char *path);
186 
195 CT_OS_API os_error_t os_dir_create(IN_STRING const char *path);
196 
203 CT_OS_API os_error_t os_dir_delete(IN_STRING const char *path);
204 
211 CT_OS_API bool os_dir_exists(IN_STRING const char *path);
212 
219 CT_OS_API os_dirent_t os_dirent_type(IN_STRING const char *path);
220 
222 
231 CT_OS_API os_error_t os_iter_begin(IN_STRING const char *path, OUT_NOTNULL os_iter_t *iter);
232 
236 CT_OS_API os_error_t os_iter_end(STA_RELEASE os_iter_t *iter);
237 
244 CT_NODISCARD STA_SUCCESS(return == true)
246 
253 CT_OS_API os_error_t os_iter_error(IN_NOTNULL const os_iter_t *iter);
254 
256 
265 CT_OS_API os_error_t os_file_open(IN_STRING const char *path, os_access_t access, OUT_NOTNULL os_file_t *file);
266 
273 CT_OS_API os_error_t os_tmpfile_open(OUT_NOTNULL os_file_t *file);
274 
279 CT_OS_API os_error_t os_file_close(STA_RELEASE os_file_t *file);
280 
291 CT_OS_API os_error_t os_file_read(
292  IN_NOTNULL os_file_t *file,
293  STA_WRITES(size) void *buffer,
294  IN_DOMAIN(>, 0) size_t size,
295  OUT_NOTNULL size_t *actual);
296 
307 CT_OS_API os_error_t os_file_write(
308  IN_NOTNULL os_file_t *file,
309  STA_READS(size) const void *buffer,
310  IN_DOMAIN(>, 0) size_t size,
311  OUT_NOTNULL size_t *actual);
312 
320 CT_OS_API os_error_t os_file_size(IN_NOTNULL os_file_t *file, OUT_NOTNULL size_t *actual);
321 
330 CT_OS_API os_error_t os_file_seek(IN_NOTNULL os_file_t *file, size_t offset, OUT_NOTNULL size_t *actual);
331 
339 CT_OS_API os_error_t os_file_tell(IN_NOTNULL os_file_t *file, OUT_NOTNULL size_t *actual);
340 
348 CT_OS_API os_error_t os_file_resize(IN_NOTNULL os_file_t *file, size_t size);
349 
359 CT_OS_API os_error_t os_file_map(
360  IN_NOTNULL os_file_t *file,
361  os_protect_t protect,
362  size_t size,
363  OUT_NOTNULL os_mapping_t *mapping);
364 
369 CT_OS_API os_error_t os_unmap(INOUT_NOTNULL os_mapping_t *mapping);
370 
377 CT_OS_API void *os_mapping_data(IN_NOTNULL os_mapping_t *mapping);
378 
385 CT_OS_API size_t os_mapping_size(IN_NOTNULL const os_mapping_t *mapping);
386 
394 CT_OS_API bool os_mapping_active(IN_NOTNULL const os_mapping_t *mapping);
395 
402 CT_OS_API const char *os_file_name(IN_NOTNULL const os_file_t *file);
403 
405 
408 
409 CT_OS_API os_error_t os_thread_init(
410  OUT_NOTNULL os_thread_t *thread,
411  IN_STRING const char *name,
413  void *arg);
414 
415 CT_OS_API os_error_t os_thread_join(
416  IN_NOTNULL os_thread_t *thread,
417  OUT_NOTNULL os_status_t *status);
418 
419 CT_OS_API const char *os_thread_name(
420  IN_NOTNULL const os_thread_t *thread);
421 
422 CT_OS_API bool os_thread_cmpid(
423  IN_NOTNULL const os_thread_t *thread,
424  os_thread_id_t id);
425 
427 
429 
CT_NODISCARD STA_WRITES(size) void *dst
CT_NODISCARD size_t size
Definition: scan.h:128
int os_exitcode_t
program exit code
Definition: core.h:67
os_dirent_t
directory entry type
Definition: core.h:56
os_protect_t
file mapping memory protection
Definition: core.h:47
os_access_t
file access mode
Definition: core.h:38
#define STA_RELEASE
annotate a pointer as invalid after the function returns
#define CT_PUREFN
mark a function as pure, always returns the same value for the same arguments
Definition: analyze.h:228
#define CT_NODISCARD
mark a function as returning a value that must be used
#define IN_NOTNULL
annotate a parameter as not being null
#define STA_READS(size)
annotate a parameter as reading expr elements
#define IN_DOMAIN(cmp, it)
annotate a parameter as being bounded by the expression of cmp and it
#define RET_INSPECT
annotate the return value as needing to be inspected this is the same as CT_NODISCARD but implies tha...
#define INOUT_NOTNULL
Definition: analyze.h:101
#define IN_STRING
annotate a parameter as being a null terminated string
#define OUT_NOTNULL
Definition: analyze.h:99
#define CT_BEGIN_API
Definition: compiler.h:129
#define CT_END_API
Definition: compiler.h:130
CT_OS_API os_error_t os_file_copy(const char *dst, const char *src)
copy a file from one location to another
Definition: os.c:304
CT_OS_API os_error_t os_thread_join(os_thread_t *thread, OUT_NOTNULL os_status_t *status)
unsigned os_status_t
thread return code
Definition: core.h:71
size_t os_thread_id_t
thread id
Definition: core.h:75
CT_NODISCARD CT_OS_API const char * os_library_name(const os_library_t *library)
get the name of a shared library
Definition: os.c:235
CT_OS_API const char * os_thread_name(const os_thread_t *thread)
Definition: os.c:469
CT_OS_API os_error_t os_library_close(STA_RELEASE os_library_t *library)
close a shared library
CT_NODISCARD CT_OS_API os_error_t os_library_symbol(os_library_t *library, OUT_NOTNULL void **symbol, const char *name)
get a symbol from a shared library
CT_OS_API os_error_t os_library_open(const char *path, OUT_NOTNULL os_library_t *library)
open a shared library from disk
CT_OS_API bool os_thread_cmpid(const os_thread_t *thread, os_thread_id_t id)
Definition: os.c:477
os_exitcode_t(* os_thread_fn_t)(void *arg)
Definition: os.h:73
CT_OS_API os_error_t os_thread_init(OUT_NOTNULL os_thread_t *thread, const char *name, os_thread_fn_t fn, void *arg)
typedefCT_BEGIN_API struct os_file_t os_file_t
file handle
Definition: core.h:25
STA_DECL bool os_iter_next(os_iter_t *iter, os_inode_t *dir)
Definition: os.c:410
CT_OS_API os_error_t os_file_seek(os_file_t *file, size_t offset, OUT_NOTNULL size_t *actual)
seek to a position in a file
CT_OS_API os_error_t os_file_close(STA_RELEASE os_file_t *file)
close a file
CT_OS_API os_error_t os_file_create(const char *path)
create a file
Definition: os.c:289
CT_NODISCARD CT_OS_API os_dirent_t os_dirent_type(const char *path)
get the type of a paths inode entry
Definition: fs.c:65
CT_NODISCARD STA_SUCCESS(return==true) CT_OS_API bool os_iter_next(os_iter_t *iter
get the next directory entry
CT_OS_API os_error_t os_dir_delete(const char *path)
delete a directory
Definition: fs.c:52
CT_NODISCARD CT_PUREFN CT_OS_API bool os_mapping_active(const os_mapping_t *mapping)
does the mapping object contain a valid mapping checks if the mapping data exists,...
Definition: os.c:175
CT_OS_API os_error_t os_file_delete(const char *path)
delete a file
Definition: fs.c:16
CT_OS_API os_error_t os_file_tell(os_file_t *file, OUT_NOTNULL size_t *actual)
get the current position in a file
CT_OS_API os_error_t os_tmpfile_open(OUT_NOTNULL os_file_t *file)
create a temporary file
CT_OS_API os_error_t os_file_exists(const char *path)
check if a file exists
Definition: file.c:38
CT_OS_API os_error_t os_file_size(os_file_t *file, OUT_NOTNULL size_t *actual)
get the size of a file
CT_NODISCARD CT_PUREFN CT_OS_API const char * os_file_name(const os_file_t *file)
get the name of a file
Definition: os.c:281
CT_OS_API os_error_t os_iter_end(STA_RELEASE os_iter_t *iter)
close a directory iterator
CT_OS_API os_error_t os_file_read(os_file_t *file, STA_WRITES(size) void *buffer, size_t size, OUT_NOTNULL size_t *actual)
read from a file
CT_OS_API os_error_t os_file_write(os_file_t *file, STA_READS(size) const void *buffer, size_t size, OUT_NOTNULL size_t *actual)
write to a file
CT_NODISCARD CT_OS_API size_t os_mapping_size(const os_mapping_t *mapping)
get the size of a file mapping
Definition: os.c:157
CT_OS_API os_error_t os_file_open(const char *path, os_access_t access, OUT_NOTNULL os_file_t *file)
file api
CT_NODISCARD OUT_NOTNULL os_inode_t * dir
Definition: os.h:245
CT_OS_API os_error_t os_file_resize(os_file_t *file, size_t size)
truncate/expand a file to a specific size
Definition: file.c:143
CT_OS_API os_error_t os_iter_error(const os_iter_t *iter)
get the error state of a directory iterator
Definition: os.c:437
CT_OS_API os_error_t os_file_map(os_file_t *file, os_protect_t protect, size_t size, OUT_NOTNULL os_mapping_t *mapping)
map a file into memory
CT_OS_API bool os_dir_exists(const char *path)
check if a directory exists
Definition: os.c:353
CT_OS_API os_error_t os_dir_create(const char *path)
check if a directory exists
Definition: fs.c:29
CT_OS_API os_error_t os_unmap(INOUT_NOTNULL os_mapping_t *mapping)
unmap a file from memory
CT_NODISCARD CT_OS_API void * os_mapping_data(os_mapping_t *mapping)
get the data of a file mapping
Definition: os.c:166
CT_OS_API os_error_t os_iter_begin(const char *path, OUT_NOTNULL os_iter_t *iter)
directory iteration
#define CT_OS_NAME_MAX
Definition: posix.h:42
pthread_t os_thread_impl_t
Definition: posix.h:14
DIR * os_iter_impl_t
Definition: posix.h:13
struct dirent * os_inode_impl_t
Definition: posix.h:12
pthread_mutex_t os_mutex_impl_t
Definition: posix.h:15
void * os_library_impl_t
Definition: posix.h:10
FILE * os_file_impl_t
Definition: posix.h:11
a file handle
Definition: os.h:40
os_file_impl_t impl
Definition: os.h:45
const char * path
Definition: os.h:42
an inode entry
Definition: os.h:52
os_dirent_t type
Definition: os.h:54
char name[CT_OS_NAME_MAX]
Definition: os.h:55
a directory iterator
Definition: os.h:62
os_inode_t inode
Definition: os.h:65
os_inode_impl_t current
Definition: os.h:69
os_iter_impl_t impl
Definition: os.h:68
os_error_t error
Definition: os.h:64
a shared library handle
Definition: os.h:28
const char * name
Definition: os.h:30
os_library_impl_t impl
Definition: os.h:33
memory mapping handle
Definition: posix.h:18
a mutex handle
Definition: os.h:95
os_mutex_impl_t impl
Definition: os.h:100
const char * name
Definition: os.h:97
a thread handle
Definition: os.h:79
os_thread_id_t id
Definition: os.h:88
os_thread_fn_t fn
Definition: os.h:83
const char * name
Definition: os.h:81
void * arg
Definition: os.h:84
os_thread_impl_t impl
Definition: os.h:87