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
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use core::{
    mem,
};
use crate::util::{
    saturating::{SaturatingCast},
    kstr::{KStr},
    data::{d8},
};
use crate::kty::{
    AtomicInt,
    c_int, ssize_t, rlimit64, pid_t, uid_t, gid_t, c_char, size_t,
    timespec, dev_t, c_void, clockid_t, itimerspec, epoll_event, sigset_t, new_utsname,
    sysinfo, c_uint, c_ulong, umode_t, k_uint, loff_t, k_ulong, F_DUPFD_CLOEXEC, F_GETFL,
    F_SETFL, F_GETFD, F_SETFD, sockaddr, msghdr, mmsghdr, FUTEX_WAIT, FUTEX_WAKE,
    siginfo_t, rusage, SIOCGSTAMPNS, SIOCINQ, SIOCOUTQ, EPOLL_CLOEXEC, O_CLOEXEC,
    O_LARGEFILE, SOCK_CLOEXEC, MSG_CMSG_CLOEXEC, TFD_CLOEXEC, SFD_CLOEXEC, sigaction,
    F_SETPIPE_SZ, F_GETPIPE_SZ, IN_CLOEXEC, tms, clock_t, MFD_CLOEXEC, F_ADD_SEALS,
    F_GET_SEALS, PAGE_SIZE, TIOCGPTN, TIOCSPTLCK, TIOCGPTLCK, TIOCSIG, TIOCPKT, TIOCGPKT,
    TIOCSTI, winsize, TIOCGWINSZ, TIOCSWINSZ, TIOCSCTTY, TIOCNOTTY, TIOCGEXCL, TIOCNXCL,
    TIOCEXCL, TIOCCONS, TIOCGDEV, TIOCVHANGUP, TIOCSETD, TIOCGETD, TIOCGSID, TIOCSPGRP,
    TIOCGPGRP, TCFLSH, TIOCOUTQ, TCXONC, TCGETS2, termios2, TCSETS2, mq_attr, sched_attr,
    __user_cap_data_struct, __user_cap_header_struct, _LINUX_CAPABILITY_VERSION_3,
    PR_CAPBSET_READ, PR_CAPBSET_DROP, PR_GET_KEEPCAPS, PR_SET_KEEPCAPS,
    PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, PR_CAP_AMBIENT_IS_SET, PR_CAP_AMBIENT_LOWER,
    PR_CAP_AMBIENT_RAISE,
    SECCOMP_SET_MODE_STRICT,
};

pub use self::raw::{
    self as r, StatType, StatfsType
};

pub mod raw;

#[cfg(test)]
mod tests;

// XXX: iovec _MUST_ be the same as &mut [u8]
//      We now have a test for that.
//
// TODO: Audit ioctl

/// Opens a file relative to a file descriptor.
///
/// [argument, dir]
/// The file descriptor relative to which relative paths are interpreted.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, flags]
/// The flags used to open the file.
///
/// [argument, mode]
/// The mode used to create new files.
///
/// [return_value]
/// Rteruns an open file descriptor or an error value.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `O_CLOEXEC` flag. This function automatically adds the `O_LARGEFILE` flag.
///
/// = See also
///
/// * link:man:openat(2)
pub fn openat(dir: c_int, path: KStr, mut flags: c_int, mode: umode_t) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= O_CLOEXEC;
    }
    flags |= O_LARGEFILE;
    unsafe { r::openat(dir, path.as_ptr(), flags, mode) }
}

/// Closes a file descriptor.
///
/// [argument, fd]
/// The file descriptor to close.
///
/// [return_value]
/// Returns a success value or an error value.
///
/// = See also
///
/// * link:man:close(2)
pub fn close(fd: c_int) -> c_int {
    unsafe { r::close(fd as k_uint) }
}

/// Seeks in a file descriptor.
///
/// [argument, fd]
/// The file descriptor in which to seek.
///
/// [argument, offset]
/// The range to seek.
///
/// [argument, whence]
/// How to seek.
///
/// [return_value]
/// Returns the new position in the file descriptor or an error value.
///
/// = See also
///
/// * link:man:lseek(2)
pub fn lseek(fd: c_int, offset: loff_t, whence: c_uint) -> loff_t {
    unsafe { r::lseek(fd as k_uint, offset, whence) }
}

/// Duplicates a file descriptor.
///
/// [argument, fd]
/// The file descriptor to duplicate.
///
/// [argument, arg]
/// The smalest value of the new file descriptor.
///
/// [return_value]
/// Returns the new file descriptor or an error value.
///
/// = See also
///
/// * link:man:fcntl(2) and F_DUPFD_CLOEXEC therein
pub fn fcntl_dupfd_cloexec(fd: c_int, arg: c_int) -> c_int {
    unsafe { r::fcntl(fd as k_uint, F_DUPFD_CLOEXEC, arg as k_ulong) }
}

/// Retrieves the file access mode and file status flags of a file descriptor.
///
/// [argument, fd]
/// The file descriptor to inspect.
///
/// [return_value]
/// Returns the file access mode and file status flags or an error value.
///
/// = See also
///
/// * link:man:fcntl(2) and F_GETFL therein
pub fn fcntl_getfl(fd: c_int) -> c_int {
    unsafe { r::fcntl(fd as k_uint, F_GETFL, 0) }
}

/// Sets the file status flags of a file descriptor.
///
/// [argument, fd]
/// The file descriptor to modify.
///
/// [argument, arg]
/// The new file status flags.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fcntl(2) and F_SETFL therein
pub fn fcntl_setfl(fd: c_int, arg: c_int) -> c_int {
    unsafe { r::fcntl(fd as k_uint, F_SETFL, arg as k_ulong) }
}

/// Retrieves the file descriptor flags of a file descriptor.
///
/// [argument, fd]
/// The file descriptor to inspect.
///
/// [return_value]
/// Returns the file descriptor flags or an error value.
///
/// = See also
///
/// * link:man:fcntl(2) and F_GETFD therein
pub fn fcntl_getfd(fd: c_int) -> c_int {
    unsafe { r::fcntl(fd as k_uint, F_GETFD, 0) }
}

/// Sets the file descriptor flags of a file descriptor.
///
/// [argument, fd]
/// The file descriptor to modify.
///
/// [argument, arg]
/// The new file descriptor flags.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fcntl(2) and F_SETFD therein
pub fn fcntl_setfd(fd: c_int, arg: c_int) -> c_int {
    unsafe { r::fcntl(fd as k_uint, F_SETFD, arg as k_ulong) }
}

/// Truncates a file descriptor to a certain length.
///
/// [argument, fd]
/// The file descriptor to truncate.
///
/// [argument, offset]
/// The new length.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:ftruncate(2)
pub fn ftruncate(fd: c_int, offset: loff_t) -> c_int {
    unsafe { r::ftruncate(fd as k_uint, offset) }
}

/// Returns the process id of this process.
///
/// = See also
///
/// * link:man:getpid(2)
pub fn getpid() -> pid_t {
    unsafe { r::getpid() }
}

/// Returns the process id of the parent of this process.
///
/// = See also
///
/// * link:man:getppid(2)
pub fn getppid() -> pid_t {
    unsafe { r::getppid() }
}

/// Sets the real, effective, and saved user ids of this process.
///
/// [argument, ruid]
/// The real user id.
///
/// [argument, ruid]
/// The effective user id.
///
/// [argument, ruid]
/// The saved user id.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:setresuid(2)
pub fn setresuid(ruid: uid_t, euid: uid_t, suid: uid_t) -> c_int {
    unsafe { r::setresuid(ruid, euid, suid) }
}

/// Sets the real, effective, and saved group ids of this process.
///
/// [argument, ruid]
/// The real user id.
///
/// [argument, ruid]
/// The effective user id.
///
/// [argument, ruid]
/// The saved user id.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:setresgid(2)
pub fn setresgid(rgid: gid_t, egid: gid_t, sgid: gid_t) -> c_int {
    unsafe { r::setresgid(rgid, egid, sgid) }
}

/// Transfers the kernel state of a file descriptor to the disk.
///
/// [argument, fd]
/// The file descriptor to be synchronized.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fsync(2)
pub fn fsync(fd: c_int) -> c_int {
    unsafe { r::fsync(fd as k_uint) }
}

/// Transfers most of the kernel state of a file descriptor to the disk.
///
/// [argument, fd]
/// The file descriptor to be synchronized.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fdatasync(2)
pub fn fdatasync(fd: c_int) -> c_int {
    unsafe { r::fdatasync(fd as k_uint) }
}

/// Transfers the kernel state to disk.
///
/// = See also
///
/// * link:man:sync(2)
pub fn sync() {
    unsafe { r::sync() }
}

/// Transfers the kernel state of a filesystem to disk.
///
/// [argument, fd]
/// An open file descriptor in the filesystem.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:syncfs(2)
pub fn syncfs(fd: c_int) -> c_int {
    unsafe { r::syncfs(fd) }
}

/// Advises the kernel of a certain usage pattern of a file descriptor.
///
/// [argument, fd]
/// The file descriptor.
///
/// [argument, offset]
/// The start of the usage.
///
/// [argument, len]
/// The length of the usage.
///
/// [argument, advice]
/// The advice given to the kernel.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fadvise(2)
pub fn fadvise(fd: c_int, offset: loff_t, len: loff_t, advice: c_int) -> c_int {
    unsafe { r::fadvise(fd, offset, len, advice) }
}

/// Changes the mode of an inode represented by a file descriptor.
///
/// [argument, fd]
/// An open file descriptor referring to an inode.
///
/// [argument, mode]
/// The new mode of the inode.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fchmod(2)
pub fn fchmod(fd: c_int, mode: umode_t) -> c_int {
    unsafe { r::fchmod(fd as k_uint, mode) }
}

/// Allocates memory for a file descriptor.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, mode]
/// The mode of the allocation.
///
/// [argument, base]
/// The base of the allocation.
///
/// [argument, len]
/// The length of the allocation.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fallocate(2)
pub fn fallocate(fd: c_int, mode: c_int, base: loff_t, len: loff_t) -> c_int {
    unsafe { r::fallocate(fd, mode, base, len) }
}

/// Creates a new timerfd.
///
/// [argument, clock]
/// The clock to be used for timekeeping.
///
/// [argument, flags]
/// The flags to be used for creating the file descriptor.
///
/// [return_value]
/// Returns the file descriptor or an error value.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `TFD_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:timerfd_create(2)
pub fn timerfd_create(clock: c_int, mut flags: c_int) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= TFD_CLOEXEC;
    }
    unsafe { r::timerfd_create(clock, flags) }
}

/// Creates a new epoll instance.
///
/// [argument, flags]
/// The flags to be used for creating the file descriptor.
///
/// [return_value]
/// Returns the file descriptor or an error value.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `EPOLL_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:epoll_create1(2)
pub fn epoll_create(mut flags: c_int) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= EPOLL_CLOEXEC;
    }
    unsafe { r::epoll_create1(flags) }
}

/// Applies or removes an advisory lock on a file descriptor.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, op]
/// The operation to be used.
///
/// [return_value]
/// Returns succcess or an error value.
///
/// = See also
///
/// * link:man:flock(2)
pub fn flock(fd: c_int, op: c_int) -> c_int {
    unsafe { r::flock(fd as k_uint, op as k_uint) }
}

/// Initiates readahead for a file descriptor in the kernel.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, offset]
/// The start of the readahead.
///
/// [argument, count]
/// The number of bytes to read.
///
/// [return_value]
/// Returns succcess or an error value.
///
/// = See also
///
/// * link:man:readahead(2)
pub fn readahead(fd: c_int, offset: loff_t, count: size_t) -> ssize_t {
    unsafe { r::readahead(fd, offset, count) }
}

/// Reads from a file descriptor.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, buf]
/// The buffer to read into.
///
/// [return_value]
/// Returns the number of bytes read or an error value.
///
/// = See also
///
/// * link:man:read(2)
pub fn read(fd: c_int, buf: &mut [d8]) -> ssize_t {
    unsafe {
        r::read(fd as k_uint, buf.as_mut_ptr() as *mut _, buf.len().saturating_cast())
    }
}

/// Writes to a file descriptor.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, buf]
/// The buffer to write.
///
/// [return_value]
/// Returns the number of bytes written or an error value.
///
/// = See also
///
/// * link:man:write(2)
pub fn write(fd: c_int, buf: &[d8]) -> ssize_t {
    unsafe {
        r::write(fd as k_uint, buf.as_ptr() as *const _, buf.len().saturating_cast())
    }
}

/// Reads from an offset in a file descriptor.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, buf]
/// The buffer to read into.
///
/// [argument, offset]
/// The offset from which to read.
///
/// [return_value]
/// Returns the number of bytes read or an error value.
///
/// = See also
///
/// * link:man:pread(2)
pub fn pread(fd: c_int, buf: &mut [d8], offset: loff_t) -> ssize_t {
    unsafe {
        r::pread(fd as k_uint, buf.as_mut_ptr() as *mut _, buf.len().saturating_cast(),
                 offset)
    }
}


/// Writes to an offset in a file descriptor.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, buf]
/// The buffer to write.
///
/// [argument, offset]
/// The offset at which to write.
///
/// [return_value]
/// Returns the number of bytes written or an error value.
///
/// = See also
///
/// * link:man:pwrite(2)
pub fn pwrite(fd: c_int, buf: &[d8], offset: loff_t) -> ssize_t {
    unsafe {
        r::pwrite(fd as k_uint, buf.as_ptr() as *const _, buf.len().saturating_cast(),
                  offset)
    }
}

/// Reads from a file descriptor into multiple buffers.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, bufs]
/// The buffers to read into.
///
/// [return_value]
/// Returns the number of bytes read or an error value.
///
/// = See also
///
/// * link:man:readv(2)
pub fn readv(fd: c_int, bufs: &mut [&mut [d8]]) -> ssize_t {
    unsafe {
        r::readv(fd as k_ulong, bufs.as_mut_ptr() as *mut _, bufs.len().saturating_cast())
    }
}

/// Writes to a file descriptor from multiple buffers.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, bufs]
/// The buffers to write.
///
/// [return_value]
/// Returns the number of bytes written or an error value.
///
/// = See also
///
/// * link:man:writev(2)
pub fn writev(fd: c_int, bufs: &[&[d8]]) -> ssize_t {
    unsafe {
        r::writev(fd as k_ulong, bufs.as_ptr() as *const _, bufs.len().saturating_cast())
    }
}

/// Reads from an offset in a file descriptor into multiple buffers.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, bufs]
/// The buffers to read into.
///
/// [argument, offset]
/// The offset from which to read.
///
/// [return_value]
/// Returns the number of bytes read or an error value.
///
/// = See also
///
/// * link:man:preadv(2)
pub fn preadv(fd: c_int, bufs: &mut [&mut [d8]], offset: loff_t) -> ssize_t {
    let lo = ((offset as u64) & 0xFFFF_FFFF) as k_ulong;
    let hi = ((offset as u64) > 32) as k_ulong;
    unsafe {
        r::preadv(fd as k_ulong, bufs.as_mut_ptr() as *mut _, bufs.len().saturating_cast(), lo, hi)
    }
}

/// Writes to an offset in a file descriptor from multiple buffers.
///
/// [argument, fd]
/// The affected file descriptor.
///
/// [argument, bufs]
/// The buffers to write.
///
/// [argument, offset]
/// The offset at which to write.
///
/// [return_value]
/// Returns the number of bytes written or an error value.
///
/// = See also
///
/// * link:man:pwritev(2)
pub fn pwritev(fd: c_int, bufs: &[&[d8]], offset: loff_t) -> ssize_t {
    let lo = ((offset as u64) & 0xFFFF_FFFF) as k_ulong;
    let hi = ((offset as u64) > 32) as k_ulong;
    unsafe {
        r::pwritev(fd as k_ulong, bufs.as_ptr() as *const _, bufs.len().saturating_cast(),
                   lo, hi)
    }
}

/// Retrieves the real, effective, and saved user ids of the process.
///
/// [argument, ruid]
/// The place where the real id will be stored.
///
/// [argument, euid]
/// The place where the effective id will be stored.
///
/// [argument, suid]
/// The place where the saved id will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:getresuid(2)
pub fn getresuid(ruid: &mut uid_t, euid: &mut uid_t, suid: &mut uid_t) -> c_int {
    unsafe { r::getresuid(ruid, euid, suid) }
}

/// Retrieves the real, effective, and saved group ids of the process.
///
/// [argument, ruid]
/// The place where the real id will be stored.
///
/// [argument, euid]
/// The place where the effective id will be stored.
///
/// [argument, suid]
/// The place where the saved id will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:getresgid(2)
pub fn getresgid(rgid: &mut gid_t, egid: &mut gid_t, sgid: &mut gid_t) -> c_int {
    unsafe { r::getresgid(rgid, egid, sgid) }
}

/// Retrieves the supplementary groups of this process.
///
/// [argument, buf]
/// The buffer in which the groups will be stored.
///
/// [return_value]
/// Returns the number of groups stored or an error value.
///
/// = See also
///
/// * link:man:getgroups(2)
pub fn getgroups(buf: &mut [gid_t]) -> c_int {
    unsafe { r::getgroups(buf.len().saturating_cast(), buf.as_mut_ptr()) }
}

/// Sets the supplementary groups of this process.
///
/// [argument, buf]
/// The buffer that contains the groups.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:setgroups(2)
pub fn setgroups(buf: &[gid_t]) -> c_int {
    unsafe { r::setgroups(buf.len().saturating_cast(), buf.as_ptr() as *mut _) }
}

/// Retrieves filesystem statistics from a path.
///
/// [argument, path]
/// A path in a mountpoint of the filesystem.
///
/// [argument, buf]
/// The buffer in which the statistics will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:statfs(2)
pub fn statfs(path: KStr, buf: &mut StatfsType) -> c_int {
    unsafe { r::statfs(path.as_ptr(), buf) }
}

/// Retrieves filesystem statistics from a file descriptor.
///
/// [argument, fd]
/// An open file descriptor in the filesystem.
///
/// [argument, buf]
/// The buffer in which the statistics will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fstatfs(2)
pub fn fstatfs(fd: c_int, buf: &mut StatfsType) -> c_int {
    unsafe { r::fstatfs(fd as k_uint, buf) }
}

/// Retrieves or sets resource limits of a process.
///
/// [argument, pid]
/// The affected process.
///
/// [argument, res]
/// The affected resource.
///
/// [argument, new]
/// The (optional) new value of the resource.
///
/// [argument, old]
/// A place where the previous value of the object will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:prlimit(2)
pub fn prlimit(pid: pid_t, res: c_int, new: Option<&rlimit64>,
               mut old: Option<&mut rlimit64>) -> c_int {
    let new_p = new.map(|v| v as *const _).unwrap_or(0 as *const _);
    let old_p = old.as_mut().map(|v| *v as *mut _).unwrap_or(0 as *mut _);
    unsafe { r::prlimit(pid, res as k_uint, new_p, old_p) }
}

/// Retrieves entries in a opened directory.
///
/// [argument, fd]
/// An open directory file descriptor.
///
/// [argument, buf]
/// The buffer in which the entries will be stored.
///
/// [return_value]
/// Returns the number of bytes read or an error value.
///
/// = See also
///
/// * link:man:getdents(2)
pub fn getdents(fd: c_int, buf: &mut [d8]) -> c_int {
    unsafe {
        r::getdents(fd as k_uint, buf.as_mut_ptr() as *mut _, buf.len().saturating_cast())
    }
}

/// Retrieves information about a file relative to a file descriptor.
///
/// [argument, dir]
/// The directory relative to which relative paths will be interpreted.
///
/// [argument, file]
/// The path of the file.
///
/// [argument, buf]
/// Where the information will be stored.
///
/// [argument, flags]
/// Flags to use while retrieving the information.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fstatat(2)
pub fn fstatat(dir: c_int, file: KStr, buf: &mut StatType, flags: c_int) -> c_int {
    unsafe { r::fstatat(dir, file.as_ptr(), buf, flags) }
}

/// Checks whether a file relative to a file descriptor can be accessed.
///
/// [argument, dir]
/// The directory relative to which relative paths will be interpreted.
///
/// [argument, file]
/// The path of the file.
///
/// [argument, mode]
/// The mode to access the file with.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:faccessat(2)
pub fn faccessat(dir: c_int, file: KStr, mode: umode_t) -> c_int {
    unsafe { r::faccessat(dir, file.as_ptr(), mode as c_int) }
}

/// Truncates a file.
///
/// [argument, file]
/// The path of the file to truncate.
///
/// [argument, len]
/// The new length of the file.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:truncate(2)
pub fn truncate(file: KStr, len: loff_t) -> c_int {
    unsafe { r::truncate(file.as_ptr(), len) }
}

/// Creates a hardlink relative to directories.
///
/// [argument, olddir]
/// The directory relative to which relative oldfile paths will be interpreted.
///
/// [argument, oldfile]
/// The path of the existing file.
///
/// [argument, newdir]
/// The directory relative to which relative newfile paths will be interpreted.
///
/// [argument, newfile]
/// The path of the new link.
///
/// [argument, flags]
/// Flags to use while creating the link.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:linkat(2)
pub fn linkat(olddir: c_int, oldfile: KStr, newdir: c_int, newfile: KStr,
              flags: c_int) -> c_int {
    unsafe { r::linkat(olddir, oldfile.as_ptr(), newdir, newfile.as_ptr(), flags) }
}

/// Changes the access and modification times of a file relative to a directory.
///
/// [argument, dir]
/// The directory relative to which relative paths will be interpreted.
///
/// [argument, file]
/// The path of the file.
///
/// [argument, times]
/// The new times of the file.
///
/// [argument, flags]
/// Flags to use while modifying the file.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:utimensat(2)
pub fn utimensat(dir: c_int, file: Option<KStr>, times: &[timespec; 2],
                 flags: c_int) -> c_int {
    let file = file.map(|f| f.as_ptr()).unwrap_or(0 as *const _);
    unsafe { r::utimensat(dir, file, times.as_ptr(), flags) }
}

/// Renames a file relative to a directory.
///
/// [argument, olddir]
/// The directory relative to which relative oldfile paths will be interpreted.
///
/// [argument, oldfile]
/// The path of the existing file.
///
/// [argument, newdir]
/// The directory relative to which relative newfile paths will be interpreted.
///
/// [argument, newfile]
/// The path of the new file.
///
/// [argument, flags]
/// Flags to use while renaming the file.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:renameat2(2)
pub fn renameat2(olddir: c_int, oldfile: KStr, newdir: c_int, newfile: KStr,
                 flags: c_int) -> c_int {
    unsafe {
        if flags == 0 {
            r::renameat(olddir, oldfile.as_ptr(), newdir, newfile.as_ptr())
        } else {
            r::renameat2(olddir, oldfile.as_ptr(), newdir, newfile.as_ptr(),
                         flags as k_uint)
        }
    }
}

/// Creates a directory relative to a directory.
///
/// [argument, dir]
/// The directory relative to which relative paths will be interpreted.
///
/// [argument, file]
/// The path of the new directory.
///
/// [argument, mode]
/// The mode of the new directory.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:mkdirat(2)
pub fn mkdirat(dir: c_int, file: KStr, mode: umode_t) -> c_int {
    unsafe {  r::mkdirat(dir, file.as_ptr(), mode) }
}

/// Unlinks a file relative to a directory.
///
/// [argument, dir]
/// The directory relative to which relative paths will be interpreted.
///
/// [argument, file]
/// The path of the file.
///
/// [argument, flags]
/// The flags used while unlinking the file.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:unlinkat(2)
pub fn unlinkat(dir: c_int, file: KStr, flags: c_int) -> c_int {
    unsafe { r::unlinkat(dir, file.as_ptr(), flags) }
}

/// Creates a symbolic link relative to a directory.
///
/// [argument, target]
/// The target of the link.
///
/// [argument, dir]
/// The directory relative to which the `link` argument is interpreted.
///
/// [argument, link]
/// The path of the new link.
///
/// [argument, flags]
/// The flags used while unlinking the file.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:symlinkat(2)
pub fn symlinkat(target: KStr, dir: c_int, link: KStr) -> c_int {
    unsafe { r::symlinkat(target.as_ptr(), dir, link.as_ptr()) }
}

/// Reads the target of a symbolic link relative to a directory.
///
/// [argument, dir]
/// The directory relative to which relative paths are interpreted.
///
/// [argument, path]
/// The path of the link.
///
/// [argument, buf]
/// The buffer in which the target will be placed.
///
/// [return_value]
/// Returns the length of the target or an error value.
///
/// = See also
///
/// * link:man:readlinkat(2)
pub fn readlinkat(dir: c_int, path: KStr, buf: &mut [d8]) -> ssize_t {
    unsafe { r::readlinkat(dir, path.as_ptr(), buf.as_mut_ptr() as *mut c_char,
                          buf.len().saturating_cast()) }
}

/// Changes the owner of a file relative to a directory.
///
/// [argument, dir]
/// The directory relative to which relative paths are interpreted.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, user]
/// The new user owner.
///
/// [argument, user]
/// The new user group.
///
/// [argument, flags]
/// Flags to use while changing the owner.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fchownat(2)
pub fn fchownat(dir: c_int, path: KStr, user: uid_t, group: gid_t,
                flags: c_int) -> c_int {
    unsafe { r::fchownat(dir, path.as_ptr(), user, group, flags) }
}

/// Changes the mode of a file relative to a directory.
///
/// [argument, dir]
/// The directory relative to which relative paths are interpreted.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, mode]
/// The new mode of the file.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fchmodat(2)
pub fn fchmodat(dir: c_int, path: KStr, mode: umode_t) -> c_int {
    unsafe { r::fchmodat(dir, path.as_ptr(), mode) }
}

/// Creates a file relative to a directory.
///
/// [argument, dir]
/// The directory relative to which relative paths are interpreted.
///
/// [argument, path]
/// The path of the new file.
///
/// [argument, mode]
/// The mode of a new file.
///
/// [argument, dev]
/// The device type of a new device.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:mknodat(2)
pub fn mknodat(dir: c_int, path: KStr, mode: umode_t, dev: dev_t) -> c_int {
    unsafe { r::mknodat(dir, path.as_ptr(), mode, dev) }
}

/// Sets an extended attribute of a file.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, name]
/// The name of the attribute.
///
/// [argument, val]
/// The value of the attribute.
///
/// [argument, flags]
/// The flags used while setting the attribute.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:setxattr(2)
pub fn setxattr(path: KStr, name: KStr, val: &[d8], flags: c_int) -> c_int {
    unsafe { r::setxattr(path.as_ptr(), name.as_ptr(), val.as_ptr() as *const c_void,
                        val.len().saturating_cast(), flags) }
}

/// Sets an extended attribute of a file without following symlinks.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, name]
/// The name of the attribute.
///
/// [argument, val]
/// The value of the attribute.
///
/// [argument, flags]
/// The flags used while setting the attribute.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:lsetxattr(2)
pub fn lsetxattr(path: KStr, name: KStr, val: &[d8], flags: c_int) -> c_int {
    unsafe { r::lsetxattr(path.as_ptr(), name.as_ptr(), val.as_ptr() as *const c_void,
                         val.len().saturating_cast(), flags) }
}

/// Sets an extended attribute of a file descriptor.
///
/// [argument, fd]
/// The file descriptor.
///
/// [argument, name]
/// The name of the attribute.
///
/// [argument, val]
/// The value of the attribute.
///
/// [argument, flags]
/// The flags used while setting the attribute.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fsetxattr(2)
pub fn fsetxattr(fd: c_int, name: KStr, val: &[d8], flags: c_int) -> c_int {
    unsafe { r::fsetxattr(fd, name.as_ptr(), val.as_ptr() as *const c_void,
                         val.len().saturating_cast(), flags) }
}

/// Retrieves an extended attribute of a file.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, name]
/// The name of the attribute.
///
/// [argument, val]
/// The buffer in which the value will be placed.
///
/// [return_value]
/// Returns the size of the value or an error value.
///
/// = See also
///
/// * link:man:getxattr(2)
pub fn getxattr(path: KStr, name: KStr, val: &mut [d8]) -> ssize_t {
    unsafe { r::getxattr(path.as_ptr(), name.as_ptr(), val.as_mut_ptr() as *mut c_void,
                        val.len().saturating_cast()) }
}

/// Retrieves an extended attribute of a file without following symlinks.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, name]
/// The name of the attribute.
///
/// [argument, val]
/// The buffer in which the value will be placed.
///
/// [return_value]
/// Returns the size of the value or an error value.
///
/// = See also
///
/// * link:man:lgetxattr(2)
pub fn lgetxattr(path: KStr, name: KStr, val: &mut [d8]) -> ssize_t {
    unsafe { r::lgetxattr(path.as_ptr(), name.as_ptr(), val.as_mut_ptr() as *mut c_void,
                         val.len().saturating_cast()) }
}

/// Retrieves an extended attribute of a file descriptor.
///
/// [argument, fd]
/// The file descriptor.
///
/// [argument, name]
/// The name of the attribute.
///
/// [argument, val]
/// The buffer in which the value will be placed.
///
/// [return_value]
/// Returns the size of the value or an error value.
///
/// = See also
///
/// * link:man:fgetxattr(2)
pub fn fgetxattr(fd: c_int, name: KStr, val: &mut [d8]) -> ssize_t {
    unsafe { r::fgetxattr(fd, name.as_ptr(), val.as_mut_ptr() as *mut c_void,
                         val.len().saturating_cast()) }
}

/// Removes an extended attribute of a file.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, name]
/// The name of the attribute.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:removexattr(2)
pub fn removexattr(path: KStr, name: KStr) -> c_int {
    unsafe { r::removexattr(path.as_ptr(), name.as_ptr()) }
}

/// Removes an extended attribute of a file without following symlinks.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, name]
/// The name of the attribute.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:lremovexattr(2)
pub fn lremovexattr(path: KStr, name: KStr) -> c_int {
    unsafe { r::lremovexattr(path.as_ptr(), name.as_ptr()) }
}

/// Removes an extended attribute of a file descriptor.
///
/// [argument, fd]
/// The file descriptor.
///
/// [argument, name]
/// The name of the attribute.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:fremovexattr(2)
pub fn fremovexattr(fd: c_int, name: KStr) -> c_int {
    unsafe { r::fremovexattr(fd, name.as_ptr()) }
}

/// Retrieves all extended attribute of a file.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, list]
/// The buffer in which the attributes will be placed.
///
/// [return_value]
/// Returns the size of all attributes or an error value.
///
/// = See also
///
/// * link:man:listxattr(2)
pub fn listxattr(path: KStr, list: &mut [d8]) -> ssize_t {
    unsafe { r::listxattr(path.as_ptr(), list.as_mut_ptr() as *mut c_char,
                         list.len().saturating_cast()) }
}

/// Retrieves all extended attribute of a file without following symlinks.
///
/// [argument, path]
/// The path of the file.
///
/// [argument, list]
/// The buffer in which the attributes will be placed.
///
/// [return_value]
/// Returns the size of all attributes or an error value.
///
/// = See also
///
/// * link:man:llistxattr(2)
pub fn llistxattr(path: KStr, list: &mut [d8]) -> ssize_t {
    unsafe { r::llistxattr(path.as_ptr(), list.as_mut_ptr() as *mut c_char,
                          list.len().saturating_cast()) }
}

/// Retrieves all extended attribute of a file descriptor.
///
/// [argument, fd]
/// The file descriptor.
///
/// [argument, list]
/// The buffer in which the attributes will be placed.
///
/// [return_value]
/// Returns the size of all attributes or an error value.
///
/// = See also
///
/// * link:man:flistxattr(2)
pub fn flistxattr(fd: c_int, list: &mut [d8]) -> ssize_t {
    unsafe {
        r::flistxattr(fd, list.as_mut_ptr() as *mut c_char, list.len().saturating_cast())
    }
}

/// Retrieves the resolution of a clock.
///
/// [argument, clock]
/// The id of the clock.
///
/// [argument, res]
/// The place in which the resolution will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:clock_getres(2)
pub fn clock_getres(clock: clockid_t, res: &mut timespec) -> c_int {
    unsafe { r::clock_getres(clock, res) }
}

/// Retrieves the time of a clock.
///
/// [argument, clock]
/// The id of the clock.
///
/// [argument, res]
/// The place in which the time will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:clock_gettime(2)
pub fn clock_gettime(clock: clockid_t, res: &mut timespec) -> c_int {
    unsafe { r::clock_gettime(clock, res) }
}

/// Sets the time of a clock.
///
/// [argument, clock]
/// The id of the clock.
///
/// [argument, res]
/// The new time of the clock.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:clock_settime(2)
pub fn clock_settime(clock: clockid_t, res: &timespec) -> c_int {
    unsafe { r::clock_settime(clock, res) }
}

/// Sleeps for a certain amount of time.
///
/// [argument, clock]
/// The clock used for timekeeping.
///
/// [argument, flags]
/// Flags used for sleeping.
///
/// [argument, req]
/// The requested amount of sleep.
///
/// [argument, rem]
/// The place where the remaining amount of time is placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:clock_nanosleep(2)
pub fn clock_nanosleep(clock: clockid_t, flags: c_int, req: &timespec,
                       rem: &mut timespec) -> c_int {
    unsafe { r::clock_nanosleep(clock, flags, req, rem) }
}

/// Arms or disarms a timerfd.
///
/// [argument, fd]
/// The timerfd.
///
/// [argument, flags]
/// Flags used to disarm or arm the timerfd.
///
/// [argument, new]
/// The new settings of the timer.
///
/// [argument, old]
/// An optional place where the old settings will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:timerfd_settime(2)
pub fn timerfd_settime(fd: c_int, flags: c_int, new: &itimerspec,
                       old: Option<&mut itimerspec>) -> c_int {
    let old = match old {
        Some(old) => old as *mut _,
        _ => 0 as *mut _,
    };
    unsafe { r::timerfd_settime(fd, flags, new, old) }
}

/// Retrieves the current settings of a timerfd.
///
/// [argument, fd]
/// The timerfd.
///
/// [argument, cur]
/// A place where the settings of the timerfd will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:timerfd_gettime(2)
pub fn timerfd_gettime(fd: c_int, cur: &mut itimerspec) -> c_int {
    unsafe { r::timerfd_gettime(fd, cur) }
}

/// Modifies a file descriptor in an epoll instance.
///
/// [argument, fd]
/// The epoll instance.
///
/// [argument, op]
/// The operation to perform on the `fd` argument.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, event]
/// An argument used by some operations.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:epoll_ctl(2)
pub fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int,
                 event: Option<&mut epoll_event>) -> c_int {
    let event = match event {
        Some(event) => event as *mut _,
        _ => 0 as *mut _,
    };
    unsafe { r::epoll_ctl(epfd, op, fd, event) }
}

/// Waits on an epoll instance.
///
/// [argument, fd]
/// The epoll instance.
///
/// [argument, events]
/// The buffer into which events will be placed.
///
/// [argument, timeout]
/// The timeout in milliseconds.
///
/// [argument, sigmask]
/// A set of signals that will be masked during the operation.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:epoll_pwait(2)
pub fn epoll_pwait(epfd: c_int, events: &mut [epoll_event], timeout: c_int,
                   sigmask: Option<&sigset_t>) -> c_int {
    let sigmask = match sigmask {
        Some(sigmask) => sigmask as *const _,
        _ => 0 as *const _,
    };
    unsafe { r::epoll_pwait(epfd, events.as_mut_ptr(), events.len().saturating_cast(),
                           timeout, sigmask, mem::size_of::<sigset_t>() as size_t) }
}

/// Retrieves a thread's CPU affinity mask.
///
/// [argument, tid]
/// The id of the thread.
///
/// [argument, set]
/// The buffer into which the mask will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = Remarks
///
/// The set size must be a multiple of the size of `k_long`.
///
/// = See also
///
/// * link:man:sched_getaffinity(2)
pub fn sched_getaffinity(tid: pid_t, set: &mut [d8]) -> c_int {
    unsafe {
        r::sched_getaffinity(tid, set.len().saturating_cast(), set.as_mut_ptr() as *mut _)
    }
}

/// Sets a thread's CPU affinity mask.
///
/// [argument, tid]
/// The id of the thread.
///
/// [argument, set]
/// The cpu affinity of the thread.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:sched_setaffinity(2)
pub fn sched_setaffinity(tid: pid_t, set: &[d8]) -> c_int {
    unsafe {
        r::sched_setaffinity(tid, set.len().saturating_cast(), set.as_ptr() as *mut _)
    }
}

/// Retrieves string-style information about the system.
///
/// [argument, buf]
/// The place in which the information will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:uname(2)
pub fn uname(buf: &mut new_utsname) -> c_int {
    unsafe { r::uname(buf) }
}

/// Retrieves integer-style information about the system.
///
/// [argument, buf]
/// The place in which the information will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:sysinfo(2)
pub fn sysinfo(buf: &mut sysinfo) -> c_int {
    unsafe { r::sysinfo(buf) }
}

/// Retrieves random bytes from the system.
///
/// [argument, buf]
/// The buffer in which the bytes will be stored.
///
/// [argument, flags]
/// Flags used while retrieving the data.
///
/// [return_value]
/// Returns the number of bytes read or an error value.
///
/// = See also
///
/// * link:man:getrandom(2)
pub fn getrandom(buf: &mut [d8], flags: c_uint) -> c_int {
    unsafe { r::getrandom(buf.as_ptr() as *mut c_char, buf.len() as size_t, flags) }
}

/// Enables or disables process accounting.
///
/// [argument, path]
/// The path into which accounting information will be written.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:acct(2)
pub fn acct(path: Option<KStr>) -> c_int {
    let ptr = path.map(|p| p.as_ptr()).unwrap_or(0 as *const _);
    unsafe { r::acct(ptr) }
}

/// Mounts a filesystem.
///
/// [argument, src]
/// The filesystem to mount.
///
/// [argument, dst]
/// Where to mount it.
///
/// [argument, ty]
/// The type of the filesystem.
///
/// [argument, flags]
/// Flags used when mounting the filesystem.
///
/// [argument, data]
/// Filesystem dependent data.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:mount(2)
pub fn mount(src: KStr, dst: KStr, ty: KStr, flags: c_ulong, data: KStr) -> c_int {
    unsafe {
        r::mount(src.as_ptr() as *mut _, dst.as_ptr() as *mut _, ty.as_ptr() as *mut _,
                 flags, data.as_ptr() as *mut _)
    }
}

/// Unmounts a filesystem.
///
/// [argument, dst]
/// Where the filesystem is mounted.
///
/// [argument, flags]
/// Flags used when unmounting the filesystem.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:mount(2)
pub fn umount(dst: KStr, flags: c_int) -> c_int {
    unsafe { r::umount(dst.as_ptr() as *mut _, flags) }
}

/// Sets the hostname of the system.
///
/// [argument, name]
/// The new hostname of the system.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:sethostname(2)
pub fn sethostname(name: &[d8]) -> c_int {
    unsafe { r::sethostname(name.as_ptr() as *mut c_char, name.len().saturating_cast()) }
}

/// Sets the domain name of the system.
///
/// [argument, name]
/// The new domain of the system.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:setdomainname(2)
pub fn setdomainname(name: &[d8]) -> c_int {
    unsafe { r::setdomainname(name.as_ptr() as *mut c_char, name.len().saturating_cast()) }
}

/// Creates a socket.
///
/// [argument, domain]
/// The domain of the socket.
///
/// [argument, ty]
/// The type of the socket.
///
/// [argument, proto]
/// The protocol of the socket.
///
/// [return_value]
/// Returns the socket or an error value.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `SOCK_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:socket(2)
pub fn socket(domain: c_int, mut ty: c_int, proto: c_int) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        ty |= SOCK_CLOEXEC;
    }
    unsafe { r::socket(domain, ty, proto) }
}

/// Connects a socket to an address.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, addr]
/// The address to connect to.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:connect(2)
pub fn connect(sockfd: c_int, addr: &[d8]) -> c_int {
    unsafe {
        r::connect(sockfd, addr.as_ptr() as *mut sockaddr, addr.len().saturating_cast())
    }
}

/// Accepts a connection request on a socket.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, addr]
/// Optional space in which the address of the peer will be stored.
///
/// [argument, addrlen]
/// The length of the peer's address.
///
/// [argument, flags]
/// Flags used for accepting a request.
///
/// [return_value]
/// Returns the new socket.
///
/// = See also
///
/// * link:man:accept4(2)
pub fn accept4(sockfd: c_int, addr: Option<&mut [d8]>, addrlen: &mut usize,
               flags: c_int) -> c_int {
    let addr = addr.unwrap_or(&mut []);
    let mut len = addr.len().saturating_cast();
    let res = unsafe {
        r::accept4(sockfd, addr.as_mut_ptr() as *mut sockaddr, &mut len, flags)
    };
    *addrlen = len as usize;
    res
}

/// Receives data on a socket.
///
/// [argument, sockfd]
/// The socket on which to receive.
///
/// [argument, buf]
/// The buffer into which the received data is placed.
///
/// [argument, flags]
/// Flags used while receiving.
///
/// [argument, src_addr]
/// An optional place where the address of the sender is placed.
///
/// [argument, addrlen]
/// A place where the length of the senders address is placed.
///
/// [return_value]
/// Returns the number of bytes received or an error value.
///
/// = See also
///
/// * link:man:recvfrom(2)
pub fn recvfrom(sockfd: c_int, buf: &mut [d8], flags: c_int, src_addr: Option<&mut [d8]>,
                addrlen: &mut usize) -> ssize_t {
    let src_addr = src_addr.unwrap_or(&mut []);
    let mut len = src_addr.len().saturating_cast();
    let res = unsafe {
        r::recvfrom(sockfd, buf.as_mut_ptr() as *mut c_void, buf.len().saturating_cast(),
                    flags as k_uint, src_addr.as_mut_ptr() as *mut sockaddr, &mut len)
    };
    *addrlen = len as usize;
    res
}

/// Receives a message on a socket.
///
/// [argument, sockfd]
/// The socket on which to receive.
///
/// [argument, msghdr]
/// The message buffer.
///
/// [argument, flags]
/// Flags used while receiving.
///
/// [return_value]
/// Returns the number of bytes received or an error value.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `MSG_CMSG_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:recvmsg(2)
pub fn recvmsg(sockfd: c_int, msg: &mut msghdr, mut flags: c_int) -> ssize_t {
    if cfg!(not(no_auto_cloexec)) {
        flags |= MSG_CMSG_CLOEXEC;
    }
    unsafe { r::recvmsg(sockfd, msg, flags as k_uint) }
}

/// Receives multiple messages on a socket.
///
/// [argument, sockfd]
/// The socket on which to receive.
///
/// [argument, msgvec]
/// A vector of message buffers.
///
/// [argument, flags]
/// Flags used while receiving.
///
/// [argument, timeout]
/// A timeout for the operation.
///
/// [return_value]
/// Returns the number of messages received or an error value.
///
/// = See also
///
/// * link:man:recvmmsg(2)
pub fn recvmmsg(sockfd: c_int, msgvec: &mut [mmsghdr], flags: c_uint,
                timeout: Option<&mut timespec>) -> c_int {
    let timeout = timeout.map(|t| t as *mut timespec).unwrap_or(0 as *mut timespec);
    unsafe {
        r::recvmmsg(sockfd, msgvec.as_mut_ptr(), msgvec.len().saturating_cast(), flags,
                    timeout) as c_int
    }
}

/// Sends data to an address.
///
/// [argument, sockfd]
/// The socket over which to send.
///
/// [argument, buf]
/// The buffer to send.
///
/// [argument, flags]
/// Flags used while sending.
///
/// [argument, dst_addr]
/// An optional destination of the message.
///
/// [return_value]
/// Returns the number of bytes sent or an error value.
///
/// = See also
///
/// * link:man:sendto(2)
pub fn sendto(sockfd: c_int, buf: &[d8], flags: c_int,
              dst_addr: Option<&[d8]>) -> ssize_t {
    let (dst_ptr, dst_len) = match dst_addr {
        Some(addr) => (addr.as_ptr(), addr.len()),
        _ => (0 as *const d8, 0),
    };
    unsafe {
        r::sendto(sockfd, buf.as_ptr() as *mut c_void, buf.len().saturating_cast(),
                  flags as k_uint, dst_ptr as *mut sockaddr, dst_len.saturating_cast())
    }
}

/// Sends a message on a socket.
///
/// [argument, sockfd]
/// The socket over which to send.
///
/// [argument, msghdr]
/// The message buffer.
///
/// [argument, flags]
/// Flags used while sending.
///
/// [return_value]
/// Returns the number of bytes sent or an error value.
///
/// = See also
///
/// * link:man:sendmsg(2)
pub fn sendmsg(sockfd: c_int, msg: &msghdr, flags: c_int) -> ssize_t {
    unsafe { r::sendmsg(sockfd, msg as *const _ as *mut _, flags as k_uint) }
}

/// Sends multiple messages on a socket.
///
/// [argument, sockfd]
/// The socket over which to send.
///
/// [argument, msgvec]
/// A vector of message buffers.
///
/// [argument, flags]
/// Flags used while sending.
///
/// [return_value]
/// Returns the number of messages sent or an error value.
///
/// = See also
///
/// * link:man:sendmmsg(2)
pub fn sendmmsg(sockfd: c_int, msgvec: &[mmsghdr], flags: c_uint) -> c_int {
    unsafe {
        r::sendmmsg(sockfd, msgvec.as_ptr() as *mut mmsghdr,
                    msgvec.len().saturating_cast(), flags) as c_int
    }
}

/// Shuts down (part of) a socket.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, how]
/// Which parts to shut down.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:shutdown(2)
pub fn shutdown(sockfd: c_int, how: c_int) -> c_int {
    unsafe { r::shutdown(sockfd, how) }
}

/// Binds a socket to an address.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, addr]
/// The address to bind to.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:bind(2)
pub fn bind(sockfd: c_int, addr: &[d8]) -> c_int {
    unsafe {
        r::bind(sockfd, addr.as_ptr() as *mut sockaddr, addr.len().saturating_cast())
    }
}

/// Marks a socket as accepting connections.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, backlog]
/// The maximum number of pending connections.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:listen(2)
pub fn listen(sockfd: c_int, backlog: u32) -> c_int {
    unsafe { r::listen(sockfd, backlog.saturating_cast()) }
}

/// Retrieves the address a socket is bound to.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, addr]
/// The buffer into which the address is placed.
///
/// [argument, addrlen]
/// A place into which the length of the address is placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:getsockname(2)
pub fn getsockname(sockfd: c_int, addr: &mut [d8], addrlen: &mut usize) -> c_int {
    let mut len = addr.len().saturating_cast();
    let res = unsafe {
        r::getsockname(sockfd, addr.as_mut_ptr() as *mut sockaddr, &mut len)
    };
    *addrlen = len as usize;
    res
}

/// Retrieves the address a socket is connected to.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, addr]
/// The buffer into which the address is placed.
///
/// [argument, addrlen]
/// A place into which the length of the address is placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:getpeername(2)
pub fn getpeername(sockfd: c_int, addr: &mut [d8], addrlen: &mut usize) -> c_int {
    let mut len = addr.len().saturating_cast();
    let res = unsafe {
        r::getpeername(sockfd, addr.as_mut_ptr() as *mut sockaddr, &mut len)
    };
    *addrlen = len as usize;
    res
}

/// Creates a pair of connected sockets.
///
/// [argument, domain]
/// The domain of the sockets.
///
/// [argument, ty]
/// The type of the sockets.
///
/// [argument, proto]
/// The protocol of the sockets.
///
/// [argument, sv]
/// The place where the sockets will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:socketpair(2)
pub fn socketpair(domain: c_int, ty: c_int, proto: c_int, sv: &mut [c_int; 2]) -> c_int {
    unsafe { r::socketpair(domain, ty, proto, sv.as_mut_ptr()) }
}

/// Sets a socket option.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, level]
/// The level of the option.
///
/// [argument, optname]
/// The name of the option.
///
/// [argument, optval]
/// The value to set.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:setsockopt(2)
pub fn setsockopt(sockfd: c_int, level: c_int, optname: c_int, optval: &[d8]) -> c_int {
    unsafe {
        r::setsockopt(sockfd, level, optname, optval.as_ptr() as *mut c_char,
                      optval.len().saturating_cast())
    }
}

/// Retrieves a socket option.
///
/// [argument, sockfd]
/// The socket.
///
/// [argument, level]
/// The level of the option.
///
/// [argument, optname]
/// The name of the option.
///
/// [argument, optval]
/// The buffer in which the value will be stored.
///
/// [argument, optlen]
/// A place into which the length of the value will be stored.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:getsockopt(2)
pub fn getsockopt(sockfd: c_int, level: c_int, optname: c_int, optval: &mut [d8],
                  optlen: &mut usize) -> c_int {
    let mut len = optval.len().saturating_cast();
    let res = unsafe {
        r::getsockopt(sockfd, level, optname, optval.as_mut_ptr() as *mut c_char,
                      &mut len)
    };
    *optlen = len as usize;
    res
}

/// Waits on a futex.
///
/// [argument, addr]
/// The address of the futex.
///
/// [argument, val]
/// The expected value of the futex.
///
/// [argument, timeout]
/// A timeout of the wait operation.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:futex(2) and FUTEX_WAIT therein
pub fn futex_wait(addr: &AtomicInt, val: c_int, timeout: Option<&timespec>) -> c_int {
    let timeout = timeout.map(|t| t as *const _ as *mut _).unwrap_or(0 as *mut _);
    unsafe {
        r::futex(addr as *const _ as *mut _, FUTEX_WAIT, val as c_uint, timeout, 0 as *mut _,
                 0)
    }
}

/// Wakes processes sleeping on a futex.
///
/// [argument, addr]
/// The address of the futex.
///
/// [argument, num]
/// The number of processes to wake.
///
/// [return_value]
/// Returns the number of processes woken or an error value.
///
/// = See also
///
/// * link:man:futex(2) and FUTEX_WAKE therein
pub fn futex_wake(addr: &AtomicInt, num: usize) -> c_int {
    let num: c_int = num.saturating_cast();
    unsafe {
        r::futex(addr as *const _ as *mut _, FUTEX_WAKE, num as c_uint, 0 as *mut _,
                 0 as *mut _, 0)
    }
}

/// Terminates the thread.
///
/// [argument, val]
/// The exit value of the thread.
///
/// = See also
///
/// * link:man:exit(2)
pub fn exit(val: c_int) -> ! {
    unsafe { r::exit(val); }
    loop { }
}

/// Terminates the process.
///
/// [argument, val]
/// The exit value of the process.
///
/// = See also
///
/// * link:man:exit_group(2)
pub fn exit_group(val: c_int) -> ! {
    unsafe { r::exit_group(val); }
    loop { }
}

/// Executes a file relative to a directory.
///
/// [argument, fd]
/// The directory relative to which relative paths will be interpreted.
///
/// [argument, filename]
/// The file to execute.
///
/// [argument, argv]
/// The argument pointer.
///
/// [argument, envp]
/// The environment pointer.
///
/// [argument, flags]
/// Flags used when executing a process.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:execveat(2)
pub fn execveat(fd: c_int, filename: KStr, argv: *const *const c_char,
                envp: *const *const c_char, flags: c_int) -> c_int {
    unsafe { r::execveat(fd, filename.as_ptr(), argv, envp, flags) }
}

/// Maps a file into memory.
///
/// [argument, addr]
/// The address at which to map the file.
///
/// [argument, len]
/// The length of the map.
///
/// [argument, prot]
/// How the memory will be protected.
///
/// [argument, flags]
/// Flags used when mapping a file.
///
/// [argument, fd]
/// The file to map.
///
/// [argument, off]
/// The offset of the file at which the map is started.
///
/// [return_value]
/// Returns a pointer to the map or an error value.
///
/// = See also
///
/// * link:man:mmap(2)
pub unsafe fn mmap(addr: usize, len: usize, prot: c_int, flags: c_int, fd: c_int,
                   off: u64) -> isize {
    r::mmap(addr as k_ulong, len as k_ulong, prot as k_ulong, flags as k_ulong,
            fd as k_ulong, off) as isize
}

/// Unmaps a file.
///
/// [argument, addr]
/// The address of the map.
///
/// [argument, len]
/// The length of the map.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:munmap(2)
pub unsafe fn munmap(addr: usize, len: usize) -> c_int {
    r::munmap(addr as k_ulong, len as size_t)
}

/// Remaps a file in memory.
///
/// [argument, addr]
/// The address of the map.
///
/// [argument, old_len]
/// The current length of the map.
///
/// [argument, new_len]
/// The new length of the map.
///
/// [argument, flags]
/// Flags used to remap the memory.
///
/// [argument, new_addr]
/// The new address at which the memory will be mapped.
///
/// [return_value]
/// Returns a pointer to the new map or an error value.
///
/// = See also
///
/// * link:man:mremap(2)
pub unsafe fn mremap(addr: usize, old_len: usize, new_len: usize, flags: c_int,
              new_addr: usize) -> isize {
    r::mremap(addr as k_ulong, old_len as k_ulong, new_len as k_ulong,
              flags as k_ulong, new_addr as k_ulong) as isize
}

/// Waits for a child process.
///
/// [argument, which]
/// The type of process to wait for.
///
/// [argument, upid]
/// The id to wait for.
///
/// [argument, infop]
/// A place into which the process information will be placed.
///
/// [argument, options]
/// What changes to wait for.
///
/// [argument, ru]
/// An optional place where resource usage of the process will be placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:waitid(2)
pub fn waitid(which: c_int, upid: pid_t, infop: &mut siginfo_t, options: c_int,
              ru: Option<&mut rusage>) -> c_int {
    let ru = ru.map(|r| r as *mut _).unwrap_or(0 as *mut _);
    unsafe { r::waitid(which, upid, infop, options, ru) }
}

/// Retrieves the current working directory.
///
/// [argument, buf]
/// The buffer into which the directory will be placed.
///
/// [return_value]
/// Returns the length of the current working directory or an error value.
///
/// = See also
///
/// * link:man:getcwd(2)
pub fn getcwd(buf: &mut [d8]) -> c_int {
    unsafe { r::getcwd(buf.as_mut_ptr() as *mut c_char, buf.len() as k_ulong) }
}

/// Changes the current working directory.
///
/// [argument, path]
/// The new working directory.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:chdir(2)
pub fn chdir(path: KStr) -> c_int {
    unsafe { r::chdir(path.as_ptr()) }
}

/// Executes ioctl with the SIOCGSTAMPNS option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, time]
/// A place into which the retrieved time will be placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:ioctl(2)
/// * link:man:socket(7) and SIOCGSTAMP therein
pub fn ioctl_siocgstampns(fd: c_int, time: &mut timespec) -> c_int {
    unsafe { r::ioctl(fd as k_uint, SIOCGSTAMPNS as k_uint, time as *mut _ as k_ulong) }
}

/// Executes ioctl with the SIOCINQ option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, unread]
/// A place into which number of unread bytes will be placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:ioctl(2)
/// * link:man:tcp(7) and SIOCINQ therein
pub fn ioctl_siocinq(fd: c_int, unread: &mut usize) -> c_int {
    let mut u: c_int = 0;
    let rv = unsafe {
        r::ioctl(fd as k_uint, SIOCINQ as k_uint, &mut u as *mut _ as k_ulong)
    };
    *unread = u as usize;
    rv
}

/// Executes ioctl with the SIOCOUTQ option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, unread]
/// A place into which number of unread bytes will be placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:ioctl(2)
/// * link:man:tcp(7) and SIOCOUTQ therein
pub fn ioctl_siocoutq(fd: c_int, unread: &mut usize) -> c_int {
    let mut u: c_int = 0;
    let rv = unsafe {
        r::ioctl(fd as k_uint, SIOCOUTQ as k_uint, &mut u as *mut _ as k_ulong)
    };
    *unread = u as usize;
    rv
}

/// Modifies or inspects the process signal mask.
///
/// [argument, how]
/// How the mask will be modified.
///
/// [argument, set]
/// The argument for modification.
///
/// [argument, old]
/// Optional place in which the old set will be stored.
///
/// = See also
///
/// * link:man:rt_sigprocmask(2)
pub fn rt_sigprocmask(how: c_int, set: Option<&sigset_t>,
                      old: Option<&mut sigset_t>) -> c_int {
    let set = set.map(|v| v as *const _ as *mut _).unwrap_or(0 as *mut _);
    let old = old.map(|v| v as *mut _).unwrap_or(0 as *mut _);
    unsafe { r::rt_sigprocmask(how, set, old, mem::size_of::<sigset_t>() as size_t) }
}

/// Examines the pending signals.
///
/// [argument, set]
/// The place in which the pending signals will be set.
///
/// = See also
///
/// * link:man:rt_sigpending(2)
pub fn rt_sigpending(set: &mut sigset_t) -> c_int {
    unsafe { r::rt_sigpending(set, mem::size_of::<sigset_t>() as size_t) }
}

/// Temporarily replace the signal mask and waits for a signal to arrive.
///
/// [argument, set]
/// The temporary signal mask.
///
/// = See also
///
/// * link:man:rt_sigsuspend(2)
pub fn rt_sigsuspend(set: &sigset_t) -> c_int {
    let set = set as *const _ as *mut _;
    unsafe { r::rt_sigsuspend(set, mem::size_of::<sigset_t>() as size_t) }
}

/// Creates or modified a signalfd.
///
/// [argument, fd]
/// The file descriptor to modify.
///
/// [argument, set]
/// The set of signals to monitor.
///
/// [argument, flags]
/// The flags used to create the fd.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `SFD_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:signalfd4(2)
pub fn signalfd4(fd: c_int, set: &sigset_t, mut flags: c_int) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= SFD_CLOEXEC;
    }
    unsafe { r::signalfd4(fd, set, mem::size_of::<sigset_t>() as size_t, flags) }
}

/// Suspends the thread until a certain signal occurs.
///
/// [argument, set]
/// The set of signals to wait for.
///
/// [argument, info]
/// Place where information about the signal will be stored.
///
/// [argument, timeout]
/// Optional timeout.
///
/// = See also
///
/// * link:man:rt_sigtimedwait(2)
pub fn rt_sigtimedwait(set: &sigset_t, info: &mut siginfo_t,
                       timeout: Option<&timespec>) -> c_int {
    let timeout = timeout.map(|t| t as *const _).unwrap_or(0 as *const _);
    unsafe {
        r::rt_sigtimedwait(set, info, timeout, mem::size_of::<sigset_t>() as size_t)
    }
}

/// Changes or inspects the handler of a signal.
///
/// [argument, signum]
/// The signal to modify.
///
/// [argument, act]
/// The new handler.
///
/// [argument, old]
/// The old handler.
///
/// = See also
///
/// * link:man:rt_sigaction(2)
pub fn rt_sigaction(signum: c_int, act: Option<&sigaction>,
                    old: Option<&mut sigaction>) -> c_int {
    let act = act.map(|a| a as *const _).unwrap_or(0 as *const _);
    let old = old.map(|a| a as *mut _).unwrap_or(0 as *mut _);
    unsafe { r::rt_sigaction(signum, act, old, mem::size_of::<sigset_t>() as size_t) }
}

// pub fn rt_sigreturn() {
//     unsafe { r::rt_sigreturn() }
// }

/// Creates a new pipe.
///
/// [argument, fds]
/// The buffer in which the ends of the pipe will be placed.
///
/// [argument, flags]
/// Flags for creating the pipe.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `O_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:pipe2(2)
pub fn pipe2(fds: &mut [c_int; 2], mut flags: c_int) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= O_CLOEXEC;
    }
    unsafe { r::pipe2(fds.as_mut_ptr(), flags) }
}

/// Sets the capacity of a pipe.
///
/// [argument, fd]
/// A pipe file descriptor.
///
/// [argument, size]
/// The new capacity of the pipe.
///
/// = See also
///
/// * link:man:fcntl(2) and F_SETPIPE_SZ therein
pub fn fcntl_setpipe_sz(fd: c_int, size: c_int) -> c_int {
    unsafe { r::fcntl(fd as c_uint, F_SETPIPE_SZ, size as k_ulong) }
}

/// Gets the capacity of a pipe.
///
/// [argument, fd]
/// A pipe file descriptor.
///
/// [return_value]
/// Returns the capacity of the pipe.
///
/// = See also
///
/// * link:man:fcntl(2) and F_GETPIPE_SZ therein
pub fn fcntl_getpipe_sz(fd: c_int) -> c_int {
    unsafe { r::fcntl(fd as c_uint, F_GETPIPE_SZ, 0) }
}

/// Executes ioctl with the FIONREAD option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, unread]
/// A place into which number of unread bytes will be placed.
///
/// [return_value]
/// Returns success or an error value.
///
/// = See also
///
/// * link:man:ioctl(2)
pub fn ioctl_fionread(fd: c_int, unread: &mut usize) -> c_int {
    ioctl_siocinq(fd, unread)
}

/// Copies data from one pipe to another.
///
/// [argument, fd_in]
/// The pipe to copy from.
///
/// [argument, fd_out]
/// The pipe to copy to.
///
/// [argument, len]
/// The number of bytes to copy.
///
/// [argument, flags]
/// Flags to use while copying.
///
/// [return_value]
/// Returns the number of bytes copied.
///
/// = See also
///
/// * link:man:tee(2)
pub fn tee(fd_in: c_int, fd_out: c_int, len: usize, flags: c_uint) -> ssize_t {
    unsafe { r::tee(fd_in, fd_out, len as size_t, flags) }
}

/// Copies data between two file descriptors.
///
/// [argument, fd_in]
/// The file to copy from.
///
/// [argument, off_in]
/// The position to copy from.
///
/// [argument, fd_out]
/// The file to copy to.
///
/// [argument, off_out]
/// The position to copy to.
///
/// [argument, len]
/// The number of bytes to copy.
///
/// [argument, flags]
/// Flags to use while copying.
///
/// [return_value]
/// Returns the number of bytes copied.
///
/// = See also
///
/// * link:man:splice(2)
pub fn splice(fd_in: c_int, mut off_in: Option<&mut u64>, fd_out: c_int,
              mut off_out: Option<&mut u64>, len: usize, flags: c_uint) -> ssize_t {
    let mut loff_in = 0;
    let mut loff_out = 0;
    let mut poff_in = 0 as *mut loff_t;
    let mut poff_out = 0 as *mut loff_t;
    if let Some(ref mut p) = off_in {
        loff_in = **p as loff_t;
        poff_in = &mut loff_in;
    }
    if let Some(ref mut p) = off_out {
        loff_out = **p as loff_t;
        poff_out = &mut loff_out;
    }
    let rv = unsafe { r::splice(fd_in, poff_in, fd_out, poff_out, len as size_t, flags) };
    if let Some(p) = off_in {
        *p = loff_in as u64;
    }
    if let Some(p) = off_out {
        *p = loff_out as u64;
    }
    rv
}

/// Creates a new inotify object.
///
/// [argument, flags]
/// Flags to use when creating the object.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `IN_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:inotify_init1(2)
pub fn inotify_init1(mut flags: c_int) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= IN_CLOEXEC;
    }
    unsafe { r::inotify_init1(flags) }
}

/// Adds or modifies an inotify watch.
///
/// [argument, fd]
/// The fd of the inotify object.
///
/// [argument, path]
/// The path to watch.
///
/// [argument, mask]
/// The events to watch.
///
/// = See also
///
/// * link:man:inotify_add_watch(2)
pub fn inotify_add_watch(fd: c_int, path: KStr, mask: u32) -> c_int {
    unsafe { r::inotify_add_watch(fd, path.as_ptr(), mask) }
}

/// Removes an inotify watch.
///
/// [argument, fd]
/// The fd of the inotify object.
///
/// [argument, wd]
/// The watch descriptor to remove.
///
/// = See also
///
/// * link:man:inotify_rm_watch(2)
pub fn inotify_rm_watch(fd: c_int, wd: c_int) -> c_int {
    unsafe { r::inotify_rm_watch(fd, wd) }
}

/// Duplicates a file descriptor by replacing another one.
///
/// [argument, oldfd]
/// The file descriptor to duplicate.
///
/// [argument, newfd]
/// The file descriptor to replace.
///
/// [argument, flags]
/// Flags to use when creating the new file descriptor.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `O_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:dup3(2)
pub fn dup3(oldfd: c_int, newfd: c_int, mut flags: c_int) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= O_CLOEXEC;
    }
    unsafe { r::dup3(oldfd as c_uint, newfd as c_uint, flags) }
}

/// Sets the file mode creating mask of the process.
///
/// [argument, mode]
/// The mode to be masked.
///
/// [return_value]
/// Returns the previous mask.
///
/// = See also
///
/// * link:man:umask(2)
pub fn umask(mode: umode_t) -> umode_t {
    unsafe { r::umask(mode as c_int) }
}

/// Creates a new eventfd.
///
/// [argument, init]
/// The initial value of the eventfd.
///
/// [argument, flags]
/// Flags to use when creating the new file descriptor.
///
/// = See also
///
/// * link:man:eventfd2(2)
pub fn eventfd2(init: c_uint, flags: c_int) -> c_int {
    unsafe { r::eventfd2(init, flags) }
}

/// Retrieves the CPU times used by this process and its children.
///
/// [argument, buf]
/// Place where the times will be stored.
///
/// = See also
///
/// * link:man:times(2)
pub fn times(buf: *mut tms) -> clock_t {
    unsafe { r::times(buf) }
}

// /// Suspends the thread until a signal handler is invoked.
// ///
// /// = See also
// ///
// /// * link:man:pause(2)
// pub fn pause() -> c_int {
//     unsafe { r::pause() }
// }

/// Performs reboot-related operations.
///
/// [argument, cmd]
/// The command to be executed.
///
/// [argument, arg]
/// An optional argument.
///
/// = Remarks
///
/// The argument is actually a void* but currently only used as a string in one case.
///
/// = See also
///
/// * link:man:reboot(2)
pub fn reboot(cmd: c_uint, arg: KStr) -> c_int {
    #![allow(overflowing_literals)]
    // Trivia: The following magic numbers can be used in the second field:
    //
    //  0x28121969
    //  0x05121996
    //  0x16041998
    //  0x20112000
    //
    // The are the birthdays of Linus Torvalds and his children.
    unsafe { r::reboot(0xfee1dead, 0x28121969, cmd, arg.as_ptr() as *mut c_void) }
}

/// Creates a memfd.
///
/// [argument, name]
/// The name of the pseudo-file.
///
/// [argument, flags]
/// Flags to use for the new file.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `MFD_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:memfd_create(2)
pub fn memfd_create(name: KStr, mut flags: c_uint) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= MFD_CLOEXEC;
    }
    unsafe { r::memfd_create(name.as_ptr(), flags) }
}

/// Adds file seals to an inode.
///
/// [argument, fd]
/// A file descriptor refering to the inode.
///
/// [argument, seals]
/// The seals to add.
///
/// = Remarks
///
/// == Kernel versions
///
/// The required kernel version is 3.17.
///
/// = See also
///
/// * link:man:fcntl(2) and F_ADD_SEALS therein
pub fn fcntl_add_seals(fd: c_int, seals: c_uint) -> c_int {
    unsafe { r::fcntl(fd as c_uint, F_ADD_SEALS, seals as k_ulong) }
}

/// Returns the seals of an inode.
///
/// [argument, fd]
/// A file descriptor refering to the inode.
///
/// = Remarks
///
/// == Kernel versions
///
/// The required kernel version is 3.17.
///
/// = See also
///
/// * link:man:fcntl(2) and F_GET_SEALS therein
pub fn fcntl_get_seals(fd: c_int) -> c_int {
    unsafe { r::fcntl(fd as c_uint, F_GET_SEALS, 0) }
}

/// Synchronizes a memory mapping with the filesystem.
///
/// [argument, addr]
/// The start of the range to be synchronized.
///
/// [argument, len]
/// The length of the range to be synchronized.
///
/// [argument, flags]
/// Flags to used for synchronization.
///
/// = See also
///
/// * link:man:msync(2)
pub fn msync(addr: usize, len: usize, flags: c_int) -> c_int {
    unsafe { r::msync(addr as k_ulong, len as size_t, flags) }
}

/// Advise the kernel of a certain memory usage pattern.
///
/// [argument, addr]
/// The start of the range to be advised.
///
/// [argument, len]
/// The length of the range to be advised.
///
/// [argument, advice]
/// The advice given.
///
/// = See also
///
/// * link:man:madvise(2)
pub unsafe fn madvise(addr: usize, len: usize, advice: c_int) -> c_int {
    r::madvise(addr as k_ulong, len as k_ulong, advice)
}

/// Change the memory protection of a region.
///
/// [argument, addr]
/// The start of the region.
///
/// [argument, len]
/// The length of the region.
///
/// [argument, protection]
/// The new protection.
///
/// = See also
///
/// * link:man:mprotect(2)
pub fn mprotect(addr: usize, len: usize, protection: c_int) -> c_int {
    unsafe { r::mprotect(addr as k_ulong, len as k_ulong, protection as k_ulong) }
}

/// Lock a memory range in memory.
///
/// [argument, addr]
/// The base address of the range.
///
/// [argument, len]
/// The length of the range.
///
/// = See also
///
/// * link:man:mlock(2)
pub fn mlock(addr: usize, len: usize) -> c_int {
    unsafe { r::mlock(addr as k_ulong, len as k_ulong) }
}

/// Unlock a memory range.
///
/// [argument, addr]
/// The base address of the range.
///
/// [argument, len]
/// The length of the range.
///
/// = See also
///
/// * link:man:munlock(2)
pub fn munlock(addr: usize, len: usize) -> c_int {
    unsafe { r::munlock(addr as k_ulong, len as k_ulong) }
}

/// Lock all pages in memory.
///
/// [argument, flags]
/// Flags to used for locking.
///
/// = See also
///
/// * link:man:mlockall(2)
pub fn mlockall(flags: c_int) -> c_int {
    unsafe { r::mlockall(flags) }
}

/// Unlock all pages in memory.
///
/// = See also
///
/// * link:man:munlockall(2)
pub fn munlockall() -> c_int {
    unsafe { r::munlockall() }
}

/// Checks whether pages are in memory or swapped out.
///
/// [argument, addr]
/// The base address of the range to check.
///
/// [argument, length]
/// The length of the range to check.
///
/// [argument, buf]
/// The buffer in which the result will be stored.
///
/// = See also
///
/// * link:man:mincore(2)
pub fn mincore(addr: usize, length: usize, buf: &mut [d8]) -> c_int {
    let pages = (buf.len() + PAGE_SIZE - 1) / PAGE_SIZE;
    if pages > buf.len() {
        return -crate::kty::EINVAL;
    }
    unsafe { r::mincore(addr as k_ulong, length as size_t, buf.as_mut_ptr() as *mut u8) }
}

/// Create a new session.
///
/// = See also
///
/// * link:man:setsid(2)
pub fn setsid() -> pid_t {
    unsafe { r::setsid() }
}

/// Get the session id of a process.
///
/// [argument, pid]
/// The process whose session id to return.
///
/// = See also
///
/// * link:man:getsid(2)
pub fn getsid(pid: pid_t) -> pid_t {
    unsafe { r::getsid(pid) }
}

/// Set the current working directory to the directory referenced by a file descriptor.
///
/// [argument, fd]
/// The file descriptor pointing to the new working directory.
///
/// = See also
///
/// * link:man:fchdir(2)
pub fn fchdir(fd: c_int) -> c_int {
    unsafe { r::fchdir(fd as c_uint) }
}

/// Sets the process group of a process.
///
/// [argument, process]
/// The process to move to the process group.
///
/// [argument, group]
/// The new group of the process.
///
/// = See also
///
/// * link:man:setpgid(2)
pub fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
    unsafe { r::setpgid(pid, pgid) }
}

/// Get the process group of a process.
///
/// [argument, pid]
/// The process whose process group to return.
///
/// = See also
///
/// * link:man:getpgid(2)
pub fn getpgid(pid: pid_t) -> pid_t {
    unsafe { r::getpgid(pid) }
}

/// Sends a signal to a process.
///
/// [argument, pid]
/// The process to send the signal to.
///
/// [argument, sig]
/// The signal to send.
///
/// = See also
///
/// * link:man:kill(2)
pub fn kill(pid: pid_t, sig: c_int) -> c_int {
    unsafe { r::kill(pid, sig) }
}

/// Sends a signal to a thread.
///
/// [argument, tgid]
/// The thread group of the thread.
///
/// [argument, tid]
/// The thread id.
///
/// [argument, sig]
/// The signal to send.
///
/// = See also
///
/// * link:man:tgkill(2)
pub fn tgkill(tgid: c_int, tid: c_int, sig: c_int) -> c_int {
    unsafe { r::tgkill(tgid, tid, sig) }
}

/// Returns the thread id of the calling thread.
///
/// = See also
///
/// * link:man:gettid(2)
pub fn gettid() -> pid_t {
    unsafe { r::gettid() }
}

/// Returns the resource usage of this thread, this process, or its children.
///
/// [argument, who]
/// Whose resource usage to get.
///
/// [argument, usage]
/// Place where the usage will be stored.
///
/// = See also
///
/// * link:man:getrusage(2)
pub fn getrusage(who: c_int, usage: &mut rusage) -> c_int {
    unsafe { r::getrusage(who, usage) }
}

/// Executes ioctl with the TIOCGPTN option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, id]
/// A place into which the slave id will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgptn(fd: c_int, id: &mut u32) -> c_int {
    let mut u: c_int = 0;
    let rv = unsafe {
        r::ioctl(fd as k_uint, TIOCGPTN(), &mut u as *mut _ as k_ulong)
    };
    *id = u as u32;
    rv
}

/// Executes ioctl with the TIOCSPTLCK option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, locked]
/// Whether the slave is locked.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocsptlck(fd: c_int, locked: bool) -> c_int {
    let mut locked = locked as c_int;
    unsafe {
        r::ioctl(fd as k_uint, TIOCSPTLCK(), &mut locked as *mut _ as k_ulong)
    }
}

/// Executes ioctl with the TIOCGPTLCK option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, locked]
/// Place where the lock status will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgptlck(fd: c_int, locked: &mut bool) -> c_int {
    let mut t: c_int = 0;
    let rv = unsafe { r::ioctl(fd as k_uint, TIOCGPTLCK(), &mut t as *mut _ as k_ulong) };
    *locked = t != 0;
    rv
}

/// Executes ioctl with the TIOCSIG option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, sig]
/// The signal to send.
///
/// = Remarks
///
/// This ioctl is undocumented but see drivers/tty/pty.c.
pub fn ioctl_tiocsig(fd: c_int, mut sig: c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCSIG(), &mut sig as *mut _ as k_ulong) }
}

/// Executes ioctl with the TIOCPKT option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, packet]
/// Whether packet mode is enabled.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocpkt(fd: c_int, packet: bool) -> c_int {
    let mut packet = packet as c_int;
    unsafe { r::ioctl(fd as k_uint, TIOCPKT, &mut packet as *mut _ as k_ulong) }
}

/// Executes ioctl with the TIOCGPKT option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, packet]
/// Place where the status of packet mode will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgpkt(fd: c_int, packet: &mut bool) -> c_int {
    let mut p: c_int = 0;
    let rv = unsafe { r::ioctl(fd as k_uint, TIOCGPKT(), &mut p as *mut _ as k_ulong) };
    *packet = p != 0;
    rv
}

/// Executes ioctl with the TIOCSTI option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, byte]
/// The byte to insert into the input queue.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocsti(fd: c_int, mut byte: u8) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCSTI, &mut byte as *mut _ as k_ulong) }
}

/// Executes ioctl with the TIOCGWINSZ option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, size]
/// Place where the window size will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgwinsz(fd: c_int, size: &mut winsize) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCGWINSZ, size as *mut _ as k_ulong) }
}

/// Executes ioctl with the TIOCSWINSZ option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, size]
/// The new window size.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocswinsz(fd: c_int, size: &winsize) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCSWINSZ, size as *const _ as k_ulong) }
}

/// Executes ioctl with the TIOCCONS option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tioccons(fd: c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCCONS, 0) }
}

/// Executes ioctl with the TIOCEXCL option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocexcl(fd: c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCEXCL, 0) }
}

/// Executes ioctl with the TIOCNXCL option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocnxcl(fd: c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCNXCL, 0) }
}

/// Executes ioctl with the TIOCGEXCL option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, packet]
/// Place where the status of exclusize mode will be stored.
///
/// = Remarks
///
/// == Kernel versions
///
/// The required kernel version is 3.8.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgexcl(fd: c_int, exclusive: &mut bool) -> c_int {
    let mut p: c_int = 0;
    let rv = unsafe { r::ioctl(fd as k_uint, TIOCGEXCL(), &mut p as *mut _ as k_ulong) };
    *exclusive = p != 0;
    rv
}

/// Executes ioctl with the TIOCNOTTY option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocnotty(fd: c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCNOTTY, 0) }
}

/// Executes ioctl with the TIOCSCTTY option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, steal]
/// Whether to steal the terminal.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocsctty(fd: c_int, steal: bool) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCSCTTY, steal as k_ulong) }
}

/// Executes ioctl with the TIOCGPGRP option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, id]
/// Place where the process group id of the foreground process group will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgpgrp(fd: c_int, id: &mut pid_t) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCGPGRP, id as *mut _ as k_ulong) }
}

/// Executes ioctl with the TIOCSPGRP option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, id]
/// The process group id of the foreground process group.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocspgrp(fd: c_int, id: pid_t) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCSPGRP, &id as *const _ as k_ulong) }
}

/// Executes ioctl with the TIOCGSID option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, id]
/// Place where the session id of the terminal will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgsid(fd: c_int, id: &mut pid_t) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCGSID, id as *mut _ as k_ulong) }
}

/// Executes ioctl with the TIOCGETD option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, discipline]
/// Place where the line discipline of the terminal will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocgetd(fd: c_int, discipline: &mut c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCGETD, discipline as *mut _ as k_ulong) }
}

/// Executes ioctl with the TIOCSETD option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, discipline]
/// The line discipline of the terminal.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocsetd(fd: c_int, discipline: c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCSETD, &discipline as *const _ as k_ulong) }
}

/// Executes ioctl with the TIOCVHANGUP option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// = Remarks
///
/// This ioctl is undocumented but see drivers/tty/tty_io.c.
pub fn ioctl_tiocvhangup(fd: c_int) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCVHANGUP, 0) }
}

/// Executes ioctl with the TIOCGDEV option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, device]
/// Place where the device of the terminal will be stored.
///
/// = Remarks
///
/// This ioctl is undocumented but see drivers/tty/tty_io.c.
pub fn ioctl_tiocgdev(fd: c_int, dev: &mut c_uint) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TIOCGDEV(), dev as *mut _ as k_ulong) }
}

/// Executes ioctl with the TCFLSH option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, how]
/// What to discard.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tcflsh(fd: c_int, how: c_uint) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TCFLSH, how as k_ulong) }
}

/// Executes ioctl with the TIOCOUTQ option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, buf]
/// Place where the number of pending output bytes will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tiocoutq(fd: c_int, buf: &mut usize) -> c_int {
    let mut tmp: c_int = 0;
    let rv = unsafe { r::ioctl(fd as k_uint, TIOCOUTQ, &mut tmp as *mut _ as k_ulong) };
    *buf = tmp as usize;
    rv
}

/// Executes ioctl with the TCXONC option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, how]
/// What to disable/enable.
///
/// = See also
///
/// * link:man:tty_ioctl(2)
pub fn ioctl_tcxonc(fd: c_int, how: c_uint) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TCXONC, how as k_ulong) }
}

/// Executes ioctl with the TCGETS2 option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, attrs]
/// Place where the tty attributes will be stored.
///
/// = See also
///
/// * link:man:tty_ioctl(2) and TCGETS therein
pub fn ioctl_tcgets2(fd: c_int, attrs: &mut termios2) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TCGETS2(), attrs as *mut _ as k_ulong) }
}

/// Executes ioctl with the TCSETS2 option.
///
/// [argument, fd]
/// The file descriptor on which to operate.
///
/// [argument, attrs]
/// The tty attributes.
///
/// = See also
///
/// * link:man:tty_ioctl(2) and TCSETS therein
pub fn ioctl_tcsets2(fd: c_int, attrs: &termios2) -> c_int {
    unsafe { r::ioctl(fd as k_uint, TCSETS2(), attrs as *const _ as k_ulong) }
}

/// Simulates a hangup on the current terminal.
///
/// = See also
///
/// * link:man:vhangup(2)
pub fn vhangup() -> c_int {
    unsafe { r::vhangup() }
}

/// Opens a message queue.
///
/// [argument, name]
/// The name of the queue.
///
/// [argument, flags]
/// Flags used when opening the queue.
///
/// [argument, mode]
/// Mode used on a new queue.
///
/// [argument, attr]
/// Attributes set on a new queue.
///
/// = Remarks
///
/// Unless lrs was compiled with the `no-auto-cloexec` flag, this function automatically
/// adds the `O_CLOEXEC` flag.
///
/// = See also
///
/// * link:man:mq_open(2)
pub fn mq_open(name: KStr, mut flags: c_int, mode: umode_t,
               attr: Option<&mq_attr>) -> c_int {
    if cfg!(not(no_auto_cloexec)) {
        flags |= O_CLOEXEC;
    }
    let attr = attr.map(|a| a as *const _ as *mut _).unwrap_or(0 as *mut _);
    unsafe { r::mq_open(name.as_ptr(), flags, mode, attr) }
}

/// Destroys a queue.
///
/// [argument, name]
/// The name of the queue to destroy.
///
/// = See also
///
/// * link:man:mq_unlink(2)
pub fn mq_unlink(name: KStr) -> c_int {
    unsafe { r::mq_unlink(name.as_ptr()) }
}

/// Sends a message over a message queue.
///
/// [argument, mq]
/// The message queue.
///
/// [argument, msg]
/// The message to send.
///
/// [argument, prio]
/// The priority of the message.
///
/// [argument, timeout]
/// The timeout of the operation.
///
/// = See also
///
/// * link:man:mq_timedsend(2)
pub fn mq_timedsend(mq: c_int, msg: &[d8], prio: c_uint,
                    timeout: Option<&timespec>) -> c_int {
    let timeout = timeout.map(|a| a as *const _).unwrap_or(0 as *const _);
    unsafe {
        r::mq_timedsend(mq, msg.as_ptr() as *const c_char, msg.len() as size_t, prio,
                        timeout)
    }
}

/// Receives a message over a message queue.
///
/// [argument, mq]
/// The message queue.
///
/// [argument, msg]
/// A place where the message will be stored.
///
/// [argument, prio]
/// A place where the priority will be stored.
///
/// [argument, timeout]
/// The timeout of the operation.
///
/// = See also
///
/// * link:man:mq_timedreceive(2)
pub fn mq_timedreceive(mq: c_int, msg: &mut [d8], prio: Option<&mut c_uint>,
                       timeout: Option<&timespec>) -> ssize_t {
    let prio = prio.map(|a| a as *mut _).unwrap_or(0 as *mut _);
    let timeout = timeout.map(|a| a as *const _).unwrap_or(0 as *const _);
    unsafe {
        r::mq_timedreceive(mq, msg.as_mut_ptr() as *mut c_char, msg.len() as size_t, prio,
                           timeout)
    }
}

/// Sets or retrieves attributes of a message queue.
///
/// [argument, mq]
/// The message queue.
///
/// [argument, new]
/// The attributes to set.
///
/// [argument, old]
/// A place where the old attributes will be stored.
///
/// = See also
///
/// * link:man:mq_getsetattr(2)
pub fn mq_getsetattr(mq: c_int, new: Option<&mq_attr>,
                     old: Option<&mut mq_attr>) -> c_int {
    let new = new.map(|a| a as *const _).unwrap_or(0 as *const _);
    let old = old.map(|a| a as *mut _).unwrap_or(0 as *mut _);
    unsafe { r::mq_getsetattr(mq, new, old) }
}

/// Sets the scheduling policy of a thread.
///
/// [argument, pid]
/// The thread to modify.
///
/// [argument, attr]
/// The policy to set.
///
/// [argument, flags]
/// Unused.
///
/// = See also
///
/// * link:man:sched_setattr(2)
pub fn sched_setattr(pid: pid_t, attr: &mut sched_attr, flags: c_uint) -> c_int {
    unsafe { r::sched_setattr(pid, attr, flags) }
}

/// Gets the scheduling policy of a thread.
///
/// [argument, pid]
/// The thread whose policy to get.
///
/// [argument, attr]
/// A place where the policy will be stored.
///
/// [argument, flags]
/// Unused.
///
/// = See also
///
/// * link:man:sched_getattr(2)
pub fn sched_getattr(pid: pid_t, attr: &mut sched_attr, flags: c_uint) -> c_int {
    unsafe {
        r::sched_getattr(pid, attr, mem::size_of::<sched_attr>() as c_uint, flags)
    }
}

/// Relinquish the CPU.
///
/// = See also
///
/// * link:man:sched_yield(2)
pub fn sched_yield() -> c_int {
    unsafe { r::sched_yield() }
}

/// Returns the maximum priority of a scheduling policy.
///
/// [argument, policy]
/// The policy.
///
/// = See also
///
/// * link:man:sched_get_priority_max(2)
pub fn sched_get_priority_max(policy: c_int) -> c_int {
    unsafe { r::sched_get_priority_max(policy) }
}

/// Returns the minimum priority of a scheduling policy.
///
/// [argument, policy]
/// The policy.
///
/// = See also
///
/// * link:man:sched_get_priority_min(2)
pub fn sched_get_priority_min(policy: c_int) -> c_int {
    unsafe { r::sched_get_priority_min(policy) }
}

/// Returns the round-robin time slice of a thread.
///
/// [argument, pid]
/// The thread to inspect.
///
/// [argument, tp]
/// Place where the time slice will be stored.
///
/// = See also
///
/// * link:man:sched_rr_get_interval(2)
pub fn sched_rr_get_interval(pid: pid_t, tp: &mut timespec) -> c_int {
    unsafe { r::sched_rr_get_interval(pid, tp) }
}

/// Returns the scheduling priority of a process, process group, or user.
///
/// [argument, which]
/// The id type of `who`.
///
/// [argument, who]
/// Whose priority to return.
///
/// = See also
///
/// * link:man:getpriority(2)
pub fn getpriority(which: c_int, who: c_int) -> c_int {
    unsafe { r::getpriority(which, who) }
}

/// Sets the scheduling priority of a process, process group, or user.
///
/// [argument, which]
/// The id type of `who`.
///
/// [argument, who]
/// Whose priority to set.
///
/// [argument, prio]
/// The scheduling priority.
///
/// = See also
///
/// * link:man:setpriority(2)
pub fn setpriority(which: c_int, who: c_int, prio: c_int) -> c_int {
    unsafe { r::setpriority(which, who, prio) }
}

/// Retrieves the capabilities of a thread.
///
/// [argument, tid]
/// The thread id.
///
/// [argument, buf]
/// The buffer in which the capabilities will be stored.
///
/// = See also
///
/// * link:man:capget(2)
pub fn capget_v3(tid: c_int, buf: &mut [__user_cap_data_struct; 2]) -> c_int {
    let mut header = __user_cap_header_struct {
        version: _LINUX_CAPABILITY_VERSION_3 as u32,
        pid: tid,
    };
    unsafe { r::capget(&mut header, buf.as_mut_ptr()) }
}

/// Sets the capabilities of this thread.
///
/// [argument, caps]
/// The capabilities.
///
/// = See also
///
/// * link:man:capset(2)
pub fn capset_v3(caps: &[__user_cap_data_struct; 2]) -> c_int {
    let mut header = __user_cap_header_struct {
        version: _LINUX_CAPABILITY_VERSION_3 as u32,
        pid: 0,
    };
    unsafe { r::capset(&mut header, caps.as_ptr() as *mut _) }
}

/// Checks whether a capability is in the bounding set of this thread.
///
/// [argument, cap]
/// The capability to check.
///
/// = See also
///
/// * link:man:prctl(2) and PR_CAPBSET_READ therein
pub fn prctl_pr_capbset_read(cap: c_int) -> c_int {
    unsafe { r::prctl(PR_CAPBSET_READ, cap as k_ulong, 0, 0, 0) }
}

/// Removes a capability from this thread's bounding set.
///
/// [argument, cap]
/// The capability to remove.
///
/// = See also
///
/// * link:man:prctl(2) and PR_CAPBSET_DROP therein
pub fn prctl_pr_capbset_drop(cap: c_int) -> c_int {
    unsafe { r::prctl(PR_CAPBSET_DROP, cap as k_ulong, 0, 0, 0) }
}

/// Checks whether capabilities are dropped when all root user ids are dropped.
///
/// = See also
///
/// * link:man:prctl(2) and PR_GET_KEEPCAPS therein
pub fn prctl_pr_get_keepcaps() -> c_int {
    unsafe { r::prctl(PR_GET_KEEPCAPS, 0, 0, 0, 0) }
}

/// Sets whether capabilities are dropped when all root user ids are dropped.
///
/// [argument, keep]
/// Whether the capabilities are kept.
///
/// = See also
///
/// * link:man:prctl(2) and PR_SET_KEEPCAPS therein
pub fn prctl_pr_set_keepcaps(keep: bool) -> c_int {
    unsafe { r::prctl(PR_SET_KEEPCAPS, keep as k_ulong, 0, 0, 0) }
}

pub fn prctl_pr_cap_ambient_raise(cap: k_ulong) -> c_int {
    unsafe { r::prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0 ) }
}

pub fn prctl_pr_cap_ambient_lower(cap: k_ulong) -> c_int {
    unsafe { r::prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, cap, 0, 0 ) }
}

pub fn prctl_pr_cap_ambient_is_set(cap: k_ulong) -> c_int {
    unsafe { r::prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, cap, 0, 0 ) }
}

pub fn prctl_pr_cap_ambient_clear_all() -> c_int {
    unsafe { r::prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0 ) }
}

/// Disassociate parts of the thread's execution context.
///
/// [argument, flags]
/// What to disassociate.
///
/// = See also
///
/// * link:man:unshare(2)
pub fn unshare(flags: c_int) -> c_int {
    unsafe { r::unshare(flags as k_ulong) }
}

/// Retrieves the CPU and NUMA node this thread is running one.
///
/// [argument, cpu]
/// Place where the CPU will be stored.
///
/// [argument, node]
/// Place where the NUMA node will be stored.
///
/// = See also
///
/// * link:man:getcpu(2)
pub fn getcpu(cpu: Option<&mut c_uint>, node: Option<&mut c_uint>) -> c_int {
    let cpu = cpu.map(|c| c as *mut _).unwrap_or(0 as *mut _);
    let node = node.map(|c| c as *mut _).unwrap_or(0 as *mut _);
    unsafe { r::getcpu(cpu, node, 0 as *mut _) }
}

/// Associate a thread with a namespace.
///
/// [argument, fd]
/// A file descriptor referring to the namespace.
///
/// [argument, nstype]
/// Which namespace types can be joined.
///
/// = See also
///
/// * link:man:setns(2)
pub fn setns(fd: c_int, nstype: c_int) -> c_int {
    unsafe { r::setns(fd, nstype) }
}

/// Enables strict seccomp mode for this thread.
///
/// = See also
///
/// * link:man:seccomp(2) and SECCOMP_SET_MODE_STRICT therein
pub fn seccomp_seccomp_set_mode_strict() -> c_int {
    unsafe { r::seccomp(SECCOMP_SET_MODE_STRICT, 0, 0 as *mut _) }
}

/// Adds a swap file/device.
///
/// [argument, path]
/// The path of the file/device.
///
/// [argument, flags]
/// Flags to modify the swap behavior.
///
/// = See also
///
/// * link:man:swapon(2)
pub fn swapon(path: KStr, flags: c_int) -> c_int {
    unsafe { r::swapon(path.as_ptr(), flags) }
}

/// Removes a swap file/device.
///
/// [argument, path]
/// The path of the file/device.
///
/// = See also
///
/// * link:man:swapoff(2)
pub fn swapoff(path: KStr) -> c_int {
    unsafe { r::swapoff(path.as_ptr()) }
}

/// Changes the root directory of the process.
///
/// [argument, path]
/// The new root directory.
///
/// = See also
///
/// * link:man:chroot(2)
pub fn chroot(path: KStr) -> c_int {
    unsafe { r::chroot(path.as_ptr()) }
}

/// Moves the current root directory and sets a new one.
///
/// [argument, new]
/// The path of the new root directory.
///
/// [argument, old]
/// Where the old root directory will me moved to.
///
/// = See also
///
/// * link:man:pivot_root(2)
pub fn pivot_root(new: KStr, old: KStr) -> c_int {
    unsafe { r::pivot_root(new.as_ptr(), old.as_ptr()) }
}

/// Sets the address on which the kernel will perform a futex_wake operation upon thread
/// exit.
///
/// [argument, tidptr]
/// The address or `None` to disable this feature.
///
/// [return_value]
/// Returns the callers thread id.
///
/// = Remarks
///
/// The kernel will first write `0` to this address and then perform a futex_wake.
///
/// This is unsafe because the address might no longer be valid when the thread exits.
///
/// = See also
///
/// * link:man:set_tid_address(2)
pub unsafe fn set_tid_address(tidptr: Option<&AtomicInt>) -> c_int {
    let addr = tidptr.map(|t| t as *const _ as *mut _).unwrap_or(0 as *mut _);
    r::set_tid_address(addr) as c_int
}