-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduef_logger.c
More file actions
107 lines (87 loc) · 2.24 KB
/
duef_logger.c
File metadata and controls
107 lines (87 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "duef_logger.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#define MAX_PATH 260
#else
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#endif
// Safe logging functions to replace insecure fprintf calls
void log_error(const char *format, ...)
{
va_list args;
va_start(args, format);
// Use vfprintf which is safer than fprintf with user input
vfprintf(stderr, format, args);
va_end(args);
}
void log_info(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
void log_verbose(const char *format, ...)
{
extern int g_is_verbose;
if (!g_is_verbose) {
return;
}
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
#ifdef _WIN32
int safe_remove_directory_windows(const char *directory_path)
{
char path[MAX_PATH + 1];
// Copy path and ensure double null termination
strncpy(path, directory_path, MAX_PATH - 1);
path[MAX_PATH - 1] = '\0';
// Use simple rmdir for now - for full recursive deletion would need more complex implementation
return _rmdir(path);
}
#else
int safe_remove_directory_unix(const char *directory_path)
{
DIR *dir;
struct dirent *entry;
char path[1024];
dir = opendir(directory_path);
if (dir == NULL) {
return -1;
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
snprintf(path, sizeof(path), "%s/%s", directory_path, entry->d_name);
struct stat statbuf;
if (stat(path, &statbuf) == 0) {
if (S_ISDIR(statbuf.st_mode)) {
safe_remove_directory_unix(path);
} else {
unlink(path);
}
}
}
closedir(dir);
return rmdir(directory_path);
}
#endif
int safe_remove_directory(const char *directory_path)
{
#ifdef _WIN32
return safe_remove_directory_windows(directory_path);
#else
return safe_remove_directory_unix(directory_path);
#endif
}