-
Notifications
You must be signed in to change notification settings - Fork 0
/
_ydbposix.m
496 lines (443 loc) · 14.6 KB
/
_ydbposix.m
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Copyright (c) 2012-2022 Fidelity National Information ;
; Services, Inc. and/or its subsidiaries. All rights reserved. ;
; ;
; Copyright (c) 2018-2023 YottaDB LLC and/or its subsidiaries. ;
; All rights reserved. ;
; ;
; This source code contains the intellectual property ;
; of its copyright holder(s), and is made available ;
; under a license. If you do not know the terms of ;
; the license, please stop and do not read further. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Current ydbposix version.
YDBPOSIXVERSION ;v6.0.0
%ydbposix
; High level wrappers to low level POSIX functions
set $etrap="set $etrap=""use $principal write $zstatus,! zhalt 1"" set tmp1=$piece($ecode,"","",2),tmp2=$text(@tmp1) if $length(tmp2) write $text(+0),@$piece(tmp2,"";"",2),! zhalt +$extract(tmp1,2,$length(tmp1))"
set $ecode=",U255," ; must call an entryref
quit
; Change the permissions of a file
CHMOD(name,mode)
new retval
set retval=$$chmod(name,mode)
quit:$quit retval quit
chmod(name,mode)
new errno
if $&ydbposix.chmod(name,$select(mode'=+mode:$$filemodeconst(mode),1:mode),.errno)
quit:$quit 1 quit
; Retrieve the time of the specified clock
CLOCKGETTIME(clock,sec,nsec)
new retval
set retval=$$clockgettime(clock,.sec,.nsec)
quit:$quit retval quit
clockgettime(clock,sec,nsec)
new errno
if $&ydbposix.clockgettime($select(clock'=+clock:$$clockval(clock),1:clock),.sec,.nsec,.errno)
quit:$quit 1 quit
clockval(clock) ; get numeric value for the specified clock
quit:$data(%ydbposix("clock",clock)) %ydbposix("clock",clock)
new clockval
if $&ydbposix.clockval(clock,.clockval)
set %ydbposix("clock",clock)=clockval
quit clockval
; Copy a file
CP(source,dest)
new retval
set retval=$$cp(source,dest)
quit:$quit retval quit
cp(source,dest)
new errno
if $&ydbposix.cp(source,dest,.errno)
quit:$quit 1 quit
; Get value for symbolic file modes - only lower case because this is an internal utility routine
filemodeconst(sym) ; get numeric value for file mode symbolic constant
quit:$data(%ydbposix("filemode",sym)) %ydbposix("filemode",sym)
new symval
if $&ydbposix.filemodeconst(sym,.symval)
set %ydbposix("filemode",sym)=symval
quit symval
; Create a directory
MKDIR(dirname,mode)
new retval
set retval=$$mkdir(dirname,mode)
quit:$quit retval quit
mkdir(dirname,mode)
new errno,retval
set retval=$&ydbposix.mkdir(dirname,$select(mode'=+mode:$$filemodeconst(mode),1:mode),.errno)
quit:$quit retval quit
; Get value for symbolic resource limits - only lower case because this is an internal utility routine
rlimitconst(sym) ; get numeric value for resource limit symbolic constant
quit:$data(%ydbposix("rlimit",sym)) %ydbposix("rlimit",sym)
new symval
if $&ydbposix.rlimitconst(sym,.symval)
set %ydbposix("rlimit",sym)=symval
quit symval
; Get a resource limit
GETRLIMIT(rlimit,cv)
new retval
set retval=$$getrlimit(rlimit,.cv)
quit:$quit retval quit
getrlimit(rlimit,cv)
new errno
if $&ydbposix.getrlimit($select(rlimit'=+rlimit:$$rlimitconst(rlimit),1:rlimit),.cv,.errno) quit:$quit *errno quit
quit:$quit 1 quit
GETUID(id)
do getuid(.id)
quit:$quit 1 quit
getuid(id)
set id=$&ydbposix.getuid()
quit:$quit 1 quit
; Convert a broken-down time structure to calendar time representation.
MKTIME(year,mon,mday,hour,min,sec,wday,yday,isdst,unixtime)
new retval
set retval=$$mktime($get(year),$get(mon),$get(mday),$get(hour),$get(min),$get(sec),.wday,.yday,.isdst,.unixtime)
quit:$quit retval quit
mktime(year,mon,mday,hour,min,sec,wday,yday,isdst,unixtime)
new errno
if $&ydbposix.mktime(year,mon,mday,hour,min,sec,.wday,.yday,.isdst,.unixtime,.errno)
quit:$quit 1 quit
; Create a temporary directory
MKTMPDIR(template)
new retval
set retval=$$mktmpdir(.template)
quit:$quit retval quit
mktmpdir(template)
new errno,mode,retval,savetemplate
set:"XXXXXX"'=$extract(template,$length(template)-5,$length(template)) $ecode=",U254,"
set savetemplate=template
do &ydbposix.mkdtemp(.template,.errno) ; discard return value since it is a pointer into heap space
set retval=0
if (savetemplate=template) do
. set $extract(template,$length(template)-5,$length(template))=$$^%RANDSTR(6)
. set retval='$$mkdir(template,"S_IRWXU")
quit:$quit 'retval quit
; Obtain a real path to the file
REALPATH(name,realpath)
new retval
set retval=$$realpath(name,.realpath)
quit:$quit retval quit
realpath(name,realpath)
new errno
if $&ydbposix.realpath(name,.realpath,.errno)
quit:$quit 1 quit
; Discard a previously compiled regular expression - *must* be passed by variable name
REGFREE(pregstr)
new retval
set retval=$$regfree(pregstr)
quit:$quit retval quit
regfree(pregstr)
if $&ydbposix.regfree(@pregstr)
zkill @pregstr
quit:$quit 1 quit
; Match a regular expression
REGMATCH(str,patt,pattflags,matchflags,matchresults,maxresults)
new retval
set retval=$$regmatch($get(str),$get(patt),$get(pattflags),$get(matchflags),.matchresults,$get(maxresults))
quit:$quit retval quit
regmatch(str,patt,pattflags,matchflags,matchresults,maxresults)
new errno,i,j,mfval,matchsuccess,nextmf,nextpf,nextrmeo,nextrmso,pfval,pregstr,regmatchtsize,regofftsize,resultbuf
if $length($get(pattflags)) for i=1:1:$length(pattflags,"+") do
. set nextpf=$piece(pattflags,"+",i)
. if $increment(pfval,$select(nextpf'=+nextpf:$$regsymval(nextpf),1:nextpf))
else set pfval=0
do:'$data(%ydbposix("regmatch",patt,pfval))
. do:'$&ydbposix.regcomp(.pregstr,patt,pfval,.errno)
. . zkill %ydbposix("regcomp","errno")
. . set %ydbposix("regmatch",patt,pfval)=pregstr
; nothing matched and that is due to an error in regcomp
if '$data(%ydbposix("regmatch",patt,pfval)) quit:$quit -errno_",regcomp" quit
set:'$data(maxresults) maxresults=1
set $zpiece(resultbuf,$zchar(0),maxresults*$$regsymval("sizeof(regmatch_t)")+1)=""
if $length($get(matchflags)) for i=1:1:$length(matchflags,"+") do
. set nextmf=$piece(matchflags,"+",i)
. if $increment(mfval,$select(nextmf'=+nextmf:$$regsymval(nextmf),1:nextmf))
else set mfval=0
if $&ydbposix.regexec(%ydbposix("regmatch",patt,pfval),str,maxresults,.resultbuf,mfval,.matchsuccess)
zkill %ydbposix("regexec","errno")
set i=0
do:matchsuccess
. kill matchresults
. set regmatchtsize=$$regsymval("sizeof(regmatch_t)"),j=1
. ; If we find `matchresults(i,"start")` is 0, it means match happened until `i-1` only hence the `kill` and `i-1` below
. for i=1:1:maxresults do if 'matchresults(i,"start") kill matchresults(i) set i=i-1 quit
. . kill nextrmso,nextrmeo
. . ; Note: `$&ydbposix.regofft2offsets()` returns 0 in all cases where 3 parameters (valid # of parms) are passed.
. . ; Therefore, the `if` check always succeeds and the `do` code always executes.
. . if '$&ydbposix.regofft2offsets($zextract(resultbuf,j,$increment(j,regmatchtsize)-1),.nextrmso,.nextrmeo) do
. . . set matchresults(i,"start")=1+nextrmso
. . . set matchresults(i,"end")=1+nextrmeo
. . ; Because the `do` code above is always executed, `matchresults(i)` is guaranteed to be set at this point.
quit:$quit i quit
; Get numeric value for regular expression symbolic constant - only lower case because this is an internal utility routine
regsymval(sym)
quit:$data(%ydbposix("regmatch",sym)) %ydbposix("regmatch",sym)
new symval
if $&ydbposix.regconst(sym,.symval)
set %ydbposix("regmatch",sym)=symval
quit symval
; Remove a directory
RMDIR(dirname)
new retval
set retval=$$rmdir(dirname)
quit:$quit retval quit
rmdir(dirname)
new errno
if $&ydbposix.rmdir(dirname,.errno)
quit:$quit 1 quit
; Set an environment variable
; This function is deprecated and retained for backward compatibility.
; Use VIEW SETENV instead.
SETENV(name,value,overwrite)
new retval
set retval=$$setenv(name,value,$get(overwrite))
quit:$quit retval quit
setenv(name,value,overwrite)
new errno
if $&ydbposix.setenv(name,value,$get(overwrite),.errno)
quit:$quit 1 quit
; Return attributes for a file in a local variable passed in by reference
STATFILE(f,s)
new retval
set retval=$$statfile(f,.s)
quit:$quit retval quit
statfile(f,s)
new atime,blksize,blocks,ctime,dev,errno,gid,ino,mode,mtime,natime,nctime,nlink,nmtime,rdev,retval,size,uid
set retval=$&ydbposix.stat(f,.dev,.ino,.mode,.nlink,.uid,.gid,.rdev,.size,.blksize,.blocks,.atime,.natime,.mtime,.nmtime,.ctime,.nctime,.errno)
kill s
set s("atime")=atime
set s("blksize")=blksize
set s("blocks")=blocks
set s("ctime")=ctime
set s("dev")=dev
set s("gid")=gid
set s("ino")=ino
set s("mode")=mode
set s("mtime")=mtime
set s("natime")=natime
set s("nctime")=nctime
set s("nlink")=nlink
set s("nmtime")=nmtime
set s("rdev")=rdev
set s("size")=size
set s("uid")=uid
quit:$quit retval quit
; Create a symbolic link
SYMLINK(target,name)
new retval
set retval=$$symlink(target,name)
quit:$quit retval quit
symlink(target,name)
new errno
if $&ydbposix.symlink(target,name,.errno)
quit:$quit 1 quit
; Get configuration information
SYSCONF(name,value)
new retval
set retval=$$sysconf(name,.value)
quit:$quit retval quit
sysconf(name,value)
new errno
if $&ydbposix.sysconf($select(name'=+name:$$sysconfval(name),1:name),.value,.errno)
quit:$quit 1 quit
sysconfval(option) ; get numeric value for the specified configuration option
quit:$data(%ydbposix("sysconf",option)) %ydbposix("sysconf",option)
new sysconfval
if $&ydbposix.sysconfval(option,.sysconfval)
set %ydbposix("sysconf",option)=sysconfval
quit sysconfval
; Log a message to the system log
; Unless you really need the fine-grained control this offers, the built-in function
; $ZSYSLOG() should suffice for most needs.
SYSLOG(message,facility,level)
new retval
set retval=$$syslog($get(message),$get(facility),$get(level))
quit:$quit retval quit
syslog(message,facility,level)
if $data(facility)#10 set:facility'=+facility facility=$$syslogval(facility)
else set facility=$$syslogval("LOG_USER")
if $data(level)#10 set:level'=+level level=$$syslogval(level)
else set level=$$syslogval("LOG_INFO")
if $&ydbposix.syslog(+facility+level,message)
quit:$quit 1 quit
syslogval(msg) ; get numeric value for syslog symbolic constant
quit:$data(%ydbposix("syslog",msg))#10 %ydbposix("syslog",msg)
new msgval
if $&ydbposix.syslogconst(msg,.msgval)
set %ydbposix("syslog",msg)=msgval
quit msgval
; Unset an environment variable
; This function is deprecated and retained for backward compatibility.
; Use VIEW UNSETENV instead.
UNSETENV(name)
new retval
set retval=$$unsetenv(name)
quit:$quit retval quit
unsetenv(name)
new errno
if $&ydbposix.unsetenv(name,.errno)
quit:$quit 1 quit
; Set user's file mode creation mask
UMASK(mode,prevMode)
new retval
set retval=$$umask(mode,.prevMode)
quit:$quit retval quit
umask(mode,prevMode)
new errno
if $&ydbposix.umask($select(mode'=+mode:$$filemodeconst(mode),1:mode),.prevMode,.errno)
quit:$quit 1 quit
; Update the timestamp of a file
UTIMES(name)
new retval
set retval=$$utimes(name)
quit:$quit retval quit
utimes(name)
new errno
if $&ydbposix.utimes(name,.errno)
quit:$quit 1 quit
; Provide a version number for this wrapper based on the value defined in YDBPOSIXVERSION's comment
VERSION() quit $$version
version()
new ver
set ver=$piece($text(YDBPOSIXVERSION),";",2)
quit:$quit ver quit
; Extrinsic special variable that extends $HOROLOG and reports in microseconds
; This function is deprecated and retained for backward compatibility.
; Consider using $ZHOROLOG instead.
ZHOROLOG() quit $$zhorolog()
zhorolog()
new day,errno,hour,isdst,mday,min,mon,retval,sec,tvsec,tvusec,wday,yday,year
if $&ydbposix.gettimeofday(.tvsec,.tvusec,.errno)
if $&ydbposix.localtime(tvsec,.sec,.min,.hour,.mday,.mon,.year,.wday,.yday,.isdst,.errno)
quit:$quit $$FUNC^%DATE(mon+1_"/"_mday_"/"_(1900+year))_","_(((hour*60)+min)*60+sec)_$select(tvusec:tvusec*1E-6,1:"") quit
; libm functions contributed to YottaDB by a contributor who wished to remain anonymous
EXP(x,errno) goto exp+1
exp(x,errno) ;$$;
new out
set errno=$&ydbposix.exp(x,.out)
quit:$quit out quit
LOG(x,errno) goto log+1
log(x,errno) ;$$;
new out
set errno=$&ydbposix.log(x,.out)
quit:$quit out quit
LOG10(x,errno) goto log10+1
log10(x,errno) ;$$;
new out
set errno=$&ydbposix.log10(x,.out)
quit:$quit out quit
COS(x,errno) goto cos+1
cos(x,errno) ;$$;
new out
set errno=$&ydbposix.cos(x,.out)
quit:$quit out quit
SIN(x,errno) goto sin+1
sin(x,errno) ;$$;
new out
set errno=$&ydbposix.sin(x,.out)
quit:$quit out quit
ARCSIN(x,errno) goto arcsin+1
arcsin(x,errno) ;$$;
new out
set errno=$&ydbposix.arcsin(x,.out)
quit:$quit out quit
ARCCOS(x,errno) goto arccos+1
arccos(x,errno) ;$$;
new out
set errno=$&ydbposix.arccos(x,.out)
quit:$quit out quit
TAN(x,errno) goto tan+1
tan(x,errno) ;$$;
new out
set errno=$&ydbposix.tan(x,.out)
quit:$quit out quit
ARCTAN(x,errno) goto arctan+1
arctan(x,errno) ;$$;
new out
set errno=$&ydbposix.arctan(x,.out)
quit:$quit out quit
SQRT(x,errno) goto sqrt+1
sqrt(x,errno) ;$$;
new out
set errno=$&ydbposix.sqrt(x,.out)
quit:$quit out quit
COSH(x,errno) goto cosh+1
cosh(x,errno) ;$$;
new out
set errno=$&ydbposix.cosh(x,.out)
quit:$quit out quit
SINH(x,errno) goto sinh+1
sinh(x,errno) ;$$;
new out
set errno=$&ydbposix.sinh(x,.out)
quit:$quit out quit
TANH(x,errno) goto tanh+1
tanh(x,errno) ;$$;
new out
set errno=$&ydbposix.tanh(x,.out)
quit:$quit out quit
ACOSH(x,errno) goto acosh+1
acosh(x,errno) ;$$;
new out
set errno=$&ydbposix.acosh(x,.out)
quit:$quit out quit
ASINH(x,errno) goto asinh+1
asinh(x,errno) ;$$;
new out
set errno=$&ydbposix.asinh(x,.out)
quit:$quit out quit
ATANH(x,errno) goto atanh+1
atanh(x,errno) ;$$;
new out
set errno=$&ydbposix.atanh(x,.out)
quit:$quit out quit
CEIL(x,errno) goto ceil+1
ceil(x,errno) ;$$;
new out
set errno=$&ydbposix.ceil(x,.out)
quit:$quit out quit
FABS(x,errno) goto fabs+1
fabs(x,errno) ;$$;
new out
set errno=$&ydbposix.fabs(x,.out)
quit:$quit out quit
FLOOR(x,errno) goto floor+1
floor(x,errno) ;$$;
new out
set errno=$&ydbposix.floor(x,.out)
quit:$quit out quit
POW(x,y,errno) goto pow+1
pow(x,y,errno) ;$$;
new out
set errno=$&ydbposix.pow(x,y,.out)
quit:$quit out quit
FMOD(x,y,errno) goto fmod+1
fmod(x,y,errno) ;$$;
new out
set errno=$&ydbposix.fmod(x,y,.out)
quit:$quit out quit
FDIM(x,y,errno) goto fdim+1
fdim(x,y,errno) ;$$;
new out
set errno=$&ydbposix.fdim(x,y,.out)
quit:$quit out quit
FMAX(x,y,errno) goto fmax+1
fmax(x,y,errno) ;$$;
new out
set errno=$&ydbposix.fmax(x,y,.out)
quit:$quit out quit
FMIN(x,y,errno) goto fmin+1
fmin(x,y,errno) ;$$;
new out
set errno=$&ydbposix.fmin(x,y,.out)
quit:$quit out quit
FMA(x,y,z,errno) goto fma+1
fma(x,y,z,errno) ;$$;
new out
set errno=$&ydbposix.fma(x,y,z,.out)
quit:$quit out quit
; Error message texts
U254 ;"-F-BADTEMPLATE Template "_template_" does not end in ""XXXXXX"""
U255 ;"-F-BADINVOCATION Must call an entryref in "_$text(+0)