ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
system.c
Go to the documentation of this file.
1
7#ifndef PLATFORM_SYSTEM_C
8#define PLATFORM_SYSTEM_C
9
10#include <ascii-chat/atomic.h>
11#include <stdarg.h>
12#include <stdio.h>
13#include <errno.h>
14#include <ascii-chat/common.h>
15#include <ascii-chat/uthash.h> // UBSan-safe uthash wrapper
16#include <ascii-chat/log/log.h>
21#include <time.h>
22
23// Forward declaration for filesystem functions
24extern bool check_binary_in_path_uncached(const char *bin_name);
25
26// Platform-specific binary suffix
27#ifdef _WIN32
28#define BIN_SUFFIX ".exe"
29#else
30#define BIN_SUFFIX ""
31#endif
32// PATH_DELIM and PATH_ENV_SEPARATOR are now defined in system.h
33
34// ============================================================================
35// Maximum Path Length
36// ============================================================================
37
49#ifdef _WIN32
50// Windows extended-length path maximum
51// Reference: https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
52#define PLATFORM_MAX_PATH_LENGTH 32767
53#elif defined(__linux__)
54// Linux PATH_MAX (typically 4096)
55#ifndef PATH_MAX
56#define PLATFORM_MAX_PATH_LENGTH 4096
57#else
58#define PLATFORM_MAX_PATH_LENGTH PATH_MAX
59#endif
60#elif defined(__APPLE__)
61// macOS PATH_MAX (typically 1024)
62#ifndef PATH_MAX
63#define PLATFORM_MAX_PATH_LENGTH 1024
64#else
65#define PLATFORM_MAX_PATH_LENGTH PATH_MAX
66#endif
67#else
68// Fallback for unknown platforms
69#define PLATFORM_MAX_PATH_LENGTH 4096
70#endif
71
72// ============================================================================
73// Binary PATH Detection Cache
74// ============================================================================
75
123typedef struct {
125 char *bin_name;
127 bool in_path;
129 UT_hash_handle hh;
131
132// ============================================================================
133// Public API
134// ============================================================================
135
148int safe_snprintf(char *buffer, size_t buffer_size, const char *format, ...) {
149 if (!buffer || !format || buffer_size == 0) {
150 return -1;
151 }
152
153 va_list args;
154 va_start(args, format);
155
156 /* Delegate to platform_snprintf for actual formatting */
157 int ret = platform_vsnprintf(buffer, buffer_size, format, args);
158
159 va_end(args);
160 return ret;
161}
162
172int safe_fprintf(FILE *stream, const char *format, ...) {
173 if (!stream || !format) {
174 return -1;
175 }
176
177 va_list args;
178 va_start(args, format);
179 int ret = vfprintf(stream, format, args);
180 va_end(args);
181
182 return ret;
183}
184
199int safe_vsnprintf(char *buffer, size_t buffer_size, const char *format, va_list ap) {
200 if (!format) {
201 return -1;
202 }
203
204 // Allow NULL buffer with size 0 for size calculation (standard vsnprintf behavior)
205 if (!buffer && buffer_size > 0) {
206 return -1; // Non-NULL buffer required when buffer_size > 0
207 }
208
209 /* Delegate to platform_vsnprintf for actual formatting */
210 return platform_vsnprintf(buffer, buffer_size, format, ap);
211}
212
213// ============================================================================
214// I/O Functions
215// ============================================================================
216
224size_t platform_write_all(int fd, const void *buf, size_t count) {
225 if (!buf || count == 0) {
226 return 0;
227 }
228
229 size_t written_total = 0;
230 int attempts = 0;
231 const int MAX_ATTEMPTS = 1000;
232
233 while (written_total < count && attempts < MAX_ATTEMPTS) {
234 ssize_t result = platform_write(fd, (const char *)buf + written_total, count - written_total);
235
236 if (result > 0) {
237 written_total += (size_t)result;
238 attempts = 0; // Reset attempt counter on successful write
239 } else if (result < 0) {
240 // Handle EAGAIN (non-blocking would-block) with sleep instead of tight loop
241 if (errno == EAGAIN || errno == EWOULDBLOCK) {
242 // Sleep 100us before retrying to avoid busy-waiting and spinning CPU
244 } else {
245 // Other write errors - log and retry
246 log_warn("platform_write_all: write() error on fd=%d (wrote %zu/%zu so far, errno=%d)", fd, written_total,
247 count, errno);
248 }
249 attempts++;
250 } else {
251 // result == 0: no bytes written, retry
252 attempts++;
253 }
254 }
255
256 if (attempts >= MAX_ATTEMPTS && written_total < count) {
257 log_warn("platform_write_all: Hit retry limit on fd=%d: wrote %zu of %zu bytes", fd, written_total, count);
258 }
259
260 return written_total;
261}
262#endif // PLATFORM_SYSTEM_C
⚛️ Atomic operations abstraction layer with debug tracking
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Cross-platform error handling utilities.
Cross-platform filesystem operations.
int buffer_size
Size of circular buffer.
Definition grep.c:90
#define log_warn(...)
Log a WARN message.
Definition log/log.h:574
int safe_fprintf(FILE *stream, const char *format,...)
Safe formatted output to file stream.
Definition system.c:172
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
size_t platform_write_all(int fd, const void *buf, size_t count)
Write all bytes to a file descriptor, handling partial writes.
Definition system.c:224
#define EWOULDBLOCK
int safe_vsnprintf(char *buffer, size_t buffer_size, const char *format, va_list ap)
Safe formatted string printing with va_list.
Definition system.c:199
void platform_sleep_us(unsigned int us)
High-precision sleep function with microsecond precision.
int platform_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Safe variable-argument string formatting.
int errno
ssize_t platform_write(int fd, const void *buf, size_t count)
Safe file write (write replacement)
Definition wasm/system.c:72
#define EAGAIN
Lock-free module lifecycle state machine using pure stdatomic.
📝 Logging API with multiple log levels and terminal output control
action_args_t args
Cache entry for binary PATH lookup.
Definition filesystem.c:75
bool check_binary_in_path_uncached(const char *bin_name)
⏱️ High-precision timing utilities using sokol_time.h and uthash
🔤 String Manipulation and Shell Escaping Utilities