summaryrefslogtreecommitdiff
path: root/manual/llio.texi
blob: 56f90b6f86f43d71be255ee0153c555bd21b1c89 (plain)
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
@node Low-Level I/O, File System Interface, I/O on Streams, Top
@chapter Low-Level Input/Output

This chapter describes functions for performing low-level input/output
operations on file descriptors.  These functions include the primitives
for the higher-level I/O functions described in @ref{I/O on Streams}, as
well as functions for performing low-level control operations for which
there are no equivalents on streams.

Stream-level I/O is more flexible and usually more convenient;
therefore, programmers generally use the descriptor-level functions only
when necessary.  These are some of the usual reasons:

@itemize @bullet
@item
For reading binary files in large chunks.

@item
For reading an entire file into core before parsing it.

@item
To perform operations other than data transfer, which can only be done
with a descriptor.  (You can use @code{fileno} to get the descriptor
corresponding to a stream.)

@item
To pass descriptors to a child process.  (The child can create its own
stream to use a descriptor that it inherits, but cannot inherit a stream
directly.)
@end itemize

@menu
* Opening and Closing Files::           How to open and close file
                                         descriptors.
* Truncating Files::                    Change the size of a file.
* I/O Primitives::                      Reading and writing data.
* File Position Primitive::             Setting a descriptor's file
                                         position.
* Descriptors and Streams::             Converting descriptor to stream
                                         or vice-versa.
* Stream/Descriptor Precautions::       Precautions needed if you use both
                                         descriptors and streams.
* Waiting for I/O::                     How to check for input or output
					 on multiple file descriptors.
* Synchronizing I/O::                   Making sure all I/O actions completed.
* Asynchronous I/O::                    Perform I/O in parallel.
* Control Operations::                  Various other operations on file
					 descriptors.
* Duplicating Descriptors::             Fcntl commands for duplicating
                                         file descriptors.
* Descriptor Flags::                    Fcntl commands for manipulating
                                         flags associated with file
                                         descriptors.
* File Status Flags::                   Fcntl commands for manipulating
                                         flags associated with open files.
* File Locks::                          Fcntl commands for implementing
                                         file locking.
* Interrupt Input::                     Getting an asynchronous signal when
                                         input arrives.
@end menu


@node Opening and Closing Files
@section Opening and Closing Files

@cindex opening a file descriptor
@cindex closing a file descriptor
This section describes the primitives for opening and closing files
using file descriptors.  The @code{open} and @code{creat} functions are
declared in the header file @file{fcntl.h}, while @code{close} is
declared in @file{unistd.h}.
@pindex unistd.h
@pindex fcntl.h

@comment fcntl.h
@comment POSIX.1
@deftypefun int open (const char *@var{filename}, int @var{flags}[, mode_t @var{mode}])
The @code{open} function creates and returns a new file descriptor
for the file named by @var{filename}.  Initially, the file position
indicator for the file is at the beginning of the file.  The argument
@var{mode} is used only when a file is created, but it doesn't hurt
to supply the argument in any case.

The @var{flags} argument controls how the file is to be opened.  This is
a bit mask; you create the value by the bitwise OR of the appropriate
parameters (using the @samp{|} operator in C).
@xref{File Status Flags}, for the parameters available.

The normal return value from @code{open} is a non-negative integer file
descriptor.  In the case of an error, a value of @code{-1} is returned
instead.  In addition to the usual file name errors (@pxref{File
Name Errors}), the following @code{errno} error conditions are defined
for this function:

@table @code
@item EACCES
The file exists but is not readable/writable as requested by the @var{flags}
argument, the file does not exist and the directory is unwritable so
it cannot be created.

@item EEXIST
Both @code{O_CREAT} and @code{O_EXCL} are set, and the named file already
exists.

@item EINTR
The @code{open} operation was interrupted by a signal.
@xref{Interrupted Primitives}.

@item EISDIR
The @var{flags} argument specified write access, and the file is a directory.

@item EMFILE
The process has too many files open.
The maximum number of file descriptors is controlled by the
@code{RLIMIT_NOFILE} resource limit; @pxref{Limits on Resources}.

@item ENFILE
The entire system, or perhaps the file system which contains the
directory, cannot support any additional open files at the moment.
(This problem cannot happen on the GNU system.)

@item ENOENT
The named file does not exist, and @code{O_CREAT} is not specified.

@item ENOSPC
The directory or file system that would contain the new file cannot be
extended, because there is no disk space left.

@item ENXIO
@code{O_NONBLOCK} and @code{O_WRONLY} are both set in the @var{flags}
argument, the file named by @var{filename} is a FIFO (@pxref{Pipes and
FIFOs}), and no process has the file open for reading.

@item EROFS
The file resides on a read-only file system and any of @w{@code{O_WRONLY}},
@code{O_RDWR}, and @code{O_TRUNC} are set in the @var{flags} argument,
or @code{O_CREAT} is set and the file does not already exist.
@end table

@c !!! umask

If on a 32 bits machine the sources are translated with
@code{_FILE_OFFSET_BITS == 64} the function @code{open} returns a file
descriptor opened in the large file mode which enables the file handling
functions to use files up to @math{2^63} in size and offset from
@math{-2^63} to @math{2^63}.  This happens transparently for the user
since all of the lowlevel file handling functions are equally replaced.

This function is a cancelation point in multi-threaded programs.  This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time @code{open} is
called.  If the thread gets canceled these resources stay allocated
until the program ends.  To avoid this calls to @code{open} should be
protected using cancelation handlers.
@c ref pthread_cleanup_push / pthread_cleanup_pop

The @code{open} function is the underlying primitive for the @code{fopen}
and @code{freopen} functions, that create streams.
@end deftypefun

@comment fcntl.h
@comment LFS
@deftypefun int open64 (const char *@var{filename}, int @var{flags}[, mode_t @var{mode}])
This function is similar to @code{open}.  It returns a file descriptor
which can be used to access the file named by @var{filename}.  The only
the difference is that on 32 bits systems the file is opened in the
large file mode.  I.e., file length and file offsets can exceed 31 bits.

To use this file descriptor one must not use the normal operations but
instead the counterparts named @code{*64}, e.g., @code{read64}.

When the sources are translated with @code{_FILE_OFFSET_BITS == 64} this
function is actually available under the name @code{open}.  I.e., the
new, extended API using 64 bit file sizes and offsets transparently
replaces the old API.
@end deftypefun

@comment fcntl.h
@comment POSIX.1
@deftypefn {Obsolete function} int creat (const char *@var{filename}, mode_t @var{mode})
This function is obsolete.  The call:

@smallexample
creat (@var{filename}, @var{mode})
@end smallexample

@noindent
is equivalent to:

@smallexample
open (@var{filename}, O_WRONLY | O_CREAT | O_TRUNC, @var{mode})
@end smallexample

If on a 32 bits machine the sources are translated with
@code{_FILE_OFFSET_BITS == 64} the function @code{creat} returns a file
descriptor opened in the large file mode which enables the file handling
functions to use files up to @math{2^63} in size and offset from
@math{-2^63} to @math{2^63}.  This happens transparently for the user
since all of the lowlevel file handling functions are equally replaced.
@end deftypefn

@comment fcntl.h
@comment LFS
@deftypefn {Obsolete function} int creat64 (const char *@var{filename}, mode_t @var{mode})
This function is similar to @code{creat}.  It returns a file descriptor
which can be used to access the file named by @var{filename}.  The only
the difference is that on 32 bits systems the file is opened in the
large file mode.  I.e., file length and file offsets can exceed 31 bits.

To use this file descriptor one must not use the normal operations but
instead the counterparts named @code{*64}, e.g., @code{read64}.

When the sources are translated with @code{_FILE_OFFSET_BITS == 64} this
function is actually available under the name @code{open}.  I.e., the
new, extended API using 64 bit file sizes and offsets transparently
replaces the old API.
@end deftypefn

@comment unistd.h
@comment POSIX.1
@deftypefun int close (int @var{filedes})
The function @code{close} closes the file descriptor @var{filedes}.
Closing a file has the following consequences:

@itemize @bullet
@item
The file descriptor is deallocated.

@item
Any record locks owned by the process on the file are unlocked.

@item
When all file descriptors associated with a pipe or FIFO have been closed,
any unread data is discarded.
@end itemize

This function is a cancelation point in multi-threaded programs.  This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time @code{close} is
called.  If the thread gets canceled these resources stay allocated
until the program ends.  To avoid this calls to @code{close} should be
protected using cancelation handlers.
@c ref pthread_cleanup_push / pthread_cleanup_pop

The normal return value from @code{close} is @code{0}; a value of @code{-1}
is returned in case of failure.  The following @code{errno} error
conditions are defined for this function:

@table @code
@item EBADF
The @var{filedes} argument is not a valid file descriptor.

@item EINTR
The @code{close} call was interrupted by a signal.
@xref{Interrupted Primitives}.
Here is an example of how to handle @code{EINTR} properly:

@smallexample
TEMP_FAILURE_RETRY (close (desc));
@end smallexample

@item ENOSPC
@itemx EIO
@itemx EDQUOT
When the file is accessed by NFS, these errors from @code{write} can sometimes
not be detected until @code{close}.  @xref{I/O Primitives}, for details
on their meaning.
@end table

Please note that there is @emph{no} separate @code{close64} function.
This is not necessary since this function does not determine nor depend
on the more of the file.  The kernel which performs the @code{close}
operation knows for which mode the descriptor is used and can handle
this situation.
@end deftypefun

To close a stream, call @code{fclose} (@pxref{Closing Streams}) instead
of trying to close its underlying file descriptor with @code{close}.
This flushes any buffered output and updates the stream object to
indicate that it is closed.


@node Truncating Files
@section Change the size of a file

In some situations it is useful to explicitly determine the size of a
file.  Since the 4.2BSD days there is a function to truncate a file to
at most a given number of bytes and POSIX defines one additional
function.  The prototypes for these functions are in @file{unistd.h}.

@comment unistd.h
@comment X/Open
@deftypefun int truncate (const char *@var{name}, off_t @var{length})
The @code{truncation} function truncates the file named by @var{name} to
at most @var{length} bytes.  I.e., if the file was larger before the
extra bytes are stripped of.  If the file was small or equal to
@var{length} in size before nothing is done.  The file must be writable
by the user to perform this operation.

When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
@code{truncate} function is in fact @code{truncate64} and the type
@code{off_t} has 64 bits which makes it possible to handle files up to
@math{2^63} bytes.

The return value is zero is everything went ok.  Otherwise the return
value is @math{-1} and the global variable @var{errno} is set to:
@table @code
@item EACCES
The file is not accessible to the user.
@item EINVAL
The @var{length} value is illegal.
@item EISDIR
The object named by @var{name} is a directory.
@item ENOENT
The file named by @var{name} does not exist.
@item ENOTDIR
One part of the @var{name} is not a directory.
@end table

This function was introduced in 4.2BSD but also was available in later
@w{System V} systems.  It is not added to POSIX since the authors felt
it is only of marginally additional utility.  See below.
@end deftypefun

@comment unistd.h
@comment LFS
@deftypefun int truncate64 (const char *@var{name}, off64_t @var{length})
This function is similar to the @code{truncate} function.  The
difference is that the @var{length} argument is even on 32 bits machines
64 bits wide which allows to handle file with a size up to @math{2^63}
bytes.

When the sources are defined using @code{_FILE_OFFSET_BITS == 64} on a
32 bits machine this function is actually available under the name
@code{truncate} and so transparently replaces the 32 bits interface.
@end deftypefun

@comment unistd.h
@comment POSIX
@deftypefun int ftruncate (int @var{fd}, off_t @var{length})
The @code{ftruncate} function is similar to the @code{truncate}
function.  The main difference is that it takes a descriptor for an
opened file instead of a file name to identify the object.  The file
must be opened for writing to successfully carry out the operation.

The POSIX standard leaves it implementation defined what happens if the
specified new @var{length} of the file is bigger than the original size.
The @code{ftruncate} function might simply leave the file alone and do
nothing or it can increase the size to the desired size.  In this later
case the extended area should be zero-filled.  So using @code{ftruncate}
is no reliable way to increase the file size but if it is possible it is
probably the fastest way.  The function also operates on POSIX shared
memory segments if these are implemented by the system.

When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
@code{ftruncate} function is in fact @code{ftruncate64} and the type
@code{off_t} has 64 bits which makes it possible to handle files up to
@math{2^63} bytes.

On success the function returns zero.  Otherwise it returns @math{-1}
and set @var{errno} to one of these values:
@table @code
@item EBADF
@var{fd} is no valid file descriptor or is not opened for writing.
@item EINVAL
The object referred to by @var{fd} does not permit this operation.
@item EROFS
The file is on a read-only file system.
@end table
@end deftypefun

@comment unistd.h
@comment LFS
@deftypefun int ftruncate64 (int @var{id}, off64_t @var{length})
This function is similar to the @code{ftruncate} function.  The
difference is that the @var{length} argument is even on 32 bits machines
64 bits wide which allows to handle file with a size up to @math{2^63}
bytes.

When the sources are defined using @code{_FILE_OFFSET_BITS == 64} on a
32 bits machine this function is actually available under the name
@code{ftruncate} and so transparently replaces the 32 bits interface.
@end deftypefun

@node I/O Primitives
@section Input and Output Primitives

This section describes the functions for performing primitive input and
output operations on file descriptors: @code{read}, @code{write}, and
@code{lseek}.  These functions are declared in the header file
@file{unistd.h}.
@pindex unistd.h

@comment unistd.h
@comment POSIX.1
@deftp {Data Type} ssize_t
This data type is used to represent the sizes of blocks that can be
read or written in a single operation.  It is similar to @code{size_t},
but must be a signed type.
@end deftp

@cindex reading from a file descriptor
@comment unistd.h
@comment POSIX.1
@deftypefun ssize_t read (int @var{filedes}, void *@var{buffer}, size_t @var{size})
The @code{read} function reads up to @var{size} bytes from the file
with descriptor @var{filedes}, storing the results in the @var{buffer}.
(This is not necessarily a character string and there is no terminating
null character added.)

@cindex end-of-file, on a file descriptor
The return value is the number of bytes actually read.  This might be
less than @var{size}; for example, if there aren't that many bytes left
in the file or if there aren't that many bytes immediately available.
The exact behavior depends on what kind of file it is.  Note that
reading less than @var{size} bytes is not an error.

A value of zero indicates end-of-file (except if the value of the
@var{size} argument is also zero).  This is not considered an error.
If you keep calling @code{read} while at end-of-file, it will keep
returning zero and doing nothing else.

If @code{read} returns at least one character, there is no way you can
tell whether end-of-file was reached.  But if you did reach the end, the
next read will return zero.

In case of an error, @code{read} returns @code{-1}.  The following
@code{errno} error conditions are defined for this function:

@table @code
@item EAGAIN
Normally, when no input is immediately available, @code{read} waits for
some input.  But if the @code{O_NONBLOCK} flag is set for the file
(@pxref{File Status Flags}), @code{read} returns immediately without
reading any data, and reports this error.

@strong{Compatibility Note:} Most versions of BSD Unix use a different
error code for this: @code{EWOULDBLOCK}.  In the GNU library,
@code{EWOULDBLOCK} is an alias for @code{EAGAIN}, so it doesn't matter
which name you use.

On some systems, reading a large amount of data from a character special
file can also fail with @code{EAGAIN} if the kernel cannot find enough
physical memory to lock down the user's pages.  This is limited to
devices that transfer with direct memory access into the user's memory,
which means it does not include terminals, since they always use
separate buffers inside the kernel.  This problem never happens in the
GNU system.

Any condition that could result in @code{EAGAIN} can instead result in a
successful @code{read} which returns fewer bytes than requested.
Calling @code{read} again immediately would result in @code{EAGAIN}.

@item EBADF
The @var{filedes} argument is not a valid file descriptor,
or is not open for reading.

@item EINTR
@code{read} was interrupted by a signal while it was waiting for input.
@xref{Interrupted Primitives}.  A signal will not necessary cause
@code{read} to return @code{EINTR}; it may instead result in a
successful @code{read} which returns fewer bytes than requested.

@item EIO
For many devices, and for disk files, this error code indicates
a hardware error.

@code{EIO} also occurs when a background process tries to read from the
controlling terminal, and the normal action of stopping the process by
sending it a @code{SIGTTIN} signal isn't working.  This might happen if
signal is being blocked or ignored, or because the process group is
orphaned.  @xref{Job Control}, for more information about job control,
and @ref{Signal Handling}, for information about signals.
@end table

Please note that there is no function named @code{read64}.  This is not
necessary since this function does not directly modify or handle the
possibly wide file offset.  Since the kernel handles this state
internally the @code{read} function can be used for all cases.

This function is a cancelation point in multi-threaded programs.  This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time @code{read} is
called.  If the thread gets canceled these resources stay allocated
until the program ends.  To avoid this calls to @code{read} should be
protected using cancelation handlers.
@c ref pthread_cleanup_push / pthread_cleanup_pop

The @code{read} function is the underlying primitive for all of the
functions that read from streams, such as @code{fgetc}.
@end deftypefun

@comment unistd.h
@comment Unix98
@deftypefun ssize_t pread (int @var{filedes}, void *@var{buffer}, size_t @var{size}, off_t @var{offset})
The @code{pread} function is similar to the @code{read} function.  The
first three arguments are identical and also the return values and error
codes correspond.

The difference is the fourth argument and its handling.  The data block
is not read from the current position of the file descriptor
@code{filedes}.  Instead the data is read from the file starting at
position @var{offset}.  The position of the file descriptor itself is
not effected by the operation.  The value is the same as before the call.

When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
@code{pread} function is in fact @code{pread64} and the type
@code{off_t} has 64 bits which makes it possible to handle files up to
@math{2^63} bytes.

The return value of @code{pread} describes the number of bytes read.
In the error case it returns @math{-1} like @code{read} does and the
error codes are also the same.  Only there are a few more error codes:
@table @code
@item EINVAL
The value given for @var{offset} is negative and therefore illegal.

@item ESPIPE
The file descriptor @var{filedes} is associate with a pipe or a FIFO and
this device does not allow positioning of the file pointer.
@end table

The function is an extension defined in the Unix Single Specification
version 2.
@end deftypefun

@comment unistd.h
@comment LFS
@deftypefun ssize_t pread64 (int @var{filedes}, void *@var{buffer}, size_t @var{size}, off64_t @var{offset})
This function is similar to the @code{pread} function.  The difference
is that the @var{offset} parameter is of type @code{off64_t} instead of
@code{off_t} which makes it possible on 32 bits machines to address
files larger then @math{2^31} bytes and up to @math{2^63} bytes.  The
file descriptor @code{filedes} must be opened using @code{open64} since
otherwise the large offsets possible with @code{off64_t} will lead to
errors with a descriptor in small file mode.

When the sources are defined using @code{_FILE_OFFSET_BITS == 64} on a
32 bits machine this function is actually available under the name
@code{pread} and so transparently replaces the 32 bits interface.
@end deftypefun

@cindex writing to a file descriptor
@comment unistd.h
@comment POSIX.1
@deftypefun ssize_t write (int @var{filedes}, const void *@var{buffer}, size_t @var{size})
The @code{write} function writes up to @var{size} bytes from
@var{buffer} to the file with descriptor @var{filedes}.  The data in
@var{buffer} is not necessarily a character string and a null character is
output like any other character.

The return value is the number of bytes actually written.  This may be
@var{size}, but can always be smaller.  Your program should always call
@code{write} in a loop, iterating until all the data is written.

Once @code{write} returns, the data is enqueued to be written and can be
read back right away, but it is not necessarily written out to permanent
storage immediately.  You can use @code{fsync} when you need to be sure
your data has been permanently stored before continuing.  (It is more
efficient for the system to batch up consecutive writes and do them all
at once when convenient.  Normally they will always be written to disk
within a minute or less.)  Modern systems provide another function
@code{fdatasync} which guarantees integrity only for the file data and
is therefore faster.
@c !!! xref fsync, fdatasync
You can use the @code{O_FSYNC} open mode to make @code{write} always
store the data to disk before returning; @pxref{Operating Modes}.

In the case of an error, @code{write} returns @code{-1}.  The following
@code{errno} error conditions are defined for this function:

@table @code
@item EAGAIN
Normally, @code{write} blocks until the write operation is complete.
But if the @code{O_NONBLOCK} flag is set for the file (@pxref{Control
Operations}), it returns immediately without writing any data, and
reports this error.  An example of a situation that might cause the
process to block on output is writing to a terminal device that supports
flow control, where output has been suspended by receipt of a STOP
character.

@strong{Compatibility Note:} Most versions of BSD Unix use a different
error code for this: @code{EWOULDBLOCK}.  In the GNU library,
@code{EWOULDBLOCK} is an alias for @code{EAGAIN}, so it doesn't matter
which name you use.

On some systems, writing a large amount of data from a character special
file can also fail with @code{EAGAIN} if the kernel cannot find enough
physical memory to lock down the user's pages.  This is limited to
devices that transfer with direct memory access into the user's memory,
which means it does not include terminals, since they always use
separate buffers inside the kernel.  This problem does not arise in the
GNU system.

@item EBADF
The @var{filedes} argument is not a valid file descriptor,
or is not open for writing.

@item EFBIG
The size of the file would become larger than the implementation can support.

@item EINTR
The @code{write} operation was interrupted by a signal while it was
blocked waiting for completion.  A signal will not necessary cause
@code{write} to return @code{EINTR}; it may instead result in a
successful @code{write} which writes fewer bytes than requested.
@xref{Interrupted Primitives}.

@item EIO
For many devices, and for disk files, this error code indicates
a hardware error.

@item ENOSPC
The device containing the file is full.

@item EPIPE
This error is returned when you try to write to a pipe or FIFO that
isn't open for reading by any process.  When this happens, a @code{SIGPIPE}
signal is also sent to the process; see @ref{Signal Handling}.
@end table

Unless you have arranged to prevent @code{EINTR} failures, you should
check @code{errno} after each failing call to @code{write}, and if the
error was @code{EINTR}, you should simply repeat the call.
@xref{Interrupted Primitives}.  The easy way to do this is with the
macro @code{TEMP_FAILURE_RETRY}, as follows:

@smallexample
nbytes = TEMP_FAILURE_RETRY (write (desc, buffer, count));
@end smallexample

Please note that there is no function named @code{write64}.  This is not
necessary since this function does not directly modify or handle the
possibly wide file offset.  Since the kernel handles this state
internally the @code{write} function can be used for all cases.

This function is a cancelation point in multi-threaded programs.  This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time @code{write} is
called.  If the thread gets canceled these resources stay allocated
until the program ends.  To avoid this calls to @code{write} should be
protected using cancelation handlers.
@c ref pthread_cleanup_push / pthread_cleanup_pop

The @code{write} function is the underlying primitive for all of the
functions that write to streams, such as @code{fputc}.
@end deftypefun

@comment unistd.h
@comment Unix98
@deftypefun ssize_t pwrite (int @var{filedes}, const void *@var{buffer}, size_t @var{size}, off_t @var{offset})
The @code{pwrite} function is similar to the @code{write} function.  The
first three arguments are identical and also the return values and error
codes correspond.

The difference is the fourth argument and its handling.  The data block
is not written to the current position of the file descriptor
@code{filedes}.  Instead the data is written to the file starting at
position @var{offset}.  The position of the file descriptor itself is
not effected by the operation.  The value is the same as before the call.

When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
@code{pwrite} function is in fact @code{pwrite64} and the type
@code{off_t} has 64 bits which makes it possible to handle files up to
@math{2^63} bytes.

The return value of @code{pwrite} describes the number of written bytes.
In the error case it returns @math{-1} like @code{write} does and the
error codes are also the same.  Only there are a few more error codes:
@table @code
@item EINVAL
The value given for @var{offset} is negative and therefore illegal.

@item ESPIPE
The file descriptor @var{filedes} is associate with a pipe or a FIFO and
this device does not allow positioning of the file pointer.
@end table

The function is an extension defined in the Unix Single Specification
version 2.
@end deftypefun

@comment unistd.h
@comment LFS
@deftypefun ssize_t pwrite64 (int @var{filedes}, const void *@var{buffer}, size_t @var{size}, off64_t @var{offset})
This function is similar to the @code{pwrite} function.  The difference
is that the @var{offset} parameter is of type @code{off64_t} instead of
@code{off_t} which makes it possible on 32 bits machines to address
files larger then @math{2^31} bytes and up to @math{2^63} bytes.  The
file descriptor @code{filedes} must be opened using @code{open64} since
otherwise the large offsets possible with @code{off64_t} will lead to
errors with a descriptor in small file mode.

When the sources are defined using @code{_FILE_OFFSET_BITS == 64} on a
32 bits machine this function is actually available under the name
@code{pwrite} and so transparently replaces the 32 bits interface.
@end deftypefun


@node File Position Primitive
@section Setting the File Position of a Descriptor

Just as you can set the file position of a stream with @code{fseek}, you
can set the file position of a descriptor with @code{lseek}.  This
specifies the position in the file for the next @code{read} or
@code{write} operation.  @xref{File Positioning}, for more information
on the file position and what it means.

To read the current file position value from a descriptor, use
@code{lseek (@var{desc}, 0, SEEK_CUR)}.

@cindex file positioning on a file descriptor
@cindex positioning a file descriptor
@cindex seeking on a file descriptor
@comment unistd.h
@comment POSIX.1
@deftypefun off_t lseek (int @var{filedes}, off_t @var{offset}, int @var{whence})
The @code{lseek} function is used to change the file position of the
file with descriptor @var{filedes}.

The @var{whence} argument specifies how the @var{offset} should be
interpreted in the same way as for the @code{fseek} function, and must be
one of the symbolic constants @code{SEEK_SET}, @code{SEEK_CUR}, or
@code{SEEK_END}.

@table @code
@item SEEK_SET
Specifies that @var{whence} is a count of characters from the beginning
of the file.

@item SEEK_CUR
Specifies that @var{whence} is a count of characters from the current
file position.  This count may be positive or negative.

@item SEEK_END
Specifies that @var{whence} is a count of characters from the end of
the file.  A negative count specifies a position within the current
extent of the file; a positive count specifies a position past the
current end.  If you set the position past the current end, and
actually write data, you will extend the file with zeros up to that
position.@end table

The return value from @code{lseek} is normally the resulting file
position, measured in bytes from the beginning of the file.
You can use this feature together with @code{SEEK_CUR} to read the
current file position.

If you want to append to the file, setting the file position to the
current end of file with @code{SEEK_END} is not sufficient.  Another
process may write more data after you seek but before you write,
extending the file so the position you write onto clobbers their data.
Instead, use the @code{O_APPEND} operating mode; @pxref{Operating Modes}.

You can set the file position past the current end of the file.  This
does not by itself make the file longer; @code{lseek} never changes the
file.  But subsequent output at that position will extend the file.
Characters between the previous end of file and the new position are
filled with zeros.  Extending the file in this way can create a
``hole'': the blocks of zeros are not actually allocated on disk, so the
file takes up less space than it appears so; it is then called a
``sparse file''.
@cindex sparse files
@cindex holes in files

If the file position cannot be changed, or the operation is in some way
invalid, @code{lseek} returns a value of @code{-1}.  The following
@code{errno} error conditions are defined for this function:

@table @code
@item EBADF
The @var{filedes} is not a valid file descriptor.

@item EINVAL
The @var{whence} argument value is not valid, or the resulting
file offset is not valid.  A file offset is invalid.

@item ESPIPE
The @var{filedes} corresponds to an object that cannot be positioned,
such as a pipe, FIFO or terminal device.  (POSIX.1 specifies this error
only for pipes and FIFOs, but in the GNU system, you always get
@code{ESPIPE} if the object is not seekable.)
@end table

When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
@code{lseek} function is in fact @code{lseek64} and the type
@code{off_t} has 64 bits which makes it possible to handle files up to
@math{2^63} bytes.

This function is a cancelation point in multi-threaded programs.  This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time @code{lseek} is
called.  If the thread gets canceled these resources stay allocated
until the program ends.  To avoid this calls to @code{lseek} should be
protected using cancelation handlers.
@c ref pthread_cleanup_push / pthread_cleanup_pop

The @code{lseek} function is the underlying primitive for the
@code{fseek}, @code{fseeko}, @code{ftell}, @code{ftello} and
@code{rewind} functions, which operate on streams instead of file
descriptors.
@end deftypefun

@comment unistd.h
@comment LFS
@deftypefun off64_t lseek64 (int @var{filedes}, off64_t @var{offset}, int @var{whence})
This function is similar to the @code{lseek} function.  The difference
is that the @var{offset} parameter is of type @code{off64_t} instead of
@code{off_t} which makes it possible on 32 bits machines to address
files larger then @math{2^31} bytes and up to @math{2^63} bytes.  The
file descriptor @code{filedes} must be opened using @code{open64} since
otherwise the large offsets possible with @code{off64_t} will lead to
errors with a descriptor in small file mode.

When the sources are defined using @code{_FILE_OFFSET_BITS == 64} on a
32 bits machine this function is actually available under the name
@code{lseek} and so transparently replaces the 32 bits interface.
@end deftypefun

You can have multiple descriptors for the same file if you open the file
more than once, or if you duplicate a descriptor with @code{dup}.
Descriptors that come from separate calls to @code{open} have independent
file positions; using @code{lseek} on one descriptor has no effect on the
other.  For example,

@smallexample
@group
@{
  int d1, d2;
  char buf[4];
  d1 = open ("foo", O_RDONLY);
  d2 = open ("foo", O_RDONLY);
  lseek (d1, 1024, SEEK_SET);
  read (d2, buf, 4);
@}
@end group
@end smallexample

@noindent
will read the first four characters of the file @file{foo}.  (The
error-checking code necessary for a real program has been omitted here
for brevity.)

By contrast, descriptors made by duplication share a common file
position with the original descriptor that was duplicated.  Anything
which alters the file position of one of the duplicates, including
reading or writing data, affects all of them alike.  Thus, for example,

@smallexample
@{
  int d1, d2, d3;
  char buf1[4], buf2[4];
  d1 = open ("foo", O_RDONLY);
  d2 = dup (d1);
  d3 = dup (d2);
  lseek (d3, 1024, SEEK_SET);
  read (d1, buf1, 4);
  read (d2, buf2, 4);
@}
@end smallexample

@noindent
will read four characters starting with the 1024'th character of
@file{foo}, and then four more characters starting with the 1028'th
character.

@comment sys/types.h
@comment POSIX.1
@deftp {Data Type} off_t
This is an arithmetic data type used to represent file sizes.
In the GNU system, this is equivalent to @code{fpos_t} or @code{long int}.
@end deftp

@comment sys/types.h
@comment LFS
@deftp {Data Type} off64_t
This type is used similar to @code{off_t}.  The difference is that even
on 32 bits machines, where the @code{off_t} type would 32 bits,
@code{off64_t} has 64 bits and so is able to address files up to
@math{2^63} bytes in length.
@end deftp

These aliases for the @samp{SEEK_@dots{}} constants exist for the sake
of compatibility with older BSD systems.  They are defined in two
different header files: @file{fcntl.h} and @file{sys/file.h}.

@table @code
@item L_SET
An alias for @code{SEEK_SET}.

@item L_INCR
An alias for @code{SEEK_CUR}.

@item L_XTND
An alias for @code{SEEK_END}.
@end table

@node Descriptors and Streams
@section Descriptors and Streams
@cindex streams, and file descriptors
@cindex converting file descriptor to stream
@cindex extracting file descriptor from stream

Given an open file descriptor, you can create a stream for it with the
@code{fdopen} function.  You can get the underlying file descriptor for
an existing stream with the @code{fileno} function.  These functions are
declared in the header file @file{stdio.h}.
@pindex stdio.h

@comment stdio.h
@comment POSIX.1
@deftypefun {FILE *} fdopen (int @var{filedes}, const char *@var{opentype})
The @code{fdopen} function returns a new stream for the file descriptor
@var{filedes}.

The @var{opentype} argument is interpreted in the same way as for the
@code{fopen} function (@pxref{Opening Streams}), except that
the @samp{b} option is not permitted; this is because GNU makes no
distinction between text and binary files.  Also, @code{"w"} and
@code{"w+"} do not cause truncation of the file; these have affect only
when opening a file, and in this case the file has already been opened.
You must make sure that the @var{opentype} argument matches the actual
mode of the open file descriptor.

The return value is the new stream.  If the stream cannot be created
(for example, if the modes for the file indicated by the file descriptor
do not permit the access specified by the @var{opentype} argument), a
null pointer is returned instead.

In some other systems, @code{fdopen} may fail to detect that the modes
for file descriptor do not permit the access specified by
@code{opentype}.  The GNU C library always checks for this.
@end deftypefun

For an example showing the use of the @code{fdopen} function,
see @ref{Creating a Pipe}.

@comment stdio.h
@comment POSIX.1
@deftypefun int fileno (FILE *@var{stream})
This function returns the file descriptor associated with the stream
@var{stream}.  If an error is detected (for example, if the @var{stream}
is not valid) or if @var{stream} does not do I/O to a file,
@code{fileno} returns @code{-1}.
@end deftypefun

@cindex standard file descriptors
@cindex file descriptors, standard
There are also symbolic constants defined in @file{unistd.h} for the
file descriptors belonging to the standard streams @code{stdin},
@code{stdout}, and @code{stderr}; see @ref{Standard Streams}.
@pindex unistd.h

@comment unistd.h
@comment POSIX.1
@table @code
@item STDIN_FILENO
@vindex STDIN_FILENO
This macro has value @code{0}, which is the file descriptor for
standard input.
@cindex standard input file descriptor

@comment unistd.h
@comment POSIX.1
@item STDOUT_FILENO
@vindex STDOUT_FILENO
This macro has value @code{1}, which is the file descriptor for
standard output.
@cindex standard output file descriptor

@comment unistd.h
@comment POSIX.1
@item STDERR_FILENO
@vindex STDERR_FILENO
This macro has value @code{2}, which is the file descriptor for
standard error output.
@end table
@cindex standard error file descriptor

@node Stream/Descriptor Precautions
@section Dangers of Mixing Streams and Descriptors
@cindex channels
@cindex streams and descriptors
@cindex descriptors and streams
@cindex mixing descriptors and streams

You can have multiple file descriptors and streams (let's call both
streams and descriptors ``channels'' for short) connected to the same
file, but you must take care to avoid confusion between channels.  There
are two cases to consider: @dfn{linked} channels that share a single
file position value, and @dfn{independent} channels that have their own
file positions.

It's best to use just one channel in your program for actual data
transfer to any given file, except when all the access is for input.
For example, if you open a pipe (something you can only do at the file
descriptor level), either do all I/O with the descriptor, or construct a
stream from the descriptor with @code{fdopen} and then do all I/O with
the stream.

@menu
* Linked Channels::	   Dealing with channels sharing a file position.
* Independent Channels::   Dealing with separately opened, unlinked channels.
* Cleaning Streams::	   Cleaning a stream makes it safe to use
                            another channel.
@end menu

@node Linked Channels
@subsection Linked Channels
@cindex linked channels

Channels that come from a single opening share the same file position;
we call them @dfn{linked} channels.  Linked channels result when you
make a stream from a descriptor using @code{fdopen}, when you get a
descriptor from a stream with @code{fileno}, when you copy a descriptor
with @code{dup} or @code{dup2}, and when descriptors are inherited
during @code{fork}.  For files that don't support random access, such as
terminals and pipes, @emph{all} channels are effectively linked.  On
random-access files, all append-type output streams are effectively
linked to each other.

@cindex cleaning up a stream
If you have been using a stream for I/O, and you want to do I/O using
another channel (either a stream or a descriptor) that is linked to it,
you must first @dfn{clean up} the stream that you have been using.
@xref{Cleaning Streams}.

Terminating a process, or executing a new program in the process,
destroys all the streams in the process.  If descriptors linked to these
streams persist in other processes, their file positions become
undefined as a result.  To prevent this, you must clean up the streams
before destroying them.

@node Independent Channels
@subsection Independent Channels
@cindex independent channels

When you open channels (streams or descriptors) separately on a seekable
file, each channel has its own file position.  These are called
@dfn{independent channels}.

The system handles each channel independently.  Most of the time, this
is quite predictable and natural (especially for input): each channel
can read or write sequentially at its own place in the file.  However,
if some of the channels are streams, you must take these precautions:

@itemize @bullet
@item
You should clean an output stream after use, before doing anything else
that might read or write from the same part of the file.

@item
You should clean an input stream before reading data that may have been
modified using an independent channel.  Otherwise, you might read
obsolete data that had been in the stream's buffer.
@end itemize

If you do output to one channel at the end of the file, this will
certainly leave the other independent channels positioned somewhere
before the new end.  You cannot reliably set their file positions to the
new end of file before writing, because the file can always be extended
by another process between when you set the file position and when you
write the data.  Instead, use an append-type descriptor or stream; they
always output at the current end of the file.  In order to make the
end-of-file position accurate, you must clean the output channel you
were using, if it is a stream.

It's impossible for two channels to have separate file pointers for a
file that doesn't support random access.  Thus, channels for reading or
writing such files are always linked, never independent.  Append-type
channels are also always linked.  For these channels, follow the rules
for linked channels; see @ref{Linked Channels}.

@node Cleaning Streams
@subsection Cleaning Streams

On the GNU system, you can clean up any stream with @code{fclean}:

@comment stdio.h
@comment GNU
@deftypefun int fclean (FILE *@var{stream})
Clean up the stream @var{stream} so that its buffer is empty.  If
@var{stream} is doing output, force it out.  If @var{stream} is doing
input, give the data in the buffer back to the system, arranging to
reread it.
@end deftypefun

On other systems, you can use @code{fflush} to clean a stream in most
cases.

You can skip the @code{fclean} or @code{fflush} if you know the stream
is already clean.  A stream is clean whenever its buffer is empty.  For
example, an unbuffered stream is always clean.  An input stream that is
at end-of-file is clean.  A line-buffered stream is clean when the last
character output was a newline.

There is one case in which cleaning a stream is impossible on most
systems.  This is when the stream is doing input from a file that is not
random-access.  Such streams typically read ahead, and when the file is
not random access, there is no way to give back the excess data already
read.  When an input stream reads from a random-access file,
@code{fflush} does clean the stream, but leaves the file pointer at an
unpredictable place; you must set the file pointer before doing any
further I/O.  On the GNU system, using @code{fclean} avoids both of
these problems.

Closing an output-only stream also does @code{fflush}, so this is a
valid way of cleaning an output stream.  On the GNU system, closing an
input stream does @code{fclean}.

You need not clean a stream before using its descriptor for control
operations such as setting terminal modes; these operations don't affect
the file position and are not affected by it.  You can use any
descriptor for these operations, and all channels are affected
simultaneously.  However, text already ``output'' to a stream but still
buffered by the stream will be subject to the new terminal modes when
subsequently flushed.  To make sure ``past'' output is covered by the
terminal settings that were in effect at the time, flush the output
streams for that terminal before setting the modes.  @xref{Terminal
Modes}.

@node Waiting for I/O
@section Waiting for Input or Output
@cindex waiting for input or output
@cindex multiplexing input
@cindex input from multiple files

Sometimes a program needs to accept input on multiple input channels
whenever input arrives.  For example, some workstations may have devices
such as a digitizing tablet, function button box, or dial box that are
connected via normal asynchronous serial interfaces; good user interface
style requires responding immediately to input on any device.  Another
example is a program that acts as a server to several other processes
via pipes or sockets.

You cannot normally use @code{read} for this purpose, because this
blocks the program until input is available on one particular file
descriptor; input on other channels won't wake it up.  You could set
nonblocking mode and poll each file descriptor in turn, but this is very
inefficient.

A better solution is to use the @code{select} function.  This blocks the
program until input or output is ready on a specified set of file
descriptors, or until a timer expires, whichever comes first.  This
facility is declared in the header file @file{sys/types.h}.
@pindex sys/types.h

In the case of a server socket (@pxref{Listening}), we say that
``input'' is available when there are pending connections that could be
accepted (@pxref{Accepting Connections}).  @code{accept} for server
sockets blocks and interacts with @code{select} just as @code{read} does
for normal input.

@cindex file descriptor sets, for @code{select}
The file descriptor sets for the @code{select} function are specified
as @code{fd_set} objects.  Here is the description of the data type
and some macros for manipulating these objects.

@comment sys/types.h
@comment BSD
@deftp {Data Type} fd_set
The @code{fd_set} data type represents file descriptor sets for the
@code{select} function.  It is actually a bit array.
@end deftp

@comment sys/types.h
@comment BSD
@deftypevr Macro int FD_SETSIZE
The value of this macro is the maximum number of file descriptors that a
@code{fd_set} object can hold information about.  On systems with a
fixed maximum number, @code{FD_SETSIZE} is at least that number.  On
some systems, including GNU, there is no absolute limit on the number of
descriptors open, but this macro still has a constant value which
controls the number of bits in an @code{fd_set}; if you get a file
descriptor with a value as high as @code{FD_SETSIZE}, you cannot put
that descriptor into an @code{fd_set}.
@end deftypevr

@comment sys/types.h
@comment BSD
@deftypefn Macro void FD_ZERO (fd_set *@var{set})
This macro initializes the file descriptor set @var{set} to be the
empty set.
@end deftypefn

@comment sys/types.h
@comment BSD
@deftypefn Macro void FD_SET (int @var{filedes}, fd_set *@var{set})
This macro adds @var{filedes} to the file descriptor set @var{set}.
@end deftypefn

@comment sys/types.h
@comment BSD
@deftypefn Macro void FD_CLR (int @var{filedes}, fd_set *@var{set})
This macro removes @var{filedes} from the file descriptor set @var{set}.
@end deftypefn

@comment sys/types.h
@comment BSD
@deftypefn Macro int FD_ISSET (int @var{filedes}, fd_set *@var{set})
This macro returns a nonzero value (true) if @var{filedes} is a member
of the file descriptor set @var{set}, and zero (false) otherwise.
@end deftypefn

Next, here is the description of the @code{select} function itself.

@comment sys/types.h
@comment BSD
@deftypefun int select (int @var{nfds}, fd_set *@var{read-fds}, fd_set *@var{write-fds}, fd_set *@var{except-fds}, struct timeval *@var{timeout})
The @code{select} function blocks the calling process until there is
activity on any of the specified sets of file descriptors, or until the
timeout period has expired.

The file descriptors specified by the @var{read-fds} argument are
checked to see if they are ready for reading; the @var{write-fds} file
descriptors are checked to see if they are ready for writing; and the
@var{except-fds} file descriptors are checked for exceptional
conditions.  You can pass a null pointer for any of these arguments if
you are not interested in checking for that kind of condition.

A file descriptor is considered ready for reading if it is at end of
file.  A server socket is considered ready for reading if there is a
pending connection which can be accepted with @code{accept};
@pxref{Accepting Connections}.  A client socket is ready for writing when
its connection is fully established; @pxref{Connecting}.

``Exceptional conditions'' does not mean errors---errors are reported
immediately when an erroneous system call is executed, and do not
constitute a state of the descriptor.  Rather, they include conditions
such as the presence of an urgent message on a socket.  (@xref{Sockets},
for information on urgent messages.)

The @code{select} function checks only the first @var{nfds} file
descriptors.  The usual thing is to pass @code{FD_SETSIZE} as the value
of this argument.

The @var{timeout} specifies the maximum time to wait.  If you pass a
null pointer for this argument, it means to block indefinitely until one
of the file descriptors is ready.  Otherwise, you should provide the
time in @code{struct timeval} format; see @ref{High-Resolution
Calendar}.  Specify zero as the time (a @code{struct timeval} containing
all zeros) if you want to find out which descriptors are ready without
waiting if none are ready.

The normal return value from @code{select} is the total number of ready file
descriptors in all of the sets.  Each of the argument sets is overwritten
with information about the descriptors that are ready for the corresponding
operation.  Thus, to see if a particular descriptor @var{desc} has input,
use @code{FD_ISSET (@var{desc}, @var{read-fds})} after @code{select} returns.

If @code{select} returns because the timeout period expires, it returns
a value of zero.

Any signal will cause @code{select} to return immediately.  So if your
program uses signals, you can't rely on @code{select} to keep waiting
for the full time specified.  If you want to be sure of waiting for a
particular amount of time, you must check for @code{EINTR} and repeat
the @code{select} with a newly calculated timeout based on the current
time.  See the example below.  See also @ref{Interrupted Primitives}.

If an error occurs, @code{select} returns @code{-1} and does not modify
the argument file descriptor sets.  The following @code{errno} error
conditions are defined for this function:

@table @code
@item EBADF
One of the file descriptor sets specified an invalid file descriptor.

@item EINTR
The operation was interrupted by a signal.  @xref{Interrupted Primitives}.

@item EINVAL
The @var{timeout} argument is invalid; one of the components is negative
or too large.
@end table
@end deftypefun

@strong{Portability Note:}  The @code{select} function is a BSD Unix
feature.

Here is an example showing how you can use @code{select} to establish a
timeout period for reading from a file descriptor.  The @code{input_timeout}
function blocks the calling process until input is available on the
file descriptor, or until the timeout period expires.

@smallexample
@include select.c.texi
@end smallexample

There is another example showing the use of @code{select} to multiplex
input from multiple sockets in @ref{Server Example}.


@node Synchronizing I/O
@section Synchronizing I/O operations

@cindex synchronizing
In most modern operation systems the normal I/O operations are not
executed synchronously.  I.e., even if a @code{write} system call
returns this does not mean the data is actually written to the media,
e.g., the disk.

In situations where synchronization points are necessary the user can
use special functions which ensure that all operations finished before
they return.

@comment unistd.h
@comment X/Open
@deftypefun int sync (void)
A call to this function will not return as long as there is data which
that is not written to the device.  All dirty buffers in the kernel will
be written and so an overall consistent system can be achieved (if no
other process in parallel writes data).

A prototype for @code{sync} can be found in @file{unistd.h}.

The return value is zero to indicate no error.
@end deftypefun

More often it is wanted that not all data in the system is committed.
Programs want to ensure that data written to a given file are all
committed and in this situation @code{sync} is overkill.

@comment unistd.h
@comment POSIX
@deftypefun int fsync (int @var{fildes})
The @code{fsync} can be used to make sure all data associated with the
open file @var{fildes} is written to the device associated with the
descriptor.  The function call does not return unless all actions have
finished.

A prototype for @code{fsync} can be found in @file{unistd.h}.

This function is a cancelation point in multi-threaded programs.  This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time @code{fsync} is
called.  If the thread gets canceled these resources stay allocated
until the program ends.  To avoid this calls to @code{fsync} should be
protected using cancelation handlers.
@c ref pthread_cleanup_push / pthread_cleanup_pop

The return value of the function is zero if no error occured.  Otherwise
it is @math{-1} and the global variable @var{errno} is set to the
following values:
@table @code
@item EBADF
The descriptor @var{fildes} is not valid.

@item EINVAL
No synchronization is possible since the system does not implement this.
@end table
@end deftypefun

Sometimes it is not even necessary to write all data associated with a
file descriptor.  E.g., in database files which do not change in size it
is enough to write all the file content data to the device.
Meta-information like the modification time etc. are not that important
and leaving such information uncommitted does not prevent a successful
recovering of the file in case of a problem.

@comment unistd.h
@comment POSIX
@deftypefun int fdatasync (int @var{fildes})
When a call to the @code{fdatasync} function returns it is made sure
that all of the file data is written to the device.  For all pending I/O
operations the parts guaranteeing data integrity finished.

Not all systems implement the @code{fdatasync} operation.  On systems
missing this functionality @code{fdatasync} is emulated by a call to
@code{fsync} since the performed actions are a superset of those
required by @code{fdatasyn}.

The prototype for @code{fdatasync} is in @file{unistd.h}.

The return value of the function is zero if no error occured.  Otherwise
it is @math{-1} and the global variable @var{errno} is set to the
following values:
@table @code
@item EBADF
The descriptor @var{fildes} is not valid.

@item EINVAL
No synchronization is possible since the system does not implement this.
@end table
@end deftypefun


@node Asynchronous I/O
@section Perform I/O Operations in Parallel

The POSIX.1b standard defines a new set of I/O operations which can
reduce the time an application spends waiting at I/O significantly.  The
new functions allow a program to initiate one or more I/O operations and
then immediately resume the normal word while the I/O operations are
executed in parallel.

These functions are part of the library with realtime functions named
@file{librt}.  They are not actually part of the @file{libc} binary.
The implementation of these functions can be done using support in the
kernel )if available) or using a implementation based on threads at
userlevel.  In the later case it might be necessary to link applications
linked with @file{librt} also with the thread library @file{libthread}.

All AIO operations operate on files which previously were opened.  There
might be arbitrary many operations for one file running.  The
asynchronous I/O operations are controlled using a data structure named
@code{struct aiocb} (@dfn{AIO control block}).  It is defined in
@file{aio.h} as follows.

@comment aio.h
@comment POSIX.1b
@deftp {Data Type} {struct aiocb}
The POSIX.1b standard mandates that the @code{struct aiocb} structure
contains at least the members described in the following table.  There
might be more elements which are used by the implementation but
depending on these elements is not portable and is highly deprecated.

@table @code
@item int aio_fildes
This element specifies the file descriptor which is used for the
operation.  It must be a legal descriptor since otherwise the operation
fails for obvious reasons.

The device on which the file is opened must allow the seek operation.
I.e., it is not possible to use any of the AIO operations on devices
like terminals where an @code{lseek} call would lead to an error.

@item off_t aio_offset
This element specified at which offset in the file the operation (input
or output) is performed.  Since the operation are carried in arbitrary
order and more than one operation for one file descriptor can be
started, one cannot expect a current read/write position of the file
descriptor.

@item volatile void *aio_buf
This is a pointer to the buffer with the data to be written or the place
where the ead data is stored.

@item size_t aio_nbytes
This element specifies the length of the buffer pointed to by @code{aio_buf}.

@item int aio_reqprio
If for the platform @code{_POSIX_PRIORITIZED_IO} and
@code{_POSIX_PRIORITY_SCHEDULING} is defined the AIO requests are
processed based on the current scheduling priority.  The
@code{aio_reqprio} element can then be used to lower the priority of the
AIO operation.

@item struct sigevent aio_sigevent
This element specifies how the calling process is notified once the
operation terminated.  If the @code{sigev_notify} element is
@code{SIGEV_NONE} no notification is send.  If it is @code{SIGEV_SIGNAL}
the signal determined by @code{sigev_signo} is send.  Otherwise
@code{sigev_notify} must be @code{SIGEV_THREAD} in which case a thread
which starts executing the function pointeed to by
@code{sigev_notify_function}.

@item int aio_lio_opcode
This element is only used by the @code{lio_listio} and
@code{[lio_listio64} functions.  Since these functions allow to start an
arbitrary number of operations at once and since each operationcan be
input or output (or nothing) the information must be stored in the
control block.  The possible values are:

@vtable @code
@item LIO_READ
Start a read operation.  Read from the file at position
@code{aio_offset} and store the next @code{aio_nbytes} bytes in the
buffer pointed to by @code{aio_buf}.

@item LIO_WRITE
Start a write operation.  Write @code{aio_nbytes} bytes starting at
@code{aio_buf} into the file starting at position @code{aio_offset}.

@item LIO_NOP
Do nothing for this control block.  This value is useful sometimes when
an array of @code{struct aiocb} values contains holes, i.e., some of the
values must not be handled allthough the whole array is presented to the
@code{lio_listio} function.
@end vtable
@end table
@end deftp

@menu
* Asynchronous Reads::           Asynchronous Read Operations.
* Cancel AIO Operations::        Cancelation of AIO Operations.
@end menu

@node Asynchronous Reads
@subsection Asynchronous Read Operations

@comment aio.h
@comment POSIX.1b
@deftypefun int aio_read (struct aiocb *@var{aiocbp})
This function initiates an asynchronous read operation.  The function
call immedaitely returns after the operation was enqueued or if before
this happens an error was encoutered.

The first @code{aiocbp->aio_nbytes} bytes from the buffer starting at
@code{aiocbp->aio_buf} are written to the file for which
@code{aiocbp->aio_fildes} is an descriptor, starting at the absolute
position @code{aiocbp->aio_offset} in the file.

If prioritized I/O is supported by the platform the
@code{aiocbp->aio_reqprio} value is used to adjust the priority before
the request is actually enqueued.

The calling process is notified about the termination of the read
request according to the @code{aiocbp->aio_sigevent} value.

When @code{aio_read} returns the return value is zero if no error
occurred that can be found before the process is enqueued.  If such an
earlier error is found the function returns @code{-1} and sets
@code{errno} to one of the following values.

@table @code
@item EAGAIN
The request was not enqueued due to (temporarily) exceeded resource
limitations.
@item ENOSYS
The @code{aio_read} function is not implemented.
@item EBADF
The @code{aiocbp->aio_fildes} descriptor is not valid.  This condition
need not be recognized before enqueueing the request and so this error
might also be signaled asynchrously.
@item EINVAL
The @code{aiocbp->aio_offset} or @code{aiocbp->aio_reqpiro} value is
invalid.  This condition need not be recognized before enqueueing the
request and so this error might also be signaled asynchrously.
@end table

In the case @code{aio_read} return zero the current status of the
request can be queried using @code{aio_error} and @code{aio_return}
questions.  As long as the value returned by @code{aio_error} is
@code{EINPROGRESS} the operation has not yet completed.  If
@code{aio_error} returns zero the operation successfully terminated,
otherwise the value is to be interpreted as an error code.  If the
function terminated the result of the operation can be get using a call
to @code{aio_return}.  The returned value is the same as an equivalent
call to @code{read} would have returned.  Possible error code returned
by @code{aio_error} are:

@table @code
@item EBADF
The @code{aiocbp->aio_fildes} descriptor is not valid.
@item ECANCELED
The operation was canceled before the operation was finished
(@pxref{Cancel AIO Operations})
@item EINVAL
The @code{aiocbp->aio_offset} value is invalid.
@end table
@end deftypefun

@comment aio.h
@comment POSIX.1b
@deftypefun int aio_read64 (struct aiocb *@var{aiocbp})
This function is similar to the @code{aio_read} function.  The only
difference is that only @w{32 bits} machines the file descriptor should
be opened in the large file mode.  Internally @code{aio_read64} uses
functionality equivalent to @code{lseek64} to position the file
descriptor correctly for the reading, as opposed to @code{lseek}
funcationality used in @code{aio_read}.
@end deftypefun


@node Cancel AIO Operations
@subsection Cancelation of AIO Operations


@node Control Operations
@section Control Operations on Files

@cindex control operations on files
@cindex @code{fcntl} function
This section describes how you can perform various other operations on
file descriptors, such as inquiring about or setting flags describing
the status of the file descriptor, manipulating record locks, and the
like.  All of these operations are performed by the function @code{fcntl}.

The second argument to the @code{fcntl} function is a command that
specifies which operation to perform.  The function and macros that name
various flags that are used with it are declared in the header file
@file{fcntl.h}.  Many of these flags are also used by the @code{open}
function; see @ref{Opening and Closing Files}.
@pindex fcntl.h

@comment fcntl.h
@comment POSIX.1
@deftypefun int fcntl (int @var{filedes}, int @var{command}, @dots{})
The @code{fcntl} function performs the operation specified by
@var{command} on the file descriptor @var{filedes}.  Some commands
require additional arguments to be supplied.  These additional arguments
and the return value and error conditions are given in the detailed
descriptions of the individual commands.

Briefly, here is a list of what the various commands are.

@table @code
@item F_DUPFD
Duplicate the file descriptor (return another file descriptor pointing
to the same open file).  @xref{Duplicating Descriptors}.

@item F_GETFD
Get flags associated with the file descriptor.  @xref{Descriptor Flags}.

@item F_SETFD
Set flags associated with the file descriptor.  @xref{Descriptor Flags}.

@item F_GETFL
Get flags associated with the open file.  @xref{File Status Flags}.

@item F_SETFL
Set flags associated with the open file.  @xref{File Status Flags}.

@item F_GETLK
Get a file lock.  @xref{File Locks}.

@item F_SETLK
Set or clear a file lock.  @xref{File Locks}.

@item F_SETLKW
Like @code{F_SETLK}, but wait for completion.  @xref{File Locks}.

@item F_GETOWN
Get process or process group ID to receive @code{SIGIO} signals.
@xref{Interrupt Input}.

@item F_SETOWN
Set process or process group ID to receive @code{SIGIO} signals.
@xref{Interrupt Input}.
@end table

This function is a cancelation point in multi-threaded programs.  This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time @code{fcntl} is
called.  If the thread gets canceled these resources stay allocated
until the program ends.  To avoid this calls to @code{fcntl} should be
protected using cancelation handlers.
@c ref pthread_cleanup_push / pthread_cleanup_pop
@end deftypefun


@node Duplicating Descriptors
@section Duplicating Descriptors

@cindex duplicating file descriptors
@cindex redirecting input and output

You can @dfn{duplicate} a file descriptor, or allocate another file
descriptor that refers to the same open file as the original.  Duplicate
descriptors share one file position and one set of file status flags
(@pxref{File Status Flags}), but each has its own set of file descriptor
flags (@pxref{Descriptor Flags}).

The major use of duplicating a file descriptor is to implement
@dfn{redirection} of input or output:  that is, to change the
file or pipe that a particular file descriptor corresponds to.

You can perform this operation using the @code{fcntl} function with the
@code{F_DUPFD} command, but there are also convenient functions
@code{dup} and @code{dup2} for duplicating descriptors.

@pindex unistd.h
@pindex fcntl.h
The @code{fcntl} function and flags are declared in @file{fcntl.h},
while prototypes for @code{dup} and @code{dup2} are in the header file
@file{unistd.h}.

@comment unistd.h
@comment POSIX.1
@deftypefun int dup (int @var{old})
This function copies descriptor @var{old} to the first available
descriptor number (the first number not currently open).  It is
equivalent to @code{fcntl (@var{old}, F_DUPFD, 0)}.
@end deftypefun

@comment unistd.h
@comment POSIX.1
@deftypefun int dup2 (int @var{old}, int @var{new})
This function copies the descriptor @var{old} to descriptor number
@var{new}.

If @var{old} is an invalid descriptor, then @code{dup2} does nothing; it
does not close @var{new}.  Otherwise, the new duplicate of @var{old}
replaces any previous meaning of descriptor @var{new}, as if @var{new}
were closed first.

If @var{old} and @var{new} are different numbers, and @var{old} is a
valid descriptor number, then @code{dup2} is equivalent to:

@smallexample
close (@var{new});
fcntl (@var{old}, F_DUPFD, @var{new})
@end smallexample

However, @code{dup2} does this atomically; there is no instant in the
middle of calling @code{dup2} at which @var{new} is closed and not yet a
duplicate of @var{old}.
@end deftypefun

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_DUPFD
This macro is used as the @var{command} argument to @code{fcntl}, to
copy the file descriptor given as the first argument.

The form of the call in this case is:

@smallexample
fcntl (@var{old}, F_DUPFD, @var{next-filedes})
@end smallexample

The @var{next-filedes} argument is of type @code{int} and specifies that
the file descriptor returned should be the next available one greater
than or equal to this value.

The return value from @code{fcntl} with this command is normally the value
of the new file descriptor.  A return value of @code{-1} indicates an
error.  The following @code{errno} error conditions are defined for
this command:

@table @code
@item EBADF
The @var{old} argument is invalid.

@item EINVAL
The @var{next-filedes} argument is invalid.

@item EMFILE
There are no more file descriptors available---your program is already
using the maximum.  In BSD and GNU, the maximum is controlled by a
resource limit that can be changed; @pxref{Limits on Resources}, for
more information about the @code{RLIMIT_NOFILE} limit.
@end table

@code{ENFILE} is not a possible error code for @code{dup2} because
@code{dup2} does not create a new opening of a file; duplicate
descriptors do not count toward the limit which @code{ENFILE}
indicates.  @code{EMFILE} is possible because it refers to the limit on
distinct descriptor numbers in use in one process.
@end deftypevr

Here is an example showing how to use @code{dup2} to do redirection.
Typically, redirection of the standard streams (like @code{stdin}) is
done by a shell or shell-like program before calling one of the
@code{exec} functions (@pxref{Executing a File}) to execute a new
program in a child process.  When the new program is executed, it
creates and initializes the standard streams to point to the
corresponding file descriptors, before its @code{main} function is
invoked.

So, to redirect standard input to a file, the shell could do something
like:

@smallexample
pid = fork ();
if (pid == 0)
  @{
    char *filename;
    char *program;
    int file;
    @dots{}
    file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));
    dup2 (file, STDIN_FILENO);
    TEMP_FAILURE_RETRY (close (file));
    execv (program, NULL);
  @}
@end smallexample

There is also a more detailed example showing how to implement redirection
in the context of a pipeline of processes in @ref{Launching Jobs}.


@node Descriptor Flags
@section File Descriptor Flags
@cindex file descriptor flags

@dfn{File descriptor flags} are miscellaneous attributes of a file
descriptor.  These flags are associated with particular file
descriptors, so that if you have created duplicate file descriptors
from a single opening of a file, each descriptor has its own set of flags.

Currently there is just one file descriptor flag: @code{FD_CLOEXEC},
which causes the descriptor to be closed if you use any of the
@code{exec@dots{}} functions (@pxref{Executing a File}).

The symbols in this section are defined in the header file
@file{fcntl.h}.
@pindex fcntl.h

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_GETFD
This macro is used as the @var{command} argument to @code{fcntl}, to
specify that it should return the file descriptor flags associated
with the @var{filedes} argument.

The normal return value from @code{fcntl} with this command is a
nonnegative number which can be interpreted as the bitwise OR of the
individual flags (except that currently there is only one flag to use).

In case of an error, @code{fcntl} returns @code{-1}.  The following
@code{errno} error conditions are defined for this command:

@table @code
@item EBADF
The @var{filedes} argument is invalid.
@end table
@end deftypevr


@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_SETFD
This macro is used as the @var{command} argument to @code{fcntl}, to
specify that it should set the file descriptor flags associated with the
@var{filedes} argument.  This requires a third @code{int} argument to
specify the new flags, so the form of the call is:

@smallexample
fcntl (@var{filedes}, F_SETFD, @var{new-flags})
@end smallexample

The normal return value from @code{fcntl} with this command is an
unspecified value other than @code{-1}, which indicates an error.
The flags and error conditions are the same as for the @code{F_GETFD}
command.
@end deftypevr

The following macro is defined for use as a file descriptor flag with
the @code{fcntl} function.  The value is an integer constant usable
as a bit mask value.

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int FD_CLOEXEC
@cindex close-on-exec (file descriptor flag)
This flag specifies that the file descriptor should be closed when
an @code{exec} function is invoked; see @ref{Executing a File}.  When
a file descriptor is allocated (as with @code{open} or @code{dup}),
this bit is initially cleared on the new file descriptor, meaning that
descriptor will survive into the new program after @code{exec}.
@end deftypevr

If you want to modify the file descriptor flags, you should get the
current flags with @code{F_GETFD} and modify the value.  Don't assume
that the flags listed here are the only ones that are implemented; your
program may be run years from now and more flags may exist then.  For
example, here is a function to set or clear the flag @code{FD_CLOEXEC}
without altering any other flags:

@smallexample
/* @r{Set the @code{FD_CLOEXEC} flag of @var{desc} if @var{value} is nonzero,}
   @r{or clear the flag if @var{value} is 0.}
   @r{Return 0 on success, or -1 on error with @code{errno} set.} */

int
set_cloexec_flag (int desc, int value)
@{
  int oldflags = fcntl (desc, F_GETFD, 0);
  /* @r{If reading the flags failed, return error indication now.}
  if (oldflags < 0)
    return oldflags;
  /* @r{Set just the flag we want to set.} */
  if (value != 0)
    oldflags |= FD_CLOEXEC;
  else
    oldflags &= ~FD_CLOEXEC;
  /* @r{Store modified flag word in the descriptor.} */
  return fcntl (desc, F_SETFD, oldflags);
@}
@end smallexample

@node File Status Flags
@section File Status Flags
@cindex file status flags

@dfn{File status flags} are used to specify attributes of the opening of a
file.  Unlike the file descriptor flags discussed in @ref{Descriptor
Flags}, the file status flags are shared by duplicated file descriptors
resulting from a single opening of the file.  The file status flags are
specified with the @var{flags} argument to @code{open};
@pxref{Opening and Closing Files}.

File status flags fall into three categories, which are described in the
following sections.

@itemize @bullet
@item
@ref{Access Modes}, specify what type of access is allowed to the
file: reading, writing, or both.  They are set by @code{open} and are
returned by @code{fcntl}, but cannot be changed.

@item
@ref{Open-time Flags}, control details of what @code{open} will do.
These flags are not preserved after the @code{open} call.

@item
@ref{Operating Modes}, affect how operations such as @code{read} and
@code{write} are done.  They are set by @code{open}, and can be fetched or
changed with @code{fcntl}.
@end itemize

The symbols in this section are defined in the header file
@file{fcntl.h}.
@pindex fcntl.h

@menu
* Access Modes::                Whether the descriptor can read or write.
* Open-time Flags::             Details of @code{open}.
* Operating Modes::             Special modes to control I/O operations.
* Getting File Status Flags::   Fetching and changing these flags.
@end menu

@node Access Modes
@subsection File Access Modes

The file access modes allow a file descriptor to be used for reading,
writing, or both.  (In the GNU system, they can also allow none of these,
and allow execution of the file as a program.)  The access modes are chosen
when the file is opened, and never change.

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_RDONLY
Open the file for read access.
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_WRONLY
Open the file for write access.
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_RDWR
Open the file for both reading and writing.
@end deftypevr

In the GNU system (and not in other systems), @code{O_RDONLY} and
@code{O_WRONLY} are independent bits that can be bitwise-ORed together,
and it is valid for either bit to be set or clear.  This means that
@code{O_RDWR} is the same as @code{O_RDONLY|O_WRONLY}.  A file access
mode of zero is permissible; it allows no operations that do input or
output to the file, but does allow other operations such as
@code{fchmod}.  On the GNU system, since ``read-only'' or ``write-only''
is a misnomer, @file{fcntl.h} defines additional names for the file
access modes.  These names are preferred when writing GNU-specific code.
But most programs will want to be portable to other POSIX.1 systems and
should use the POSIX.1 names above instead.

@comment fcntl.h
@comment GNU
@deftypevr Macro int O_READ
Open the file for reading.  Same as @code{O_RDWR}; only defined on GNU.
@end deftypevr

@comment fcntl.h
@comment GNU
@deftypevr Macro int O_WRITE
Open the file for reading.  Same as @code{O_WRONLY}; only defined on GNU.
@end deftypevr

@comment fcntl.h
@comment GNU
@deftypevr Macro int O_EXEC
Open the file for executing.  Only defined on GNU.
@end deftypevr

To determine the file access mode with @code{fcntl}, you must extract
the access mode bits from the retrieved file status flags.  In the GNU
system, you can just test the @code{O_READ} and @code{O_WRITE} bits in
the flags word.  But in other POSIX.1 systems, reading and writing
access modes are not stored as distinct bit flags.  The portable way to
extract the file access mode bits is with @code{O_ACCMODE}.

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_ACCMODE
This macro stands for a mask that can be bitwise-ANDed with the file
status flag value to produce a value representing the file access mode.
The mode will be @code{O_RDONLY}, @code{O_WRONLY}, or @code{O_RDWR}.
(In the GNU system it could also be zero, and it never includes the
@code{O_EXEC} bit.)
@end deftypevr

@node Open-time Flags
@subsection Open-time Flags

The open-time flags specify options affecting how @code{open} will behave.
These options are not preserved once the file is open.  The exception to
this is @code{O_NONBLOCK}, which is also an I/O operating mode and so it
@emph{is} saved.  @xref{Opening and Closing Files}, for how to call
@code{open}.

There are two sorts of options specified by open-time flags.

@itemize @bullet
@item
@dfn{File name translation flags} affect how @code{open} looks up the
file name to locate the file, and whether the file can be created.
@cindex file name translation flags
@cindex flags, file name translation

@item
@dfn{Open-time action flags} specify extra operations that @code{open} will
perform on the file once it is open.
@cindex open-time action flags
@cindex flags, open-time action
@end itemize

Here are the file name translation flags.

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_CREAT
If set, the file will be created if it doesn't already exist.
@c !!! mode arg, umask
@cindex create on open (file status flag)
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_EXCL
If both @code{O_CREAT} and @code{O_EXCL} are set, then @code{open} fails
if the specified file already exists.  This is guaranteed to never
clobber an existing file.
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_NONBLOCK
@cindex non-blocking open
This prevents @code{open} from blocking for a ``long time'' to open the
file.  This is only meaningful for some kinds of files, usually devices
such as serial ports; when it is not meaningful, it is harmless and
ignored.  Often opening a port to a modem blocks until the modem reports
carrier detection; if @code{O_NONBLOCK} is specified, @code{open} will
return immediately without a carrier.

Note that the @code{O_NONBLOCK} flag is overloaded as both an I/O operating
mode and a file name translation flag.  This means that specifying
@code{O_NONBLOCK} in @code{open} also sets nonblocking I/O mode;
@pxref{Operating Modes}.  To open the file without blocking but do normal
I/O that blocks, you must call @code{open} with @code{O_NONBLOCK} set and
then call @code{fcntl} to turn the bit off.
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_NOCTTY
If the named file is a terminal device, don't make it the controlling
terminal for the process.  @xref{Job Control}, for information about
what it means to be the controlling terminal.

In the GNU system and 4.4 BSD, opening a file never makes it the
controlling terminal and @code{O_NOCTTY} is zero.  However, other
systems may use a nonzero value for @code{O_NOCTTY} and set the
controlling terminal when you open a file that is a terminal device; so
to be portable, use @code{O_NOCTTY} when it is important to avoid this.
@cindex controlling terminal, setting
@end deftypevr

The following three file name translation flags exist only in the GNU system.

@comment fcntl.h
@comment GNU
@deftypevr Macro int O_IGNORE_CTTY
Do not recognize the named file as the controlling terminal, even if it
refers to the process's existing controlling terminal device.  Operations
on the new file descriptor will never induce job control signals.
@xref{Job Control}.
@end deftypevr

@comment fcntl.h
@comment GNU
@deftypevr Macro int O_NOLINK
If the named file is a symbolic link, open the link itself instead of
the file it refers to.  (@code{fstat} on the new file descriptor will
return the information returned by @code{lstat} on the link's name.)
@cindex symbolic link, opening
@end deftypevr

@comment fcntl.h
@comment GNU
@deftypevr Macro int O_NOTRANS
If the named file is specially translated, do not invoke the translator.
Open the bare file the translator itself sees.
@end deftypevr


The open-time action flags tell @code{open} to do additional operations
which are not really related to opening the file.  The reason to do them
as part of @code{open} instead of in separate calls is that @code{open}
can do them @i{atomically}.

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_TRUNC
Truncate the file to zero length.  This option is only useful for
regular files, not special files such as directories or FIFOs.  POSIX.1
requires that you open the file for writing to use @code{O_TRUNC}.  In
BSD and GNU you must have permission to write the file to truncate it,
but you need not open for write access.

This is the only open-time action flag specified by POSIX.1.  There is
no good reason for truncation to be done by @code{open}, instead of by
calling @code{ftruncate} afterwards.  The @code{O_TRUNC} flag existed in
Unix before @code{ftruncate} was invented, and is retained for backward
compatibility.
@end deftypevr

@comment fcntl.h
@comment BSD
@deftypevr Macro int O_SHLOCK
Acquire a shared lock on the file, as with @code{flock}.
@xref{File Locks}.

If @code{O_CREAT} is specified, the locking is done atomically when
creating the file.  You are guaranteed that no other process will get
the lock on the new file first.
@end deftypevr

@comment fcntl.h
@comment BSD
@deftypevr Macro int O_EXLOCK
Acquire an exclusive lock on the file, as with @code{flock}.
@xref{File Locks}.  This is atomic like @code{O_SHLOCK}.
@end deftypevr

@node Operating Modes
@subsection I/O Operating Modes

The operating modes affect how input and output operations using a file
descriptor work.  These flags are set by @code{open} and can be fetched
and changed with @code{fcntl}.

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_APPEND
The bit that enables append mode for the file.  If set, then all
@code{write} operations write the data at the end of the file, extending
it, regardless of the current file position.  This is the only reliable
way to append to a file.  In append mode, you are guaranteed that the
data you write will always go to the current end of the file, regardless
of other processes writing to the file.  Conversely, if you simply set
the file position to the end of file and write, then another process can
extend the file after you set the file position but before you write,
resulting in your data appearing someplace before the real end of file.
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int O_NONBLOCK
The bit that enables nonblocking mode for the file.  If this bit is set,
@code{read} requests on the file can return immediately with a failure
status if there is no input immediately available, instead of blocking.
Likewise, @code{write} requests can also return immediately with a
failure status if the output can't be written immediately.

Note that the @code{O_NONBLOCK} flag is overloaded as both an I/O
operating mode and a file name translation flag; @pxref{Open-time Flags}.
@end deftypevr

@comment fcntl.h
@comment BSD
@deftypevr Macro int O_NDELAY
This is an obsolete name for @code{O_NONBLOCK}, provided for
compatibility with BSD.  It is not defined by the POSIX.1 standard.
@end deftypevr

The remaining operating modes are BSD and GNU extensions.  They exist only
on some systems.  On other systems, these macros are not defined.

@comment fcntl.h
@comment BSD
@deftypevr Macro int O_ASYNC
The bit that enables asynchronous input mode.  If set, then @code{SIGIO}
signals will be generated when input is available.  @xref{Interrupt Input}.

Asynchronous input mode is a BSD feature.
@end deftypevr

@comment fcntl.h
@comment BSD
@deftypevr Macro int O_FSYNC
The bit that enables synchronous writing for the file.  If set, each
@code{write} call will make sure the data is reliably stored on disk before
returning. @c !!! xref fsync

Synchronous writing is a BSD feature.
@end deftypevr

@comment fcntl.h
@comment BSD
@deftypevr Macro int O_SYNC
This is another name for @code{O_FSYNC}.  They have the same value.
@end deftypevr

@comment fcntl.h
@comment GNU
@deftypevr Macro int O_NOATIME
If this bit is set, @code{read} will not update the access time of the
file.  @xref{File Times}.  This is used by programs that do backups, so
that backing a file up does not count as reading it.
Only the owner of the file or the superuser may use this bit.

This is a GNU extension.
@end deftypevr

@node Getting File Status Flags
@subsection Getting and Setting File Status Flags

The @code{fcntl} function can fetch or change file status flags.

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_GETFL
This macro is used as the @var{command} argument to @code{fcntl}, to
read the file status flags for the open file with descriptor
@var{filedes}.

The normal return value from @code{fcntl} with this command is a
nonnegative number which can be interpreted as the bitwise OR of the
individual flags.  Since the file access modes are not single-bit values,
you can mask off other bits in the returned flags with @code{O_ACCMODE}
to compare them.

In case of an error, @code{fcntl} returns @code{-1}.  The following
@code{errno} error conditions are defined for this command:

@table @code
@item EBADF
The @var{filedes} argument is invalid.
@end table
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_SETFL
This macro is used as the @var{command} argument to @code{fcntl}, to set
the file status flags for the open file corresponding to the
@var{filedes} argument.  This command requires a third @code{int}
argument to specify the new flags, so the call looks like this:

@smallexample
fcntl (@var{filedes}, F_SETFL, @var{new-flags})
@end smallexample

You can't change the access mode for the file in this way; that is,
whether the file descriptor was opened for reading or writing.

The normal return value from @code{fcntl} with this command is an
unspecified value other than @code{-1}, which indicates an error.  The
error conditions are the same as for the @code{F_GETFL} command.
@end deftypevr

If you want to modify the file status flags, you should get the current
flags with @code{F_GETFL} and modify the value.  Don't assume that the
flags listed here are the only ones that are implemented; your program
may be run years from now and more flags may exist then.  For example,
here is a function to set or clear the flag @code{O_NONBLOCK} without
altering any other flags:

@smallexample
@group
/* @r{Set the @code{O_NONBLOCK} flag of @var{desc} if @var{value} is nonzero,}
   @r{or clear the flag if @var{value} is 0.}
   @r{Return 0 on success, or -1 on error with @code{errno} set.} */

int
set_nonblock_flag (int desc, int value)
@{
  int oldflags = fcntl (desc, F_GETFL, 0);
  /* @r{If reading the flags failed, return error indication now.} */
  if (oldflags == -1)
    return -1;
  /* @r{Set just the flag we want to set.} */
  if (value != 0)
    oldflags |= O_NONBLOCK;
  else
    oldflags &= ~O_NONBLOCK;
  /* @r{Store modified flag word in the descriptor.} */
  return fcntl (desc, F_SETFL, oldflags);
@}
@end group
@end smallexample

@node File Locks
@section File Locks

@cindex file locks
@cindex record locking
The remaining @code{fcntl} commands are used to support @dfn{record
locking}, which permits multiple cooperating programs to prevent each
other from simultaneously accessing parts of a file in error-prone
ways.

@cindex exclusive lock
@cindex write lock
An @dfn{exclusive} or @dfn{write} lock gives a process exclusive access
for writing to the specified part of the file.  While a write lock is in
place, no other process can lock that part of the file.

@cindex shared lock
@cindex read lock
A @dfn{shared} or @dfn{read} lock prohibits any other process from
requesting a write lock on the specified part of the file.  However,
other processes can request read locks.

The @code{read} and @code{write} functions do not actually check to see
whether there are any locks in place.  If you want to implement a
locking protocol for a file shared by multiple processes, your application
must do explicit @code{fcntl} calls to request and clear locks at the
appropriate points.

Locks are associated with processes.  A process can only have one kind
of lock set for each byte of a given file.  When any file descriptor for
that file is closed by the process, all of the locks that process holds
on that file are released, even if the locks were made using other
descriptors that remain open.  Likewise, locks are released when a
process exits, and are not inherited by child processes created using
@code{fork} (@pxref{Creating a Process}).

When making a lock, use a @code{struct flock} to specify what kind of
lock and where.  This data type and the associated macros for the
@code{fcntl} function are declared in the header file @file{fcntl.h}.
@pindex fcntl.h

@comment fcntl.h
@comment POSIX.1
@deftp {Data Type} {struct flock}
This structure is used with the @code{fcntl} function to describe a file
lock.  It has these members:

@table @code
@item short int l_type
Specifies the type of the lock; one of @code{F_RDLCK}, @code{F_WRLCK}, or
@code{F_UNLCK}.

@item short int l_whence
This corresponds to the @var{whence} argument to @code{fseek} or
@code{lseek}, and specifies what the offset is relative to.  Its value
can be one of @code{SEEK_SET}, @code{SEEK_CUR}, or @code{SEEK_END}.

@item off_t l_start
This specifies the offset of the start of the region to which the lock
applies, and is given in bytes relative to the point specified by
@code{l_whence} member.

@item off_t l_len
This specifies the length of the region to be locked.  A value of
@code{0} is treated specially; it means the region extends to the end of
the file.

@item pid_t l_pid
This field is the process ID (@pxref{Process Creation Concepts}) of the
process holding the lock.  It is filled in by calling @code{fcntl} with
the @code{F_GETLK} command, but is ignored when making a lock.
@end table
@end deftp

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_GETLK
This macro is used as the @var{command} argument to @code{fcntl}, to
specify that it should get information about a lock.  This command
requires a third argument of type @w{@code{struct flock *}} to be passed
to @code{fcntl}, so that the form of the call is:

@smallexample
fcntl (@var{filedes}, F_GETLK, @var{lockp})
@end smallexample

If there is a lock already in place that would block the lock described
by the @var{lockp} argument, information about that lock overwrites
@code{*@var{lockp}}.  Existing locks are not reported if they are
compatible with making a new lock as specified.  Thus, you should
specify a lock type of @code{F_WRLCK} if you want to find out about both
read and write locks, or @code{F_RDLCK} if you want to find out about
write locks only.

There might be more than one lock affecting the region specified by the
@var{lockp} argument, but @code{fcntl} only returns information about
one of them.  The @code{l_whence} member of the @var{lockp} structure is
set to @code{SEEK_SET} and the @code{l_start} and @code{l_len} fields
set to identify the locked region.

If no lock applies, the only change to the @var{lockp} structure is to
update the @code{l_type} to a value of @code{F_UNLCK}.

The normal return value from @code{fcntl} with this command is an
unspecified value other than @code{-1}, which is reserved to indicate an
error.  The following @code{errno} error conditions are defined for
this command:

@table @code
@item EBADF
The @var{filedes} argument is invalid.

@item EINVAL
Either the @var{lockp} argument doesn't specify valid lock information,
or the file associated with @var{filedes} doesn't support locks.
@end table
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_SETLK
This macro is used as the @var{command} argument to @code{fcntl}, to
specify that it should set or clear a lock.  This command requires a
third argument of type @w{@code{struct flock *}} to be passed to
@code{fcntl}, so that the form of the call is:

@smallexample
fcntl (@var{filedes}, F_SETLK, @var{lockp})
@end smallexample

If the process already has a lock on any part of the region, the old lock
on that part is replaced with the new lock.  You can remove a lock
by specifying a lock type of @code{F_UNLCK}.

If the lock cannot be set, @code{fcntl} returns immediately with a value
of @code{-1}.  This function does not block waiting for other processes
to release locks.  If @code{fcntl} succeeds, it return a value other
than @code{-1}.

The following @code{errno} error conditions are defined for this
function:

@table @code
@item EAGAIN
@itemx EACCES
The lock cannot be set because it is blocked by an existing lock on the
file.  Some systems use @code{EAGAIN} in this case, and other systems
use @code{EACCES}; your program should treat them alike, after
@code{F_SETLK}.  (The GNU system always uses @code{EAGAIN}.)

@item EBADF
Either: the @var{filedes} argument is invalid; you requested a read lock
but the @var{filedes} is not open for read access; or, you requested a
write lock but the @var{filedes} is not open for write access.

@item EINVAL
Either the @var{lockp} argument doesn't specify valid lock information,
or the file associated with @var{filedes} doesn't support locks.

@item ENOLCK
The system has run out of file lock resources; there are already too
many file locks in place.

Well-designed file systems never report this error, because they have no
limitation on the number of locks.  However, you must still take account
of the possibility of this error, as it could result from network access
to a file system on another machine.
@end table
@end deftypevr

@comment fcntl.h
@comment POSIX.1
@deftypevr Macro int F_SETLKW
This macro is used as the @var{command} argument to @code{fcntl}, to
specify that it should set or clear a lock.  It is just like the
@code{F_SETLK} command, but causes the process to block (or wait)
until the request can be specified.

This command requires a third argument of type @code{struct flock *}, as
for the @code{F_SETLK} command.

The @code{fcntl} return values and errors are the same as for the
@code{F_SETLK} command, but these additional @code{errno} error conditions
are defined for this command:

@table @code
@item EINTR
The function was interrupted by a signal while it was waiting.
@xref{Interrupted Primitives}.

@item EDEADLK
The specified region is being locked by another process.  But that
process is waiting to lock a region which the current process has
locked, so waiting for the lock would result in deadlock.  The system
does not guarantee that it will detect all such conditions, but it lets
you know if it notices one.
@end table
@end deftypevr


The following macros are defined for use as values for the @code{l_type}
member of the @code{flock} structure.  The values are integer constants.

@table @code
@comment fcntl.h
@comment POSIX.1
@vindex F_RDLCK
@item F_RDLCK
This macro is used to specify a read (or shared) lock.

@comment fcntl.h
@comment POSIX.1
@vindex F_WRLCK
@item F_WRLCK
This macro is used to specify a write (or exclusive) lock.

@comment fcntl.h
@comment POSIX.1
@vindex F_UNLCK
@item F_UNLCK
This macro is used to specify that the region is unlocked.
@end table

As an example of a situation where file locking is useful, consider a
program that can be run simultaneously by several different users, that
logs status information to a common file.  One example of such a program
might be a game that uses a file to keep track of high scores.  Another
example might be a program that records usage or accounting information
for billing purposes.

Having multiple copies of the program simultaneously writing to the
file could cause the contents of the file to become mixed up.  But
you can prevent this kind of problem by setting a write lock on the
file before actually writing to the file.

If the program also needs to read the file and wants to make sure that
the contents of the file are in a consistent state, then it can also use
a read lock.  While the read lock is set, no other process can lock
that part of the file for writing.

@c ??? This section could use an example program.

Remember that file locks are only a @emph{voluntary} protocol for
controlling access to a file.  There is still potential for access to
the file by programs that don't use the lock protocol.

@node Interrupt Input
@section Interrupt-Driven Input

@cindex interrupt-driven input
If you set the @code{O_ASYNC} status flag on a file descriptor
(@pxref{File Status Flags}), a @code{SIGIO} signal is sent whenever
input or output becomes possible on that file descriptor.  The process
or process group to receive the signal can be selected by using the
@code{F_SETOWN} command to the @code{fcntl} function.  If the file
descriptor is a socket, this also selects the recipient of @code{SIGURG}
signals that are delivered when out-of-band data arrives on that socket;
see @ref{Out-of-Band Data}.  (@code{SIGURG} is sent in any situation
where @code{select} would report the socket as having an ``exceptional
condition''.  @xref{Waiting for I/O}.)

If the file descriptor corresponds to a terminal device, then @code{SIGIO}
signals are sent to the foreground process group of the terminal.
@xref{Job Control}.

@pindex fcntl.h
The symbols in this section are defined in the header file
@file{fcntl.h}.

@comment fcntl.h
@comment BSD
@deftypevr Macro int F_GETOWN
This macro is used as the @var{command} argument to @code{fcntl}, to
specify that it should get information about the process or process
group to which @code{SIGIO} signals are sent.  (For a terminal, this is
actually the foreground process group ID, which you can get using
@code{tcgetpgrp}; see @ref{Terminal Access Functions}.)

The return value is interpreted as a process ID; if negative, its
absolute value is the process group ID.

The following @code{errno} error condition is defined for this command:

@table @code
@item EBADF
The @var{filedes} argument is invalid.
@end table
@end deftypevr

@comment fcntl.h
@comment BSD
@deftypevr Macro int F_SETOWN
This macro is used as the @var{command} argument to @code{fcntl}, to
specify that it should set the process or process group to which
@code{SIGIO} signals are sent.  This command requires a third argument
of type @code{pid_t} to be passed to @code{fcntl}, so that the form of
the call is:

@smallexample
fcntl (@var{filedes}, F_SETOWN, @var{pid})
@end smallexample

The @var{pid} argument should be a process ID.  You can also pass a
negative number whose absolute value is a process group ID.

The return value from @code{fcntl} with this command is @code{-1}
in case of error and some other value if successful.  The following
@code{errno} error conditions are defined for this command:

@table @code
@item EBADF
The @var{filedes} argument is invalid.

@item ESRCH
There is no process or process group corresponding to @var{pid}.
@end table
@end deftypevr

@c ??? This section could use an example program.