/usr/share/lua/5.1/posix/deprecated.lua is in lua-posix 33.4.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 | --[[
POSIX library for Lua 5.1, 5.2 & 5.3.
Copyright (C) 2014-2016 Gary V. Vaughan
]]
--[[--
Legacy Lua POSIX bindings.
Undocumented Legacy APIs for compatibility with previous releases.
@module posix.deprecated
]]
local _argcheck = require "posix._argcheck"
local bit = require "bit32"
-- Lua 5.3 has table.unpack but not _G.unpack
-- Lua 5.2 has table.unpack and _G.unpack
-- Lua 5.1 has _G.unpack but not table.unpack
local unpack = table.unpack or unpack
local argerror, argtypeerror, badoption =
_argcheck.argerror, _argcheck.argtypeerror, _argcheck.badoption
local band, bnot, bor = bit.band, bit.bnot, bit.bor
local checkint, checkselection, checkstring, checktable =
_argcheck.checkint, _argcheck.checkselection, _argcheck.checkstring, _argcheck.checktable
local optint, optstring, opttable =
_argcheck.optint, _argcheck.optstring, _argcheck.opttable
local toomanyargerror = _argcheck.toomanyargerror
-- Convert a legacy API tm table to a posix.time.PosixTm compatible table.
local function PosixTm (legacytm)
return {
tm_sec = legacytm.sec,
tm_min = legacytm.min,
tm_hour = legacytm.hour,
-- For compatibility with Lua os.date() use "day" if "monthday" is
-- not set.
tm_mday = legacytm.monthday or legacytm.day,
tm_mon = legacytm.month - 1,
tm_year = legacytm.year - 1900,
tm_wday = legacytm.weekday,
tm_yday = legacytm.yearday,
tm_isdst = legacytm.is_dst and 1 or 0,
}
end
-- Convert a posix.time.PosixTm into a legacy API tm table.
local function LegacyTm (posixtm)
return {
sec = posixtm.tm_sec,
min = posixtm.tm_min,
hour = posixtm.tm_hour,
monthday = posixtm.tm_mday,
day = posixtm.tm_mday,
month = posixtm.tm_mon + 1,
year = posixtm.tm_year + 1900,
weekday = posixtm.tm_wday,
yearday = posixtm.tm_yday,
is_dst = posixtm.tm_isdst ~= 0,
}
end
local function doselection (name, argoffset, fields, map)
if #fields == 1 and type (fields[1]) == "table" then fields = fields[1] end
if not (next (fields)) then
return map
else
local r = {}
for i, v in ipairs (fields) do
if map[v] then
r[#r + 1] = map[v]
else
argerror (name, i + argoffset, "invalid option '" .. v .. "'", 2)
end
end
return unpack (r)
end
end
local st = require "posix.sys.stat"
local S_IRUSR, S_IWUSR, S_IXUSR = st.S_IRUSR, st.S_IWUSR, st.S_IXUSR
local S_IRGRP, S_IWGRP, S_IXGRP = st.S_IRGRP, st.S_IWGRP, st.S_IXGRP
local S_IROTH, S_IWOTH, S_IXOTH = st.S_IROTH, st.S_IWOTH, st.S_IXOTH
local S_ISUID, S_ISGID, S_IRWXU, S_IRWXG, S_IRWXO =
st.S_ISUID, st.S_ISGID, st.S_IRWXU, st.S_IRWXG, st.S_IRWXO
local mode_map = {
{ c = "r", b = S_IRUSR }, { c = "w", b = S_IWUSR }, { c = "x", b = S_IXUSR },
{ c = "r", b = S_IRGRP }, { c = "w", b = S_IWGRP }, { c = "x", b = S_IXGRP },
{ c = "r", b = S_IROTH }, { c = "w", b = S_IWOTH }, { c = "x", b = S_IXOTH },
}
local function pushmode (mode)
local m = {}
for i = 1, 9 do
if band (mode, mode_map[i].b) ~= 0 then m[i] = mode_map[i].c else m[i] = "-" end
end
if band (mode, S_ISUID) ~= 0 then
if band (mode, S_IXUSR) ~= 0 then m[3] = "s" else m[3] = "S" end
end
if band (mode, S_ISGID) ~= 0 then
if band (mode, S_IXGRP) ~= 0 then m[6] = "s" else m[6] = "S" end
end
return table.concat (m)
end
local M = {}
--- Bind an address to a socket.
-- @function bind
-- @int fd socket descriptor to act on
-- @tparam PosixSockaddr addr socket address
-- @treturn[1] bool `true`, if successful
-- @return[2] nil
-- @treturn[2] string error messag
-- @treturn[2] int errnum
-- @see bind(2)
local sock = require "posix.sys.socket"
local bind = sock.bind
function M.bind (...)
local rt = { bind (...) }
if rt[1] == 0 then return true end
return unpack (rt)
end
--- Find the precision of a clock.
-- @function clock_getres
-- @string[opt="realtime"] name name of clock, one of "monotonic",
-- "process\_cputime\_id", "realtime", or "thread\_cputime\_id"
-- @treturn[1] int seconds
-- @treturn[21 int nanoseconds, if successful
-- @return[2] nil
-- @treturn[2] string error message
-- @treturn[2] int errnum
-- @see clock_getres(3)
local tm = require "posix.time"
local _clock_getres = tm.clock_getres
local function get_clk_id_const (name)
local map = {
monotonic = tm.CLOCK_MONOTONIC,
process_cputime_id = tm.CLOCK_PROCESS_TIME_ID,
thread_cputime_id = tm.CLOCK_THREAD_CPUTIME_ID,
}
return map[name] or tm.CLOCK_REALTIME
end
local function clock_getres (name)
local ts = _clock_getres (get_clk_id_const (name))
return ts.tv_sec, ts.tv_nsec
end
if _clock_getres == nil then
-- Not supported by underlying system
elseif _DEBUG ~= false then
M.clock_getres = function (...)
local argt = {...}
optstring ("clock_getres", 1, argt[1], "realtime")
if #argt > 1 then toomanyargerror ("clock_getres", 1, #argt) end
return clock_getres (...)
end
else
M.clock_getres = clock_getres
end
--- Read a clock
-- @function clock_gettime
-- @string[opt="realtime"] name name of clock, one of "monotonic",
-- "process\_cputime\_id", "realtime", or "thread\_cputime\_id"
-- @treturn[1] int seconds
-- @treturn[21 int nanoseconds, if successful
-- @return[2] nil
-- @treturn[2] string error message
-- @treturn[2] int errnum
-- @see clock_gettime(3)
local tm = require "posix.time"
local _clock_gettime = tm.clock_gettime
local function clock_gettime (name)
local ts = _clock_gettime (get_clk_id_const (name))
return ts.tv_sec, ts.tv_nsec
end
if _clock_gettime == nil then
-- Not supported by underlying system
elseif _DEBUG ~= false then
M.clock_gettime = function (...)
local argt = {...}
optstring ("clock_gettime", 1, argt[1], "realtime")
if #argt > 1 then toomanyargerror ("clock_gettime", 1, #argt) end
return clock_gettime (...)
end
else
M.clock_gettime = clock_gettime
end
--- Initiate a connection on a socket.
-- @function connect
-- @int fd socket descriptor to act on
-- @tparam PosixSockaddr addr socket address
-- @treturn[1] bool `true`, if successful
-- @return[2] nil
-- @treturn[2] string error message
-- @treturn[2] int errnum
-- @see connect(2)
local sock = require "posix.sys.socket"
local connect = sock.connect
function M.connect (...)
local rt = { connect (...) }
if rt[1] == 0 then return true end
return unpack (rt)
end
--- Execute a program without using the shell.
-- @function exec
-- @string path
-- @tparam[opt] table|strings ... table or tuple of arguments (table can include index 0)
-- @return nil
-- @treturn string error message
-- @see execve(2)
local unistd = require "posix.unistd"
local _exec = unistd.exec
local function exec (path, ...)
local argt = {...}
if #argt == 1 and type (argt[1]) == "table" then
argt = argt[1]
end
return _exec (path, argt)
end
if _DEBUG ~= false then
M.exec = function (...)
local argt = {...}
checkstring ("exec", 1, argt[1])
if type (argt[2]) ~= "table" and type (argt[2]) ~= "string" and type (argt[2]) ~= "nil" then
argtypeerror ("exec", 2, "string, table or nil", argt[2])
end
if #argt > 2 then
if type (argt[2]) == "table" then
toomanyargerror ("exec", 2, #argt)
else
for i = 3, #argt do
checkstring ("exec", i, argt[i])
end
end
end
return exec (...)
end
else
M.exec = exec
end
--- Execute a program with the shell.
-- @function execp
-- @string path
-- @tparam[opt] table|strings ... table or tuple of arguments (table can include index 0)
-- @return nil
-- @treturn string error message
-- @see execve(2)
local unistd = require "posix.unistd"
local _execp = unistd.execp
local function execp (path, ...)
local argt = {...}
if #argt == 1 and type (argt[1]) == "table" then
argt = argt[1]
end
return _execp (path, argt)
end
if _DEBUG ~= false then
M.execp = function (...)
local argt = {...}
checkstring ("execp", 1, argt[1])
if type (argt[2]) ~= "table" and type (argt[2]) ~= "string" and type (argt[2]) ~= "nil" then
argtypeerror ("execp", 2, "string, table or nil", argt[2])
end
if #argt > 2 then
if type (argt[2]) == "table" then
toomanyargerror ("execp", 2, #argt)
else
for i = 3, #argt do
checkstring ("execp", i, argt[i])
end
end
end
return execp (...)
end
else
M.execp = execp
end
--- Instruct kernel on appropriate cache behaviour for a file or file segment.
-- @function fadvise
-- @tparam file fh Lua file object
-- @int offset start of region
-- @int len number of bytes in region
-- @int advice one of `POSIX_FADV_NORMAL, `POSIX_FADV_SEQUENTIAL,
-- `POSIX_FADV_RANDOM`, `POSIX_FADV_\NOREUSE`, `POSIX_FADV_WILLNEED` or
-- `POSIX_FADV_DONTNEED`
-- @treturn[1] int `0`, if successful
-- @return[2] nil
-- @treturn[2] string error message
-- @treturn[2] int errnum
-- @see posix_fadvise(2)
local fc = require "posix.fcntl"
local stdio = require "posix.stdio"
local posix_fadvise = fc.posix_fadvise
local fileno = stdio.fileno
local function fadvise (fh, ...)
return posix_fadvise (fileno (fh), ...)
end
if posix_fadvise == nil then
-- Not supported by underlying system
elseif _DEBUG ~= false then
M.fadvise = function (...)
argt = {...}
if io.type (argt[1]) ~= "file" then
argtypeerror ("fadvise", 1, "FILE*", argt[1])
end
checkint ("fadvise", 2, argt[2])
checkint ("fadvise", 3, argt[3])
checkint ("fadvise", 4, argt[4])
if #argt > 4 then toomanyargerror ("fadvise", 4, #argt) end
return fadvise (...)
end
else
M.fadvise = fadvise
end
--- Match a filename against a shell pattern.
-- @function fnmatch
-- @string pat shell pattern
-- @string name filename
-- @return true or false
-- @raise error if fnmatch failed
-- @see posix.fnmatch.fnmatch
local fnm = require "posix.fnmatch"
function M.fnmatch (...)
local r = fnm.fnmatch (...)
if r == 0 then
return true
elseif r == fnm.FNM_NOMATCH then
return false
end
error "fnmatch failed"
end
--- Group information.
-- @table group
-- @string name name of group
-- @int gid unique group id
-- @string ... list of group members
--- Information about a group.
-- @function getgroup
-- @tparam[opt=current group] int|string group id or group name
-- @treturn group group information
-- @usage
-- print (P.getgroup (P.getgid ()).name)
local grp = require "posix.grp"
local unistd = require "posix.unistd"
local getgrgid, getgrnam = grp.getgrgid, grp.getgrnam
local getegid = unistd.getegid
local function getgroup (grp)
if grp == nil then grp = getegid () end
local g
if type (grp) == "number" then
g = getgrgid (grp)
elseif type (grp) == "string" then
g = getgrnam (grp)
else
argtypeerror ("getgroup", 1, "string, int or nil", grp)
end
if g ~= nil then
return {name=g.gr_name, gid=g.gr_gid, mem=g.gr_mem}
end
end
if _DEBUG ~= false then
M.getgroup = function (...)
local argt = {...}
if #argt > 1 then toomanyargerror ("getgroup", 1, #argt) end
return getgroup (...)
end
else
M.getgroup = getgroup
end
--- Get the password entry for a user.
-- @function getpasswd
-- @tparam[opt=current user] int|string user name or id
-- @string ... field names, each one of "uid", "name", "gid", "passwd",
-- "dir" or "shell"
-- @return ... values, or a table of all fields if *user* is `nil`
-- @usage for a,b in pairs (P.getpasswd "root") do print (a, b) end
-- @usage print (P.getpasswd ("root", "shell"))
local pwd = require "posix.pwd"
local unistd = require "posix.unistd"
local getpwnam, getpwuid = pwd.getpwnam, pwd.getpwuid
local geteuid = unistd.geteuid
local function getpasswd (user, ...)
if user == nil then user = geteuid () end
local p
if type (user) == "number" then
p = getpwuid (user)
elseif type (user) == "string" then
p = getpwnam (user)
else
argtypeerror ("getpasswd", 1, "string, int or nil", user)
end
if p ~= nil then
return doselection ("getpasswd", 1, {...}, {
dir = p.pw_dir,
gid = p.pw_gid,
name = p.pw_name,
passwd = p.pw_passwd,
shell = p.pw_shell,
uid = p.pw_uid,
})
end
end
if _DEBUG ~= false then
M.getpasswd = function (user, ...)
checkselection ("getpasswd", 2, {...}, 2)
return getpasswd (user, ...)
end
else
M.getpasswd = getpasswd
end
--- Get process identifiers.
-- @function getpid
-- @tparam[opt] table|string type one of "egid", "euid", "gid", "uid",
-- "pgrp", "pid" or "ppid"; or a single list of the same
-- @string[opt] ... unless *type* was a table, zero or more additional
-- type strings
-- @return ... values, or a table of all fields if no option given
-- @usage for a,b in pairs (P.getpid ()) do print (a, b) end
-- @usage print (P.getpid ("uid", "euid"))
local unistd = require "posix.unistd"
local getegid, geteuid, getgid, getuid =
unistd.getegid, unistd.geteuid, unistd.getgid, unistd.getuid
local _getpid, getpgrp, getppid =
unistd.getpid, unistd.getpgrp, unistd.getppid
local function getpid (...)
return doselection ("getpid", 0, {...}, {
egid = getegid (),
euid = geteuid (),
gid = getgid (),
uid = getuid (),
pgrp = getpgrp (),
pid = _getpid (),
ppid = getppid (),
})
end
if _DEBUG ~= false then
M.getpid = function (...)
checkselection ("getpid", 1, {...}, 2)
return getpid (...)
end
else
M.getpid = getpid
end
--- Get resource limits for this process.
-- @function getrlimit
-- @string resource one of "core", "cpu", "data", "fsize", "nofile",
-- "stack" or "as"
-- @treturn[1] int soft limit
-- @treturn[1] int hard limit, if successful
-- @return[2] nil
-- @treturn[2] string error message
-- @treturn[2] int errnum
local resource = require "posix.sys.resource"
local _getrlimit = resource.getrlimit
local rlimit_map = {
core = resource.RLIMIT_CORE,
cpu = resource.RLIMIT_CPU,
data = resource.RLIMIT_DATA,
fsize = resource.RLIMIT_FSIZE,
nofile = resource.RLIMIT_NOFILE,
stack = resource.RLIMIT_STACK,
as = resource.RLIMIT_AS,
}
local function getrlimit (rcstr)
local rc = rlimit_map[string.lower (rcstr)]
if rc == nil then
argerror("getrlimit", 1, "invalid option '" .. rcstr .. "'")
end
local rlim = _getrlimit (rc)
return rlim.rlim_cur, rlim.rlim_max
end
if _DEBUG ~= false then
M.getrlimit = function (...)
local argt = {...}
checkstring ("getrlimit", 1, argt[1])
if #argt > 1 then toomanyargerror ("getrlimit", 1, #argt) end
return getrlimit (...)
end
else
M.getrlimit = getrlimit
end
--- Get time of day.
-- @function gettimeofday
-- @treturn timeval time elapsed since *epoch*
-- @see gettimeofday(2)
local systime = require "posix.sys.time"
local gettimeofday = systime.gettimeofday
function M.gettimeofday (...)
local tv = gettimeofday (...)
return { sec = tv.tv_sec, usec = tv.tv_usec }
end
--- Convert epoch time value to a broken-down UTC time.
-- Here, broken-down time tables the month field is 1-based not
-- 0-based, and the year field is the full year, not years since
-- 1900.
-- @function gmtime
-- @int[opt=now] t seconds since epoch
-- @treturn table broken-down time
local tm = require "posix.time"
local _gmtime, time = tm.gmtime, tm.time
local function gmtime (epoch)
return LegacyTm (_gmtime (epoch or time ()))
end
if _DEBUG ~= false then
M.gmtime = function (...)
local argt = {...}
optint ("gmtime", 1, argt[1])
if #argt > 1 then toomanyargerror ("gmtime", 1, #argt) end
return gmtime (...)
end
else
M.gmtime = gmtime
end
--- Get host id.
-- @function hostid
-- @treturn int unique host identifier
local unistd = require "posix.unistd"
M.hostid = unistd.gethostid
--- Check for any printable character except space.
-- @function isgraph
-- @see isgraph(3)
-- @string character to act on
-- @treturn bool non-`false` if character is in the class
local ctype = require "posix.ctype"
local isgraph = ctype.isgraph
function M.isgraph (...)
return isgraph (...) ~= 0
end
--- Check for any printable character including space.
-- @function isprint
-- @string character to act on
-- @treturn bool non-`false` if character is in the class
-- @see isprint(3)
local ctype = require "posix.ctype"
local isprint = ctype.isprint
function M.isprint (...)
return isprint (...) ~= 0
end
--- Convert epoch time value to a broken-down local time.
-- Here, broken-down time tables the month field is 1-based not
-- 0-based, and the year field is the full year, not years since
-- 1900.
-- @function localtime
-- @int[opt=now] t seconds since epoch
-- @treturn table broken-down time
local tm = require "posix.time"
local _localtime, time = tm.localtime, tm.time
local function localtime (epoch)
return LegacyTm (_localtime (epoch or time ()))
end
if _DEBUG ~= false then
M.localtime = function (...)
local argt = {...}
optint ("localtime", 1, argt[1])
if #argt > 1 then toomanyargerror ("localtime", 1, #argt) end
return localtime (...)
end
else
M.localtime = localtime
end
--- Convert a broken-down localtime table into an epoch time.
-- @function mktime
-- @tparam tm broken-down localtime table
-- @treturn in seconds since epoch
-- @see mktime(3)
-- @see localtime
local tm = require "posix.time"
local _mktime, localtime, time = tm.mktime, tm.localtime, tm.time
local function mktime (legacytm)
local posixtm = legacytm and PosixTm (legacytm) or localtime (time ())
return _mktime (posixtm)
end
if _DEBUG ~= false then
M.mktime = function (...)
local argt = {...}
opttable ("mktime", 1, argt[1])
if #argt > 1 then toomanyargerror ("mktime", 1, #argt) end
return mktime (...)
end
else
M.mktime = mktime
end
--- Sleep with nanosecond precision.
-- @function nanosleep
-- @int seconds requested sleep time
-- @int nanoseconds requested sleep time
-- @treturn[1] int `0` if requested time has elapsed
-- @return[2] nil
-- @treturn[2] string error message
-- @treturn[2] int errnum
-- @treturn[2] int unslept seconds remaining, if interrupted
-- @treturn[2] int unslept nanoseconds remaining, if interrupted
-- @see nanosleep(2)
-- @see posix.unistd.sleep
local tm = require "posix.time"
local _nanosleep = tm.nanosleep
local function nanosleep (sec, nsec)
local r, errmsg, errno, timespec = _nanosleep {tv_sec = sec, tv_nsec = nsec}
if r == 0 then return 0 end
return r, errmsg, errno, timespec.tv_sec, timespec.tv_nsec
end
if _DEBUG ~= false then
M.nanosleep = function (...)
local argt = {...}
checkint ("nanosleep", 1, argt[1])
checkint ("nanosleep", 2, argt[2])
if #argt > 2 then toomanyargerror ("nanosleep", 2, #argt) end
return nanosleep (...)
end
else
M.nanosleep = nanosleep
end
--- Open the system logger.
-- @function openlog
-- @string ident all messages will start with this
-- @string[opt] option any combination of 'c' (directly to system console
-- if an error sending), 'n' (no delay) and 'p' (show PID)
-- @int [opt=`LOG_USER`] facility one of `LOG_AUTH`, `LOG_AUTHORITY`,
-- `LOG_CRON`, `LOG_DAEMON`, `LOG_FTP`, `LOG_KERN`, `LOG_LPR`, `LOG_MAIL`,
-- `LOG_NEWS`, `LOG_SECURITY`, `LOG_SYSLOG`, `LOG_USER`, `LOG_UUCP` or
-- `LOG_LOCAL0` through `LOG_LOCAL7`
-- @see syslog(3)
local bit = require "bit32"
local log = require "posix.syslog"
local bor = bit.bor
local _openlog = log.openlog
local optionmap = {
[' '] = 0,
c = log.LOG_CONS,
n = log.LOG_NDELAY,
p = log.LOG_PID,
}
local function openlog (ident, optstr, facility)
local option = 0
if optstr then
for i = 1, #optstr do
local c = optstr:sub (i, i)
if optionmap[c] == nil then
badoption ("openlog", 2, "openlog", c)
end
option = bor (option, optionmap[c])
end
end
return _openlog (ident, option, facility)
end
if _DEBUG ~= false then
M.openlog = function (...)
local argt = {...}
checkstring ("openlog", 1, argt[1])
optstring ("openlog", 2, argt[2])
optint ("openlog", 3, argt[3])
if #argt > 3 then toomanyargerror ("openlog", 3, #argt) end
return openlog (...)
end
else
M.openlog = openlog
end
--- Get configuration information at runtime.
-- @function pathconf
-- @string[opt="."] path file to act on
-- @tparam[opt] table|string key one of "CHOWN_RESTRICTED", "LINK_MAX",
-- "MAX_CANON", "MAX_INPUT", "NAME_MAX", "NO_TRUNC", "PATH_MAX", "PIPE_BUF"
-- or "VDISABLE"
-- @string[opt] ... unless *type* was a table, zero or more additional
-- type strings
-- @return ... values, or a table of all fields if no option given
-- @see sysconf(2)
-- @usage for a,b in pairs (P.pathconf "/dev/tty") do print (a, b) end
local unistd = require "posix.unistd"
local _pathconf = unistd.pathconf
local Spathconf = { CHOWN_RESTRICTED = 1, LINK_MAX = 1, MAX_CANON = 1,
MAX_INPUT = 1, NAME_MAX = 1, NO_TRUNC = 1, PATH_MAX = 1, PIPE_BUF = 1,
VDISABLE = 1 }
local function pathconf (path, ...)
local argt, map = {...}, {}
if path ~= nil and Spathconf[path] ~= nil then
path, argt = ".", {path, ...}
end
for k in pairs (Spathconf) do
map[k] = _pathconf (path or ".", unistd["_PC_" .. k])
end
return doselection ("pathconf", 1, {...}, map)
end
if _DEBUG ~= false then
M.pathconf = function (path, ...)
if path ~= nil and Spathconf[path] ~= nil then
checkselection ("pathconf", 1, {path, ...}, 2)
else
optstring ("pathconf", 1, path, ".", 2)
checkselection ("pathconf", 2, {...}, 2)
end
return pathconf (path, ...)
end
else
M.pathconf = pathconf
end
--- Set resource limits for this process.
-- @function setrlimit
-- @string resource one of "core", "cpu", "data", "fsize", "nofile",
-- "stack" or "as"
-- @int[opt] softlimit process may receive a signal when reached
-- @int[opt] hardlimit process may be terminated when reached
-- @treturn[1] int `0`, if successful
-- @return[2] nil
-- @treturn[2] string error message
-- @treturn[2] int errnum
local resource = require "posix.sys.resource"
local _setrlimit = resource.setrlimit
local rlimit_map = {
core = resource.RLIMIT_CORE,
cpu = resource.RLIMIT_CPU,
data = resource.RLIMIT_DATA,
fsize = resource.RLIMIT_FSIZE,
nofile = resource.RLIMIT_NOFILE,
stack = resource.RLIMIT_STACK,
as = resource.RLIMIT_AS,
}
local function setrlimit (rcstr, cur, max)
local rc = rlimit_map[string.lower (rcstr)]
if rc == nil then
argerror("setrlimit", 1, "invalid option '" .. rcstr .. "'")
end
local lim
if cur == nil or max == nil then
lim= _getrlimit (rc)
end
return _setrlimit (rc, {
rlim_cur = cur or lim.rlim_cur,
rlim_max = max or lim.rlim_max,
})
end
if _DEBUG ~= false then
M.setrlimit = function (...)
local argt = {...}
checkstring ("setrlimit", 1, argt[1])
optint ("setrlimit", 2, argt[2])
optint ("setrlimit", 3, argt[3])
if #argt > 3 then toomanyargerror ("setrlimit", 3, #argt) end
return setrlimit (...)
end
else
M.getrlimit = getrlimit
end
--- Information about an existing file path.
-- If the file is a symbolic link, return information about the link
-- itself.
-- @function stat
-- @string path file to act on
-- @tparam[opt] table|string field one of "dev", "ino", "mode", "nlink",
-- "uid", "gid", "rdev", "size", "atime", "mtime", "ctime" or "type"
-- @string[opt] ... unless *field* was a table, zero or more additional
-- field names
-- @return values, or table of all fields if no option given
-- @see stat(2)
-- @usage for a,b in pairs (P,stat "/etc/") do print (a, b) end
local st = require "posix.sys.stat"
local S_ISREG, S_ISLNK, S_ISDIR, S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK =
st.S_ISREG, st.S_ISLNK, st.S_ISDIR, st.S_ISCHR, st.S_ISBLK, st.S_ISFIFO, st.S_ISSOCK
local function filetype (mode)
if S_ISREG (mode) ~= 0 then
return "regular"
elseif S_ISLNK (mode) ~= 0 then
return "link"
elseif S_ISDIR (mode) ~= 0 then
return "directory"
elseif S_ISCHR (mode) ~= 0 then
return "character device"
elseif S_ISBLK (mode) ~= 0 then
return "block device"
elseif S_ISFIFO (mode) ~= 0 then
return "fifo"
elseif S_ISSOCK (mode) ~= 0 then
return "socket"
else
return "?"
end
end
local _stat = st.lstat -- for bugwards compatibility with v<=32
local function stat (path, ...)
local info = _stat (path)
if info ~= nil then
return doselection ("stat", 1, {...}, {
dev = info.st_dev,
ino = info.st_ino,
mode = pushmode (info.st_mode),
nlink = info.st_nlink,
uid = info.st_uid,
gid = info.st_gid,
size = info.st_size,
atime = info.st_atime,
mtime = info.st_mtime,
ctime = info.st_ctime,
type = filetype (info.st_mode),
})
end
end
if _DEBUG ~= false then
M.stat = function (path, ...)
checkstring ("stat", 1, path, 2)
checkselection ("stat", 2, {...}, 2)
return stat (path, ...)
end
else
M.stat = stat
end
--- Fetch file system statistics.
-- @function statvfs
-- @string path any path within the mounted file system
-- @tparam[opt] table|string field one of "bsize", "frsize", "blocks",
-- "bfree", "bavail", "files", "ffree", "favail", "fsid", "flag",
-- "namemax"
-- @string[opt] ... unless *field* was a table, zero or more additional
-- field names
-- @return values, or table of all fields if no option given
-- @see statvfs(2)
-- @usage for a,b in pairs (P,statvfs "/") do print (a, b) end
local sv = require "posix.sys.statvfs"
local _statvfs = sv.statvfs
local function statvfs (path, ...)
local info = _statvfs (path)
if info ~= nil then
return doselection ("statvfs", 1, {...}, {
bsize = info.f_bsize,
frsize = info.f_frsize,
blocks = info.f_blocks,
bfree = info.f_bfree,
bavail = info.f_bavail,
files = info.f_files,
ffree = info.f_ffree,
favail = info.f_favail,
fsid = info.f_fsid,
flag = info.f_flag,
namemax = info.f_namemax,
})
end
end
if _DEBUG ~= false then
M.statvfs = function (path, ...)
checkstring ("statvfs", 1, path, 2)
checkselection ("statvfs", 2, {...}, 2)
return statvfs (path, ...)
end
else
M.statvfs = statvfs
end
--- Write a time out according to a format.
-- @function strftime
-- @string format specifier with `%` place-holders
-- @tparam PosixTm tm broken-down local time
-- @treturn string *format* with place-holders plugged with *tm* values
-- @see strftime(3)
local tm = require "posix.time"
local _strftime, localtime, time = tm.strftime, tm.localtime, tm.time
local function strftime (fmt, legacytm)
local posixtm = legacytm and PosixTm (legacytm) or localtime (time ())
return _strftime (fmt, posixtm)
end
if _DEBUG ~= false then
M.strftime = function (...)
local argt = {...}
checkstring ("strftime", 1, argt[1])
opttable ("strftime", 2, argt[2])
if #argt > 2 then toomanyargerror ("strftime", 2, #argt) end
return strftime (...)
end
else
M.strftime = strftime
end
--- Parse a date string.
-- @function strptime
-- @string s
-- @string format same as for `strftime`
-- @usage posix.strptime('20','%d').monthday == 20
-- @treturn[1] PosixTm broken-down local time
-- @treturn[1] int next index of first character not parsed as part of the date
-- @return[2] nil
-- @see strptime(3)
local tm = require "posix.time"
local _strptime = tm.strptime
local function strptime (s, fmt)
return _strptime (s, fmt)
end
if _DEBUG ~= false then
M.strptime = function (...)
local argt = {...}
checkstring ("strptime", 1, argt[1])
checkstring ("strptime", 2, argt[2])
if #argt > 2 then toomanyargerror ("strptime", 2, #argt) end
local tm, i = strptime (...)
return LegacyTm (tm), i
end
else
M.strptime = strptime
end
--- Get configuration information at runtime.
-- @function sysconf
-- @tparam[opt] table|string key one of "ARG_MAX", "CHILD_MAX",
-- "CLK_TCK", "JOB_CONTROL", "NGROUPS_MAX", "OPEN_MAX", "SAVED_IDS",
-- "STREAM_MAX", "TZNAME_MAX" or "VERSION"
-- @string[opt] ... unless *type* was a table, zero or more additional
-- type strings
-- @return ... values, or a table of all fields if no option given
-- @see sysconf(2)
-- @usage for a,b in pairs (P.sysconf ()) do print (a, b) end
-- @usage print (P.sysconf ("STREAM_MAX", "ARG_MAX"))
local unistd = require "posix.unistd"
local _sysconf = unistd.sysconf
local function sysconf (...)
return doselection ("sysconf", 0, {...}, {
ARG_MAX = _sysconf (unistd._SC_ARG_MAX),
CHILD_MAX = _sysconf (unistd._SC_CHILD_MAX),
CLK_TCK = _sysconf (unistd._SC_CLK_TCK),
JOB_CONTROL = _sysconf (unistd._SC_JOB_CONTROL),
NGROUPS_MAX = _sysconf (unistd._SC_NGROUPS_MAX),
OPEN_MAX = _sysconf (unistd._SC_OPEN_MAX),
SAVED_IDS = _sysconf (unistd._SC_SAVED_IDS),
STREAM_MAX = _sysconf (unistd._SC_STREAM_MAX),
TZNAME_MAX = _sysconf (unistd._SC_TZNAME_MAX),
VERSION = _sysconf (unistd._SC_VERSION),
})
end
if _DEBUG ~= false then
M.sysconf = function (...)
checkselection ("sysconf", 1, {...}, 2)
return sysconf (...)
end
else
M.sysconf = sysconf
end
--- Get the current process times.
-- @function times
-- @tparam[opt] table|string key one of "utime", "stime", "cutime",
-- "cstime" or "elapsed"
-- @string[opt] ... unless *key* was a table, zero or more additional
-- key strings.
-- @return values, or a table of all fields if no keys given
-- @see times(2)
-- @usage for a,b in pairs(P.times ()) do print (a, b) end
-- @usage print (P.times ("utime", "elapsed")
local tms = require "posix.sys.times"
local _times = tms.times
local function times (...)
local info = _times ()
return doselection ("times", 0, {...}, {
utime = info.tms_utime,
stime = info.tms_stime,
cutime = info.tms_cutime,
cstime = info.tms_cstime,
elapsed = info.elapsed,
})
end
if _DEBUG ~= false then
M.times = function (...)
checkselection ("times", 1, {...}, 2)
return times (...)
end
else
M.times = times
end
--- Return information about this machine.
-- @function uname
-- @see uname(2)
-- @string[opt="%s %n %r %v %m"] format contains zero or more of:
--
-- * %m machine name
-- * %n node name
-- * %r release
-- * %s sys name
-- * %v version
--
--@treturn[1] string filled *format* string, if successful
--@return[2] nil
--@treturn string error message
local utsname = require "posix.sys.utsname"
local _uname = utsname.uname
local function uname (spec)
local u = _uname ()
return optstring ("uname", 1, spec, "%s %n %r %v %m"):gsub ("%%(.)", function (s)
if s == "%" then return "%"
elseif s == "m" then return u.machine
elseif s == "n" then return u.nodename
elseif s == "r" then return u.release
elseif s == "s" then return u.sysname
elseif s == "v" then return u.version
else
badoption ("uname", 1, "format", s)
end
end)
end
if _DEBUG ~= false then
M.uname = function (s, ...)
local argt = {s, ...}
if #argt > 1 then
toomanyargerror ("uname", 1, #argt)
end
return uname (s)
end
else
M.uname = uname
end
return M
|