Skip to content

Commit e9b859e

Browse files
committed
Fix fopencookie, stub on windows.
1 parent 373ae46 commit e9b859e

File tree

2 files changed

+11
-99
lines changed

2 files changed

+11
-99
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.o
22
*.os
33
*.so
4+
build

src/csnip/x/fopencookie.c

Lines changed: 10 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -99,113 +99,24 @@ FILE* x_fopencookie(void* restrict cookie,
9999

100100
#elif defined(_WIN32)
101101

102-
/* Windows implementation - simplified version for write-only logging streams */
102+
/* Windows stub implementation - fopencookie is not available on Windows */
103103

104-
#include <windows.h>
105-
#include <stdbool.h>
106-
#include <string.h>
107-
108-
/* Custom FILE* wrapper structure for Windows */
109-
struct win_fopencookie_file {
110-
FILE base; /* Must be first member to allow casting */
111-
void* cookie;
112-
x_cookie_io_functions_t io_funcs;
113-
char* write_buffer;
114-
size_t write_buffer_size;
115-
size_t write_buffer_pos;
116-
bool is_write_only;
117-
};
118-
119-
/* Global registry of our custom FILE* objects (simple approach) */
120-
static struct win_fopencookie_file* g_custom_files[64];
121-
static size_t g_custom_file_count = 0;
122-
123-
static struct win_fopencookie_file* get_custom_file(FILE* fp)
124-
{
125-
for (size_t i = 0; i < g_custom_file_count; ++i) {
126-
if ((FILE*)g_custom_files[i] == fp) {
127-
return g_custom_files[i];
128-
}
129-
}
130-
return NULL;
131-
}
132-
133-
/* Override functions - these will be called through function pointers if we could set them */
134-
static int win_fwrite_override(const void* ptr, size_t size, size_t count, FILE* stream)
135-
{
136-
struct win_fopencookie_file* custom = get_custom_file(stream);
137-
if (!custom || !custom->io_funcs.write) {
138-
return 0;
139-
}
140-
141-
size_t total_bytes = size * count;
142-
ssize_t result = custom->io_funcs.write(custom->cookie, (const char*)ptr, total_bytes);
143-
144-
if (result < 0) {
145-
return 0;
146-
}
147-
148-
return (int)(result / size);
149-
}
150-
151-
static int win_fclose_override(FILE* stream)
152-
{
153-
struct win_fopencookie_file* custom = get_custom_file(stream);
154-
if (!custom) {
155-
return EOF;
156-
}
157-
158-
int result = 0;
159-
if (custom->io_funcs.close) {
160-
result = custom->io_funcs.close(custom->cookie);
161-
}
162-
163-
/* Remove from registry */
164-
for (size_t i = 0; i < g_custom_file_count; ++i) {
165-
if (g_custom_files[i] == custom) {
166-
memmove(&g_custom_files[i], &g_custom_files[i + 1],
167-
(g_custom_file_count - i - 1) * sizeof(struct win_fopencookie_file*));
168-
g_custom_file_count--;
169-
break;
170-
}
171-
}
172-
173-
free(custom->write_buffer);
174-
free(custom);
175-
176-
return result;
177-
}
104+
#include <stdio.h>
178105

179106
FILE* x_fopencookie(void* restrict cookie,
180107
const char* restrict mode,
181108
x_cookie_io_functions_t io_funcs)
182109
{
183-
/* Simplified Windows implementation - we'll create a fake FILE* structure
184-
* and intercept operations manually. This is not perfect but works for
185-
* basic write-only logging use cases.
110+
/* fopencookie is not supported on Windows.
111+
* This is a stub that prints a warning and returns NULL.
186112
*/
113+
(void)cookie; /* Suppress unused parameter warnings */
114+
(void)mode;
115+
(void)io_funcs;
187116

188-
if (g_custom_file_count >= sizeof(g_custom_files)/sizeof(g_custom_files[0])) {
189-
return NULL; /* Too many custom files */
190-
}
191-
192-
struct win_fopencookie_file* custom = malloc(sizeof(struct win_fopencookie_file));
193-
if (!custom) {
194-
return NULL;
195-
}
196-
197-
memset(custom, 0, sizeof(*custom));
198-
custom->cookie = cookie;
199-
custom->io_funcs = io_funcs;
200-
custom->is_write_only = (mode && (strchr(mode, 'w') || strchr(mode, 'a')));
201-
202-
/* Add to registry */
203-
g_custom_files[g_custom_file_count++] = custom;
204-
205-
/* Return the custom structure cast as FILE*
206-
* This is a hack, but necessary for Windows compatibility
207-
*/
208-
return (FILE*)custom;
117+
fprintf(stderr, "WARNING: x_fopencookie() called but not supported on Windows\n");
118+
errno = ENOSYS; /* Function not implemented */
119+
return NULL;
209120
}
210121

211122
#endif

0 commit comments

Comments
 (0)