@@ -86,18 +86,18 @@ static const char *fossil_fstream_mode_from_keyword(const char *keyword) {
86
86
// Open a stream for file operations
87
87
int32_t fossil_fstream_open (fossil_fstream_t * stream , const char * filename , const char * mode ) {
88
88
if (stream == NULL || filename == NULL || mode == NULL ) {
89
- fprintf ( stderr , "Error: Null pointer\n" );
89
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
90
90
return FOSSIL_ERROR_CNULL_POINTER ;
91
91
}
92
92
93
93
if (strlen (filename ) >= FOSSIL_BUFFER_MEDIUM ) {
94
- fprintf ( stderr , "Error: Limit reached\n" );
94
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Limit reached\n" );
95
95
return FOSSIL_ERROR_LIMIT_REACHED ;
96
96
}
97
97
98
98
stream -> file = fopen (filename , fossil_fstream_mode_from_keyword (mode ));
99
99
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 );
101
101
return FOSSIL_ERROR_FILE_NOT_FOUND ;
102
102
}
103
103
@@ -110,21 +110,49 @@ int32_t fossil_fstream_open(fossil_fstream_t *stream, const char *filename, cons
110
110
void fossil_fstream_close (fossil_fstream_t * stream ) {
111
111
if (stream != NULL && stream -> file != NULL ) {
112
112
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 );
114
114
}
115
115
stream -> file = NULL ;
116
116
}
117
117
}
118
118
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
+
119
147
int32_t fossil_fstream_freopen (fossil_fstream_t * stream , const char * filename , const char * mode , FILE * file ) {
120
148
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" );
122
150
return FOSSIL_ERROR_CNULL_POINTER ;
123
151
}
124
152
125
153
FILE * new_file = freopen (filename , fossil_fstream_mode_from_keyword (mode ), file );
126
154
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 );
128
156
return FOSSIL_ERROR_FILE_NOT_FOUND ;
129
157
}
130
158
@@ -137,14 +165,14 @@ int32_t fossil_fstream_freopen(fossil_fstream_t *stream, const char *filename, c
137
165
// Read data from an open stream
138
166
size_t fossil_fstream_read (fossil_fstream_t * stream , void * buffer , size_t size , size_t count ) {
139
167
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" );
141
169
return FOSSIL_ERROR_CNULL_POINTER ;
142
170
}
143
171
144
172
size_t bytes_read = fread (buffer , size , count , stream -> file );
145
173
146
174
if (bytes_read == 0 && ferror (stream -> file )) {
147
- fprintf ( stderr , "Error: File corruption\n" );
175
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File corruption\n" );
148
176
return FOSSIL_ERROR_FILE_CORRUPTION ;
149
177
}
150
178
@@ -154,14 +182,14 @@ size_t fossil_fstream_read(fossil_fstream_t *stream, void *buffer, size_t size,
154
182
// Write data to an open stream
155
183
size_t fossil_fstream_write (fossil_fstream_t * stream , const void * buffer , size_t size , size_t count ) {
156
184
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" );
158
186
return FOSSIL_ERROR_CNULL_POINTER ;
159
187
}
160
188
161
189
size_t bytes_written = fwrite (buffer , size , count , stream -> file );
162
190
163
191
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 ));
165
193
return FOSSIL_ERROR_FILE_NOT_FOUND ;
166
194
}
167
195
@@ -171,15 +199,15 @@ size_t fossil_fstream_write(fossil_fstream_t *stream, const void *buffer, size_t
171
199
// Append data to the end of an open stream
172
200
int32_t fossil_fstream_append (fossil_fstream_t * stream , const void * restrict buffer , size_t size , int32_t count ) {
173
201
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" );
175
203
return FOSSIL_ERROR_CNULL_POINTER ;
176
204
}
177
205
178
206
fseek (stream -> file , 0 , SEEK_END );
179
207
int32_t result = fwrite (buffer , size , count , stream -> file );
180
208
181
209
if (result != count ) {
182
- fprintf ( stderr , "Error: File not found\n" );
210
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found\n" );
183
211
return FOSSIL_ERROR_FILE_NOT_FOUND ;
184
212
}
185
213
@@ -189,14 +217,14 @@ int32_t fossil_fstream_append(fossil_fstream_t *stream, const void * restrict bu
189
217
// Seek to a specified position in an open stream
190
218
int32_t fossil_fstream_seek (fossil_fstream_t * stream , int64_t offset , int32_t origin ) {
191
219
if (stream == NULL || stream -> file == NULL ) {
192
- fprintf ( stderr , "Error: Null pointer\n" );
220
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
193
221
return FOSSIL_ERROR_CNULL_POINTER ;
194
222
}
195
223
196
224
int32_t result = fseek (stream -> file , offset , origin );
197
225
198
226
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 ));
200
228
return FOSSIL_ERROR_FILE_NOT_FOUND ;
201
229
}
202
230
@@ -206,14 +234,14 @@ int32_t fossil_fstream_seek(fossil_fstream_t *stream, int64_t offset, int32_t or
206
234
// Get the current position of the file pointer in an open stream
207
235
int32_t fossil_fstream_tell (fossil_fstream_t * stream ) {
208
236
if (stream == NULL || stream -> file == NULL ) {
209
- fprintf ( stderr , "Error: Null pointer\n" );
237
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
210
238
return FOSSIL_ERROR_CNULL_POINTER ;
211
239
}
212
240
213
241
long position = ftell (stream -> file );
214
242
215
243
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" );
217
245
return FOSSIL_ERROR_IO ;
218
246
}
219
247
@@ -223,27 +251,27 @@ int32_t fossil_fstream_tell(fossil_fstream_t *stream) {
223
251
// Save an open stream to a new file
224
252
int32_t fossil_fstream_save (fossil_fstream_t * stream , const char * new_filename ) {
225
253
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" );
227
255
return FOSSIL_ERROR_CNULL_POINTER ;
228
256
}
229
257
230
258
if (strlen (new_filename ) >= FOSSIL_BUFFER_MEDIUM ) {
231
- fprintf ( stderr , "Error: Limit reached\n" );
259
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Limit reached\n" );
232
260
return FOSSIL_ERROR_LIMIT_REACHED ;
233
261
}
234
262
235
263
fclose (stream -> file );
236
264
237
265
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 );
239
267
return FOSSIL_ERROR_CNULL_POINTER ;
240
268
}
241
269
242
270
// Reopen the file with the new name
243
271
int32_t result = fossil_fstream_open (stream , new_filename , "r" );
244
272
245
273
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 ));
247
275
return FOSSIL_ERROR_FILE_NOT_FOUND ;
248
276
}
249
277
return result ;
@@ -252,19 +280,19 @@ int32_t fossil_fstream_save(fossil_fstream_t *stream, const char *new_filename)
252
280
// Copy a file from the source to the destination
253
281
int32_t fossil_fstream_copy (const char * source_filename , const char * destination_filename ) {
254
282
if (source_filename == NULL || destination_filename == NULL ) {
255
- fprintf ( stderr , "Error: Null pointer\n" );
283
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
256
284
return FOSSIL_ERROR_CNULL_POINTER ;
257
285
}
258
286
259
287
FILE * source_file = fopen (source_filename , "rb" );
260
288
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 );
262
290
return FOSSIL_ERROR_FILE_NOT_FOUND ;
263
291
}
264
292
265
293
FILE * destination_file = fopen (destination_filename , "wb" );
266
294
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 );
268
296
fclose (source_file );
269
297
return FOSSIL_ERROR_FILE_NOT_FOUND ;
270
298
}
@@ -275,7 +303,7 @@ int32_t fossil_fstream_copy(const char *source_filename, const char *destination
275
303
while ((bytesRead = fread (buffer , 1 , FOSSIL_BUFFER_MEDIUM , source_file )) > 0 ) {
276
304
size_t bytesWritten = fwrite (buffer , 1 , bytesRead , destination_file );
277
305
if (bytesWritten != bytesRead ) {
278
- fprintf ( stderr , "Error: File not found\n" );
306
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: File not found\n" );
279
307
fclose (source_file );
280
308
fclose (destination_file );
281
309
return FOSSIL_ERROR_FILE_NOT_FOUND ;
@@ -290,26 +318,26 @@ int32_t fossil_fstream_copy(const char *source_filename, const char *destination
290
318
291
319
int32_t fossil_fstream_remove (const char * filename ) {
292
320
if (filename == NULL ) {
293
- fprintf ( stderr , "Error: Null pointer\n" );
321
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
294
322
return FOSSIL_ERROR_CNULL_POINTER ;
295
323
}
296
324
297
325
if (remove (filename ) == 0 ) {
298
326
return FOSSIL_ERROR_OK ; // File removed successfully
299
327
}
300
328
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 );
302
330
return FOSSIL_ERROR_IO ;
303
331
}
304
332
305
333
int32_t fossil_fstream_rename (const char * old_filename , const char * new_filename ) {
306
334
if (old_filename == NULL || new_filename == NULL ) {
307
- fprintf ( stderr , "Error: Null pointer\n" );
335
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
308
336
return FOSSIL_ERROR_CNULL_POINTER ;
309
337
}
310
338
311
339
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 );
313
341
return FOSSIL_ERROR_IO ;
314
342
}
315
343
@@ -318,12 +346,12 @@ int32_t fossil_fstream_rename(const char *old_filename, const char *new_filename
318
346
319
347
int32_t fossil_fstream_flush (fossil_fstream_t * stream ) {
320
348
if (stream == NULL || stream -> file == NULL ) {
321
- fprintf ( stderr , "Error: Null pointer\n" );
349
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
322
350
return FOSSIL_ERROR_CNULL_POINTER ;
323
351
}
324
352
325
353
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" );
327
355
return FOSSIL_ERROR_IO ;
328
356
}
329
357
@@ -332,12 +360,12 @@ int32_t fossil_fstream_flush(fossil_fstream_t *stream) {
332
360
333
361
int32_t fossil_fstream_setpos (fossil_fstream_t * stream , int32_t pos ) {
334
362
if (stream == NULL || stream -> file == NULL ) {
335
- fprintf ( stderr , "Error: Null pointer\n" );
363
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
336
364
return FOSSIL_ERROR_CNULL_POINTER ;
337
365
}
338
366
339
367
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" );
341
369
return FOSSIL_ERROR_IO ;
342
370
}
343
371
@@ -346,13 +374,13 @@ int32_t fossil_fstream_setpos(fossil_fstream_t *stream, int32_t pos) {
346
374
347
375
int32_t fossil_fstream_getpos (fossil_fstream_t * stream , int32_t * pos ) {
348
376
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" );
350
378
return FOSSIL_ERROR_CNULL_POINTER ;
351
379
}
352
380
353
381
* pos = ftell (stream -> file );
354
382
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" );
356
384
return FOSSIL_ERROR_IO ;
357
385
}
358
386
@@ -361,7 +389,7 @@ int32_t fossil_fstream_getpos(fossil_fstream_t *stream, int32_t *pos) {
361
389
362
390
int32_t fossil_fstream_rotate (const char * filename , int32_t n ) {
363
391
if (filename == NULL ) {
364
- fprintf ( stderr , "Error: Null pointer\n" );
392
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
365
393
return FOSSIL_ERROR_CNULL_POINTER ;
366
394
}
367
395
@@ -377,7 +405,7 @@ int32_t fossil_fstream_rotate(const char *filename, int32_t n) {
377
405
378
406
snprintf (new_filename , FOSSIL_BUFFER_MEDIUM , "%s.%d" , filename , i );
379
407
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 );
381
409
return FOSSIL_ERROR_IO ;
382
410
}
383
411
}
@@ -388,15 +416,15 @@ int32_t fossil_fstream_rotate(const char *filename, int32_t n) {
388
416
// Create a backup of a file with a specified backup suffix
389
417
int32_t fossil_fstream_backup (const char * filename , const char * backup_suffix ) {
390
418
if (filename == NULL || backup_suffix == NULL ) {
391
- fprintf ( stderr , "Error: Null pointer\n" );
419
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
392
420
return FOSSIL_ERROR_CNULL_POINTER ;
393
421
}
394
422
395
423
char backup_filename [FOSSIL_BUFFER_MEDIUM + 10 ]; // Length of backup_suffix + maximum integer length
396
424
snprintf (backup_filename , FOSSIL_BUFFER_MEDIUM + 10 , "%s%s" , filename , backup_suffix );
397
425
398
426
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 );
400
428
return FOSSIL_ERROR_IO ;
401
429
}
402
430
@@ -406,7 +434,7 @@ int32_t fossil_fstream_backup(const char *filename, const char *backup_suffix) {
406
434
// Check if a file exists
407
435
int32_t fossil_fstream_file_exists (const char * filename ) {
408
436
if (filename == NULL ) {
409
- fprintf ( stderr , "Error: Null pointer\n" );
437
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
410
438
return FOSSIL_ERROR_CNULL_POINTER ;
411
439
}
412
440
@@ -421,7 +449,7 @@ int32_t fossil_fstream_file_exists(const char *filename) {
421
449
// Get the size of an open stream
422
450
int32_t fossil_fstream_get_size (fossil_fstream_t * stream ) {
423
451
if (stream == NULL || stream -> file == NULL ) {
424
- fprintf ( stderr , "Error: Null pointer\n" );
452
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
425
453
return FOSSIL_ERROR_CNULL_POINTER ;
426
454
}
427
455
@@ -430,7 +458,7 @@ int32_t fossil_fstream_get_size(fossil_fstream_t *stream) {
430
458
rewind (stream -> file );
431
459
432
460
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" );
434
462
return FOSSIL_ERROR_OK ;
435
463
}
436
464
@@ -440,22 +468,22 @@ int32_t fossil_fstream_get_size(fossil_fstream_t *stream) {
440
468
// Delete a file
441
469
int32_t fossil_fstream_delete (const char * filename ) {
442
470
if (filename == NULL ) {
443
- fprintf ( stderr , "Error: Null pointer\n" );
471
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
444
472
return FOSSIL_ERROR_CNULL_POINTER ;
445
473
}
446
474
447
475
if (remove (filename ) == 0 ) {
448
476
return FOSSIL_ERROR_OK ; // File deleted successfully
449
477
}
450
478
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 );
452
480
return FOSSIL_ERROR_IO ;
453
481
}
454
482
455
483
// Detect file type (Regular file, Directory, Symbolic link)
456
484
int fossil_fstream_get_type (const char * filename ) {
457
485
if (filename == NULL ) {
458
- fprintf ( stderr , "Error: Null pointer\n" );
486
+ fossil_io_fprintf ( FOSSIL_STDERR , "Error: Null pointer\n" );
459
487
return -1 ;
460
488
}
461
489
0 commit comments