-
Notifications
You must be signed in to change notification settings - Fork 1
/
shims.c
110 lines (84 loc) · 2.3 KB
/
shims.c
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
#include "shims.h"
#if defined(_WIN32) || defined(_WIN64)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
static const size_t INITIAL_SIZE = 128;
static const size_t GROWTH_FACTOR = 2;
if (lineptr == NULL || stream == NULL || n == NULL) {
errno = EINVAL;
return -1;
}
char *buffer = *lineptr;
size_t size = *n;
size_t position = 0;
if (buffer == NULL || size == 0) {
size = INITIAL_SIZE;
buffer = malloc(size);
if (buffer == NULL) {
errno = ENOMEM;
return -1;
}
}
int c;
while ((c = fgetc(stream)) != EOF) {
if (position >= size - 1) {
size_t new_size = size * GROWTH_FACTOR;
char *new_buffer = realloc(buffer, new_size);
if (new_buffer == NULL) {
errno = ENOMEM;
free(buffer);
return -1;
}
buffer = new_buffer;
size = new_size;
}
buffer[position++] = (char)c;
if (c == '\n') break;
}
buffer[position] = '\0';
*lineptr = buffer;
*n = position;
return (position == 0 && c == EOF) ? -1 : (ssize_t)position;
}
char* readline(const char* prompt) {
printf("%s", prompt); // Display the prompt
char* line = NULL;
size_t bufferSize = 0;
ssize_t lineSize;
lineSize = getline(&line, &bufferSize, stdin); // Read input
if (lineSize == -1) {
free(line);
return NULL; // Error or EOF
}
if (lineSize > 0 && line[lineSize - 1] == '\n') {
line[lineSize - 1] = '\0'; // Remove newline character
}
return line; // Caller must free this memory
}
void using_history(void) {
// Placeholder - no operation
}
int read_history(const char *filename) {
// Placeholder - no operation
return 0; // Success
}
int write_history(const char *filename) {
// Placeholder - no operation
return 0; // Success
}
int rl_bind_key(int key, int func) {
// Placeholder - no operation
return 0; // Success
}
HIST_ENTRY* history_get(int index) {
// Placeholder - no operation
return NULL;
}
int add_history(const char *line) {
// Placeholder - no operation
return 0; // Success
}
#endif