@@ -86,18 +86,18 @@ static const char *fossil_fstream_mode_from_keyword(const char *keyword) {
8686// Open a stream for file operations
8787int32_t fossil_fstream_open (fossil_fstream_t * stream , const char * filename , const char * mode ) {
8888 if (stream == NULL || filename == NULL || mode == NULL ) {
89- fprintf ( stderr , "Error: Null pointer\n" );
89+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
9090 return FOSSIL_ERROR_CNULL_POINTER ;
9191 }
9292
9393 if (strlen (filename ) >= FOSSIL_BUFFER_MEDIUM ) {
94- fprintf ( stderr , "Error: Limit reached\n" );
94+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Limit reached\n" );
9595 return FOSSIL_ERROR_LIMIT_REACHED ;
9696 }
9797
9898 stream -> file = fopen (filename , fossil_fstream_mode_from_keyword (mode ));
9999 if (stream -> file == NULL ) {
100- fprintf ( stderr , "Error: File not found - %s\n" , filename );
100+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found - %s\n" , filename );
101101 return FOSSIL_ERROR_FILE_NOT_FOUND ;
102102 }
103103
@@ -110,21 +110,49 @@ int32_t fossil_fstream_open(fossil_fstream_t *stream, const char *filename, cons
110110void fossil_fstream_close (fossil_fstream_t * stream ) {
111111 if (stream != NULL && stream -> file != NULL ) {
112112 if (fclose (stream -> file ) != 0 ) {
113- fprintf ( stderr , "Error: Failed to close file - %s\n" , stream -> filename );
113+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Failed to close file - %s\n" , stream -> filename );
114114 }
115115 stream -> file = NULL ;
116116 }
117117}
118118
119+ int32_t fossil_fstream_redirect_to_devnull (fossil_fstream_t * stream ) {
120+ if (!stream ) {
121+ return -1 ; // Invalid stream
122+ }
123+
124+ #ifdef _WIN32
125+ FILE * devnull = fopen ("NUL" , "w" );
126+ #else
127+ FILE * devnull = fopen ("/dev/null" , "w" );
128+ #endif
129+
130+ if (!devnull ) {
131+ return -1 ; // Failed to open null device
132+ }
133+
134+ if (stream -> file ) {
135+ fclose (stream -> file );
136+ }
137+
138+ stream -> file = devnull ;
139+ #ifdef _WIN32
140+ snprintf (stream -> filename , sizeof (stream -> filename ), "NUL" );
141+ #else
142+ snprintf (stream -> filename , sizeof (stream -> filename ), "/dev/null" );
143+ #endif
144+ return 0 ;
145+ }
146+
119147int32_t fossil_fstream_freopen (fossil_fstream_t * stream , const char * filename , const char * mode , FILE * file ) {
120148 if (stream == NULL || filename == NULL || mode == NULL || file == NULL ) {
121- fprintf ( stderr , "Error: Null pointer\n" );
149+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
122150 return FOSSIL_ERROR_CNULL_POINTER ;
123151 }
124152
125153 FILE * new_file = freopen (filename , fossil_fstream_mode_from_keyword (mode ), file );
126154 if (new_file == NULL ) {
127- fprintf ( stderr , "Error: File not found - %s\n" , filename );
155+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found - %s\n" , filename );
128156 return FOSSIL_ERROR_FILE_NOT_FOUND ;
129157 }
130158
@@ -137,14 +165,14 @@ int32_t fossil_fstream_freopen(fossil_fstream_t *stream, const char *filename, c
137165// Read data from an open stream
138166size_t fossil_fstream_read (fossil_fstream_t * stream , void * buffer , size_t size , size_t count ) {
139167 if (stream == NULL || buffer == NULL || stream -> file == NULL ) {
140- fprintf ( stderr , "Error: Null pointer\n" );
168+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
141169 return FOSSIL_ERROR_CNULL_POINTER ;
142170 }
143171
144172 size_t bytes_read = fread (buffer , size , count , stream -> file );
145173
146174 if (bytes_read == 0 && ferror (stream -> file )) {
147- fprintf ( stderr , "Error: File corruption\n" );
175+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File corruption\n" );
148176 return FOSSIL_ERROR_FILE_CORRUPTION ;
149177 }
150178
@@ -154,14 +182,14 @@ size_t fossil_fstream_read(fossil_fstream_t *stream, void *buffer, size_t size,
154182// Write data to an open stream
155183size_t fossil_fstream_write (fossil_fstream_t * stream , const void * buffer , size_t size , size_t count ) {
156184 if (stream == NULL || buffer == NULL || stream -> file == NULL ) {
157- fprintf ( stderr , "Error: Null pointer\n" );
185+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
158186 return FOSSIL_ERROR_CNULL_POINTER ;
159187 }
160188
161189 size_t bytes_written = fwrite (buffer , size , count , stream -> file );
162190
163191 if (bytes_written == 0 && ferror (stream -> file )) {
164- fprintf ( stderr , "Error: File not found - %s\n" , strerror (errno ));
192+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found - %s\n" , strerror (errno ));
165193 return FOSSIL_ERROR_FILE_NOT_FOUND ;
166194 }
167195
@@ -171,15 +199,15 @@ size_t fossil_fstream_write(fossil_fstream_t *stream, const void *buffer, size_t
171199// Append data to the end of an open stream
172200int32_t fossil_fstream_append (fossil_fstream_t * stream , const void * restrict buffer , size_t size , int32_t count ) {
173201 if (stream == NULL || buffer == NULL || stream -> file == NULL ) {
174- fprintf ( stderr , "Error: Null pointer\n" );
202+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
175203 return FOSSIL_ERROR_CNULL_POINTER ;
176204 }
177205
178206 fseek (stream -> file , 0 , SEEK_END );
179207 int32_t result = fwrite (buffer , size , count , stream -> file );
180208
181209 if (result != count ) {
182- fprintf ( stderr , "Error: File not found\n" );
210+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found\n" );
183211 return FOSSIL_ERROR_FILE_NOT_FOUND ;
184212 }
185213
@@ -189,14 +217,14 @@ int32_t fossil_fstream_append(fossil_fstream_t *stream, const void * restrict bu
189217// Seek to a specified position in an open stream
190218int32_t fossil_fstream_seek (fossil_fstream_t * stream , int64_t offset , int32_t origin ) {
191219 if (stream == NULL || stream -> file == NULL ) {
192- fprintf ( stderr , "Error: Null pointer\n" );
220+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
193221 return FOSSIL_ERROR_CNULL_POINTER ;
194222 }
195223
196224 int32_t result = fseek (stream -> file , offset , origin );
197225
198226 if (result != 0 ) {
199- fprintf ( stderr , "Error: File not found - %s\n" , strerror (errno ));
227+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found - %s\n" , strerror (errno ));
200228 return FOSSIL_ERROR_FILE_NOT_FOUND ;
201229 }
202230
@@ -206,14 +234,14 @@ int32_t fossil_fstream_seek(fossil_fstream_t *stream, int64_t offset, int32_t or
206234// Get the current position of the file pointer in an open stream
207235int32_t fossil_fstream_tell (fossil_fstream_t * stream ) {
208236 if (stream == NULL || stream -> file == NULL ) {
209- fprintf ( stderr , "Error: Null pointer\n" );
237+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
210238 return FOSSIL_ERROR_CNULL_POINTER ;
211239 }
212240
213241 long position = ftell (stream -> file );
214242
215243 if (position == -1L && ferror (stream -> file )) {
216- fprintf ( stderr , "Error: IO error from getting file position\n" );
244+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: IO error from getting file position\n" );
217245 return FOSSIL_ERROR_IO ;
218246 }
219247
@@ -223,27 +251,27 @@ int32_t fossil_fstream_tell(fossil_fstream_t *stream) {
223251// Save an open stream to a new file
224252int32_t fossil_fstream_save (fossil_fstream_t * stream , const char * new_filename ) {
225253 if (stream == NULL || stream -> file == NULL || new_filename == NULL ) {
226- fprintf ( stderr , "Error: Null pointer\n" );
254+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
227255 return FOSSIL_ERROR_CNULL_POINTER ;
228256 }
229257
230258 if (strlen (new_filename ) >= FOSSIL_BUFFER_MEDIUM ) {
231- fprintf ( stderr , "Error: Limit reached\n" );
259+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Limit reached\n" );
232260 return FOSSIL_ERROR_LIMIT_REACHED ;
233261 }
234262
235263 fclose (stream -> file );
236264
237265 if (rename (stream -> filename , new_filename ) != 0 ) {
238- fprintf ( stderr , "Error: Failed to save %s\n" , new_filename );
266+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Failed to save %s\n" , new_filename );
239267 return FOSSIL_ERROR_CNULL_POINTER ;
240268 }
241269
242270 // Reopen the file with the new name
243271 int32_t result = fossil_fstream_open (stream , new_filename , "r" );
244272
245273 if (result != FOSSIL_ERROR_OK ) {
246- fprintf ( stderr , "Error: File not found - %s\n" , strerror (errno ));
274+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found - %s\n" , strerror (errno ));
247275 return FOSSIL_ERROR_FILE_NOT_FOUND ;
248276 }
249277 return result ;
@@ -252,19 +280,19 @@ int32_t fossil_fstream_save(fossil_fstream_t *stream, const char *new_filename)
252280// Copy a file from the source to the destination
253281int32_t fossil_fstream_copy (const char * source_filename , const char * destination_filename ) {
254282 if (source_filename == NULL || destination_filename == NULL ) {
255- fprintf ( stderr , "Error: Null pointer\n" );
283+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
256284 return FOSSIL_ERROR_CNULL_POINTER ;
257285 }
258286
259287 FILE * source_file = fopen (source_filename , "rb" );
260288 if (source_file == NULL ) {
261- fprintf ( stderr , "Error: File not found - %s\n" , source_filename );
289+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found - %s\n" , source_filename );
262290 return FOSSIL_ERROR_FILE_NOT_FOUND ;
263291 }
264292
265293 FILE * destination_file = fopen (destination_filename , "wb" );
266294 if (destination_file == NULL ) {
267- fprintf ( stderr , "Error: File not found - %s\n" , destination_filename );
295+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found - %s\n" , destination_filename );
268296 fclose (source_file );
269297 return FOSSIL_ERROR_FILE_NOT_FOUND ;
270298 }
@@ -275,7 +303,7 @@ int32_t fossil_fstream_copy(const char *source_filename, const char *destination
275303 while ((bytesRead = fread (buffer , 1 , FOSSIL_BUFFER_MEDIUM , source_file )) > 0 ) {
276304 size_t bytesWritten = fwrite (buffer , 1 , bytesRead , destination_file );
277305 if (bytesWritten != bytesRead ) {
278- fprintf ( stderr , "Error: File not found\n" );
306+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found\n" );
279307 fclose (source_file );
280308 fclose (destination_file );
281309 return FOSSIL_ERROR_FILE_NOT_FOUND ;
@@ -290,26 +318,26 @@ int32_t fossil_fstream_copy(const char *source_filename, const char *destination
290318
291319int32_t fossil_fstream_remove (const char * filename ) {
292320 if (filename == NULL ) {
293- fprintf ( stderr , "Error: Null pointer\n" );
321+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
294322 return FOSSIL_ERROR_CNULL_POINTER ;
295323 }
296324
297325 if (remove (filename ) == 0 ) {
298326 return FOSSIL_ERROR_OK ; // File removed successfully
299327 }
300328
301- fprintf ( stderr , "Error: IO error when removing file %s\n" , filename );
329+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: IO error when removing file %s\n" , filename );
302330 return FOSSIL_ERROR_IO ;
303331}
304332
305333int32_t fossil_fstream_rename (const char * old_filename , const char * new_filename ) {
306334 if (old_filename == NULL || new_filename == NULL ) {
307- fprintf ( stderr , "Error: Null pointer\n" );
335+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
308336 return FOSSIL_ERROR_CNULL_POINTER ;
309337 }
310338
311339 if (rename (old_filename , new_filename ) != 0 ) {
312- fprintf ( stderr , "Error: Failed to rename file %s\n" , old_filename );
340+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Failed to rename file %s\n" , old_filename );
313341 return FOSSIL_ERROR_IO ;
314342 }
315343
@@ -318,12 +346,12 @@ int32_t fossil_fstream_rename(const char *old_filename, const char *new_filename
318346
319347int32_t fossil_fstream_flush (fossil_fstream_t * stream ) {
320348 if (stream == NULL || stream -> file == NULL ) {
321- fprintf ( stderr , "Error: Null pointer\n" );
349+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
322350 return FOSSIL_ERROR_CNULL_POINTER ;
323351 }
324352
325353 if (fflush (stream -> file ) != 0 ) {
326- fprintf ( stderr , "Error: Failed to flush file\n" );
354+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Failed to flush file\n" );
327355 return FOSSIL_ERROR_IO ;
328356 }
329357
@@ -332,12 +360,12 @@ int32_t fossil_fstream_flush(fossil_fstream_t *stream) {
332360
333361int32_t fossil_fstream_setpos (fossil_fstream_t * stream , int32_t pos ) {
334362 if (stream == NULL || stream -> file == NULL ) {
335- fprintf ( stderr , "Error: Null pointer\n" );
363+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
336364 return FOSSIL_ERROR_CNULL_POINTER ;
337365 }
338366
339367 if (fseek (stream -> file , pos , SEEK_SET ) != 0 ) {
340- fprintf ( stderr , "Error: Failed to set file position\n" );
368+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Failed to set file position\n" );
341369 return FOSSIL_ERROR_IO ;
342370 }
343371
@@ -346,13 +374,13 @@ int32_t fossil_fstream_setpos(fossil_fstream_t *stream, int32_t pos) {
346374
347375int32_t fossil_fstream_getpos (fossil_fstream_t * stream , int32_t * pos ) {
348376 if (stream == NULL || stream -> file == NULL || pos == NULL ) {
349- fprintf ( stderr , "Error: Null pointer\n" );
377+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
350378 return FOSSIL_ERROR_CNULL_POINTER ;
351379 }
352380
353381 * pos = ftell (stream -> file );
354382 if (* pos == -1L && ferror (stream -> file )) {
355- fprintf ( stderr , "Error: IO error from getting file position\n" );
383+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: IO error from getting file position\n" );
356384 return FOSSIL_ERROR_IO ;
357385 }
358386
@@ -361,7 +389,7 @@ int32_t fossil_fstream_getpos(fossil_fstream_t *stream, int32_t *pos) {
361389
362390int32_t fossil_fstream_rotate (const char * filename , int32_t n ) {
363391 if (filename == NULL ) {
364- fprintf ( stderr , "Error: Null pointer\n" );
392+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
365393 return FOSSIL_ERROR_CNULL_POINTER ;
366394 }
367395
@@ -377,7 +405,7 @@ int32_t fossil_fstream_rotate(const char *filename, int32_t n) {
377405
378406 snprintf (new_filename , FOSSIL_BUFFER_MEDIUM , "%s.%d" , filename , i );
379407 if (fossil_fstream_rename (old_filename , new_filename ) != FOSSIL_ERROR_OK ) {
380- fprintf ( stderr , "Error: Failed to rotate file %s\n" , filename );
408+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Failed to rotate file %s\n" , filename );
381409 return FOSSIL_ERROR_IO ;
382410 }
383411 }
@@ -388,15 +416,15 @@ int32_t fossil_fstream_rotate(const char *filename, int32_t n) {
388416// Create a backup of a file with a specified backup suffix
389417int32_t fossil_fstream_backup (const char * filename , const char * backup_suffix ) {
390418 if (filename == NULL || backup_suffix == NULL ) {
391- fprintf ( stderr , "Error: Null pointer\n" );
419+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
392420 return FOSSIL_ERROR_CNULL_POINTER ;
393421 }
394422
395423 char backup_filename [FOSSIL_BUFFER_MEDIUM + 10 ]; // Length of backup_suffix + maximum integer length
396424 snprintf (backup_filename , FOSSIL_BUFFER_MEDIUM + 10 , "%s%s" , filename , backup_suffix );
397425
398426 if (fossil_fstream_copy (filename , backup_filename ) != FOSSIL_ERROR_OK ) {
399- fprintf ( stderr , "Error: Failed to create backup for %s\n" , filename );
427+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Failed to create backup for %s\n" , filename );
400428 return FOSSIL_ERROR_IO ;
401429 }
402430
@@ -406,7 +434,7 @@ int32_t fossil_fstream_backup(const char *filename, const char *backup_suffix) {
406434// Check if a file exists
407435int32_t fossil_fstream_file_exists (const char * filename ) {
408436 if (filename == NULL ) {
409- fprintf ( stderr , "Error: Null pointer\n" );
437+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
410438 return FOSSIL_ERROR_CNULL_POINTER ;
411439 }
412440
@@ -421,7 +449,7 @@ int32_t fossil_fstream_file_exists(const char *filename) {
421449// Get the size of an open stream
422450int32_t fossil_fstream_get_size (fossil_fstream_t * stream ) {
423451 if (stream == NULL || stream -> file == NULL ) {
424- fprintf ( stderr , "Error: Null pointer\n" );
452+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
425453 return FOSSIL_ERROR_CNULL_POINTER ;
426454 }
427455
@@ -430,7 +458,7 @@ int32_t fossil_fstream_get_size(fossil_fstream_t *stream) {
430458 rewind (stream -> file );
431459
432460 if (size == -1L && ferror (stream -> file )) {
433- fprintf ( stderr , "Error: IO error from getting file size\n" );
461+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: IO error from getting file size\n" );
434462 return FOSSIL_ERROR_OK ;
435463 }
436464
@@ -440,22 +468,22 @@ int32_t fossil_fstream_get_size(fossil_fstream_t *stream) {
440468// Delete a file
441469int32_t fossil_fstream_delete (const char * filename ) {
442470 if (filename == NULL ) {
443- fprintf ( stderr , "Error: Null pointer\n" );
471+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
444472 return FOSSIL_ERROR_CNULL_POINTER ;
445473 }
446474
447475 if (remove (filename ) == 0 ) {
448476 return FOSSIL_ERROR_OK ; // File deleted successfully
449477 }
450478
451- fprintf ( stderr , "Error: IO error when deleting file %s\n" , filename );
479+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: IO error when deleting file %s\n" , filename );
452480 return FOSSIL_ERROR_IO ;
453481}
454482
455483// Detect file type (Regular file, Directory, Symbolic link)
456484int fossil_fstream_get_type (const char * filename ) {
457485 if (filename == NULL ) {
458- fprintf ( stderr , "Error: Null pointer\n" );
486+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
459487 return -1 ;
460488 }
461489
0 commit comments