File: unix_specific.cpp

package info (click to toggle)
polyml 5.2.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 19,692 kB
  • ctags: 17,567
  • sloc: cpp: 37,221; sh: 9,591; asm: 4,120; ansic: 428; makefile: 203; ml: 191; awk: 91; sed: 10
file content (2030 lines) | stat: -rw-r--r-- 69,862 bytes parent folder | download | duplicates (2)
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
/*
    Title:      Operating Specific functions: Unix version.

    Copyright (c) 2000-8 David C. J. Matthews
    Portions of this code are derived from the original stream io
    package copyright CUTS 1983-2000.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


#ifdef WIN32
#include "winconfig.h"
#else
#include "config.h"
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef HAVE_PWD_H
#include <pwd.h>
#endif

#ifdef HAVE_GRP_H
#include <grp.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#ifdef HAVE_SYS_SIGNAL_H
#include <sys/signal.h>
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#ifdef HAVE_SYS_TERMIOS_H
#include <sys/termios.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

#include "globals.h"
#include "arb.h"
#include "run_time.h"
#include "io_internal.h"
#include "sys.h"
#include "diagnostics.h"
#include "machine_dep.h"
#include "os_specific.h"
#include "gc.h"
#include "processes.h"
#include "mpoly.h"
#include "sighandler.h"
#include "polystring.h"
#include "save_vec.h"
#include "rts_module.h"

#define STREAMID(x) (DEREFSTREAMHANDLE(x)->streamNo)

#define SAVE(x) taskData->saveVec.push(x)
#define ALLOC(n) alloc_and_save(taskData, n)
#define SIZEOF(x) (sizeof(x)/sizeof(PolyWord))

/* Table of constants returned by call 4. */
static int unixConstVec[] =
{
    /* Error codes. */
    E2BIG, /* 0 */
    EACCES,
    EAGAIN,
    EBADF,
#ifdef EBADMSG
/* This is not defined in FreeBSD. */
    EBADMSG,
#else
    0,
#endif
    EBUSY,
#ifdef ECANCELED
/* This is not defined in Linux.  Perhaps somone knows how to spell "cancelled". */
    ECANCELED,
#else
    0, /* Perhaps some other value. */
#endif
    ECHILD,
    EDEADLK,
    EDOM,
    EEXIST,
    EFAULT,
    EFBIG,
    EINPROGRESS,
    EINTR,
    EINVAL,
    EIO,
    EISDIR,
    ELOOP,
    EMFILE,
    EMLINK, /* 20 */
    EMSGSIZE,
    ENAMETOOLONG,
    ENFILE,
    ENODEV,
    ENOENT,
    ENOEXEC,
    ENOLCK,
    ENOMEM,
    ENOSPC,
    ENOSYS,
    ENOTDIR,
    ENOTEMPTY,
#ifdef ENOTSUP
/* Not defined in Linux. */
    ENOTSUP,
#else
    0,
#endif
    ENOTTY,
    ENXIO,
    EPERM,
    EPIPE,
    ERANGE,
    EROFS,
    ESPIPE,
    ESRCH,
    EXDEV, /* 42 */

    /* Signals. */
    SIGABRT, /* 43 */
    SIGALRM,
    SIGBUS,
    SIGFPE,
    SIGHUP,
    SIGILL,
    SIGINT,
    SIGKILL,
    SIGPIPE,
    SIGQUIT,
    SIGSEGV,
    SIGTERM,
    SIGUSR1,
    SIGUSR2,
    SIGCHLD,
    SIGCONT,
    SIGSTOP,
    SIGTSTP,
    SIGTTIN,
    SIGTTOU, /* 62 */

    /* Open flags. */
    O_RDONLY, /* 63 */
    O_WRONLY,
    O_RDWR,
    O_APPEND,
    O_EXCL,
    O_NOCTTY,
    O_NONBLOCK,
#ifdef O_SYNC
    O_SYNC, /* Not defined in FreeBSD. */
#else
    0,
#endif
    O_TRUNC, /* 71 */

    /* TTY: Special characters. */
    VEOF, /* 72 */
    VEOL,
    VERASE,
    VINTR,
    VKILL,
    VMIN,
    VQUIT,
    VSUSP,
    VTIME,
    VSTART,
    VSTOP,
    NCCS, /* 83 */

    /* TTY: Input mode. */
    BRKINT, /* 84 */
    ICRNL,
    IGNBRK,
    IGNCR,
    IGNPAR,
    INLCR,
    INPCK,
    ISTRIP,
    IXOFF,
    IXON,
    PARMRK, /* 94 */

    /* TTY: Output mode. */
    OPOST, /* 95 */

    /* TTY: Control modes. */
    CLOCAL, /* 96 */
    CREAD,
    CS5,
    CS6,
    CS7,
    CS8,
    CSIZE,
    CSTOPB,
    HUPCL,
    PARENB,
    PARODD, /* 106 */

    /* TTY: Local modes. */
    ECHO, /* 107 */
    ECHOE,
    ECHOK,
    ECHONL,
    ICANON,
    IEXTEN,
    ISIG,
    NOFLSH,
    TOSTOP, /* 115 */

    /* TTY: Speeds. */
    B0, /* 116 */
    B50,
    B75,
    B110,
    B134,
    B150,
    B200,
    B300,
    B600,
    B1200,
    B1800,
    B2400,
    B4800,
    B9600,
    B19200,
    B38400, /* 131 */

    /* FD flags. */
    FD_CLOEXEC, /* 132 */

    /* Wait flags. */
    WUNTRACED, /* 133 */
    WNOHANG, /* 134 */

    /* tcsetattr flags. */
    TCSANOW, /* 135 */
    TCSADRAIN,
    TCSAFLUSH,
    /* tcflow flags. */
    TCOOFF, /* 138 */
    TCOON,
    TCIOFF,
    TCION,
    /* tcflush flags. */
    TCIFLUSH, /* 142 */
    TCOFLUSH,
    TCIOFLUSH,
    
    /* File permissions. */
    S_IRUSR, /* 145 */
    S_IWUSR,
    S_IXUSR,
    S_IRGRP,
    S_IWGRP,
    S_IXGRP,
    S_IROTH,
    S_IWOTH,
    S_IXOTH,
    S_ISUID,
    S_ISGID, /* 155 */

    /* Bits for access function. */
    R_OK, /* 156 */
    W_OK,
    X_OK,
    F_OK, /* 159 */
    
    /* Values for lseek. */
    SEEK_SET, /* 160 */
    SEEK_CUR,
    SEEK_END, /* 162 */

    /* Values for lock types. */
    F_RDLCK, /* 163 */
    F_WRLCK,
    F_UNLCK, /* 165 */

    /* Mask for file access. */
    O_ACCMODE, /* 166 */
};

/* Auxiliary functions which implement the more complex cases. */
static Handle waitForProcess(TaskData *taskData, Handle args);
static Handle makePasswordEntry(TaskData *taskData, struct passwd *pw);
static Handle makeGroupEntry(TaskData *taskData, struct group *grp);
static Handle getUname(TaskData *taskData);
static Handle getSysConf(TaskData *taskData, Handle args);
static Handle getTTYattrs(TaskData *taskData, Handle args);
static Handle setTTYattrs(TaskData *taskData, Handle args);
static Handle getStatInfo(TaskData *taskData, struct stat *buf);
static Handle lockCommand(TaskData *taskData, int cmd, Handle args);
static int findPathVar(TaskData *taskData, PolyWord ps);

// Unmask all signals just before exec.
static void restoreSignals(void)
{
    sigset_t sigset;
    sigemptyset(&sigset);
    sigprocmask(SIG_SETMASK, &sigset, NULL);
}

Handle OS_spec_dispatch_c(TaskData *taskData, Handle args, Handle code)
{
    int c = get_C_long(taskData, DEREFWORDHANDLE(code));
    switch (c)
    {
    case 0: /* Return our OS type.  Not in any structure. */
        return Make_arbitrary_precision(taskData, 0); /* 0 for Unix. */

    case 4: /* Return a constant. */
        {
            unsigned i = get_C_long(taskData, DEREFWORDHANDLE(args));
            if (i < 0 || i >= sizeof(unixConstVec)/sizeof(unixConstVec[0]))
                raise_syscall(taskData, "Invalid index", 0);
            return Make_unsigned(taskData, unixConstVec[i]);
        }

    case 5: /* fork. */
        {
            pid_t pid = fork();
            if (pid < 0) raise_syscall(taskData, "fork failed", errno);
            if (pid == 0) processes->SetSingleThreaded();
            return Make_unsigned(taskData, pid);
        }

    case 6: /* kill */
        {
            int pid = get_C_long(taskData, DEREFHANDLE(args)->Get(0));
            int sig = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (kill(pid, sig) < 0) raise_syscall(taskData, "kill failed", errno);
            return Make_unsigned(taskData, 0);
        }

    case 7: /* get process id */
        {
            pid_t pid = getpid();
            if (pid < 0) raise_syscall(taskData, "getpid failed", errno);
            return Make_unsigned(taskData, pid);
        }

    case 8: /* get process id of parent */
        {
            pid_t pid = getppid();
            if (pid < 0) raise_syscall(taskData, "getppid failed", errno);
            return Make_unsigned(taskData, pid);
        }

    case 9: /* get real user id */
        {
            uid_t uid = getuid();
            if (uid < 0) raise_syscall(taskData, "getuid failed", errno);
            return Make_unsigned(taskData, uid);
        }

    case 10: /* get effective user id */
        {
            uid_t uid = geteuid();
            if (uid < 0) raise_syscall(taskData, "geteuid failed", errno);
            return Make_unsigned(taskData, uid);
        }

    case 11: /* get real group id */
        {
            gid_t gid = getgid();
            if (gid < 0) raise_syscall(taskData, "getgid failed", errno);
            return Make_unsigned(taskData, gid);
        }

    case 12: /* get effective group id */
        {
            gid_t gid = getegid();
            if (gid < 0) raise_syscall(taskData, "getegid failed", errno);
            return Make_unsigned(taskData, gid);
        }

    case 13: /* Return process group */
        {
            pid_t pid = getpgrp();
            if (pid < 0) raise_syscall(taskData, "getpgrp failed", errno);
            return Make_unsigned(taskData, pid);
        }

    case 14: /* Wait for child process to terminate. */
        return waitForProcess(taskData, args);

    case 15: /* Unpack a process result. */
        {
            int resType, resVal;
            Handle result,  typeHandle, resHandle;
            int status = get_C_long(taskData, DEREFWORDHANDLE(args));
            if (WIFEXITED(status))
            {
                resType = 1;
                resVal = WEXITSTATUS(status);
            }
            else if (WIFSIGNALED(status))
            {
                resType = 2;
                resVal = WTERMSIG(status);
            }
            else if (WIFSTOPPED(status))
            {
                resType = 3;
                resVal = WSTOPSIG(status);
            }
            else { /* ?? */
                resType = 0;
                resVal = 0;
            }
            typeHandle = Make_arbitrary_precision(taskData, resType);
            resHandle = Make_arbitrary_precision(taskData, resVal);

            result = ALLOC(2);
            DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(typeHandle));
            DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(resHandle));
            return result;
        }

    case 16: /* Pack up a process result.  The inverse of the previous call. */
        {
            int resType = get_C_long(taskData, DEREFHANDLE(args)->Get(0));
            int resVal = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int result = 0;
            switch (resType)
            {
            case 1: /* Exited */ result = resVal << 8; break;
            case 2: /* Signalled */ result = resVal; break;
            case 3: /* Stopped */ result = (resVal << 8) | 0177;
            }
            return Make_unsigned(taskData, result);
        }

    case 17: /* Run a new executable. */
        {
            char *path = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            char **argl = stringListToVector(SAVE(DEREFHANDLE(args)->Get(1)));
            int err;
            restoreSignals();
            execv(path, argl);
            err = errno;
            /* We only get here if there's been an error. */
            free(path);
            freeStringVector(argl);
            raise_syscall(taskData, "execv failed", err);
        }

    case 18: /* Run a new executable with given environment. */
        {
            char *path = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            char **argl = stringListToVector(SAVE(DEREFHANDLE(args)->Get(1)));
            char **envl = stringListToVector(SAVE(DEREFHANDLE(args)->Get(2)));
            int err;
            restoreSignals();
            execve(path, argl, envl);
            err = errno;
            /* We only get here if there's been an error. */
            free(path);
            freeStringVector(argl);
            freeStringVector(envl);
            raise_syscall(taskData, "execve failed", err);
        }

    case 19: /* Run a new executable using PATH environment variable. */
        {
            char *path = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            char **argl = stringListToVector(SAVE(DEREFHANDLE(args)->Get(1)));
            int err;
            restoreSignals();
            execvp(path, argl);
            err = errno;
            /* We only get here if there's been an error. */
            free(path);
            freeStringVector(argl);
            raise_syscall(taskData, "execvp failed", err);
        }

    case 20: /* Sets an alarm and returns the current alarm time.  A value of
                zero for the time cancels the timer. */
        {
            /* We have a value in microseconds.  We need to split
               it into seconds and microseconds. */
            Handle hTime = args;
            Handle hMillion = Make_arbitrary_precision(taskData, 1000000);
            struct itimerval newTimer, oldTimer;
            newTimer.it_interval.tv_sec = 0;
            newTimer.it_interval.tv_usec = 0;
            newTimer.it_value.tv_sec =
                get_C_long(taskData, DEREFWORDHANDLE(div_longc(taskData, hMillion, hTime)));
            newTimer.it_value.tv_usec =
                get_C_long(taskData, DEREFWORDHANDLE(rem_longc(taskData, hMillion, hTime)));
            if (setitimer(ITIMER_REAL, &newTimer, &oldTimer) != 0)
                raise_syscall(taskData, "setitimer failed", errno);
            Handle result = /* Return the previous setting. */
                Make_arb_from_pair_scaled(taskData, oldTimer.it_value.tv_sec,
                        oldTimer.it_value.tv_usec, 1000000);
            return result;
        }

    case 21: /* Pause until signal. */
        {
            /* This never returns.  When a signal is handled it will
               be interrupted. */
            processes->BlockAndRestart(taskData, -1, true /* Interruptable. */, POLY_SYS_os_specific);
        }

    case 22: /* Sleep until given time or until a signal.  Note: this is called
            with an absolute time as an argument and returns a relative time as
            result.  This RTS call is tried repeatedly until either the time has
            expired or a signal has occurred. */
        {
            struct timeval tv;
            /* We have a value in microseconds.  We need to split
               it into seconds and microseconds. */
            Handle hTime = args;
            Handle hMillion = Make_arbitrary_precision(taskData, 1000000);
            unsigned long secs = get_C_ulong(taskData, DEREFWORDHANDLE(div_longc(taskData, hMillion, hTime)));
            unsigned long usecs = get_C_ulong(taskData, DEREFWORDHANDLE(rem_longc(taskData, hMillion, hTime)));
            /* Has the time expired? */
            if (gettimeofday(&tv, NULL) != 0)
                raise_syscall(taskData, "gettimeofday failed", errno);
            /* If the timeout time is earlier than the current time
               we must return, otherwise we block.  This can be interrupted
               by a signal. */
            if ((unsigned long)tv.tv_sec < secs ||
                ((unsigned long)tv.tv_sec == secs && (unsigned long)tv.tv_usec < usecs))
                processes->BlockAndRestart(taskData, -1, true /* Interruptable. */, POLY_SYS_os_specific);
            return Make_arbitrary_precision(taskData, 0);
        }
    
    case 23: /* Set uid. */
        {
            uid_t uid = get_C_long(taskData, DEREFWORDHANDLE(args));
            if (setuid(uid) != 0) raise_syscall(taskData, "setuid failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 24: /* Set gid. */
        {
            gid_t gid = get_C_long(taskData, DEREFWORDHANDLE(args));
            if (setgid(gid) != 0) raise_syscall(taskData, "setgid failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 25: /* Get group list. */
        {
            // This previously allocated gid_t[NGROUPS_MAX] on the stack but this
            // requires quite a bit of stack space.
            gid_t gid[1];
            int ngroups = getgroups(0, gid); // Just get the number.
            if (ngroups < 0) raise_syscall(taskData, "getgroups failed", errno);
            if (ngroups == 0) return SAVE(ListNull);
            gid_t *groups = (gid_t*)calloc(sizeof(gid_t), ngroups);
            if (groups == 0) raise_syscall(taskData, "Unable to allocate memory", errno);
            if (getgroups(ngroups, groups) < 0)
            {
                int lasterr = errno;
                free(groups);
                raise_syscall(taskData, "getgroups failed", lasterr);
            }
            Handle saved = taskData->saveVec.mark();
            Handle list  = SAVE(ListNull);

            /* It's simplest to process the integers in reverse order */
            while (--ngroups >= 0)
            {
                Handle value = Make_arbitrary_precision(taskData, groups[ngroups]);
                Handle next  = ALLOC(SIZEOF(ML_Cons_Cell));
                DEREFLISTHANDLE(next)->h = DEREFWORDHANDLE(value); 
                DEREFLISTHANDLE(next)->t = DEREFLISTHANDLE(list);
                taskData->saveVec.reset(saved);
                list = SAVE(DEREFHANDLE(next));
            }
            free(groups);
            return list;
        }

    case 26: /* Get login name. */
        {
            char *login = getlogin();
            if (login == 0) raise_syscall(taskData, "getlogin failed", errno);
            return SAVE(C_string_to_Poly(taskData, login));
        }
        
    case 27: /* Set sid */
        {
            pid_t pid = setsid();
            if (pid < 0) raise_syscall(taskData, "setsid failed", errno);
            return Make_arbitrary_precision(taskData, pid);
        }

    case 28: /* Set process group. */
        {
            pid_t pid = get_C_long(taskData, DEREFHANDLE(args)->Get(0));
            pid_t pgid = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (setpgid(pid, pgid) < 0 ) raise_syscall(taskData, "setpgid failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 29: /* uname */ return getUname(taskData);

    case 30: /* Get controlling terminal. */
        {
            char *term = ctermid(0);
            /* Can this generate an error? */
            if (term == 0) raise_syscall(taskData, "ctermid failed", errno);
            return SAVE(C_string_to_Poly(taskData, term));
        }

    case 31: /* Get terminal name for file descriptor. */
        {
            PIOSTRUCT str = get_stream(args->WordP());
            char *term;
            if (str == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            term = ttyname(str->device.ioDesc);
            if (term == 0) raise_syscall(taskData, "ttyname failed", errno);
            return SAVE(C_string_to_Poly(taskData, term));
        }

    case 32: /* Test if file descriptor is a terminal.  Returns false if
            the stream is closed. */
        {
            PIOSTRUCT str = get_stream(args->WordP());
            if (str != NULL && isatty(str->device.ioDesc))
                return Make_arbitrary_precision(taskData, 1);
            else return Make_arbitrary_precision(taskData, 0);
        }

    case 33: /* sysconf. */
        return getSysConf(taskData, args);

        /* Filesys entries. */
    case 50: /* Set the file creation mask and return the old one. */
        {
            mode_t mode = get_C_ulong(taskData, DEREFWORDHANDLE(args));
            return Make_unsigned(taskData, umask(mode));
        }

    case 51: /* Create a hard link. */
        {
            char *old = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            char *newp = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(1));
            int err, res;
            res = link(old, newp);
            err = errno; /* Save the error result in case free changes it. */
            free(old);
            free(newp);
            if (res < 0) raise_syscall(taskData, "link failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 52: /* Create a directory.  There is an OS-independent version in
            basicio which uses a default creation mode. */
        {
            char *name = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            mode_t mode = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int err, res;
            res = mkdir(name, mode);
            err = errno; /* Save the error result in case free changes it. */
            free(name);
            if (res < 0) raise_syscall(taskData, "mkdir failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 53: /* Create a fifo. */
        {
            char *name = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            mode_t mode = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int err, res;
            res = mkfifo(name, mode);
            err = errno; /* Save the error result in case free changes it. */
            free(name);
            if (res < 0) raise_syscall(taskData, "mkfifo failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 54: /* Create a symbolic link. */
        {
            char *old = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            char *newp = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(1));
            int err, res;
            res = symlink(old, newp);
            err = errno; /* Save the error result in case free changes it. */
            free(old);
            free(newp);
            if (res < 0) raise_syscall(taskData, "link failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 55: /* Get information about a file. */
        {
            struct stat buf;
            int res, err;
            char *name = Poly_string_to_C_alloc(DEREFWORD(args));
            res = stat(name, &buf);
            err = errno;
            free(name);
            if (res < 0) raise_syscall(taskData, "stat failed", err);
            return getStatInfo(taskData, &buf);
        }

    case 56: /* Get information about a symbolic link. */
        {
            struct stat buf;
            int res, err;
            char *name = Poly_string_to_C_alloc(DEREFWORD(args));
            res = lstat(name, &buf);
            err = errno;
            free(name);
            if (res < 0) raise_syscall(taskData, "lstat failed", err);
            return getStatInfo(taskData, &buf);
        }

    case 57: /* Get information about an open file. */
        {
            struct stat buf;
            PIOSTRUCT strm = get_stream(args->WordP());
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (fstat(strm->device.ioDesc, &buf) < 0)
                raise_syscall(taskData, "fstat failed", errno);
            return getStatInfo(taskData, &buf);
        }

    case 58: /* Test access rights to a file. */
        {
            char *name = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            int amode = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int res;
            res = access(name, amode);
            free(name);
            /* Return false if error, true if not.  It's not clear that
               this is correct since there are several reasons why we
               might get -1 as the result. */
            return Make_arbitrary_precision(taskData, res < 0 ? 0 : 1);
        }

    case 59: /* Change access rights. */
        {
            char *name = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            mode_t mode = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int err, res;
            res = chmod(name, mode);
            err = errno; /* Save the error result in case free changes it. */
            free(name);
            if (res < 0) raise_syscall(taskData, "chmod failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 60: /* Change access rights on open file. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            mode_t mode = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (fchmod(strm->device.ioDesc, mode) < 0)
                raise_syscall(taskData, "fchmod failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 61: /* Change owner and group. */
        {
            char *name = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            uid_t uid = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            gid_t gid = get_C_long(taskData, DEREFHANDLE(args)->Get(2));
            int err, res;
            res = chown(name, uid, gid);
            err = errno; /* Save the error result in case free changes it. */
            free(name);
            if (res < 0) raise_syscall(taskData, "chown failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 62: /* Change owner and group on open file. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            uid_t uid = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            gid_t gid = get_C_long(taskData, DEREFHANDLE(args)->Get(2));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (fchown(strm->device.ioDesc, uid, gid) < 0)
                raise_syscall(taskData, "fchown failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 63: /* Set access and modification times.  We use utimes rather than utime
            since it allows us to be more accurate.  There's a similar function
            in basicio which sets both the access and modification times to the
            same time. */
        {
            char *name = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            Handle hAccess = SAVE(DEREFHANDLE(args)->Get(1));
            Handle hMod = SAVE(DEREFHANDLE(args)->Get(2));
            struct timeval times[2];
            /* We have a value in microseconds.  We need to split
               it into seconds and microseconds.  N.B. The arguments to
               div_longc and rem_longc are in reverse order. */
            Handle hMillion = Make_arbitrary_precision(taskData, 1000000);
            unsigned secsAccess =
                get_C_ulong(taskData, DEREFWORDHANDLE(div_longc(taskData, hMillion, hAccess)));
            unsigned usecsAccess =
                get_C_ulong(taskData, DEREFWORDHANDLE(rem_longc(taskData, hMillion, hAccess)));
            unsigned secsMod =
                get_C_ulong(taskData, DEREFWORDHANDLE(div_longc(taskData, hMillion, hMod)));
            unsigned usecsMod =
                get_C_ulong(taskData, DEREFWORDHANDLE(rem_longc(taskData, hMillion, hMod)));
            int err, res;
            times[0].tv_sec = secsAccess;
            times[0].tv_usec = usecsAccess;
            times[1].tv_sec = secsMod;
            times[1].tv_usec = usecsMod;
            res = utimes(name, times);
            err = errno; /* Save the error result in case free changes it. */
            free(name);
            if (res < 0) raise_syscall(taskData, "utimes failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 64: /* Set access and modification times to the current time.  This could be
            defined in terms of the previous call and Time.now but it could result
            in an error due to rounding.  This is probably safer. */
        {
            char *name = Poly_string_to_C_alloc(DEREFWORD(args));
            int err, res;
            res = utimes(name, 0);
            err = errno; /* Save the error result in case free changes it. */
            free(name);
            if (res < 0) raise_syscall(taskData, "utimes failed", err);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 65: /* Truncate an open file. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int size = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (ftruncate(strm->device.ioDesc, size) < 0)
                raise_syscall(taskData, "ftruncate failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 66: /* Get the configured limits for a file. */
        {
            /* Look up the variable. May raise an exception. */
            int nvar = findPathVar(taskData, DEREFHANDLE(args)->Get(1));
            char *name = Poly_string_to_C_alloc(DEREFHANDLE(args)->Get(0));
            int err, res;
            /* Set errno to zero.  If there is no limit pathconf returns -1 but
               does not change errno.  */
            errno = 0;
            res = pathconf(name, nvar);
            err = errno; /* Save the error result in case free changes it. */
            free(name);
            /* We return -1 as a valid result indicating no limit. */
            if (res < 0 && err != 0) raise_syscall(taskData, "pathconf failed", err);
            return Make_arbitrary_precision(taskData, res);
        }

    case 67: /* Get the configured limits for an open file. */
        {
            /* Look up the variable. May raise an exception. */
            int nvar = findPathVar(taskData, DEREFHANDLE(args)->Get(1));
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int res;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            errno = 0; /* Unchanged if there is no limit. */
            res = fpathconf(strm->device.ioDesc, nvar);
            if (res < 0 && errno != 0) raise_syscall(taskData, "fpathconf failed", errno);
            return Make_arbitrary_precision(taskData, res);
        }

        /* Password and group entries. */
    case 100: /* Get Password entry by name. */
        {
            char pwName[200];
            int length;
            struct passwd *pw;
            length = Poly_string_to_C(DEREFWORD(args), pwName, 200);
            if (length > 200)
                raise_syscall(taskData, "Password name too long", ENAMETOOLONG);
            pw = getpwnam(pwName);
            if (pw == NULL)
                raise_syscall(taskData, "Password entry not found", ENOENT);
            return makePasswordEntry(taskData, pw);
        }

    case 101: /* Get password entry by uid. */
        {
            int uid = get_C_long(taskData, DEREFWORDHANDLE(args));
            struct passwd *pw = getpwuid(uid);
            if (pw == NULL)
                raise_syscall(taskData, "Password entry not found", ENOENT);
            return makePasswordEntry(taskData, pw);
        }

    case 102: /* Get group entry by name. */
        {
            struct group *grp;
            char grpName[200];
            int length;
            length = Poly_string_to_C(DEREFWORD(args), grpName, 200);
            if (length > 200)
                raise_syscall(taskData, "Group name too long", ENAMETOOLONG);
            grp = getgrnam(grpName);
            if (grp == NULL)
                raise_syscall(taskData, "Group entry not found", ENOENT);
            return makeGroupEntry(taskData, grp);
        }
        
    case 103: /* Get group entry by gid. */
        {
            int gid = get_C_long(taskData, DEREFWORDHANDLE(args));
            struct group *grp = getgrgid(gid);
            if (grp == NULL)
                raise_syscall(taskData, "Group entry not found", ENOENT);
            return makeGroupEntry(taskData, grp);
        }

        /* IO Entries. */
    case 110: /* Create a pipe. */
        {
            int filedes[2];
            Handle strRead = make_stream_entry(taskData);
            Handle strWrite = make_stream_entry(taskData);
            Handle result;
            PIOSTRUCT instrm, outstrm;
            if (pipe(filedes) < 0) raise_syscall(taskData, "pipe failed", errno);
            instrm = &basic_io_vector[STREAMID(strRead)];
            outstrm = &basic_io_vector[STREAMID(strWrite)];
            instrm->device.ioDesc = filedes[0];
            instrm->ioBits = IO_BIT_OPEN | IO_BIT_READ;
            outstrm->device.ioDesc = filedes[1];
            outstrm->ioBits = IO_BIT_OPEN | IO_BIT_WRITE;
            result = ALLOC(2);
            DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(strRead));
            DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(strWrite));
            return result;
        }

    case 111: /* Duplicate a file descriptor. */
        {
            PIOSTRUCT str = get_stream(args->WordP());
            PIOSTRUCT newstr;
            Handle strToken = make_stream_entry(taskData);
            int fd;
            if (str == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            fd = dup(str->device.ioDesc);
            if (fd < 0) raise_syscall(taskData, "dup failed", errno);
            newstr = &basic_io_vector[STREAMID(strToken)];
            newstr->device.ioDesc = fd;
            /* I'm assuming that we're not going to put any other
               status information in the bits. */
            newstr->ioBits = str->ioBits;
            return strToken;
        }

    case 112: /* Duplicate a file descriptor to a given entry. */
        {
            PIOSTRUCT old = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            PIOSTRUCT newp = get_stream(DEREFHANDLE(args)->Get(1).AsObjPtr());
            /* The "newp" entry must be an open entry in our io table.
               It may, though, be an entry added through wordToFD
               (basicio call 31) which may not refer to a valid
               descriptor. */
            if (old == NULL || newp == NULL)
                raise_syscall(taskData, "Stream is closed", EBADF);
            if (dup2(old->device.ioDesc, newp->device.ioDesc) < 0)
                raise_syscall(taskData, "dup2 failed", errno);
            newp->ioBits = old->ioBits;
            return Make_arbitrary_precision(taskData, 0);
        }

    case 113: /* Duplicate a file descriptor to an entry equal to or greater
             than the given value. */
        {
            PIOSTRUCT old = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            PIOSTRUCT base = get_stream(DEREFHANDLE(args)->Get(1).AsObjPtr());
            int newfd;
            Handle strToken = make_stream_entry(taskData);
            PIOSTRUCT newstr;
            /* The "base" entry must be an open entry in our io table.
               It may, though, be an entry added through wordToFD
               (basicio call 31) which may not refer to a valid
               descriptor. */
            if (old == NULL || base == NULL)
                raise_syscall(taskData, "Stream is closed", EBADF);
            newfd = fcntl(old->device.ioDesc, F_DUPFD, base->device.ioDesc);
            if (newfd < 0) raise_syscall(taskData, "dup2 failed", errno);
            newstr = &basic_io_vector[STREAMID(strToken)];
            newstr->device.ioDesc = newfd;
            /* I'm assuming that we're not going to put any other
               status information in the bits. */
            newstr->ioBits = old->ioBits;
            return strToken;
        }

    case 114: /* Get the file descriptor flags. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            int res;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            res = fcntl(strm->device.ioDesc, F_GETFD);
            if (res < 0) raise_syscall(taskData, "fcntl failed", errno);
            return Make_arbitrary_precision(taskData, res);
        }

    case 115: /* Set the file descriptor flags. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int flags = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (fcntl(strm->device.ioDesc, F_SETFD, flags) < 0)
                raise_syscall(taskData, "fcntl failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 116: /* Get the file status and access flags. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            int res;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            res = fcntl(strm->device.ioDesc, F_GETFL);
            if (res < 0) raise_syscall(taskData, "fcntl failed", errno);
            return Make_arbitrary_precision(taskData, res);
        }

    case 117: /* Set the file status and access flags. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int flags = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (fcntl(strm->device.ioDesc, F_SETFL, flags) < 0)
                raise_syscall(taskData, "fcntl failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 118: /* Seek to a position on the stream. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            long position = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int whence = get_C_long(taskData, DEREFHANDLE(args)->Get(2));
            long newpos;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            newpos = lseek(strm->device.ioDesc, position, whence);
            if (newpos < 0) raise_syscall(taskData, "lseek failed", errno);
            return Make_arbitrary_precision(taskData, newpos);
        }

    case 119: /* Synchronise file contents. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (fsync(strm->device.ioDesc) < 0) raise_syscall(taskData, "fsync failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 120: /* get lock */
        return lockCommand(taskData, F_GETLK, args);

    case 121: /* set lock */
        return lockCommand(taskData, F_SETLK, args);

    case 122: /* wait for lock */
        /* TODO: This may well block the whole process.  We should look at the
           result and retry if need be. */
        return lockCommand(taskData, F_SETLKW, args);

        /* TTY entries. */
    case 150: /* Get attributes. */
        return getTTYattrs(taskData, args);

    case 151: /* Set attributes. */
        return setTTYattrs(taskData, args);

    case 152: /* Send a break. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int duration = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (tcsendbreak(strm->device.ioDesc, duration) < 0)
                raise_syscall(taskData, "tcsendbreak failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }
    
    case 153: /* Wait for output to drain. */
        {
            /* TODO: This will block the process.  It really needs to
               check whether the stream has drained and run another
               process until it has. */
            PIOSTRUCT strm = get_stream(args->WordP());
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (tcdrain(strm->device.ioDesc) < 0)
                raise_syscall(taskData, "tcdrain failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }
    
    case 154: /* Flush terminal stream. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int qs = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (tcflush(strm->device.ioDesc, qs) < 0)
                raise_syscall(taskData, "tcflush failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }
    
    case 155: /* Flow control. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int action = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (tcflow(strm->device.ioDesc, action) < 0)
                raise_syscall(taskData, "tcflow failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }
    
    case 156: /* Get process group. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            pid_t pid;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            pid = tcgetpgrp(strm->device.ioDesc);
            if (pid < 0) raise_syscall(taskData, "tcgetpgrp failed", errno);
            return Make_arbitrary_precision(taskData, pid);
        }
    
    case 157: /* Set process group. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            pid_t pid = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (tcsetpgrp(strm->device.ioDesc, pid) < 0)
                raise_syscall(taskData, "tcsetpgrp failed", errno);
            return Make_arbitrary_precision(taskData, 0);
        }
    
    default:
        {
            char msg[100];
            sprintf(msg, "Unknown unix-specific function: %d", c);
            raise_exception_string(taskData, EXC_Fail, msg);
        }
    }
}

Handle waitForProcess(TaskData *taskData, Handle args)
/* Get result status of a child process. */
{
TryAgain:
    int kind = get_C_long(taskData, DEREFHANDLE(args)->Get(0));
    int pid = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
    int callFlags = get_C_long(taskData, DEREFHANDLE(args)->Get(2));
    int flags = callFlags | WNOHANG; // Add in WNOHANG so we never block.
    pid_t pres = 0;
    int status;
    switch (kind)
    {
    case 0: /* Wait for any child. */
        pres = waitpid(-1, &status, flags);
        break;
    case 1: /* Wait for specific process. */
        pres = waitpid(pid, &status, flags);
        break;
    case 2: /* Wait for any in current process group. */
        pres = waitpid(0, &status, flags);
        break;
    case 3: /* Wait for child in given process group */
        pres = waitpid(-pid, &status, flags);
        break;
    }
    if (pres < 0)
    {
        if (errno == EINTR)
            goto TryAgain;
        else
            raise_syscall(taskData, "wait failed", errno);
    }
    /* If the caller did not specify WNOHANG but there
       wasn't a child process waiting we have to block
       and come back here later. */
    if (pres == 0 && !(callFlags & WNOHANG))
        processes->BlockAndRestart(taskData, -1, false, POLY_SYS_os_specific);

    /* Construct the result tuple. */
    {
        Handle result, pidHandle, resHandle;
        pidHandle = Make_arbitrary_precision(taskData, pres);
        resHandle = Make_arbitrary_precision(taskData, status);

        result = ALLOC(2);
        DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(pidHandle));
        DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(resHandle));
        return result;
    }
}

static Handle makePasswordEntry(TaskData *taskData, struct passwd *pw)
/* Return a password entry. */
{
    Handle nameHandle, uidHandle, gidHandle, homeHandle, shellHandle, result;
    nameHandle = SAVE(C_string_to_Poly(taskData, pw->pw_name));
    uidHandle = Make_arbitrary_precision(taskData, pw->pw_uid);
    gidHandle = Make_arbitrary_precision(taskData, pw->pw_gid);
    homeHandle = SAVE(C_string_to_Poly(taskData, pw->pw_dir));
    shellHandle = SAVE(C_string_to_Poly(taskData, pw->pw_shell));
    result = ALLOC(5);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(nameHandle));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(uidHandle));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(gidHandle));
    DEREFHANDLE(result)->Set(3, DEREFWORDHANDLE(homeHandle));
    DEREFHANDLE(result)->Set(4, DEREFWORDHANDLE(shellHandle));
    return result;
}

static Handle makeGroupEntry(TaskData *taskData, struct group *grp)
{
    Handle nameHandle, gidHandle, membersHandle, result;
    int i;
    char **p;
    nameHandle = SAVE(C_string_to_Poly(taskData, grp->gr_name));
    gidHandle = Make_arbitrary_precision(taskData, grp->gr_gid);
    /* Group members. */
    for (i=0, p = grp->gr_mem; *p != NULL; p++, i++);
    membersHandle = convert_string_list(taskData, i, grp->gr_mem);
    result = ALLOC(3);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(nameHandle));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(gidHandle));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(membersHandle));
    return result;
}

/* Make a cons cell for a pair of strings. */
// Doesn't currently reset the save vec so it's only safe for a small number
// of cells.
static void makeStringPairList(TaskData *taskData, Handle &list, const char *s1, const char *s2)
{
    Handle nameHandle, valueHandle, pairHandle, next;
    /* This has to be done carefully to ensure we don't throw anything
       away if we garbage-collect and also to ensure that each object is
       fully initialised before the next object is created. */
    /* Make the strings. */
    nameHandle = SAVE(C_string_to_Poly(taskData, s1));
    valueHandle = SAVE(C_string_to_Poly(taskData, s2));
    /* Make the pair. */
    pairHandle = ALLOC(2);
    DEREFHANDLE(pairHandle)->Set(0, DEREFWORDHANDLE(nameHandle));
    DEREFHANDLE(pairHandle)->Set(1, DEREFWORDHANDLE(valueHandle));
    /* Make the cons cell. */
    next  = ALLOC(SIZEOF(ML_Cons_Cell));
    DEREFLISTHANDLE(next)->h = DEREFWORDHANDLE(pairHandle); 
    DEREFLISTHANDLE(next)->t = DEREFLISTHANDLE(list);
    list = SAVE(DEREFHANDLE(next));
}

/* Return the uname information.  */
static Handle getUname(TaskData *taskData)
{
#ifdef HAVE_SYS_UTSNAME_H
    struct utsname name;
    Handle list = SAVE(ListNull);
    if (uname(&name) < 0) raise_syscall(taskData, "uname failed", errno);
    makeStringPairList(taskData, list, "sysname", name.sysname);
    makeStringPairList(taskData, list, "nodename", name.nodename);
    makeStringPairList(taskData, list, "release", name.release);
    makeStringPairList(taskData, list, "version", name.version);
    makeStringPairList(taskData, list, "machine", name.machine);
    return list;
#else
    raise_syscall(taskData, "uname not available on this machine", errno);
#endif
}

/* Return the contents of a stat buffer. */
static Handle getStatInfo(TaskData *taskData, struct stat *buf)
{
    int kind;
    Handle result, modeHandle, kindHandle, inoHandle, devHandle, linkHandle;
    Handle uidHandle, gidHandle, sizeHandle, atimeHandle, mtimeHandle, ctimeHandle;
    /* Get the protection mode, masking off the file type info. */
    modeHandle =
        Make_unsigned(taskData, buf->st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
    if (S_ISDIR(buf->st_mode)) kind = 1;
    else if (S_ISCHR(buf->st_mode)) kind = 2;
    else if (S_ISBLK(buf->st_mode)) kind = 3;
    else if (S_ISFIFO(buf->st_mode)) kind = 4;
    else if ((buf->st_mode & S_IFMT) == S_IFLNK) kind = 5;
    else if ((buf->st_mode & S_IFMT) == S_IFSOCK) kind = 6;
    else /* Regular. */ kind = 0;
    kindHandle = Make_unsigned(taskData, kind);
    inoHandle = Make_unsigned(taskData, buf->st_ino);
    devHandle = Make_unsigned(taskData, buf->st_dev);
    linkHandle = Make_unsigned(taskData, buf->st_nlink);
    uidHandle = Make_unsigned(taskData, buf->st_uid);
    gidHandle = Make_unsigned(taskData, buf->st_gid);
    sizeHandle = Make_unsigned(taskData, buf->st_size);
    /* TODO: The only really standard time fields give the seconds part.  There
       are various extensions which would give us microseconds or nanoseconds. */
    atimeHandle = Make_arb_from_pair_scaled(taskData, buf->st_atime, 0, 1000000);
    mtimeHandle = Make_arb_from_pair_scaled(taskData, buf->st_mtime, 0, 1000000);
    ctimeHandle = Make_arb_from_pair_scaled(taskData, buf->st_ctime, 0, 1000000);
    result = ALLOC(11);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(modeHandle));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(kindHandle));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(inoHandle));
    DEREFHANDLE(result)->Set(3, DEREFWORDHANDLE(devHandle));
    DEREFHANDLE(result)->Set(4, DEREFWORDHANDLE(linkHandle));
    DEREFHANDLE(result)->Set(5, DEREFWORDHANDLE(uidHandle));
    DEREFHANDLE(result)->Set(6, DEREFWORDHANDLE(gidHandle));
    DEREFHANDLE(result)->Set(7, DEREFWORDHANDLE(sizeHandle));
    DEREFHANDLE(result)->Set(8, DEREFWORDHANDLE(atimeHandle));
    DEREFHANDLE(result)->Set(9, DEREFWORDHANDLE(mtimeHandle));
    DEREFHANDLE(result)->Set(10, DEREFWORDHANDLE(ctimeHandle));
    return result;
}



static Handle getTTYattrs(TaskData *taskData, Handle args)
{
    PIOSTRUCT strm = get_stream(args->WordP());
    struct termios tios;
    speed_t ispeed, ospeed;
    Handle ifHandle, ofHandle, cfHandle, lfHandle, ccHandle;
    Handle isHandle, osHandle, result;
    if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
    if (tcgetattr(strm->device.ioDesc, &tios) < 0)
        raise_syscall(taskData, "tcgetattr failed", errno);
    /* Extract the speed entries. */
    ospeed = cfgetospeed(&tios);
    ispeed = cfgetispeed(&tios);
    /* Set the speed entries to zero.  In Solaris, at least, the speed is
       encoded in the flags and we don't want any confusion.  The order of
       these functions is significant.  */
    cfsetospeed(&tios, B0);
    cfsetispeed(&tios, B0);
    /* Convert the values to ML representation. */
    ifHandle = Make_unsigned(taskData, tios.c_iflag);
    ofHandle = Make_unsigned(taskData, tios.c_oflag);
    cfHandle = Make_unsigned(taskData, tios.c_cflag);
    lfHandle = Make_unsigned(taskData, tios.c_lflag);
    /* The cc vector is treated as a string. */
    ccHandle = SAVE(Buffer_to_Poly(taskData, (const char *)tios.c_cc, NCCS));
    isHandle = Make_unsigned(taskData, ispeed);
    osHandle = Make_unsigned(taskData, ospeed);
    /* We can now create the result tuple. */
    result = ALLOC(7);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(ifHandle));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(ofHandle));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(cfHandle));
    DEREFHANDLE(result)->Set(3, DEREFWORDHANDLE(lfHandle));
    DEREFHANDLE(result)->Set(4, DEREFWORDHANDLE(ccHandle));
    DEREFHANDLE(result)->Set(5, DEREFWORDHANDLE(isHandle));
    DEREFHANDLE(result)->Set(6, DEREFWORDHANDLE(osHandle));
    return result;
}

/* Assemble the tios structure from the arguments and set the TTY attributes. */
static Handle setTTYattrs(TaskData *taskData, Handle args)
{
    PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
    int actions = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
    struct termios tios;
    speed_t ispeed, ospeed;
    if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
    /* Make sure anything unset is zero.  It might be better to call
       tcgetattr instead. */
    memset(&tios, 0, sizeof(tios));
    tios.c_iflag = get_C_ulong(taskData, DEREFHANDLE(args)->Get(2));
    tios.c_oflag = get_C_ulong(taskData, DEREFHANDLE(args)->Get(3));
    tios.c_cflag = get_C_ulong(taskData, DEREFHANDLE(args)->Get(4));
    tios.c_lflag = get_C_ulong(taskData, DEREFHANDLE(args)->Get(5));
    /* The cc vector should be a string of exactly NCCS characters.  It
       may well contain nulls so we can't use Poly_string_to_C to copy it. */
    PolyWord ccv = DEREFHANDLE(args)->Get(6);
    if (ccv.IsTagged()) // Just to check.
        raise_syscall(taskData, "Incorrect cc vector", EINVAL);
    PolyStringObject * ccvs = (PolyStringObject *)ccv.AsObjPtr();
    if (ccvs->length != NCCS) // Just to check. */
        raise_syscall(taskData, "Incorrect cc vector", EINVAL);
    memcpy(tios.c_cc, ccvs->chars, NCCS);
    ispeed = get_C_ulong(taskData, DEREFHANDLE(args)->Get(7));
    ospeed = get_C_ulong(taskData, DEREFHANDLE(args)->Get(8));
    if (cfsetispeed(&tios, ispeed) < 0)
        raise_syscall(taskData, "cfsetispeed failed", errno);
    if (cfsetospeed(&tios, ospeed) < 0)
        raise_syscall(taskData, "cfsetospeed failed", errno);
    /* Now it's all set we can call tcsetattr to do the work. */
    if (tcsetattr(strm->device.ioDesc, actions, &tios) < 0)
        raise_syscall(taskData, "tcsetattr failed", errno);
    return Make_arbitrary_precision(taskData, 0);
}

/* Lock/unlock/test file locks.  Returns the, possibly modified, argument structure. */
static Handle lockCommand(TaskData *taskData, int cmd, Handle args)
{
    PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
    struct flock lock;
    Handle result, typeHandle, whenceHandle, startHandle, lenHandle, pidHandle;
    memset(&lock, 0, sizeof(lock)); /* Make sure unused fields are zero. */
    if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
    lock.l_type = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
    lock.l_whence = get_C_long(taskData, DEREFHANDLE(args)->Get(2));
    lock.l_start = get_C_long(taskData, DEREFHANDLE(args)->Get(3));
    lock.l_len = get_C_long(taskData, DEREFHANDLE(args)->Get(4));
    lock.l_pid = get_C_long(taskData, DEREFHANDLE(args)->Get(5));
    if (fcntl(strm->device.ioDesc, cmd, &lock) < 0) 
        raise_syscall(taskData, "fcntl failed", errno);
    /* Construct the result. */
    typeHandle = Make_arbitrary_precision(taskData, lock.l_type);
    whenceHandle = Make_arbitrary_precision(taskData, lock.l_whence);
    startHandle = Make_arbitrary_precision(taskData, lock.l_start);
    lenHandle = Make_arbitrary_precision(taskData, lock.l_len);
    pidHandle = Make_arbitrary_precision(taskData, lock.l_pid);
    result = ALLOC(5);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(typeHandle));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(whenceHandle));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(startHandle));
    DEREFHANDLE(result)->Set(3, DEREFWORDHANDLE(lenHandle));
    DEREFHANDLE(result)->Set(4, DEREFWORDHANDLE(pidHandle));
    return result;
}


/* This table maps string arguments for sysconf into the corresponding constants. */
/* These are highly OS dependent.  It has been configured on Solaris 2.8, Linux Redhat 5.2
   and FreeBSD 3.4. */
static struct {
    const char *saName;
    int saVal;
} sysArgTable[] =
{
    { "_SC_ARG_MAX",        _SC_ARG_MAX },
    { "_SC_CHILD_MAX",      _SC_CHILD_MAX },
    { "_SC_CLK_TCK",        _SC_CLK_TCK },
    { "_SC_NGROUPS_MAX",        _SC_NGROUPS_MAX },
    { "_SC_OPEN_MAX",       _SC_OPEN_MAX },
    { "_SC_JOB_CONTROL",        _SC_JOB_CONTROL },
    { "_SC_SAVED_IDS",      _SC_SAVED_IDS },
    { "_SC_VERSION",        _SC_VERSION },
#ifdef _SC_PASS_MAX
    { "_SC_PASS_MAX",       _SC_PASS_MAX },
#endif
#ifdef _SC_LOGNAME_MAX
    { "_SC_LOGNAME_MAX",        _SC_LOGNAME_MAX },
#endif
#ifdef _SC_PAGESIZE
    { "_SC_PAGESIZE",       _SC_PAGESIZE },
#endif
#ifdef _SC_XOPEN_VERSION
    { "_SC_XOPEN_VERSION",      _SC_XOPEN_VERSION },
#endif
#ifdef _SC_NPROCESSORS_CONF
    { "_SC_NPROCESSORS_CONF",   _SC_NPROCESSORS_CONF },
#endif
#ifdef _SC_NPROCESSORS_ONLN
    { "_SC_NPROCESSORS_ONLN",   _SC_NPROCESSORS_ONLN },
#endif
#ifdef _SC_STREAM_MAX
    { "_SC_STREAM_MAX",     _SC_STREAM_MAX },
#endif
#ifdef _SC_TZNAME_MAX
    { "_SC_TZNAME_MAX",     _SC_TZNAME_MAX },
#endif
#ifdef _SC_AIO_LISTIO_MAX
    { "_SC_AIO_LISTIO_MAX",     _SC_AIO_LISTIO_MAX },
#endif
#ifdef _SC_AIO_MAX
    { "_SC_AIO_MAX",        _SC_AIO_MAX },
#endif
#ifdef _SC_AIO_PRIO_DELTA_MAX
    { "_SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX },
#endif
#ifdef _SC_ASYNCHRONOUS_IO
    { "_SC_ASYNCHRONOUS_IO",    _SC_ASYNCHRONOUS_IO },
#endif
#ifdef _SC_DELAYTIMER_MAX
    { "_SC_DELAYTIMER_MAX",     _SC_DELAYTIMER_MAX },
#endif
#ifdef _SC_FSYNC
    { "_SC_FSYNC",          _SC_FSYNC },
#endif
#ifdef _SC_MAPPED_FILES
    { "_SC_MAPPED_FILES",       _SC_MAPPED_FILES },
#endif
#ifdef _SC_MEMLOCK
    { "_SC_MEMLOCK",        _SC_MEMLOCK },
#endif
#ifdef _SC_MEMLOCK_RANGE
    { "_SC_MEMLOCK_RANGE",      _SC_MEMLOCK_RANGE },
#endif
#ifdef _SC_MEMORY_PROTECTION
    { "_SC_MEMORY_PROTECTION",  _SC_MEMORY_PROTECTION },
#endif
#ifdef _SC_MESSAGE_PASSING
    { "_SC_MESSAGE_PASSING",    _SC_MESSAGE_PASSING },
#endif
#ifdef _SC_MQ_OPEN_MAX
    { "_SC_MQ_OPEN_MAX",        _SC_MQ_OPEN_MAX },
#endif
#ifdef _SC_MQ_PRIO_MAX
    { "_SC_MQ_PRIO_MAX",        _SC_MQ_PRIO_MAX },
#endif
#ifdef _SC_PRIORITIZED_IO
    { "_SC_PRIORITIZED_IO",     _SC_PRIORITIZED_IO },
#endif
#ifdef _SC_PRIORITY_SCHEDULING
    { "_SC_PRIORITY_SCHEDULING",    _SC_PRIORITY_SCHEDULING },
#endif
#ifdef _SC_REALTIME_SIGNALS
    { "_SC_REALTIME_SIGNALS",   _SC_REALTIME_SIGNALS },
#endif
#ifdef _SC_RTSIG_MAX
    { "_SC_RTSIG_MAX",      _SC_RTSIG_MAX },
#endif
#ifdef _SC_SEMAPHORES
    { "_SC_SEMAPHORES",     _SC_SEMAPHORES },
#endif
#ifdef _SC_SEM_NSEMS_MAX
    { "_SC_SEM_NSEMS_MAX",      _SC_SEM_NSEMS_MAX },
#endif
#ifdef _SC_SEM_VALUE_MAX
    { "_SC_SEM_VALUE_MAX",      _SC_SEM_VALUE_MAX },
#endif
#ifdef _SC_SHARED_MEMORY_OBJECTS
    { "_SC_SHARED_MEMORY_OBJECTS",  _SC_SHARED_MEMORY_OBJECTS },
#endif
#ifdef _SC_SIGQUEUE_MAX
    { "_SC_SIGQUEUE_MAX",       _SC_SIGQUEUE_MAX },
#endif
#ifdef _SC_SIGRT_MIN
    { "_SC_SIGRT_MIN",      _SC_SIGRT_MIN },
#endif
#ifdef _SC_SIGRT_MAX
    { "_SC_SIGRT_MAX",      _SC_SIGRT_MAX },
#endif
#ifdef _SC_SYNCHRONIZED_IO
    { "_SC_SYNCHRONIZED_IO",    _SC_SYNCHRONIZED_IO },
#endif
#ifdef _SC_TIMERS
    { "_SC_TIMERS",         _SC_TIMERS },
#endif
#ifdef _SC_TIMER_MAX
    { "_SC_TIMER_MAX",      _SC_TIMER_MAX },
#endif
#ifdef _SC_2_C_BIND
    { "_SC_2_C_BIND",       _SC_2_C_BIND },
#endif
#ifdef _SC_2_C_DEV
    { "_SC_2_C_DEV",        _SC_2_C_DEV },
#endif
#ifdef _SC_2_C_VERSION
    { "_SC_2_C_VERSION",        _SC_2_C_VERSION },
#endif
#ifdef _SC_2_FORT_DEV
    { "_SC_2_FORT_DEV",     _SC_2_FORT_DEV },
#endif
#ifdef _SC_2_FORT_RUN
    { "_SC_2_FORT_RUN",     _SC_2_FORT_RUN },
#endif
#ifdef _SC_2_LOCALEDEF
    { "_SC_2_LOCALEDEF",        _SC_2_LOCALEDEF },
#endif
#ifdef _SC_2_SW_DEV
    { "_SC_2_SW_DEV",       _SC_2_SW_DEV },
#endif
#ifdef _SC_2_UPE
    { "_SC_2_UPE",          _SC_2_UPE },
#endif
#ifdef _SC_2_VERSION
    { "_SC_2_VERSION",      _SC_2_VERSION },
#endif
#ifdef _SC_BC_BASE_MAX
    { "_SC_BC_BASE_MAX",        _SC_BC_BASE_MAX },
#endif
#ifdef _SC_BC_DIM_MAX
    { "_SC_BC_DIM_MAX",     _SC_BC_DIM_MAX },
#endif
#ifdef _SC_BC_SCALE_MAX
    { "_SC_BC_SCALE_MAX",       _SC_BC_SCALE_MAX },
#endif
#ifdef _SC_BC_STRING_MAX
    { "_SC_BC_STRING_MAX",      _SC_BC_STRING_MAX },
#endif
#ifdef _SC_COLL_WEIGHTS_MAX
    { "_SC_COLL_WEIGHTS_MAX",   _SC_COLL_WEIGHTS_MAX },
#endif
#ifdef _SC_EXPR_NEST_MAX
    { "_SC_EXPR_NEST_MAX",      _SC_EXPR_NEST_MAX },
#endif
#ifdef _SC_LINE_MAX
    { "_SC_LINE_MAX",       _SC_LINE_MAX },
#endif
#ifdef _SC_RE_DUP_MAX
   { "_SC_RE_DUP_MAX",     _SC_RE_DUP_MAX },
#endif
#ifdef _SC_XOPEN_CRYPT
    { "_SC_XOPEN_CRYPT",        _SC_XOPEN_CRYPT },
#endif
#ifdef _SC_XOPEN_ENH_I18N
    { "_SC_XOPEN_ENH_I18N",     _SC_XOPEN_ENH_I18N },
#endif
#ifdef _SC_XOPEN_SHM
    { "_SC_XOPEN_SHM",      _SC_XOPEN_SHM },
#endif
#ifdef _SC_2_CHAR_TERM
    { "_SC_2_CHAR_TERM",        _SC_2_CHAR_TERM },
#endif
#ifdef _SC_XOPEN_XCU_VERSION
    { "_SC_XOPEN_XCU_VERSION",  _SC_XOPEN_XCU_VERSION },
#endif
#ifdef _SC_ATEXIT_MAX
    { "_SC_ATEXIT_MAX",     _SC_ATEXIT_MAX },
#endif
#ifdef _SC_IOV_MAX
    { "_SC_IOV_MAX",        _SC_IOV_MAX },
#endif
#ifdef _SC_XOPEN_UNIX
    { "_SC_XOPEN_UNIX",     _SC_XOPEN_UNIX },
#endif
#ifdef _SC_PAGE_SIZE
    { "_SC_PAGE_SIZE",      _SC_PAGE_SIZE },
#endif
#ifdef _SC_T_IOV_MAX
    { "_SC_T_IOV_MAX",      _SC_T_IOV_MAX },
#endif
#ifdef _SC_PHYS_PAGES
    { "_SC_PHYS_PAGES",     _SC_PHYS_PAGES },
#endif
#ifdef _SC_AVPHYS_PAGES
    { "_SC_AVPHYS_PAGES",       _SC_AVPHYS_PAGES },
#endif
#ifdef _SC_COHER_BLKSZ
    { "_SC_COHER_BLKSZ",        _SC_COHER_BLKSZ },
#endif
#ifdef _SC_SPLIT_CACHE
    { "_SC_SPLIT_CACHE",        _SC_SPLIT_CACHE },
#endif
#ifdef _SC_ICACHE_SZ
    { "_SC_ICACHE_SZ",      _SC_ICACHE_SZ },
#endif
#ifdef _SC_DCACHE_SZ
    { "_SC_DCACHE_SZ",      _SC_DCACHE_SZ },
#endif
#ifdef _SC_ICACHE_LINESZ
    { "_SC_ICACHE_LINESZ",      _SC_ICACHE_LINESZ },
#endif
#ifdef _SC_DCACHE_LINESZ
    { "_SC_DCACHE_LINESZ",      _SC_DCACHE_LINESZ },
#endif
#ifdef _SC_ICACHE_BLKSZ
    { "_SC_ICACHE_BLKSZ",       _SC_ICACHE_BLKSZ },
#endif
#ifdef _SC_DCACHE_BLKSZ
    { "_SC_DCACHE_BLKSZ",       _SC_DCACHE_BLKSZ },
#endif
#ifdef _SC_DCACHE_TBLKSZ
    { "_SC_DCACHE_TBLKSZ",      _SC_DCACHE_TBLKSZ },
#endif
#ifdef _SC_ICACHE_ASSOC
    { "_SC_ICACHE_ASSOC",       _SC_ICACHE_ASSOC },
#endif
#ifdef _SC_DCACHE_ASSOC
    { "_SC_DCACHE_ASSOC",       _SC_DCACHE_ASSOC },
#endif
#ifdef _SC_MAXPID
    { "_SC_MAXPID",         _SC_MAXPID },
#endif
#ifdef _SC_STACK_PROT
    { "_SC_STACK_PROT",     _SC_STACK_PROT },
#endif
#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
    { "_SC_THREAD_DESTRUCTOR_ITERATIONS",   _SC_THREAD_DESTRUCTOR_ITERATIONS },
#endif
#ifdef _SC_GETGR_R_SIZE_MAX
    { "_SC_GETGR_R_SIZE_MAX",   _SC_GETGR_R_SIZE_MAX },
#endif
#ifdef _SC_GETPW_R_SIZE_MAX
    { "_SC_GETPW_R_SIZE_MAX",   _SC_GETPW_R_SIZE_MAX },
#endif
#ifdef _SC_LOGIN_NAME_MAX
    { "_SC_LOGIN_NAME_MAX",     _SC_LOGIN_NAME_MAX },
#endif
#ifdef _SC_THREAD_KEYS_MAX
    { "_SC_THREAD_KEYS_MAX",    _SC_THREAD_KEYS_MAX },
#endif
#ifdef _SC_THREAD_STACK_MI
    { "_SC_THREAD_STACK_MIN",   _SC_THREAD_STACK_MIN },
#endif
#ifdef _SC_THREAD_THREADS_MAX
    { "_SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX },
#endif
#ifdef _SC_THREAD_ATTR_STACKADDR
    { "_SC_THREAD_ATTR_STACKADDR",  _SC_THREAD_ATTR_STACKADDR },
#endif
#ifdef _SC_THREAD_ATTR_STACKSIZE
    { "_SC_THREAD_ATTR_STACKSIZE",  _SC_THREAD_ATTR_STACKSIZE },
#endif
#ifdef _SC_THREAD_PRIORITY_SCHEDULING
    { "_SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING },
#endif
#ifdef _SC_THREAD_PRIO_INHERIT
    { "_SC_THREAD_PRIO_INHERIT",    _SC_THREAD_PRIO_INHERIT },
#endif
#ifdef _SC_THREAD_PRIO_PROTECT
    { "_SC_THREAD_PRIO_PROTECT",    _SC_THREAD_PRIO_PROTECT },
#endif
#ifdef _SC_THREAD_PROCESS_SHARED
    { "_SC_THREAD_PROCESS_SHARED",  _SC_THREAD_PROCESS_SHARED },
#endif
#ifdef _SC_XOPEN_LEGACY
    { "_SC_XOPEN_LEGACY",       _SC_XOPEN_LEGACY },
#endif
#ifdef _SC_XOPEN_REALTIME
    { "_SC_XOPEN_REALTIME",     _SC_XOPEN_REALTIME },
#endif
#ifdef _SC_XOPEN_REALTIME_THREADS
    { "_SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS },
#endif
#ifdef _SC_XBS5_ILP32_OFF32
    { "_SC_XBS5_ILP32_OFF32",   _SC_XBS5_ILP32_OFF32 },
#endif
#ifdef _SC_XBS5_ILP32_OFFBIG
    { "_SC_XBS5_ILP32_OFFBIG",  _SC_XBS5_ILP32_OFFBIG },
#endif
#ifdef _SC_XBS5_LP64_OFF64
    { "_SC_XBS5_LP64_OFF64",    _SC_XBS5_LP64_OFF64 },
#endif
#ifdef _SC_XBS5_LPBIG_OFFBIG
    { "_SC_XBS5_LPBIG_OFFBIG",  _SC_XBS5_LPBIG_OFFBIG },
#endif
#ifdef _SC_EQUIV_CLASS_MAX
    { "_SC_EQUIV_CLASS_MAX",    _SC_EQUIV_CLASS_MAX },
#endif
#ifdef _SC_CHARCLASS_NAME_MAX
    { "_SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX },
#endif
#ifdef _SC_PII
    { "_SC_PII",            _SC_PII },
#endif
#ifdef _SC_PII_XTI
    { "_SC_PII_XTI",        _SC_PII_XTI },
#endif
#ifdef _SC_PII_SOCKET
    { "_SC_PII_SOCKET",     _SC_PII_SOCKET },
#endif
#ifdef _SC_PII_INTERNET
    { "_SC_PII_INTERNET",       _SC_PII_INTERNET },
#endif
#ifdef _SC_PII_OSI
    { "_SC_PII_OSI",        _SC_PII_OSI },
#endif
#ifdef _SC_POLL
    { "_SC_POLL",           _SC_POLL },
#endif
#ifdef _SC_SELECT
    { "_SC_SELECT",         _SC_SELECT },
#endif
#ifdef _SC_UIO_MAXIOV
    { "_SC_UIO_MAXIOV",     _SC_UIO_MAXIOV },
#endif
#ifdef _SC_PII_INTERNET_STREAM
    { "_SC_PII_INTERNET_STREAM",    _SC_PII_INTERNET_STREAM },
#endif
#ifdef _SC_PII_INTERNET_DGRAM
    { "_SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM },
#endif
#ifdef _SC_PII_OSI_COTS
    { "_SC_PII_OSI_COTS",       _SC_PII_OSI_COTS },
#endif
#ifdef _SC_PII_OSI_CLTS
    { "_SC_PII_OSI_CLTS",       _SC_PII_OSI_CLTS },
#endif
#ifdef _SC_PII_OSI_M
    { "_SC_PII_OSI_M",      _SC_PII_OSI_M },
#endif
#ifdef _SC_T_IOV_MAX
    { "_SC_T_IOV_MAX",      _SC_T_IOV_MAX },
#endif
#ifdef _SC_THREADS
    { "_SC_THREADS",        _SC_THREADS },
#endif
#ifdef _SC_THREAD_SAFE_FUNCTIONS
    { "_SC_THREAD_SAFE_FUNCTIONS",  _SC_THREAD_SAFE_FUNCTIONS },
#endif
#ifdef _SC_TTY_NAME_MAX
    { "_SC_TTY_NAME_MAX",       _SC_TTY_NAME_MAX },
#endif
#ifdef _SC_XOPEN_XPG2
    { "_SC_XOPEN_XPG2",     _SC_XOPEN_XPG2 },
#endif
#ifdef _SC_XOPEN_XPG3
    { "_SC_XOPEN_XPG3",     _SC_XOPEN_XPG3 },
#endif
#ifdef _SC_XOPEN_XPG4
    { "_SC_XOPEN_XPG4",     _SC_XOPEN_XPG4 },
#endif
#ifdef _SC_CHAR_BIT
    { "_SC_CHAR_BIT",       _SC_CHAR_BIT },
#endif
#ifdef _SC_CHAR_MAX
    { "_SC_CHAR_MAX",       _SC_CHAR_MAX },
#endif
#ifdef _SC_CHAR_MIN
    { "_SC_CHAR_MIN",       _SC_CHAR_MIN },
#endif
#ifdef _SC_INT_MAX
    { "_SC_INT_MAX",        _SC_INT_MAX },
#endif
#ifdef _SC_INT_MIN
    { "_SC_INT_MIN",        _SC_INT_MIN },
#endif
#ifdef _SC_LONG_BIT
    { "_SC_LONG_BIT",       _SC_LONG_BIT },
#endif
#ifdef _SC_WORD_BIT
    { "_SC_WORD_BIT",       _SC_WORD_BIT },
#endif
#ifdef _SC_MB_LEN_MAX
    { "_SC_MB_LEN_MAX",     _SC_MB_LEN_MAX },
#endif
#ifdef _SC_NZERO
    { "_SC_NZERO",          _SC_NZERO },
#endif
#ifdef _SC_SSIZE_MAX
    { "_SC_SSIZE_MAX",      _SC_SSIZE_MAX },
#endif
#ifdef _SC_SCHAR_MAX
    { "_SC_SCHAR_MAX",      _SC_SCHAR_MAX },
#endif
#ifdef _SC_SCHAR_MIN
    { "_SC_SCHAR_MIN",      _SC_SCHAR_MIN },
#endif
#ifdef _SC_SHRT_MAX
    { "_SC_SHRT_MAX",       _SC_SHRT_MAX },
#endif
#ifdef _SC_SHRT_MIN
    { "_SC_SHRT_MIN",       _SC_SHRT_MIN },
#endif
#ifdef _SC_UCHAR_MAX
    { "_SC_UCHAR_MAX",      _SC_UCHAR_MAX },
#endif
#ifdef _SC_UINT_MAX
    { "_SC_UINT_MAX",       _SC_UINT_MAX },
#endif
#ifdef _SC_ULONG_MAX
    { "_SC_ULONG_MAX",      _SC_ULONG_MAX },
#endif
#ifdef _SC_USHRT_MAX
    { "_SC_USHRT_MAX",      _SC_USHRT_MAX },
#endif
#ifdef _SC_NL_ARGMAX
    { "_SC_NL_ARGMAX",      _SC_NL_ARGMAX },
#endif
#ifdef _SC_NL_LANGMAX
    { "_SC_NL_LANGMAX",     _SC_NL_LANGMAX },
#endif
#ifdef _SC_NL_MSGMAX
    { "_SC_NL_MSGMAX",      _SC_NL_MSGMAX },
#endif
#ifdef _SC_NL_NMAX
    { "_SC_NL_NMAX",        _SC_NL_NMAX },
#endif
#ifdef _SC_NL_SETMAX
    { "_SC_NL_SETMAX",      _SC_NL_SETMAX },
#endif
};

static Handle getSysConf(TaskData *taskData, Handle args)
{
    char argName[200];
    int length;
    unsigned i;
    long res;
    length = Poly_string_to_C(DEREFWORD(args), argName, 200);
    if (length > 200) raise_syscall(taskData, "Argument name too long", ENAMETOOLONG);

    for (i = 0; i < sizeof(sysArgTable)/sizeof(sysArgTable[0]); i++) {
        if (strcmp(argName, sysArgTable[i].saName) == 0) break;
        /* See if it matches without the _SC_ at the beginning. */
        if (strcmp(argName, sysArgTable[i].saName+4) == 0) break;
    }
    if (i == sizeof(sysArgTable)/sizeof(sysArgTable[0]))
        raise_syscall(taskData, "sysconf argument not found", EINVAL);
    errno = 0; /* Sysconf may return -1 without updating errno. */
    res = sysconf(sysArgTable[i].saVal);
    if (res < 0) raise_syscall(taskData, "sysconf failed", errno);
    return Make_arbitrary_precision(taskData, res);
}


static struct {
    const char *pcName;
    int pcVal;
} pathConfTable[] =
{
    { "_PC_LINK_MAX",   _PC_LINK_MAX },
    { "_PC_MAX_CANON",  _PC_MAX_CANON },
    { "_PC_MAX_INPUT",  _PC_MAX_INPUT },
    { "_PC_NAME_MAX",   _PC_NAME_MAX },
    { "_PC_PATH_MAX",   _PC_PATH_MAX },
    { "_PC_PIPE_BUF",   _PC_PIPE_BUF },
    { "_PC_NO_TRUNC",   _PC_NO_TRUNC },
    { "_PC_VDISABLE",   _PC_VDISABLE },
    { "_PC_CHOWN_RESTRICTED",   _PC_CHOWN_RESTRICTED },
#ifdef _PC_ASYNC_IO
    { "_PC_ASYNC_IO",   _PC_ASYNC_IO },
#endif
#ifdef _PC_PRIO_IO
    { "_PC_PRIO_IO",    _PC_PRIO_IO },
#endif
#ifdef _PC_SYNC_IO
    { "_PC_SYNC_IO",    _PC_SYNC_IO },
#endif
#ifdef _PC_FILESIZEBITS
    { "_PC_FILESIZEBITS",   _PC_FILESIZEBITS },
#endif
#ifdef _PC_SOCK_MAXBUF
    { "_PC_SOCK_MAXBUF",    _PC_SOCK_MAXBUF },
#endif
};

/* Look up a path variable in the table. */
static int findPathVar(TaskData *taskData, PolyWord ps)
{
    char argName[200];
    int length;
    unsigned i;
    length = Poly_string_to_C(ps, argName, 200);
    if (length > 200) raise_syscall(taskData, "Argument name too long", ENAMETOOLONG);

    for (i = 0; i < sizeof(pathConfTable)/sizeof(pathConfTable[0]); i++) {
        if (strcmp(argName, pathConfTable[i].pcName) == 0)
            return pathConfTable[i].pcVal;
        /* See if it matches without the _PC_ at the beginning. */
        if (strcmp(argName, pathConfTable[i].pcName+4) == 0)
            return pathConfTable[i].pcVal;
    }
    raise_syscall(taskData, "pathconf argument not found", EINVAL);
}

class UnixSpecific: public RtsModule
{
public:
    virtual void Init(void);
};

// Declare this.  It will be automatically added to the table.
static UnixSpecific unixModule;

void UnixSpecific::Init(void)
{
    struct sigaction sigcatch;
    /* Ignore SIGPIPE - return any errors as failure to write. */
    memset(&sigcatch, 0, sizeof(sigcatch));
    sigcatch.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &sigcatch, NULL);
}