diff options
author | pommicket <pommicket@gmail.com> | 2023-01-01 23:11:55 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-01-01 23:11:55 -0500 |
commit | 850ab378946e8d6f0818b4ccf8eee413f68dcb95 (patch) | |
tree | 579230cbcf4f6f4a4a00504178ea6ca1b7d5ab8f /os.h | |
parent | 3ee947ced94ce24463838e0c587cb076ac81ec98 (diff) |
start os.h merging
Diffstat (limited to 'os.h')
-rw-r--r-- | os.h | 79 |
1 files changed, 79 insertions, 0 deletions
@@ -0,0 +1,79 @@ +#ifndef OS_H_ +#define OS_H_ + +typedef enum { + FS_NON_EXISTENT, + FS_FILE, + FS_DIRECTORY, + FS_OTHER +} FsType; + +enum { + FS_PERMISSION_READ = 0x01, + FS_PERMISSION_WRITE = 0x02, +}; +typedef u8 FsPermission; + +typedef struct { + FsType type; + char name[]; +} FsDirectoryEntry; + +// returns what kind of thing this is. +FsType fs_path_type(char const *path); +FsPermission fs_path_permission(char const *path); +// Does this file exist? Returns false for directories. +bool fs_file_exists(char const *path); +// Returns a NULL-terminated array of the files/directories in this directory, or NULL if the directory does not exist/out of memory. +// When you're done with the entries, call fs_dir_entries_free (or call free on each entry, then on the whole array). +// NOTE: The files/directories aren't returned in any particular order! +FsDirectoryEntry **fs_list_directory(char const *dirname); +// Create the directory specified by `path` +// Returns: +// 1 if the directory was created successfully +// 0 if the directory already exists +// -1 if the path already exists, but it's not a directory, or if there's another error (e.g. don't have permission to create directory). +int fs_mkdir(char const *path); +// Puts the current working directory into buf, including a null-terminator, writing at most buflen bytes. +// Returns: +// 1 if the working directory was inserted into buf successfully +// 0 if buf is too short to hold the cwd +// -1 if we can't get the cwd for whatever reason. +int fs_get_cwd(char *buf, size_t buflen); +struct timespec time_last_modified(char const *filename); +struct timespec time_get(void); +// sleep for a certain number of nanoseconds +void time_sleep_ns(u64 ns); + + +// free the entries generated by fs_list_directory. +static void fs_dir_entries_free(FsDirectoryEntry **entries) { + for (int i = 0; entries[i]; ++i) + free(entries[i]); + free(entries); +} + +static double time_get_seconds(void) { + return timespec_to_seconds(time_get()); +} + + + +// sleep for microseconds +static void time_sleep_us(u64 us) { + time_sleep_ns(us * 1000); +} + +// sleep for milliseconds +static void time_sleep_ms(u64 ms) { + time_sleep_ns(ms * 1000000); +} + +// sleep for seconds +static void time_sleep_s(u64 s) { + time_sleep_ns(s * 1000000000); +} + + +#endif // OS_H_ + |