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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
#+build linux, darwin, netbsd, openbsd, freebsd, haiku
package posix
import "base:intrinsics"
import "core:c"
when ODIN_OS == .Darwin {
foreign import lib "system:System"
} else {
foreign import lib "system:c"
}
foreign lib {
/*
Takes a pointer to a radix-64 representation, in which the first digit is the least significant,
and return the corresponding long value.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/a64l.html ]]
*/
a64l :: proc(s: cstring) -> c.long ---
/*
The l64a() function shall take a long argument and return a pointer to the corresponding
radix-64 representation.
Returns: a string that may be invalidated by subsequent calls
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/a64l.html ]]
*/
l64a :: proc(value: c.long) -> cstring ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
Returns: non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0,1.0)
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
drand48 :: proc() -> c.double ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
Returns: non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0,1.0)
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
erand48 :: proc(xsubi: ^[3]c.ushort) -> c.double ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
Returns: return signed long integers uniformly distributed over the interval [-231,231)
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
mrand48 :: proc() -> c.long ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
Returns: return signed long integers uniformly distributed over the interval [-231,231)
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
jrand48 :: proc(xsubi: ^[3]c.ushort) -> c.long ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
Returns: non-negative, long integers, uniformly distributed over the interval [0,231)
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
lrand48 :: proc() -> c.long ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
Returns: non-negative, long integers, uniformly distributed over the interval [0,231)
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
nrand48 :: proc(xsubi: ^[3]c.ushort) -> c.long ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
The srand48(), seed48(), and lcong48() functions are initialization entry points, one of which should be invoked before either drand48(), lrand48(), or mrand48() is called.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
srand48 :: proc(seedval: c.long) ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
The srand48(), seed48(), and lcong48() functions are initialization entry points, one of which should be invoked before either drand48(), lrand48(), or mrand48() is called.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
lcong48 :: proc(param: ^[7]c.ushort) ---
/*
This family of functions shall generate pseudo-random numbers using a linear congruential algorithm and 48-bit integer arithmetic.
The srand48(), seed48(), and lcong48() functions are initialization entry points, one of which should be invoked before either drand48(), lrand48(), or mrand48() is called.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html ]]
*/
seed48 :: proc(seed16v: ^[3]c.ushort) -> ^[3]c.ushort ---
/*
Parses suboption arguments in a flag argument.
Returns: the index of the matched token string, or -1 if no token strings were matched
Example:
args := runtime.args__
Opt :: enum {
RO,
RW,
NAME,
NIL,
}
token := [Opt]cstring{
.RO = "ro",
.RW = "rw",
.NAME = "name",
.NIL = nil,
}
Options :: struct {
readonly, readwrite: bool,
name: cstring,
}
opts: Options
errfnd: bool
for {
opt := posix.getopt(i32(len(args)), raw_data(args), "o:")
if opt == -1 {
break
}
switch opt {
case 'o':
subopt := posix.optarg
value: cstring
for subopt != "" && !errfnd {
o := posix.getsubopt(&subopt, &token[.RO], &value)
switch Opt(o) {
case .RO: opts.readonly = true
case .RW: opts.readwrite = true
case .NAME:
if value == nil {
fmt.eprintfln("missing value for suboption %s", token[.NAME])
errfnd = true
continue
}
opts.name = value
case .NIL:
fallthrough
case:
fmt.eprintfln("no match found for token: %s", value)
errfnd = true
}
}
if opts.readwrite && opts.readonly {
fmt.eprintfln("Only one of %s and %s can be specified", token[.RO], token[.RW])
errfnd = true
}
case:
errfnd = true
}
}
if errfnd || len(args) == 1 {
fmt.eprintfln("\nUsage: %s -o <suboptstring>", args[0])
fmt.eprintfln("suboptions are 'ro', 'rw', and 'name=<value>'")
posix.exit(1)
}
fmt.println(opts)
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsubopt.html ]]
*/
getsubopt :: proc(optionp: ^cstring, keylistp: [^]cstring, valuep: ^cstring) -> c.int ---
/*
Changes the mode and ownership of the slave pseudo-terminal device associated with its master pseudo-terminal counterpart.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/grantpt.html ]]
*/
grantpt :: proc(fildes: FD) -> result ---
/*
Allows a state array, pointed to by the state argument, to be initialized for future use.
Returns: the previous state array or nil on failure
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/initstate.html ]]
*/
@(link_name=LINITSTATE)
initstate :: proc(seed: c.uint, state: [^]byte, size: c.size_t) -> [^]byte ---
/*
Sets the state array of the random number generator.
Returns: the previous state array or nil on failure
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/initstate.html ]]
*/
setstate :: proc(state: [^]byte) -> [^]byte ---
/*
Use a non-linear additive feedback random-number generator employing a default state array
size of 31 long integers to return successive pseudo-random numbers in the range from 0 to 231-1.
The period of this random-number generator is approximately 16 x (231-1).
The size of the state array determines the period of the random-number generator.
Increasing the state array size shall increase the period.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/initstate.html ]]
*/
random :: proc() -> c.long ---
/*
Initializes the current state array using the value of seed.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/initstate.html ]]
*/
@(link_name=LSRANDOM)
srandom :: proc(seed: c.uint) ---
/*
Creates a directory with a unique name derived from template.
The application shall ensure that the string provided in template is a pathname ending
with at least six trailing 'X' characters.
Returns: nil (setting errno) on failure, template on success
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdtemp.html ]]
*/
mkdtemp :: proc(template: [^]byte) -> cstring ---
/*
Creates a regular file with a unique name derived from template and return a file descriptor
for the file open for reading and writing.
The application shall ensure that the string provided in template is a pathname ending with
at least six trailing 'X' characters.
Returns: -1 (setting errno) on failure, an open file descriptor on success
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdtemp.html ]]
*/
mkstemp :: proc(template: cstring) -> FD ---
/*
Allocates size bytes aligned on a boundary specified by alignment, and shall return a pointer
to the allocated memory in memptr.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html ]]
*/
posix_memalign :: proc(memptr: ^[^]byte, alignment: c.size_t, size: c.size_t) -> Errno ---
/*
Establishes a connection between a master device for a pseudo-terminal and a file descriptor.
Returns: -1 (setting errno) on failure, an open file descriptor otherwise
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_openpt.html ]]
*/
posix_openpt :: proc(oflag: O_Flags) -> FD ---
/*
Returns the name of the slave pseudo-terminal device associated with a master pseudo-terminal device.
Returns: nil (setting errno) on failure, the name on success, which may be invalidated on subsequent calls
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/ptsname.html ]]
*/
ptsname :: proc(fildes: FD) -> cstring ---
/*
Unlocks the slave pseudo-terminal device associated with the master to which fildes refers.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlockpt.html ]]
*/
unlockpt :: proc(fildes: FD) -> result ---
/*
Updates or add a variable in the environment of the calling process.
Example:
if posix.setenv("HOME", "/usr/home") != .OK {
fmt.panicf("putenv failure: %v", posix.strerror(posix.errno()))
}
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html ]]
*/
setenv :: proc(envname: cstring, envval: cstring, overwrite: b32) -> result ---
/*
Removes an environment variable from the environment of the calling process.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html ]]
*/
@(link_name=LUNSETENV)
unsetenv :: proc(name: cstring) -> result ---
/*
Computes a sequence of pseudo-random integers in the range [0, {RAND_MAX}].
(The value of the {RAND_MAX} macro shall be at least 32767.)
If rand_r() is called with the same initial value for the object pointed to by seed and that object is not modified between successive returns and calls to rand_r(), the same sequence shall be generated.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/rand_r.html ]]
*/
rand_r :: proc(seed: ^c.uint) -> c.int ---
/*
Derive, from the pathname file_name, an absolute pathname that resolves to the same directory entry,
whose resolution does not involve '.', '..', or symbolic links.
If resolved_name is not `nil` it should be larger than `PATH_MAX` and the result will use it as a backing buffer.
If resolved_name is `nil` the returned string is allocated by `malloc`.
Returns: `nil` (setting errno) on failure, the "real path" otherwise
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html ]]
*/
realpath :: proc(file_name: cstring, resolved_name: [^]byte = nil) -> cstring ---
/*
Provides access to an implementation-defined encoding algorithm.
The argument of setkey() is an array of length 64 bytes containing only the bytes with numerical
value of 0 and 1.
If this string is divided into groups of 8, the low-order bit in each group is ignored; this gives a 56-bit key which is used by the algorithm.
This is the key that shall be used with the algorithm to encode a string block passed to encrypt().
The setkey() function shall not change the setting of errno if successful.
An application wishing to check for error situations should set errno to 0 before calling setkey().
If errno is non-zero on return, an error has occurred.
Example:
key: [64]byte
// set key bytes...
posix.set_errno(.NONE)
posix.setkey(raw_data(key))
if errno := posix.errno(); errno != .NONE {
fmt.panicf("setkey failure: %s", posix.strerror(errno))
}
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/setkey.html ]]
*/
setkey :: proc(key: [^]byte) ---
}
when ODIN_OS == .NetBSD {
@(private) LINITSTATE :: "__initstate60"
@(private) LSRANDOM :: "__srandom60"
@(private) LUNSETENV :: "__unsetenv13"
} else {
@(private) LINITSTATE :: "initstate"
@(private) LSRANDOM :: "srandom"
@(private) LUNSETENV :: "unsetenv"
}
|