Debian Codegraph @

wine/common/crt.c

raw
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <stdint.h>
5 #include <windows.h>
6 #include <ntstatus.h>
7 #include <winternl.h>
8
9 #include "os.h"
10
11 int __mingw_sprintf(char* buffer, const char* format, ...)
12 {
13 va_list args;
14 int ret;
15
16 va_start(args, format);
17 ret = vsprintf(buffer, format, args);
18 va_end(args);
19
20 return ret;
21 }
22
23 int __isnanf(float x)
24 {
25 union { float x; unsigned int i; } u = { x };
26 return (u.i & 0x7fffffff) > 0x7f800000;
27 }
28
29 int __isnan(double x)
30 {
31 union { double x; unsigned __int64 i; } u = { x };
32 return (u.i & ~0ull >> 1) > 0x7ffull << 52;
33 }
34
35 double math_error(int type, const char *name, double arg1, double arg2, double retval)
36 {
37 return retval;
38 }
39
40 int __fpclassify(double x)
41 {
42 union {double f; uint64_t i;} u = {x};
43 int e = u.i>>52 & 0x7ff;
44 if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
45 if (e==0x7ff) return u.i<<12 ? FP_NAN : FP_INFINITE;
46 return FP_NORMAL;
47 }
48
49 int __fpclassifyf(float x)
50 {
51 union {float f; uint32_t i;} u = {x};
52 int e = u.i>>23 & 0xff;
53 if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
54 if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
55 return FP_NORMAL;
56 }
57
58 int fegetround (void)
59 {
60 return 0;
61 }
62
63 int fesetround (int __rounding_direction)
64 {
65 return 0;
66 }
67
68 div_t __cdecl div(int num, int denom)
69 {
70 div_t ret;
71
72 ret.quot = num / denom;
73 ret.rem = num % denom;
74 return ret;
75 }
76
77 ldiv_t __cdecl ldiv(long num, long denom)
78 {
79 ldiv_t ret;
80
81 ret.quot = num / denom;
82 ret.rem = num % denom;
83 return ret;
84 }
85
86 void _assert (const char *_Message, const char *_File, unsigned _Line)
87 {
88 // NYI
89 }
90
91 char* strerror(int e)
92 {
93 return "error";
94 }
95
96 int snprintf(char* restrict s, size_t n, const char* restrict fmt, ...)
97 {
98 va_list args;
99 va_start(args, fmt);
100 int result = _vsnprintf(s, n, fmt, args);
101 va_end(args);
102 return result;
103 }
104
105 char* strdup(const char* s)
106 {
107 char* buf = (char*)RtlAllocateHeap(GetProcessHeap(), 0, strlen(s) + 1);
108 if (buf) strcpy(buf, s);
109 return buf;
110 }
111
112 long long atoll(const char* str)
113 {
114 ULONG tmp;
115 RtlCharToInteger(str, 10, &tmp);
116 return (LONGLONG)tmp;
117 }
118
119 long long strtoll(const char* restrict str, char** restrict str_end, int base)
120 {
121 // FIXME: it kinda work, but not identical to the C version.
122 ULONG tmp;
123 if (base == 0) {
124 NTSTATUS status = RtlCharToInteger(str, 10, &tmp);
125 if (status != STATUS_SUCCESS) RtlCharToInteger(str, 16, &tmp);
126 } else {
127 RtlCharToInteger(str, base, &tmp);
128 }
129 return (LONGLONG)tmp;
130 }
131
132 int strcasecmp(const char *s1, const char *s2)
133 {
134 while (*s1 && *s2) {
135 int diff = tolower(*s1) - tolower(*s2);
136 if (diff) return diff;
137 s1++;
138 s2++;
139 }
140 return tolower(*s1) - tolower(*s2);
141 }
142
143 BOXFILE* box_fopen(const char* filename, const char* mode)
144 {
145 DWORD dwDesiredAccess = 0;
146 DWORD dwCreationDisposition = 0;
147
148 if (strcmp(mode, "r") == 0) {
149 dwDesiredAccess = GENERIC_READ;
150 dwCreationDisposition = OPEN_EXISTING;
151 } else if (strcmp(mode, "w") == 0) {
152 dwDesiredAccess = GENERIC_WRITE;
153 dwCreationDisposition = CREATE_ALWAYS;
154 } else if (strcmp(mode, "a") == 0) {
155 dwDesiredAccess = FILE_APPEND_DATA;
156 dwCreationDisposition = OPEN_ALWAYS;
157 } else {
158 return NULL;
159 }
160
161 HANDLE hFile = CreateFileA(
162 filename,
163 dwDesiredAccess,
164 FILE_SHARE_READ,
165 NULL,
166 dwCreationDisposition,
167 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
168 NULL);
169
170 if (hFile == INVALID_HANDLE_VALUE) return NULL;
171
172 BOXFILE* file = (BOXFILE*)WinMalloc(sizeof(BOXFILE));
173 if (!file) {
174 CloseHandle(hFile);
175 return NULL;
176 }
177
178 file->hFile = hFile;
179 file->buf_pos = 0;
180 file->buf_size = 0;
181 file->eof = 0;
182
183 return file;
184 }
185
186 char* box_fgets(char* str, int num, BOXFILE* stream)
187 {
188 if (stream == NULL || str == NULL || num <= 0 || stream->eof) return NULL;
189
190 int i = 0;
191 while (i < num - 1) {
192 if (stream->buf_pos >= stream->buf_size) {
193 DWORD bytesRead;
194 if (!ReadFile(stream->hFile, stream->buffer, BOXFILE_BUFSIZE, &bytesRead, NULL) || bytesRead == 0) {
195 stream->eof = 1;
196 break;
197 }
198 stream->buf_size = bytesRead;
199 stream->buf_pos = 0;
200 }
201
202 char c = stream->buffer[stream->buf_pos++];
203 str[i++] = c;
204
205 if (c == '\n')
206 break;
207 }
208
209 if (i == 0) return NULL;
210
211 str[i] = '\0';
212 return str;
213 }
214
215 int box_fclose(BOXFILE* stream)
216 {
217 if (stream == NULL) return EOF;
218 BOOL closed = CloseHandle(stream->hFile);
219 WinFree(stream);
220 return closed ? 0 : EOF;
221 }