vxx2 / vlib / builtin / cfns.c.v
620 lines · 381 sloc · 15.12 KB · 2b81918e62db9ab38787518603a7f93bee80868c
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
102$if windows {
103 fn C.fseek(stream &C.FILE, offset i32, whence i32) i32
104} $else {
105 fn C.fseek(stream &C.FILE, offset isize, whence i32) i32
106}
107
108fn C.fopen(filename &char, mode &char) &C.FILE
109
110fn C.fileno(&C.FILE) i32
111
112fn C.fread(ptr voidptr, item_size usize, items usize, stream &C.FILE) usize
113
114fn C.fwrite(ptr voidptr, item_size usize, items usize, stream &C.FILE) usize
115
116fn C.fclose(stream &C.FILE) i32
117
118fn C.pclose(stream &C.FILE) i32
119
120fn C.open(path &char, flags i32, mode ...int) i32
121fn C.close(fd i32) i32
122
123fn C.strrchr(s &char, c i32) &char
124fn C.strchr(s &char, c i32) &char
125fn C.strstr(const_haystack &char, const_needle &char) &char
126
127// process execution, os.process:
128@[trusted]
129fn C.GetCurrentProcessId() u32
130@[trusted]
131fn C._getpid() i32
132@[trusted]
133fn C.getpid() i32
134
135@[trusted]
136fn C.GetCurrentThreadId() u32
137@[trusted]
138fn C.gettid() u32
139
140@[trusted]
141fn C.getuid() i32
142
143@[trusted]
144fn C.geteuid() i32
145
146fn C.system(cmd &char) i32
147
148fn C.posix_spawn(child_pid &int, path &char, file_actions voidptr, attrp voidptr, argv &&char, envp &&char) i32
149
150fn C.posix_spawnp(child_pid &int, exefile &char, file_actions voidptr, attrp voidptr, argv &&char, envp &&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._execve(cmd_path &char, args voidptr, envs voidptr) i32
157
158fn C._execvp(cmd_path &char, args &&char) i32
159
160fn C.strcmp(s1 &char, s2 &char) i32
161
162@[trusted]
163fn C.fork() i32
164
165fn C.wait(status &int) i32
166
167fn C.waitpid(pid i32, status &int, options i32) i32
168
169@[trusted]
170fn C.kill(pid i32, sig i32) i32
171
172fn C.setenv(&char, &char, i32) i32
173
174fn C.unsetenv(&char) i32
175
176fn C.access(path &char, amode i32) i32
177
178fn C.remove(filename &char) i32
179
180fn C.rmdir(path &char) i32
181
182fn C.chdir(path &char) i32
183
184fn C.rewind(stream &C.FILE) i32
185
186fn C.ftell(&C.FILE) isize
187
188fn C.stat(&char, voidptr) i32
189
190fn C.lstat(path &char, buf &C.stat) i32
191
192fn C.statvfs(const_path &char, buf &C.statvfs) i32
193
194fn C.rename(old_filename &char, new_filename &char) i32
195
196fn C.fgets(str &char, n i32, stream &C.FILE) i32
197
198fn C.fgetpos(&C.FILE, voidptr) i32
199
200@[trusted]
201fn C.sigemptyset() i32
202
203fn C.getcwd(buf &char, size usize) &char
204
205@[trusted]
206fn C.mktime() i32
207
208fn C.gettimeofday(tv &C.timeval, tz &C.timezone) i32
209
210@[trusted]
211fn C.sleep(seconds u32) u32
212
213// fn C.usleep(usec useconds_t) int
214@[trusted]
215fn C.usleep(usec u32) i32
216
217@[typedef]
218pub struct C.DIR {
219}
220
221fn C.opendir(&char) &C.DIR
222
223fn C.closedir(dirp &C.DIR) i32
224
225// fn C.mkdir(path &char, mode mode_t) int
226fn C.mkdir(path &char, mode u32) i32
227
228// C.rand returns a pseudorandom integer from 0 (inclusive) to C.RAND_MAX (exclusive)
229@[trusted]
230fn C.rand() i32
231
232// C.srand seeds the internal PRNG with the given value.
233@[trusted]
234fn C.srand(seed u32)
235
236fn C.atof(str &char) f64
237
238@[trusted]
239fn C.tolower(c i32) i32
240
241@[trusted]
242fn C.toupper(c i32) i32
243
244@[trusted]
245fn C.isspace(c i32) i32
246
247fn C.strchr(s &char, c i32) &char
248
249@[trusted]
250fn C.getchar() i32
251
252@[trusted]
253fn C.putchar(i32) i32
254
255fn C.strdup(s &char) &char
256
257fn C.strncasecmp(s &char, s2 &char, n i32) i32
258
259fn C.strcasecmp(s &char, s2 &char) i32
260
261fn C.strncmp(s &char, s2 &char, n i32) i32
262
263@[trusted]
264fn C.strerror(i32) &char
265
266@[trusted]
267fn C.WIFEXITED(status i32) bool
268
269@[trusted]
270fn C.WEXITSTATUS(status i32) i32
271
272@[trusted]
273fn C.WIFSIGNALED(status i32) bool
274
275@[trusted]
276fn C.WTERMSIG(status i32) i32
277
278@[trusted]
279fn C.isatty(fd i32) i32
280
281fn C.syscall(number i32, va ...voidptr) i32
282
283fn C.sysctl(name &int, namelen u32, oldp voidptr, oldlenp voidptr, newp voidptr, newlen usize) i32
284
285@[trusted]
286fn C._fileno(&C.FILE) i32
287
288pub type C.intptr_t = voidptr
289
290fn C._get_osfhandle(fd i32) C.intptr_t
291
292fn C.GetModuleFileName(hModule voidptr, lpFilename &u16, nSize u32) u32
293
294fn C.GetModuleFileNameW(hModule voidptr, lpFilename &u16, nSize u32) u32
295
296fn C.CreateFile(lpFilename &u16, dwDesiredAccess u32, dwShareMode u32, lpSecurityAttributes &u16, dwCreationDisposition u32,
297 dwFlagsAndAttributes u32, hTemplateFile voidptr) voidptr
298
299fn C.CreateFileW(lpFilename &u16, dwDesiredAccess u32, dwShareMode u32, lpSecurityAttributes &u16, dwCreationDisposition u32,
300 dwFlagsAndAttributes u32, hTemplateFile voidptr) voidptr
301
302$if windows {
303 // The TCC-only declaration is guarded with `#ifdef __TINYC__` in the
304 // header, so it survives cross compilation; GCC/MSVC use the SDK header.
305 #insert "@VEXEROOT/vlib/builtin/cfns_windows_tcc.h"
306
307 fn C.GetFinalPathNameByHandleW(hFile voidptr, lpFilePath &u16, nSize u32, dwFlags u32) u32
308}
309
310fn C.CreatePipe(hReadPipe &voidptr, hWritePipe &voidptr, lpPipeAttributes voidptr, nSize u32) bool
311
312fn C.SetHandleInformation(hObject voidptr, dwMask u32, dw_flags u32) bool
313
314fn C.ExpandEnvironmentStringsW(lpSrc &u16, lpDst &u16, nSize u32) u32
315
316fn C.GetComputerNameW(&u16, &u32) bool
317
318fn C.GetUserNameW(&u16, &u32) bool
319
320@[trusted]
321fn C.SendMessageTimeout() isize
322
323fn C.SendMessageTimeoutW(hWnd voidptr, msg u32, wParam &u16, lParam &u32, fuFlags u32, uTimeout u32, lpdwResult &u64) isize
324
325fn C.CreateProcessW(lpApplicationName &u16, lpCommandLine &u16, lpProcessAttributes voidptr, lpThreadAttributes voidptr,
326 bInheritHandles bool, dwCreationFlags u32, lpEnvironment voidptr, lpCurrentDirectory &u16, lpStartupInfo voidptr,
327 lpProcessInformation voidptr) bool
328
329fn C.ReadFile(hFile voidptr, lpBuffer voidptr, nNumberOfBytesToRead u32, lpNumberOfBytesRead &u32, lpOverlapped voidptr) bool
330
331fn C.GetFileAttributesW(lpFileName &u8) u32
332
333fn C.RegQueryValueEx(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) i32
334
335fn C.RegQueryValueExW(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) i32
336
337fn C.RegOpenKeyEx(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) i32
338
339fn C.RegOpenKeyExW(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) i32
340
341fn C.RegSetValueEx(hKey voidptr, lpValueName &u16, dwType u32, lpData &u16, cbData u32) i32
342
343fn C.RegSetValueExW(hKey voidptr, lpValueName &u16, reserved u32, dwType u32, const_lpData &u8, cbData u32) i32
344
345fn C.RegCloseKey(hKey voidptr) i32
346
347fn C.RemoveDirectory(lpPathName &u16) bool
348
349fn C.RemoveDirectoryW(lpPathName &u16) bool
350
351fn C.GetStdHandle(u32) voidptr
352
353fn C.SetConsoleMode(voidptr, u32) bool
354
355fn C.GetConsoleMode(voidptr, &u32) bool
356
357// fn C.setbuf()
358fn C.setbuf(voidptr, &char)
359
360fn C.SymCleanup(hProcess voidptr)
361
362fn C.MultiByteToWideChar(codePage u32, dwFlags u32, lpMultiMyteStr &char, cbMultiByte i32, lpWideCharStr &u16,
363 cchWideChar i32) i32
364
365fn C.wcslen(str voidptr) usize
366
367fn C.WideCharToMultiByte(codePage u32, dwFlags u32, lpWideCharStr &u16, cchWideChar i32, lpMultiByteStr &char,
368 cbMultiByte i32, lpDefaultChar &char, lpUsedDefaultChar &int) i32
369
370fn C._wstat(path &u16, buffer &C._stat) i32
371
372fn C._wrename(oldname &u16, newname &u16) i32
373
374fn C._wfopen(filename &u16, mode &u16) voidptr
375
376fn C._wpopen(command &u16, mode &u16) voidptr
377
378fn C._pclose(stream &C.FILE) i32
379
380fn C._wsystem(command &u16) i32
381
382fn C._wgetenv(varname &u16) voidptr
383
384fn C._putenv(envstring &char) i32
385fn C._wputenv(envstring &u16) i32
386
387fn C._waccess(path &u16, mode i32) i32
388
389fn C._wremove(path &u16) i32
390
391fn C.ReadConsole(in_input_handle voidptr, out_buffer voidptr, in_chars_to_read u32, out_read_chars &u32,
392 in_input_control voidptr) bool
393
394fn C.WriteConsole() voidptr
395
396fn C.WriteFile(hFile voidptr, lpBuffer &u8, nNumberOfBytesToWrite u32, lpNumberOfBytesWritten &u32, lpOverlapped voidptr) bool
397
398fn C._wchdir(dirname &u16) i32
399
400fn C._wgetcwd(buffer &u16, maxlen i32) i32
401
402fn C._fullpath() i32
403
404fn C.GetFullPathName(voidptr, u32, voidptr, voidptr) u32
405
406@[trusted]
407fn C.GetCommandLine() voidptr
408
409fn C.LocalFree(voidptr)
410
411fn C.FindFirstFileW(lpFileName &u16, lpFindFileData voidptr) voidptr
412
413fn C.FindFirstFile(lpFileName &u8, lpFindFileData voidptr) voidptr
414
415fn C.FindNextFile(hFindFile voidptr, lpFindFileData voidptr) i32
416
417fn C.FindClose(hFindFile voidptr)
418
419// macro
420fn C.MAKELANGID(lgid voidptr, srtid voidptr) i32
421
422fn C.FormatMessageW(dwFlags u32, lpSource voidptr, dwMessageId u32, dwLanguageId u32, lpBuffer voidptr,
423 nSize u32, arguments ...voidptr) u32
424
425fn C.CloseHandle(voidptr) i32
426
427fn C.GetExitCodeProcess(hProcess voidptr, lpExitCode &u32)
428
429@[trusted]
430fn C.GetTickCount() i64
431
432@[trusted]
433fn C.Sleep(dwMilliseconds u32)
434
435fn C.WSAStartup(u16, &voidptr) i32
436
437@[trusted]
438fn C.WSAGetLastError() i32
439
440fn C.closesocket(i32) i32
441
442fn C.vschannel_init(&C.TlsContext, C.BOOL)
443
444fn C.request(&C.TlsContext, i32, &u16, &u8, u32, &&u8, fn (voidptr, isize) voidptr) i32
445
446fn C.vschannel_cleanup(&C.TlsContext)
447
448fn C.URLDownloadToFile(i32, &u16, &u16, i32, i32)
449
450@[trusted]
451fn C.GetLastError() u32
452
453fn C.CreateDirectory(&u8, i32) bool
454
455// win crypto
456fn C.BCryptGenRandom(i32, voidptr, i32, i32) i32
457
458// win synchronization
459fn C.CreateMutex(i32, bool, &u8) voidptr
460
461fn C.WaitForSingleObject(voidptr, i32) i32
462
463fn C.ReleaseMutex(voidptr) bool
464
465fn C.CreateEvent(i32, bool, bool, &u8) voidptr
466
467fn C.SetEvent(voidptr) i32
468
469fn C.CreateSemaphore(voidptr, i32, i32, voidptr) voidptr
470
471fn C.ReleaseSemaphore(voidptr, i32, voidptr) voidptr
472
473$if windows {
474 fn C.InitializeSRWLock(voidptr)
475 fn C.AcquireSRWLockShared(voidptr)
476 fn C.AcquireSRWLockExclusive(voidptr)
477 fn C.ReleaseSRWLockShared(voidptr)
478 fn C.ReleaseSRWLockExclusive(voidptr)
479}
480
481// pthread.h
482fn C.pthread_self() usize
483
484fn C.pthread_create(thread voidptr, attr voidptr, start_routine voidptr, arg voidptr) i32
485
486fn C.pthread_join(thread voidptr, retval voidptr) i32
487
488fn C.pthread_attr_init(attr voidptr) i32
489
490fn C.pthread_attr_setstacksize(attr voidptr, stacksize usize) i32
491
492fn C.pthread_attr_destroy(attr voidptr) i32
493
494fn C.pthread_mutex_init(voidptr, voidptr) i32
495
496fn C.pthread_mutex_lock(voidptr) i32
497
498fn C.pthread_mutex_unlock(voidptr) i32
499
500fn C.pthread_mutex_destroy(voidptr) i32
501
502fn C.pthread_rwlockattr_init(voidptr) i32
503
504fn C.pthread_rwlockattr_setkind_np(voidptr, i32) i32
505
506fn C.pthread_rwlockattr_setpshared(voidptr, i32) i32
507
508fn C.pthread_rwlock_init(voidptr, voidptr) i32
509
510fn C.pthread_rwlock_rdlock(voidptr) i32
511
512fn C.pthread_rwlock_wrlock(voidptr) i32
513
514fn C.pthread_rwlock_unlock(voidptr) i32
515
516fn C.pthread_condattr_init(voidptr) i32
517
518fn C.pthread_condattr_setpshared(voidptr, i32) i32
519
520fn C.pthread_condattr_destroy(voidptr) i32
521
522fn C.pthread_cond_init(voidptr, voidptr) i32
523
524fn C.pthread_cond_signal(voidptr) i32
525
526fn C.pthread_cond_wait(voidptr, voidptr) i32
527
528fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) i32
529
530fn C.pthread_cond_destroy(voidptr) i32
531
532fn C.sem_init(voidptr, i32, u32) i32
533
534fn C.sem_post(voidptr) i32
535
536fn C.sem_wait(voidptr) i32
537
538fn C.sem_trywait(voidptr) i32
539
540fn C.sem_timedwait(voidptr, voidptr) i32
541
542fn C.sem_destroy(voidptr) i32
543
544// MacOS semaphore functions
545@[trusted]
546fn C.dispatch_semaphore_create(i64) voidptr
547
548fn C.dispatch_semaphore_signal(voidptr) i64
549
550fn C.dispatch_semaphore_wait(voidptr, u64) i64
551
552@[trusted]
553fn C.dispatch_time(u64, i64) u64
554
555fn C.dispatch_release(voidptr)
556
557// file descriptor based reading/writing
558fn C.read(fd i32, buf voidptr, count usize) i32
559
560fn C.write(fd i32, buf voidptr, count usize) i32
561
562fn C.close(fd i32) i32
563
564// pipes
565fn C.pipe(pipefds &int) i32
566
567fn C.dup2(oldfd i32, newfd i32) i32
568
569fn C.fcntl(fd i32, cmd i32, arg ...voidptr) i32
570
571fn C.execlp(file &char, arg &char, va ...voidptr) i32
572
573fn C._exit(code i32)
574
575fn C.signal(sig i32, handler voidptr) voidptr
576
577// used by gl, stbi, freetype
578fn C.glTexImage2D()
579
580// used by ios for println
581fn C.WrappedNSLog(str &u8)
582
583// absolute value
584@[trusted]
585fn C.abs(number i32) i32
586
587$if windows {
588 fn C.GetDiskFreeSpaceExA(const_path &char, free_bytes_available_to_caller &u64, total_number_of_bytes &u64, total_number_of_free_bytes &u64) bool
589
590 fn C.GetNativeSystemInfo(voidptr)
591
592 // 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.
593 @[typedef]
594 pub struct C.SYSTEM_INFO {
595 // workaround: v doesn't support a truely C anon union/struct here
596 // union {
597 dwOemId u32
598 // struct {
599 wProcessorArchitecture u16
600 wReserved u16
601 // }
602 //}
603 dwPageSize u32
604 lpMinimumApplicationAddress voidptr
605 lpMaximumApplicationAddress voidptr
606 dwActiveProcessorMask u32
607 dwNumberOfProcessors u32
608 dwProcessorType u32
609 dwAllocationGranularity u32
610 wProcessorLevel u16
611 wProcessorRevision u16
612 }
613
614 fn C.GetSystemInfo(&C.SYSTEM_INFO)
615
616 @[typedef]
617 pub struct C.SRWLOCK {}
618}
619
620fn C.sysconf(name i32) i32
621