v2 / vlib / builtin / cfns.c.v
591 lines · 363 sloc · 14.36 KB · d398402707ab2298a12fbeabefe274eba5b1c2cd
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
298fn C.GetFinalPathNameByHandleW(hFile voidptr, lpFilePath &u16, nSize u32, dwFlags u32) u32
299
300fn C.CreatePipe(hReadPipe &voidptr, hWritePipe &voidptr, lpPipeAttributes voidptr, nSize u32) bool
301
302fn C.SetHandleInformation(hObject voidptr, dwMask u32, dw_flags u32) bool
303
304fn C.ExpandEnvironmentStringsW(lpSrc &u16, lpDst &u16, nSize u32) u32
305
306fn C.GetComputerNameW(&u16, &u32) bool
307
308fn C.GetUserNameW(&u16, &u32) bool
309
310@[trusted]
311fn C.SendMessageTimeout() isize
312
313fn C.SendMessageTimeoutW(hWnd voidptr, msg u32, wParam &u16, lParam &u32, fuFlags u32, uTimeout u32, lpdwResult &u64) isize
314
315fn C.CreateProcessW(lpApplicationName &u16, lpCommandLine &u16, lpProcessAttributes voidptr, lpThreadAttributes voidptr,
316 bInheritHandles bool, dwCreationFlags u32, lpEnvironment voidptr, lpCurrentDirectory &u16, lpStartupInfo voidptr,
317 lpProcessInformation voidptr) bool
318
319fn C.ReadFile(hFile voidptr, lpBuffer voidptr, nNumberOfBytesToRead u32, lpNumberOfBytesRead &u32, lpOverlapped voidptr) bool
320
321fn C.GetFileAttributesW(lpFileName &u8) u32
322
323fn C.RegQueryValueEx(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) i32
324
325fn C.RegQueryValueExW(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) i32
326
327fn C.RegOpenKeyEx(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) i32
328
329fn C.RegOpenKeyExW(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) i32
330
331fn C.RegSetValueEx(hKey voidptr, lpValueName &u16, dwType u32, lpData &u16, cbData u32) i32
332
333fn C.RegSetValueExW(hKey voidptr, lpValueName &u16, reserved u32, dwType u32, const_lpData &u8, cbData u32) i32
334
335fn C.RegCloseKey(hKey voidptr) i32
336
337fn C.RemoveDirectory(lpPathName &u16) bool
338
339fn C.RemoveDirectoryW(lpPathName &u16) bool
340
341fn C.GetStdHandle(u32) voidptr
342
343fn C.SetConsoleMode(voidptr, u32) bool
344
345fn C.GetConsoleMode(voidptr, &u32) bool
346
347// fn C.setbuf()
348fn C.setbuf(voidptr, &char)
349
350fn C.SymCleanup(hProcess voidptr)
351
352fn C.MultiByteToWideChar(codePage u32, dwFlags u32, lpMultiMyteStr &char, cbMultiByte i32, lpWideCharStr &u16,
353 cchWideChar i32) i32
354
355fn C.wcslen(str voidptr) usize
356
357fn C.WideCharToMultiByte(codePage u32, dwFlags u32, lpWideCharStr &u16, cchWideChar i32, lpMultiByteStr &char,
358 cbMultiByte i32, lpDefaultChar &char, lpUsedDefaultChar &int) i32
359
360fn C._wstat(path &u16, buffer &C._stat) i32
361
362fn C._wrename(oldname &u16, newname &u16) i32
363
364fn C._wfopen(filename &u16, mode &u16) voidptr
365
366fn C._wpopen(command &u16, mode &u16) voidptr
367
368fn C._pclose(stream &C.FILE) i32
369
370fn C._wsystem(command &u16) i32
371
372fn C._wgetenv(varname &u16) voidptr
373
374fn C._putenv(envstring &char) i32
375fn C._wputenv(envstring &u16) i32
376
377fn C._waccess(path &u16, mode i32) i32
378
379fn C._wremove(path &u16) i32
380
381fn C.ReadConsole(in_input_handle voidptr, out_buffer voidptr, in_chars_to_read u32, out_read_chars &u32,
382 in_input_control voidptr) bool
383
384fn C.WriteConsole() voidptr
385
386fn C.WriteFile(hFile voidptr, lpBuffer voidptr, nNumberOfBytesToWrite u32, lpNumberOfBytesWritten &u32, lpOverlapped voidptr) bool
387
388fn C._wchdir(dirname &u16) i32
389
390fn C._wgetcwd(buffer &u16, maxlen i32) i32
391
392fn C._fullpath() i32
393
394fn C.GetFullPathName(voidptr, u32, voidptr, voidptr) u32
395
396@[trusted]
397fn C.GetCommandLine() voidptr
398
399fn C.LocalFree(voidptr)
400
401fn C.FindFirstFileW(lpFileName &u16, lpFindFileData voidptr) voidptr
402
403fn C.FindFirstFile(lpFileName &u8, lpFindFileData voidptr) voidptr
404
405fn C.FindNextFile(hFindFile voidptr, lpFindFileData voidptr) i32
406
407fn C.FindClose(hFindFile voidptr)
408
409// macro
410fn C.MAKELANGID(lgid voidptr, srtid voidptr) i32
411
412fn C.FormatMessageW(dwFlags u32, lpSource voidptr, dwMessageId u32, dwLanguageId u32, lpBuffer voidptr,
413 nSize u32, arguments ...voidptr) u32
414
415fn C.CloseHandle(voidptr) i32
416
417fn C.GetExitCodeProcess(hProcess voidptr, lpExitCode &u32)
418
419@[trusted]
420fn C.GetTickCount() i64
421
422@[trusted]
423fn C.Sleep(dwMilliseconds u32)
424
425fn C.WSAStartup(u16, &voidptr) i32
426
427@[trusted]
428fn C.WSAGetLastError() i32
429
430fn C.closesocket(i32) i32
431
432fn C.vschannel_init(&C.TlsContext, C.BOOL)
433
434fn C.request(&C.TlsContext, i32, &u16, &u8, u32, &&u8, fn (voidptr, isize) voidptr) i32
435
436fn C.vschannel_cleanup(&C.TlsContext)
437
438fn C.URLDownloadToFile(i32, &u16, &u16, i32, i32)
439
440@[trusted]
441fn C.GetLastError() u32
442
443fn C.CreateDirectory(&u8, i32) bool
444
445// win crypto
446fn C.BCryptGenRandom(i32, voidptr, i32, i32) i32
447
448// win synchronization
449fn C.CreateMutex(i32, bool, &u8) voidptr
450
451fn C.WaitForSingleObject(voidptr, i32) i32
452
453fn C.ReleaseMutex(voidptr) bool
454
455fn C.CreateEvent(i32, bool, bool, &u8) voidptr
456
457fn C.SetEvent(voidptr) i32
458
459fn C.CreateSemaphore(voidptr, i32, i32, voidptr) voidptr
460
461fn C.ReleaseSemaphore(voidptr, i32, voidptr) voidptr
462
463$if windows {
464 fn C.InitializeSRWLock(voidptr)
465 fn C.AcquireSRWLockShared(voidptr)
466 fn C.AcquireSRWLockExclusive(voidptr)
467 fn C.ReleaseSRWLockShared(voidptr)
468 fn C.ReleaseSRWLockExclusive(voidptr)
469}
470
471// pthread.h
472fn C.pthread_self() usize
473fn C.pthread_mutex_init(voidptr, voidptr) i32
474
475fn C.pthread_mutex_lock(voidptr) i32
476
477fn C.pthread_mutex_unlock(voidptr) i32
478
479fn C.pthread_mutex_destroy(voidptr) i32
480
481fn C.pthread_rwlockattr_init(voidptr) i32
482
483fn C.pthread_rwlockattr_setkind_np(voidptr, i32) i32
484
485fn C.pthread_rwlockattr_setpshared(voidptr, i32) i32
486
487fn C.pthread_rwlock_init(voidptr, voidptr) i32
488
489fn C.pthread_rwlock_rdlock(voidptr) i32
490
491fn C.pthread_rwlock_wrlock(voidptr) i32
492
493fn C.pthread_rwlock_unlock(voidptr) i32
494
495fn C.pthread_condattr_init(voidptr) i32
496
497fn C.pthread_condattr_setpshared(voidptr, i32) i32
498
499fn C.pthread_condattr_destroy(voidptr) i32
500
501fn C.pthread_cond_init(voidptr, voidptr) i32
502
503fn C.pthread_cond_signal(voidptr) i32
504
505fn C.pthread_cond_wait(voidptr, voidptr) i32
506
507fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) i32
508
509fn C.pthread_cond_destroy(voidptr) i32
510
511fn C.sem_init(voidptr, i32, u32) i32
512
513fn C.sem_post(voidptr) i32
514
515fn C.sem_wait(voidptr) i32
516
517fn C.sem_trywait(voidptr) i32
518
519fn C.sem_timedwait(voidptr, voidptr) i32
520
521fn C.sem_destroy(voidptr) i32
522
523// MacOS semaphore functions
524@[trusted]
525fn C.dispatch_semaphore_create(i64) voidptr
526
527fn C.dispatch_semaphore_signal(voidptr) i64
528
529fn C.dispatch_semaphore_wait(voidptr, u64) i64
530
531@[trusted]
532fn C.dispatch_time(u64, i64) u64
533
534fn C.dispatch_release(voidptr)
535
536// file descriptor based reading/writing
537fn C.read(fd i32, buf voidptr, count usize) i32
538
539fn C.write(fd i32, buf voidptr, count usize) i32
540
541fn C.close(fd i32) i32
542
543// pipes
544fn C.pipe(pipefds &int) i32
545
546fn C.dup2(oldfd i32, newfd i32) i32
547
548// used by gl, stbi, freetype
549fn C.glTexImage2D()
550
551// used by ios for println
552fn C.WrappedNSLog(str &u8)
553
554// absolute value
555@[trusted]
556fn C.abs(number i32) i32
557
558$if windows {
559 fn C.GetDiskFreeSpaceExA(const_path &char, free_bytes_available_to_caller &u64, total_number_of_bytes &u64, total_number_of_free_bytes &u64) bool
560
561 fn C.GetNativeSystemInfo(voidptr)
562
563 // 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.
564 @[typedef]
565 pub struct C.SYSTEM_INFO {
566 // workaround: v doesn't support a truely C anon union/struct here
567 // union {
568 dwOemId u32
569 // struct {
570 wProcessorArchitecture u16
571 wReserved u16
572 // }
573 //}
574 dwPageSize u32
575 lpMinimumApplicationAddress voidptr
576 lpMaximumApplicationAddress voidptr
577 dwActiveProcessorMask u32
578 dwNumberOfProcessors u32
579 dwProcessorType u32
580 dwAllocationGranularity u32
581 wProcessorLevel u16
582 wProcessorRevision u16
583 }
584
585 fn C.GetSystemInfo(&C.SYSTEM_INFO)
586
587 @[typedef]
588 pub struct C.SRWLOCK {}
589}
590
591fn C.sysconf(name i32) i32
592