v4 / vlib / builtin / cfns.c.v
597 lines · 368 sloc · 14.57 KB · beca086a88005cf571aa90172e666a8cd64a1536
Raw
1module builtin
2
3@[typedef]
4pub struct C.FILE {}
5
6// Virtual C globals that are available through libc or generated C headers.
7__global C.errno int
8__global C.stdin &C.FILE
9__global C.stdout &C.FILE
10__global C.stderr &C.FILE
11__global C.environ &&char
12__global C._wyp &u64
13
14// <string.h>
15fn C.memcpy(dest voidptr, const_src voidptr, n usize) voidptr
16
17fn C.memcmp(const_s1 voidptr, const_s2 voidptr, n usize) i32
18
19fn C.memmove(dest voidptr, const_src voidptr, n usize) voidptr
20
21fn C.memset(str voidptr, c i32, n usize) voidptr
22
23fn C.memchr(str voidptr, c i32, n usize) voidptr
24
25fn C.memmem(haystack voidptr, haystacklen usize, needle voidptr, needlelen usize) voidptr
26
27fn C.mempcpy(dest voidptr, src voidptr, n usize) voidptr
28
29@[trusted]
30fn C.calloc(usize, usize) voidptr
31
32fn C.atoi(&char) i32
33
34fn C.malloc(usize) voidptr
35
36fn C.realloc(a voidptr, b usize) voidptr
37
38fn C.free(ptr voidptr)
39
40fn C.mmap(addr_length voidptr, length usize, prot i32, flags i32, fd i32, offset isize) voidptr
41fn C.mprotect(addr_length voidptr, len usize, prot i32) i32
42
43fn C.aligned_alloc(align usize, size usize) voidptr
44
45// windows aligned memory functions
46fn C._aligned_malloc(size isize, align isize) voidptr
47fn C._aligned_free(voidptr)
48fn C._aligned_realloc(voidptr, size isize, align isize) voidptr
49fn C._aligned_offset_malloc(size isize, align isize, offset isize) voidptr
50fn C._aligned_offset_realloc(voidptr, size isize, align isize, offset isize) voidptr
51fn C._aligned_msize(voidptr, align isize, offset isize) isize
52fn C._aligned_recalloc(voidptr, num isize, size isize, align isize) voidptr
53
54$if windows {
55 fn C.VirtualAlloc(voidptr, isize, u32, u32) voidptr
56 fn C.VirtualProtect(voidptr, isize, u32, &u32) bool
57}
58
59@[noreturn; trusted]
60fn C.exit(code i32)
61
62fn C.qsort(base voidptr, items usize, item_size usize, cb C.qsort_callback_func)
63
64fn C.strlen(s &char) i32
65
66@[trusted]
67fn C.isdigit(c i32) bool
68
69// stdio.h
70fn C.popen(c &char, t &char) voidptr
71
72// <libproc.h>
73fn C.proc_pidpath(i32, voidptr, i32) i32
74
75fn C.realpath(const_path &char, resolved_path &char) &char
76
77// fn C.chmod(byteptr, mode_t) int
78fn C.chmod(path &char, mode u32) i32
79
80fn C.printf(const_format &char, opt ...voidptr) i32
81fn C.dprintf(fd i32, const_format &char, opt ...voidptr) i32
82fn C.fprintf(fstream &C.FILE, const_format &char, opt ...voidptr) i32
83fn C.sprintf(str &char, const_format &char, opt ...voidptr) i32
84fn C.snprintf(str &char, size usize, const_format &char, opt ...voidptr) i32
85fn C.wprintf(const_format &u16, opt ...voidptr) i32
86
87// used by Android for (e)println to output to the Android log system / logcat
88pub fn C.android_print(fstream voidptr, format &char, opt ...voidptr)
89
90fn C.sscanf(str &char, const_format &char, opt ...voidptr) i32
91fn C.scanf(const_format &char, opt ...voidptr) i32
92
93fn C.puts(msg &char) i32
94@[trusted]
95fn C.abs(f64) f64
96
97fn C.fputs(msg &char, fstream &C.FILE) i32
98
99fn C.fflush(fstream &C.FILE) i32
100
101// TODO: define args in these functions
102fn C.fseek(stream &C.FILE, offset i32, whence i32) i32
103
104fn C.fopen(filename &char, mode &char) &C.FILE
105
106fn C.fileno(&C.FILE) i32
107
108fn C.fread(ptr voidptr, item_size usize, items usize, stream &C.FILE) usize
109
110fn C.fwrite(ptr voidptr, item_size usize, items usize, stream &C.FILE) usize
111
112fn C.fclose(stream &C.FILE) i32
113
114fn C.pclose(stream &C.FILE) i32
115
116fn C.open(path &char, flags i32, mode ...int) i32
117fn C.close(fd i32) i32
118
119fn C.strrchr(s &char, c i32) &char
120fn C.strchr(s &char, c i32) &char
121fn C.strstr(const_haystack &char, const_needle &char) &char
122
123// process execution, os.process:
124@[trusted]
125fn C.GetCurrentProcessId() u32
126@[trusted]
127fn C._getpid() i32
128@[trusted]
129fn C.getpid() i32
130
131@[trusted]
132fn C.GetCurrentThreadId() u32
133@[trusted]
134fn C.gettid() u32
135
136@[trusted]
137fn C.getuid() i32
138
139@[trusted]
140fn C.geteuid() i32
141
142fn C.system(cmd &char) i32
143
144fn C.posix_spawn(child_pid &int, path &char, file_actions voidptr, attrp voidptr, argv &&char, envp &&char) i32
145
146fn C.posix_spawnp(child_pid &int, exefile &char, file_actions voidptr, attrp voidptr, argv &&char, envp &&char) i32
147
148fn C.execve(cmd_path &char, args voidptr, envs voidptr) i32
149
150fn C.execvp(cmd_path &char, args &&char) i32
151
152fn C._execve(cmd_path &char, args voidptr, envs voidptr) i32
153
154fn C._execvp(cmd_path &char, args &&char) i32
155
156fn C.strcmp(s1 &char, s2 &char) i32
157
158@[trusted]
159fn C.fork() i32
160
161fn C.wait(status &int) i32
162
163fn C.waitpid(pid i32, status &int, options i32) i32
164
165@[trusted]
166fn C.kill(pid i32, sig i32) i32
167
168fn C.setenv(&char, &char, i32) i32
169
170fn C.unsetenv(&char) i32
171
172fn C.access(path &char, amode i32) i32
173
174fn C.remove(filename &char) i32
175
176fn C.rmdir(path &char) i32
177
178fn C.chdir(path &char) i32
179
180fn C.rewind(stream &C.FILE) i32
181
182fn C.ftell(&C.FILE) isize
183
184fn C.stat(&char, voidptr) i32
185
186fn C.lstat(path &char, buf &C.stat) i32
187
188fn C.statvfs(const_path &char, buf &C.statvfs) i32
189
190fn C.rename(old_filename &char, new_filename &char) i32
191
192fn C.fgets(str &char, n i32, stream &C.FILE) i32
193
194fn C.fgetpos(&C.FILE, voidptr) i32
195
196@[trusted]
197fn C.sigemptyset() i32
198
199fn C.getcwd(buf &char, size usize) &char
200
201@[trusted]
202fn C.mktime() i32
203
204fn C.gettimeofday(tv &C.timeval, tz &C.timezone) i32
205
206@[trusted]
207fn C.sleep(seconds u32) u32
208
209// fn C.usleep(usec useconds_t) int
210@[trusted]
211fn C.usleep(usec u32) i32
212
213@[typedef]
214pub struct C.DIR {
215}
216
217fn C.opendir(&char) &C.DIR
218
219fn C.closedir(dirp &C.DIR) i32
220
221// fn C.mkdir(path &char, mode mode_t) int
222fn C.mkdir(path &char, mode u32) i32
223
224// C.rand returns a pseudorandom integer from 0 (inclusive) to C.RAND_MAX (exclusive)
225@[trusted]
226fn C.rand() i32
227
228// C.srand seeds the internal PRNG with the given value.
229@[trusted]
230fn C.srand(seed u32)
231
232fn C.atof(str &char) f64
233
234@[trusted]
235fn C.tolower(c i32) i32
236
237@[trusted]
238fn C.toupper(c i32) i32
239
240@[trusted]
241fn C.isspace(c i32) i32
242
243fn C.strchr(s &char, c i32) &char
244
245@[trusted]
246fn C.getchar() i32
247
248@[trusted]
249fn C.putchar(i32) i32
250
251fn C.strdup(s &char) &char
252
253fn C.strncasecmp(s &char, s2 &char, n i32) i32
254
255fn C.strcasecmp(s &char, s2 &char) i32
256
257fn C.strncmp(s &char, s2 &char, n i32) i32
258
259@[trusted]
260fn C.strerror(i32) &char
261
262@[trusted]
263fn C.WIFEXITED(status i32) bool
264
265@[trusted]
266fn C.WEXITSTATUS(status i32) i32
267
268@[trusted]
269fn C.WIFSIGNALED(status i32) bool
270
271@[trusted]
272fn C.WTERMSIG(status i32) i32
273
274@[trusted]
275fn C.isatty(fd i32) i32
276
277fn C.syscall(number i32, va ...voidptr) i32
278
279fn C.sysctl(name &int, namelen u32, oldp voidptr, oldlenp voidptr, newp voidptr, newlen usize) i32
280
281@[trusted]
282fn C._fileno(&C.FILE) i32
283
284pub type C.intptr_t = voidptr
285
286fn C._get_osfhandle(fd i32) C.intptr_t
287
288fn C.GetModuleFileName(hModule voidptr, lpFilename &u16, nSize u32) u32
289
290fn C.GetModuleFileNameW(hModule voidptr, lpFilename &u16, nSize u32) u32
291
292fn C.CreateFile(lpFilename &u16, dwDesiredAccess u32, dwShareMode u32, lpSecurityAttributes &u16, dwCreationDisposition u32,
293 dwFlagsAndAttributes u32, hTemplateFile voidptr) voidptr
294
295fn C.CreateFileW(lpFilename &u16, dwDesiredAccess u32, dwShareMode u32, lpSecurityAttributes &u16, dwCreationDisposition u32,
296 dwFlagsAndAttributes u32, hTemplateFile voidptr) voidptr
297
298$if windows {
299 // The TCC-only declaration is guarded with `#ifdef __TINYC__` in the
300 // header, so it survives cross compilation; GCC/MSVC use the SDK header.
301 #insert "@VEXEROOT/vlib/builtin/cfns_windows_tcc.h"
302
303 fn C.GetFinalPathNameByHandleW(hFile voidptr, lpFilePath &u16, nSize u32, dwFlags u32) u32
304}
305
306fn C.CreatePipe(hReadPipe &voidptr, hWritePipe &voidptr, lpPipeAttributes voidptr, nSize u32) bool
307
308fn C.SetHandleInformation(hObject voidptr, dwMask u32, dw_flags u32) bool
309
310fn C.ExpandEnvironmentStringsW(lpSrc &u16, lpDst &u16, nSize u32) u32
311
312fn C.GetComputerNameW(&u16, &u32) bool
313
314fn C.GetUserNameW(&u16, &u32) bool
315
316@[trusted]
317fn C.SendMessageTimeout() isize
318
319fn C.SendMessageTimeoutW(hWnd voidptr, msg u32, wParam &u16, lParam &u32, fuFlags u32, uTimeout u32, lpdwResult &u64) isize
320
321fn C.CreateProcessW(lpApplicationName &u16, lpCommandLine &u16, lpProcessAttributes voidptr, lpThreadAttributes voidptr,
322 bInheritHandles bool, dwCreationFlags u32, lpEnvironment voidptr, lpCurrentDirectory &u16, lpStartupInfo voidptr,
323 lpProcessInformation voidptr) bool
324
325fn C.ReadFile(hFile voidptr, lpBuffer voidptr, nNumberOfBytesToRead u32, lpNumberOfBytesRead &u32, lpOverlapped voidptr) bool
326
327fn C.GetFileAttributesW(lpFileName &u8) u32
328
329fn C.RegQueryValueEx(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) i32
330
331fn C.RegQueryValueExW(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) i32
332
333fn C.RegOpenKeyEx(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) i32
334
335fn C.RegOpenKeyExW(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) i32
336
337fn C.RegSetValueEx(hKey voidptr, lpValueName &u16, dwType u32, lpData &u16, cbData u32) i32
338
339fn C.RegSetValueExW(hKey voidptr, lpValueName &u16, reserved u32, dwType u32, const_lpData &u8, cbData u32) i32
340
341fn C.RegCloseKey(hKey voidptr) i32
342
343fn C.RemoveDirectory(lpPathName &u16) bool
344
345fn C.RemoveDirectoryW(lpPathName &u16) bool
346
347fn C.GetStdHandle(u32) voidptr
348
349fn C.SetConsoleMode(voidptr, u32) bool
350
351fn C.GetConsoleMode(voidptr, &u32) bool
352
353// fn C.setbuf()
354fn C.setbuf(voidptr, &char)
355
356fn C.SymCleanup(hProcess voidptr)
357
358fn C.MultiByteToWideChar(codePage u32, dwFlags u32, lpMultiMyteStr &char, cbMultiByte i32, lpWideCharStr &u16,
359 cchWideChar i32) i32
360
361fn C.wcslen(str voidptr) usize
362
363fn C.WideCharToMultiByte(codePage u32, dwFlags u32, lpWideCharStr &u16, cchWideChar i32, lpMultiByteStr &char,
364 cbMultiByte i32, lpDefaultChar &char, lpUsedDefaultChar &int) i32
365
366fn C._wstat(path &u16, buffer &C._stat) i32
367
368fn C._wrename(oldname &u16, newname &u16) i32
369
370fn C._wfopen(filename &u16, mode &u16) voidptr
371
372fn C._wpopen(command &u16, mode &u16) voidptr
373
374fn C._pclose(stream &C.FILE) i32
375
376fn C._wsystem(command &u16) i32
377
378fn C._wgetenv(varname &u16) voidptr
379
380fn C._putenv(envstring &char) i32
381fn C._wputenv(envstring &u16) i32
382
383fn C._waccess(path &u16, mode i32) i32
384
385fn C._wremove(path &u16) i32
386
387fn C.ReadConsole(in_input_handle voidptr, out_buffer voidptr, in_chars_to_read u32, out_read_chars &u32,
388 in_input_control voidptr) bool
389
390fn C.WriteConsole() voidptr
391
392fn C.WriteFile(hFile voidptr, lpBuffer &u8, nNumberOfBytesToWrite u32, lpNumberOfBytesWritten &u32, lpOverlapped voidptr) bool
393
394fn C._wchdir(dirname &u16) i32
395
396fn C._wgetcwd(buffer &u16, maxlen i32) i32
397
398fn C._fullpath() i32
399
400fn C.GetFullPathName(voidptr, u32, voidptr, voidptr) u32
401
402@[trusted]
403fn C.GetCommandLine() voidptr
404
405fn C.LocalFree(voidptr)
406
407fn C.FindFirstFileW(lpFileName &u16, lpFindFileData voidptr) voidptr
408
409fn C.FindFirstFile(lpFileName &u8, lpFindFileData voidptr) voidptr
410
411fn C.FindNextFile(hFindFile voidptr, lpFindFileData voidptr) i32
412
413fn C.FindClose(hFindFile voidptr)
414
415// macro
416fn C.MAKELANGID(lgid voidptr, srtid voidptr) i32
417
418fn C.FormatMessageW(dwFlags u32, lpSource voidptr, dwMessageId u32, dwLanguageId u32, lpBuffer voidptr,
419 nSize u32, arguments ...voidptr) u32
420
421fn C.CloseHandle(voidptr) i32
422
423fn C.GetExitCodeProcess(hProcess voidptr, lpExitCode &u32)
424
425@[trusted]
426fn C.GetTickCount() i64
427
428@[trusted]
429fn C.Sleep(dwMilliseconds u32)
430
431fn C.WSAStartup(u16, &voidptr) i32
432
433@[trusted]
434fn C.WSAGetLastError() i32
435
436fn C.closesocket(i32) i32
437
438fn C.vschannel_init(&C.TlsContext, C.BOOL)
439
440fn C.request(&C.TlsContext, i32, &u16, &u8, u32, &&u8, fn (voidptr, isize) voidptr) i32
441
442fn C.vschannel_cleanup(&C.TlsContext)
443
444fn C.URLDownloadToFile(i32, &u16, &u16, i32, i32)
445
446@[trusted]
447fn C.GetLastError() u32
448
449fn C.CreateDirectory(&u8, i32) bool
450
451// win crypto
452fn C.BCryptGenRandom(i32, voidptr, i32, i32) i32
453
454// win synchronization
455fn C.CreateMutex(i32, bool, &u8) voidptr
456
457fn C.WaitForSingleObject(voidptr, i32) i32
458
459fn C.ReleaseMutex(voidptr) bool
460
461fn C.CreateEvent(i32, bool, bool, &u8) voidptr
462
463fn C.SetEvent(voidptr) i32
464
465fn C.CreateSemaphore(voidptr, i32, i32, voidptr) voidptr
466
467fn C.ReleaseSemaphore(voidptr, i32, voidptr) voidptr
468
469$if windows {
470 fn C.InitializeSRWLock(voidptr)
471 fn C.AcquireSRWLockShared(voidptr)
472 fn C.AcquireSRWLockExclusive(voidptr)
473 fn C.ReleaseSRWLockShared(voidptr)
474 fn C.ReleaseSRWLockExclusive(voidptr)
475}
476
477// pthread.h
478fn C.pthread_self() usize
479fn C.pthread_mutex_init(voidptr, voidptr) i32
480
481fn C.pthread_mutex_lock(voidptr) i32
482
483fn C.pthread_mutex_unlock(voidptr) i32
484
485fn C.pthread_mutex_destroy(voidptr) i32
486
487fn C.pthread_rwlockattr_init(voidptr) i32
488
489fn C.pthread_rwlockattr_setkind_np(voidptr, i32) i32
490
491fn C.pthread_rwlockattr_setpshared(voidptr, i32) i32
492
493fn C.pthread_rwlock_init(voidptr, voidptr) i32
494
495fn C.pthread_rwlock_rdlock(voidptr) i32
496
497fn C.pthread_rwlock_wrlock(voidptr) i32
498
499fn C.pthread_rwlock_unlock(voidptr) i32
500
501fn C.pthread_condattr_init(voidptr) i32
502
503fn C.pthread_condattr_setpshared(voidptr, i32) i32
504
505fn C.pthread_condattr_destroy(voidptr) i32
506
507fn C.pthread_cond_init(voidptr, voidptr) i32
508
509fn C.pthread_cond_signal(voidptr) i32
510
511fn C.pthread_cond_wait(voidptr, voidptr) i32
512
513fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) i32
514
515fn C.pthread_cond_destroy(voidptr) i32
516
517fn C.sem_init(voidptr, i32, u32) i32
518
519fn C.sem_post(voidptr) i32
520
521fn C.sem_wait(voidptr) i32
522
523fn C.sem_trywait(voidptr) i32
524
525fn C.sem_timedwait(voidptr, voidptr) i32
526
527fn C.sem_destroy(voidptr) i32
528
529// MacOS semaphore functions
530@[trusted]
531fn C.dispatch_semaphore_create(i64) voidptr
532
533fn C.dispatch_semaphore_signal(voidptr) i64
534
535fn C.dispatch_semaphore_wait(voidptr, u64) i64
536
537@[trusted]
538fn C.dispatch_time(u64, i64) u64
539
540fn C.dispatch_release(voidptr)
541
542// file descriptor based reading/writing
543fn C.read(fd i32, buf voidptr, count usize) i32
544
545fn C.write(fd i32, buf voidptr, count usize) i32
546
547fn C.close(fd i32) i32
548
549// pipes
550fn C.pipe(pipefds &int) i32
551
552fn C.dup2(oldfd i32, newfd i32) i32
553
554// used by gl, stbi, freetype
555fn C.glTexImage2D()
556
557// used by ios for println
558fn C.WrappedNSLog(str &u8)
559
560// absolute value
561@[trusted]
562fn C.abs(number i32) i32
563
564$if windows {
565 fn C.GetDiskFreeSpaceExA(const_path &char, free_bytes_available_to_caller &u64, total_number_of_bytes &u64, total_number_of_free_bytes &u64) bool
566
567 fn C.GetNativeSystemInfo(voidptr)
568
569 // C.SYSTEM_INFO contains information about the current computer system. This includes the architecture and type of the processor, the number of processors in the system, the page size, and other such information.
570 @[typedef]
571 pub struct C.SYSTEM_INFO {
572 // workaround: v doesn't support a truely C anon union/struct here
573 // union {
574 dwOemId u32
575 // struct {
576 wProcessorArchitecture u16
577 wReserved u16
578 // }
579 //}
580 dwPageSize u32
581 lpMinimumApplicationAddress voidptr
582 lpMaximumApplicationAddress voidptr
583 dwActiveProcessorMask u32
584 dwNumberOfProcessors u32
585 dwProcessorType u32
586 dwAllocationGranularity u32
587 wProcessorLevel u16
588 wProcessorRevision u16
589 }
590
591 fn C.GetSystemInfo(&C.SYSTEM_INFO)
592
593 @[typedef]
594 pub struct C.SRWLOCK {}
595}
596
597fn C.sysconf(name i32) i32
598