aboutsummaryrefslogtreecommitdiff
path: root/core/sys/posix/stdlib_libc.odin
blob: e31c5170496080ee7a4d3df840963d3c178384fd (plain)
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
#+build linux, windows, darwin, netbsd, openbsd, freebsd, haiku
package posix

import "base:intrinsics"

import "core:c"
import "core:c/libc"

when ODIN_OS == .Windows {
	foreign import lib "system:libucrt.lib"
} else when ODIN_OS == .Darwin {
	foreign import lib "system:System"
} else {
	foreign import lib "system:c"
}

// stdlib.h - standard library definitions

atof          :: libc.atof
atoi          :: libc.atoi
atol          :: libc.atol
atoll         :: libc.atoll
strtod        :: libc.strtod
strtof        :: libc.strtof
strtol        :: libc.strtol
strtoll       :: libc.strtoll
strtoul       :: libc.strtoul
strtoull      :: libc.strtoull

rand          :: libc.rand
srand         :: libc.srand

calloc        :: libc.calloc
malloc        :: libc.malloc
realloc       :: libc.realloc

abort         :: libc.abort
atexit        :: libc.atexit
at_quick_exit :: libc.at_quick_exit
exit          :: libc.exit
_Exit         :: libc._Exit
getenv        :: libc.getenv
quick_exit    :: libc.quick_exit
system        :: libc.system

bsearch       :: libc.bsearch
qsort         :: libc.qsort

abs           :: libc.abs
labs          :: libc.labs
llabs         :: libc.llabs
div           :: libc.div
ldiv          :: libc.ldiv
lldiv         :: libc.lldiv

mblen         :: libc.mblen
mbtowc        :: libc.mbtowc
wctomb        :: libc.wctomb

mbstowcs      :: libc.mbstowcs
wcstombs      :: libc.wcstombs

free :: #force_inline proc(ptr: $T) where intrinsics.type_is_pointer(T) || intrinsics.type_is_multi_pointer(T) || T == cstring {
	libc.free(rawptr(ptr))
}

foreign lib {

	/*
	Uses the string argument to set environment variable values. 

	Returns: 0 on success, non-zero (setting errno) on failure

	Example:
		if posix.putenv("HOME=/usr/home") != 0 {
			fmt.panicf("putenv failure: %v", posix.strerror(posix.errno()))
		}

	[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html ]]
	*/
	@(link_name=LPUTENV)
	putenv :: proc(string: cstring) -> c.int ---
}

EXIT_FAILURE :: libc.EXIT_FAILURE
EXIT_SUCCESS :: libc.EXIT_SUCCESS

RAND_MAX   :: libc.RAND_MAX
MB_CUR_MAX :: libc.MB_CUR_MAX

div_t   :: libc.div_t
ldiv_t  :: libc.ldiv_t
lldiv_t :: libc.lldiv_t

when ODIN_OS == .Windows {
	@(private) LPUTENV :: "_putenv"
} else when ODIN_OS == .NetBSD {
	@(private) LPUTENV :: "__putenv50"
} else {
	@(private) LPUTENV :: "putenv"
}