ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
system.h
Go to the documentation of this file.
1#pragma once
2
29#include <stdio.h>
30#include <stddef.h>
31#include <time.h>
32#include "../common.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
44typedef void (*signal_handler_t)(int);
45
46// ============================================================================
47// System Functions
48// ============================================================================
49
60
69void platform_cleanup(void);
70
79void platform_sleep_ms(unsigned int ms);
80
99
100#ifdef _WIN32
101// usleep macro - only define if not already declared (GCC's unistd.h declares it as a function)
102// On Windows with GCC/Clang, usleep may be available via unistd.h, so we check for that
103// If it's not available, we provide our macro wrapper
104#ifndef _WIN32_USLEEP_AVAILABLE
105// Check if usleep is declared (GCC's unistd.h provides it)
106#if defined(__GNUC__) && !defined(__clang__)
107// INFO: GCC on Windows: unistd.h declares usleep, so don't redefine it
108// Code should use usleep() directly or platform_sleep_usec() directly
109#else
110// MSVC/Clang on Windows: Define usleep macro to use platform_sleep_usec
111#define usleep(usec) platform_sleep_usec(usec)
112#endif
113#endif
114#endif
115
128asciichat_error_t platform_localtime(const time_t *timer, struct tm *result);
129
142asciichat_error_t platform_gtime(const time_t *timer, struct tm *result);
143
151
162const char *platform_get_username(void);
163
175
188
200
219
231const char *platform_getenv(const char *name);
232
243int platform_setenv(const char *name, const char *value);
244
252int platform_isatty(int fd);
253
265const char *platform_ttyname(int fd);
266
276int platform_fsync(int fd);
277
289int platform_backtrace(void **buffer, int size);
290
304char **platform_backtrace_symbols(void *const *buffer, int size);
305
315
327
335typedef bool (*backtrace_frame_filter_t)(const char *frame);
336
353void platform_print_backtrace_symbols(const char *label, char **symbols, int count, int skip_frames, int max_frames,
355
373int platform_format_backtrace_symbols(char *buffer, size_t buffer_size, const char *label, char **symbols, int count,
374 int skip_frames, int max_frames, backtrace_frame_filter_t filter);
375
385void platform_print_backtrace(int skip_frames);
386
387// ============================================================================
388// Safe String Functions
389// ============================================================================
390
405int safe_snprintf(char *buffer, size_t buffer_size, const char *format, ...);
406
419int safe_fprintf(FILE *stream, const char *format, ...);
420
427#define SAFE_IGNORE_PRINTF_RESULT(expr) ((void)(expr))
428
429// ============================================================================
430// Safe Memory Functions
431// ============================================================================
432
447asciichat_error_t platform_memcpy(void *dest, size_t dest_size, const void *src, size_t count);
448
463asciichat_error_t platform_memset(void *dest, size_t dest_size, int ch, size_t count);
464
479asciichat_error_t platform_memmove(void *dest, size_t dest_size, const void *src, size_t count);
480
494asciichat_error_t platform_strcpy(char *dest, size_t dest_size, const char *src);
495
509asciichat_error_t platform_resolve_hostname_to_ipv4(const char *hostname, char *ipv4_out, size_t ipv4_out_size);
510
541asciichat_error_t platform_load_system_ca_certs(char **pem_data_out, size_t *pem_size_out);
542
570bool platform_is_binary_in_path(const char *bin_name);
571
584
585// ============================================================================
586// Path Separator Constants
587// ============================================================================
588
601#ifdef _WIN32
602#define PATH_DELIM '\\'
603#define PATH_SEPARATOR_STR "\\"
604#else
605#define PATH_DELIM '/'
606#define PATH_SEPARATOR_STR "/"
607#endif
608
617#ifdef _WIN32
618#define PATH_ENV_SEPARATOR ";"
619#else
620#define PATH_ENV_SEPARATOR ":"
621#endif
622
623// ============================================================================
624// File Permission Constants
625// ============================================================================
626
637#define FILE_PERM_PRIVATE 0600
638
649#define DIR_PERM_PRIVATE 0700
650
659#define FILE_PERM_PUBLIC_READ 0644
660
669#define FILE_PERM_MASK 0777
670
671// ============================================================================
672// Maximum Path Length
673// ============================================================================
674
688#ifdef _WIN32
689#define PLATFORM_MAX_PATH_LENGTH 32767
690#elif defined(__linux__)
691#ifndef PATH_MAX
692#define PLATFORM_MAX_PATH_LENGTH 4096
693#else
694#define PLATFORM_MAX_PATH_LENGTH PATH_MAX
695#endif
696#elif defined(__APPLE__)
697#ifndef PATH_MAX
698#define PLATFORM_MAX_PATH_LENGTH 1024
699#else
700#define PLATFORM_MAX_PATH_LENGTH PATH_MAX
701#endif
702#else
703#define PLATFORM_MAX_PATH_LENGTH 4096
704#endif
705
734bool platform_get_executable_path(char *exe_path, size_t path_size);
735
767bool platform_get_temp_dir(char *temp_dir, size_t path_size);
768
779bool platform_get_cwd(char *cwd, size_t path_size);
780
786#define PLATFORM_ACCESS_EXISTS 0
787#define PLATFORM_ACCESS_WRITE 2
788#define PLATFORM_ACCESS_READ 4
789
817int platform_access(const char *path, int mode);
818
819#ifdef __cplusplus
820}
821#endif
822
unsigned long long uint64_t
Definition common.h:59
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
int safe_fprintf(FILE *stream, const char *format,...)
Safe version of fprintf.
signal_handler_t platform_signal(int sig, signal_handler_t handler)
Set a signal handler.
int platform_setenv(const char *name, const char *value)
Set an environment variable.
bool platform_get_executable_path(char *exe_path, size_t path_size)
Get the path to the current executable.
Definition system.c:351
int platform_fsync(int fd)
Synchronize a file descriptor to disk.
int platform_isatty(int fd)
Check if a file descriptor is a terminal.
void platform_cleanup(void)
Cleanup platform-specific subsystems.
bool platform_get_cwd(char *cwd, size_t path_size)
Get the current working directory of the process.
int platform_backtrace(void **buffer, int size)
Get a backtrace of the current call stack.
bool(* console_ctrl_handler_t)(console_ctrl_event_t event)
Console control handler callback type.
Definition system.h:199
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe version of snprintf that ensures null termination.
asciichat_error_t platform_strcpy(char *dest, size_t dest_size, const char *src)
Platform-safe strcpy wrapper.
void platform_print_backtrace_symbols(const char *label, char **symbols, int count, int skip_frames, int max_frames, backtrace_frame_filter_t filter)
Print pre-resolved backtrace symbols with consistent formatting.
bool platform_set_console_ctrl_handler(console_ctrl_handler_t handler)
Register a console control handler (for Ctrl+C, etc.)
int platform_access(const char *path, int mode)
Check file/directory access permissions.
asciichat_error_t platform_memset(void *dest, size_t dest_size, int ch, size_t count)
Platform-safe memset wrapper.
int platform_get_pid(void)
Get the current process ID.
asciichat_error_t platform_localtime(const time_t *timer, struct tm *result)
Platform-safe localtime wrapper.
asciichat_error_t platform_load_system_ca_certs(char **pem_data_out, size_t *pem_size_out)
Load system CA certificates for TLS/HTTPS.
const char * platform_getenv(const char *name)
Get an environment variable value.
const char * platform_ttyname(int fd)
Get the name of the terminal associated with a file descriptor.
bool platform_is_binary_in_path(const char *bin_name)
Check if a binary is available in the system PATH.
Definition system.c:288
void(* signal_handler_t)(int)
Signal handler function type.
Definition system.h:44
void platform_backtrace_symbols_free(char **strings)
Free symbol array returned by platform_backtrace_symbols()
void platform_print_backtrace(int skip_frames)
Print a backtrace of the current call stack.
bool platform_get_temp_dir(char *temp_dir, size_t path_size)
Get the system temporary directory path.
uint64_t platform_get_monotonic_time_us(void)
Get monotonic time in microseconds.
int platform_format_backtrace_symbols(char *buffer, size_t buffer_size, const char *label, char **symbols, int count, int skip_frames, int max_frames, backtrace_frame_filter_t filter)
Format pre-resolved backtrace symbols to a buffer.
void platform_sleep_ms(unsigned int ms)
Sleep for a specified number of milliseconds.
asciichat_error_t platform_memcpy(void *dest, size_t dest_size, const void *src, size_t count)
Platform-safe memcpy wrapper.
char ** platform_backtrace_symbols(void *const *buffer, int size)
Convert backtrace addresses to symbol names.
asciichat_error_t platform_memmove(void *dest, size_t dest_size, const void *src, size_t count)
Platform-safe memmove wrapper.
asciichat_error_t platform_resolve_hostname_to_ipv4(const char *hostname, char *ipv4_out, size_t ipv4_out_size)
Resolve hostname to IPv4 address.
asciichat_error_t platform_gtime(const time_t *timer, struct tm *result)
Platform-safe gmtime wrapper.
console_ctrl_event_t
Console control event types (cross-platform Ctrl+C handling)
Definition system.h:181
const char * platform_get_username(void)
Get the current username.
void platform_cleanup_binary_path_cache(void)
Cleanup the binary PATH cache.
Definition system.c:261
asciichat_error_t platform_init(void)
Initialize platform-specific subsystems.
bool(* backtrace_frame_filter_t)(const char *frame)
Callback type for filtering backtrace frames.
Definition system.h:335
void platform_install_crash_handler(void)
Install crash handlers for the application.
@ CONSOLE_CTRL_BREAK
Definition system.h:183
@ CONSOLE_SHUTDOWN
Definition system.h:186
@ CONSOLE_LOGOFF
Definition system.h:185
@ CONSOLE_CTRL_C
Definition system.h:182
@ CONSOLE_CLOSE
Definition system.h:184
#define bool
Definition stdbool.h:22
⏱️ High-precision timing utilities using sokol_time.h and uthash