1 /**
2  * D header file for POSIX.
3  *
4  * Copyright: Copyright Sean Kelly 2005 - 2009.
5  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Sean Kelly, Alex Rønne Petersen
7  * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
8  */
9 
10 /*          Copyright Sean Kelly 2005 - 2009.
11  * Distributed under the Boost Software License, Version 1.0.
12  *    (See accompanying file LICENSE or copy at
13  *          http://www.boost.org/LICENSE_1_0.txt)
14  */
15 module core.sys.posix.fcntl;
16 
17 import core.sys.posix.config;
18 import core.stdc.stdint;
19 public import core.sys.posix.sys.types; // for off_t, mode_t
20 public import core.sys.posix.sys.stat;  // for S_IFMT, etc.
21 
22 version (OSX)
23     version = Darwin;
24 else version (iOS)
25     version = Darwin;
26 else version (TVOS)
27     version = Darwin;
28 else version (WatchOS)
29     version = Darwin;
30 
31 version (ARM)     version = ARM_Any;
32 version (AArch64) version = ARM_Any;
33 version (HPPA)    version = HPPA_Any;
34 version (MIPS32)  version = MIPS_Any;
35 version (MIPS64)  version = MIPS_Any;
36 version (PPC)     version = PPC_Any;
37 version (PPC64)   version = PPC_Any;
38 version (RISCV32) version = RISCV_Any;
39 version (RISCV64) version = RISCV_Any;
40 version (S390)    version = IBMZ_Any;
41 version (SPARC)   version = SPARC_Any;
42 version (SPARC64) version = SPARC_Any;
43 version (SystemZ) version = IBMZ_Any;
44 version (X86)     version = X86_Any;
45 version (X86_64)  version = X86_Any;
46 
47 version (Posix):
48 extern (C):
49 
50 nothrow:
51 @nogc:
52 
53 //
54 // Required
55 //
56 /*
57 F_DUPFD
58 F_GETFD
59 F_SETFD
60 F_GETFL
61 F_SETFL
62 F_GETLK
63 F_SETLK
64 F_SETLKW
65 F_GETOWN
66 F_SETOWN
67 
68 FD_CLOEXEC
69 
70 F_RDLCK
71 F_UNLCK
72 F_WRLCK
73 
74 O_CREAT
75 O_EXCL
76 O_NOCTTY
77 O_TRUNC
78 
79 O_APPEND
80 O_DSYNC
81 O_NONBLOCK
82 O_RSYNC
83 O_SYNC
84 
85 O_ACCMODE
86 O_RDONLY
87 O_RDWR
88 O_WRONLY
89 
90 struct flock
91 {
92     short   l_type;
93     short   l_whence;
94     off_t   l_start;
95     off_t   l_len;
96     pid_t   l_pid;
97 }
98 */
99 version (linux)
100 {
101     enum F_DUPFD        = 0;
102     enum F_GETFD        = 1;
103     enum F_SETFD        = 2;
104     enum F_GETFL        = 3;
105     enum F_SETFL        = 4;
106   version (X86_64)
107   {
108     static assert(off_t.sizeof == 8);
109     enum F_GETLK        = 5;
110     enum F_SETLK        = 6;
111     enum F_SETLKW       = 7;
112   }
113   else version (AArch64)
114   {
115     enum F_GETLK        = 5;
116     enum F_SETLK        = 6;
117     enum F_SETLKW       = 7;
118   }
119   else version (PPC64)
120   {
121     enum F_GETLK        = 5;
122     enum F_SETLK        = 6;
123     enum F_SETLKW       = 7;
124   }
125   else version (RISCV64)
126   {
127     enum F_GETLK        = 5;
128     enum F_SETLK        = 6;
129     enum F_SETLKW       = 7;
130   }
131   else version (SystemZ)
132   {
133     static assert(off_t.sizeof == 8);
134     enum F_GETLK        = 5;
135     enum F_SETLK        = 6;
136     enum F_SETLKW       = 7;
137   }
138   else version (MIPS_N64)
139   {
140     enum F_GETLK        = 14;
141     enum F_SETLK        = 6;
142     enum F_SETLKW       = 7;
143   }
144   else version (MIPS_Any)
145   {
146     static if ( __USE_FILE_OFFSET64 )
147     {
148       enum F_GETLK      = 33;
149       enum F_SETLK      = 34;
150       enum F_SETLKW     = 35;
151     }
152     else
153     {
154       enum F_GETLK      = 14;
155       enum F_SETLK      = 6;
156       enum F_SETLKW     = 7;
157     }
158   }
159   else version (LoongArch64)
160   {
161     static assert(off_t.sizeof == 8);
162     enum F_GETLK        = 5;
163     enum F_SETLK        = 6;
164     enum F_SETLKW       = 7;
165   } else
166   static if ( __USE_FILE_OFFSET64 )
167   {
168     enum F_GETLK        = 12;
169     enum F_SETLK        = 13;
170     enum F_SETLKW       = 14;
171   }
172   else
173   {
174     enum F_GETLK        = 5;
175     enum F_SETLK        = 6;
176     enum F_SETLKW       = 7;
177   }
178     enum F_GETOWN       = 9;
179     enum F_SETOWN       = 8;
180 
181     enum FD_CLOEXEC     = 1;
182 
183     enum F_RDLCK        = 0;
184     enum F_UNLCK        = 2;
185     enum F_WRLCK        = 1;
186 
187     version (X86_Any)
188     {
189         enum O_CREAT        = 0x40;     // octal     0100
190         enum O_EXCL         = 0x80;     // octal     0200
191         enum O_NOCTTY       = 0x100;    // octal     0400
192         enum O_TRUNC        = 0x200;    // octal    01000
193 
194         enum O_APPEND       = 0x400;    // octal    02000
195         enum O_NONBLOCK     = 0x800;    // octal    04000
196         enum O_CLOEXEC      = 0x80000;  // octal 02000000
197         enum O_SYNC         = 0x101000; // octal 04010000
198         enum O_DSYNC        = 0x1000;   // octal   010000
199         enum O_RSYNC        = O_SYNC;
200 
201         enum O_DIRECTORY    = 0x010000; // octal   0200000
202         enum O_NOFOLLOW     = 0x020000; // octal   0400000
203         enum O_DIRECT       = 0x004000; // octal    040000
204         version (X86_64)
205             enum O_LARGEFILE = 0;
206         else
207             enum O_LARGEFILE = 0x08000; // octal   0100000
208         enum O_TMPFILE      = 0x410000; // octal 020200000
209         enum O_ASYNC        = 0x2000;   // octal    020000
210         enum O_NOATIME      = 0x40000;  // octal  01000000
211         enum O_PATH         = 0x200000; // octal 010000000
212         enum O_NDELAY       = O_NONBLOCK;
213     }
214     else version (HPPA_Any)
215     {
216         enum O_CREAT        = 0x00100;  // octal    04000
217         enum O_EXCL         = 0x00400;  // octal     0200
218         enum O_NOCTTY       = 0x20000;  // octal     0400
219         enum O_TRUNC        = 0x00200;  // octal    01000
220 
221         enum O_APPEND       = 0x00008;  // octal      010
222         enum O_NONBLOCK     = 0x10004;  // octal  0200004
223         enum O_CLOEXEC      = 0x200000; // octal 02000000
224         enum O_SYNC         = 0x48000;  // octal 01100000
225         enum O_DSYNC        = 0x40000;  // octal 01000000
226         enum O_RSYNC        = 0x80000;  // octal 02000000
227 
228         enum O_DIRECTORY    = 0x001000; // octal 000010000
229         enum O_NOFOLLOW     = 0x000080; // octal 000000200
230         enum O_DIRECT       = 0x004000; // octal    040000
231         enum O_LARGEFILE    = 0x000800; // octal  00004000
232         enum O_TMPFILE      = 0x801000; // octal 040010000
233         enum O_ASYNC        = 0x2000;   // octal    020000
234         enum O_NOATIME      = 0x100000; // octal 004000000
235         enum O_PATH         = 0x400000; // octal 020000000
236         enum O_NDELAY       = O_NONBLOCK;
237     }
238     else version (MIPS_Any)
239     {
240         enum O_CREAT        = 0x0100;
241         enum O_EXCL         = 0x0400;
242         enum O_NOCTTY       = 0x0800;
243         enum O_TRUNC        = 0x0200;
244 
245         enum O_APPEND       = 0x0008;
246         enum O_DSYNC        = 0x0010;
247         enum O_NONBLOCK     = 0x0080;
248         enum O_CLOEXEC      = 0x80000;
249         enum O_RSYNC        = O_SYNC;
250         enum O_SYNC         = 0x4010;
251 
252         enum O_DIRECTORY    = 0x010000;
253         enum O_NOFOLLOW     = 0x020000;
254         enum O_DIRECT       = 0x8000;
255         version (MIPS_N64)
256             enum O_LARGEFILE = 0;
257         else
258             enum O_LARGEFILE = 0x2000;
259         enum O_TMPFILE      = 0x410000;
260         enum O_ASYNC        = 0x1000;
261         enum O_NOATIME      = 0x40000;
262         enum O_PATH         = 0x200000;
263         enum O_NDELAY       = O_NONBLOCK;
264     }
265     else version (PPC_Any)
266     {
267         enum O_CREAT        = 0x40;     // octal     0100
268         enum O_EXCL         = 0x80;     // octal     0200
269         enum O_NOCTTY       = 0x100;    // octal     0400
270         enum O_TRUNC        = 0x200;    // octal    01000
271 
272         enum O_APPEND       = 0x400;    // octal    02000
273         enum O_NONBLOCK     = 0x800;    // octal    04000
274         enum O_CLOEXEC      = 0x80000;  // octal 02000000
275         enum O_SYNC         = 0x101000; // octal 04010000
276         enum O_DSYNC        = 0x1000;   // octal   010000
277         enum O_RSYNC        = O_SYNC;
278 
279         enum O_DIRECTORY    = 0x004000; // octal    040000
280         enum O_NOFOLLOW     = 0x008000; // octal   0100000
281         enum O_DIRECT       = 0x020000; // octal   0400000
282         version (D_LP64)
283             enum O_LARGEFILE = 0;
284         else
285             enum O_LARGEFILE = 0x10000; // octal   0200000
286         enum O_TMPFILE      = 0x404000; // octal 020040000
287         enum O_ASYNC        = 0x2000;   // octal    020000
288         enum O_NOATIME      = 0x40000;  // octal  01000000
289         enum O_PATH         = 0x200000;
290         enum O_NDELAY       = O_NONBLOCK;
291     }
292     else version (ARM_Any)
293     {
294         enum O_CREAT        = 0x40;     // octal     0100
295         enum O_EXCL         = 0x80;     // octal     0200
296         enum O_NOCTTY       = 0x100;    // octal     0400
297         enum O_TRUNC        = 0x200;    // octal    01000
298 
299         enum O_APPEND       = 0x400;    // octal    02000
300         enum O_NONBLOCK     = 0x800;    // octal    04000
301         enum O_CLOEXEC      = 0x80000;  // octal 02000000
302         enum O_SYNC         = 0x101000; // octal 04010000
303         enum O_DSYNC        = 0x1000;   // octal   010000
304         enum O_RSYNC        = O_SYNC;
305 
306         enum O_DIRECTORY    = 0x004000; // octal    040000
307         enum O_NOFOLLOW     = 0x008000; // octal   0100000
308         enum O_DIRECT       = 0x010000; // octal   0200000
309         version (D_LP64)
310             enum O_LARGEFILE = 0;
311         else
312             enum O_LARGEFILE = 0x20000; // octal   0400000
313         enum O_TMPFILE      = 0x404000; // octal 020040000
314         enum O_ASYNC        = 0x2000;   // octal    020000
315         enum O_NOATIME      = 0x40000;  // octal  01000000
316         enum O_PATH         = 0x200000; // octal 010000000
317         enum O_NDELAY       = O_NONBLOCK;
318     }
319     else version (RISCV_Any)
320     {
321         enum O_CREAT        = 0x40;     // octal     0100
322         enum O_EXCL         = 0x80;     // octal     0200
323         enum O_NOCTTY       = 0x100;    // octal     0400
324         enum O_TRUNC        = 0x200;    // octal    01000
325 
326         enum O_APPEND       = 0x400;    // octal    02000
327         enum O_NONBLOCK     = 0x800;    // octal    04000
328         enum O_CLOEXEC      = 0x80000;  // octal 02000000
329         enum O_SYNC         = 0x101000; // octal 04010000
330         enum O_DSYNC        = 0x1000;   // octal   010000
331         enum O_RSYNC        = O_SYNC;
332 
333         enum O_DIRECTORY    = 0x010000;
334         enum O_NOFOLLOW     = 0x020000;
335         enum O_DIRECT       = 0x004000;
336         version (D_LP64)
337             enum O_LARGEFILE = 0;
338         else
339             enum O_LARGEFILE = 0x8000;
340         enum O_TMPFILE      = 0x410000;
341         enum O_ASYNC        = 0x2000;
342         enum O_NOATIME      = 0x40000;
343         enum O_PATH         = 0x200000;
344         enum O_NDELAY       = O_NONBLOCK;
345     }
346     else version (SPARC_Any)
347     {
348         enum O_CREAT        = 0x200;
349         enum O_EXCL         = 0x800;
350         enum O_NOCTTY       = 0x8000;
351         enum O_TRUNC        = 0x400;
352 
353         enum O_APPEND       = 0x8;
354         enum O_NONBLOCK     = 0x4000;
355         enum O_CLOEXEC      = 0x400000;
356         enum O_SYNC         = 0x802000;
357         enum O_DSYNC        = 0x2000;
358         enum O_RSYNC        = O_SYNC;
359 
360         enum O_DIRECTORY    = 0x10000;
361         enum O_NOFOLLOW     = 0x20000;
362         enum O_DIRECT       = 0x100000;
363         version (D_LP64)
364             enum O_LARGEFILE = 0;
365         else
366             enum O_LARGEFILE = 0x40000;
367         enum O_TMPFILE      = 0x2010000;
368         enum O_ASYNC        = 0x0040;
369         enum O_NOATIME      = 0x200000;
370         enum O_PATH         = 0x1000000;
371         enum O_NDELAY       = (0x0004|O_NONBLOCK);
372     }
373     else version (IBMZ_Any)
374     {
375         enum O_CREAT        = 0x40;     // octal     0100
376         enum O_EXCL         = 0x80;     // octal     0200
377         enum O_NOCTTY       = 0x100;    // octal     0400
378         enum O_TRUNC        = 0x200;    // octal    01000
379 
380         enum O_APPEND       = 0x400;    // octal    02000
381         enum O_NONBLOCK     = 0x800;    // octal    04000
382         enum O_CLOEXEC      = 0x80000;  // octal 02000000
383         enum O_SYNC         = 0x101000; // octal 04010000
384         enum O_DSYNC        = 0x1000;   // octal   010000
385         enum O_RSYNC        = O_SYNC;
386 
387         enum O_DIRECTORY    = 0x010000; // octal   0200000
388         enum O_NOFOLLOW     = 0x020000; // octal   0400000
389         enum O_DIRECT       = 0x004000; // octal    040000
390         version (D_LP64)
391             enum O_LARGEFILE = 0;
392         else
393             enum O_LARGEFILE = 0x08000; // octal   0100000
394         enum O_TMPFILE      = 0x410000; // octal 020200000
395         enum O_ASYNC        = 0x2000;   // octal    020000
396         enum O_NOATIME      = 0x40000;  // octal  01000000
397         enum O_PATH         = 0x200000; // octal 010000000
398         enum O_NDELAY       = O_NONBLOCK;
399     }
400     else version (LoongArch64)
401     {
402         enum O_CREAT        = 0x40;     // octal     0100
403         enum O_EXCL         = 0x80;     // octal     0200
404         enum O_NOCTTY       = 0x100;    // octal     0400
405         enum O_TRUNC        = 0x200;    // octal    01000
406 
407         enum O_APPEND       = 0x400;    // octal    02000
408         enum O_NONBLOCK     = 0x800;    // octal    04000
409         enum O_CLOEXEC      = 0x80000;  // octal 02000000
410         enum O_SYNC         = 0x101000; // octal 04010000
411         enum O_DSYNC        = 0x1000;   // octal   010000
412         enum O_RSYNC        = O_SYNC;
413 
414         enum O_DIRECTORY    = 0x010000; // octal    200000
415         enum O_NOFOLLOW     = 0x020000; // octal    400000
416         enum O_DIRECT       = 0x004000; // octal    040000
417         version (D_LP64)
418             enum O_LARGEFILE = 0;
419         else
420             enum O_LARGEFILE = 0x8000;  // octal   0100000
421         enum O_TMPFILE      = 0x404000; // octal 020040000
422         enum O_ASYNC        = 0x2000;   // octal    020000
423         enum O_NOATIME      = 0x40000;  // octal  01000000
424         enum O_PATH         = 0x200000; // octal 010000000
425         enum O_NDELAY       = O_NONBLOCK;
426     }
427     else
428         static assert(0, "unimplemented");
429 
430     version (CRuntime_Musl)
431     {
432         enum O_SEARCH   = O_PATH;
433         enum O_EXEC     = O_PATH;
434         enum O_ACCMODE  = (3|O_SEARCH);
435     }
436     else
437     {
438         enum O_ACCMODE  = 0x3;
439     }
440     enum O_RDONLY       = 0x0;
441     enum O_WRONLY       = 0x1;
442     enum O_RDWR         = 0x2;
443 
444     struct flock
445     {
446         short   l_type;
447         short   l_whence;
448         off_t   l_start;
449         off_t   l_len;
450         pid_t   l_pid;
451     }
452 
453     enum AT_SYMLINK_NOFOLLOW = 0x100;
454     enum AT_FDCWD = -100;
455     enum AT_REMOVEDIR = 0x200;
456     enum AT_SYMLINK_FOLLOW = 0x400;
457     enum AT_EACCESS = 0x200;
458 }
459 else version (Darwin)
460 {
461     enum F_DUPFD        = 0;
462     enum F_GETFD        = 1;
463     enum F_SETFD        = 2;
464     enum F_GETFL        = 3;
465     enum F_SETFL        = 4;
466     enum F_GETOWN       = 5;
467     enum F_SETOWN       = 6;
468     enum F_GETLK        = 7;
469     enum F_SETLK        = 8;
470     enum F_SETLKW       = 9;
471 
472     enum FD_CLOEXEC     = 1;
473 
474     enum F_RDLCK        = 1;
475     enum F_UNLCK        = 2;
476     enum F_WRLCK        = 3;
477 
478     enum O_CREAT        = 0x0200;
479     enum O_EXCL         = 0x0800;
480     enum O_NOCTTY       = 0;
481     enum O_TRUNC        = 0x0400;
482 
483     enum O_RDONLY       = 0x0000;
484     enum O_WRONLY       = 0x0001;
485     enum O_RDWR         = 0x0002;
486     enum O_ACCMODE      = 0x0003;
487 
488     enum O_NONBLOCK     = 0x0004;
489     enum O_APPEND       = 0x0008;
490     enum O_SYNC         = 0x0080;
491     //enum O_DSYNC
492     //enum O_RSYNC
493 
494     struct flock
495     {
496         off_t   l_start;
497         off_t   l_len;
498         pid_t   l_pid;
499         short   l_type;
500         short   l_whence;
501     }
502 }
503 else version (FreeBSD)
504 {
505     enum F_DUPFD        = 0;
506     enum F_GETFD        = 1;
507     enum F_SETFD        = 2;
508     enum F_GETFL        = 3;
509     enum F_SETFL        = 4;
510     enum F_GETOWN       = 5;
511     enum F_SETOWN       = 6;
512     enum F_GETLK        = 11;
513     enum F_SETLK        = 12;
514     enum F_SETLKW       = 13;
515     enum F_OGETLK       = 7;
516     enum F_OSETLK       = 8;
517     enum F_OSETLKW      = 9;
518     enum F_DUP2FD       = 10;
519 
520     enum FD_CLOEXEC     = 1;
521 
522     enum F_RDLCK        = 1;
523     enum F_UNLCK        = 2;
524     enum F_WRLCK        = 3;
525 
526     enum O_CREAT        = 0x0200;
527     enum O_EXCL         = 0x0800;
528     enum O_NOCTTY       = 0x8000;
529     enum O_TRUNC        = 0x0400;
530 
531     enum O_RDONLY       = 0x0000;
532     enum O_WRONLY       = 0x0001;
533     enum O_RDWR         = 0x0002;
534     enum O_ACCMODE      = 0x0003;
535 
536     enum O_NONBLOCK     = 0x0004;
537     enum O_APPEND       = 0x0008;
538     enum O_SYNC         = 0x0080;
539     //enum O_DSYNC
540     //enum O_RSYNC
541 
542     struct flock
543     {
544         off_t   l_start;
545         off_t   l_len;
546         pid_t   l_pid;
547         short   l_type;
548         short   l_whence;
549         int     l_sysid;
550     }
551 
552     struct oflock
553     {
554         off_t   l_start;
555         off_t   l_len;
556         pid_t   l_pid;
557         short   l_type;
558         short   l_whence;
559     }
560 
561     enum AT_SYMLINK_NOFOLLOW = 0x200;
562     enum AT_FDCWD = -100;
563 }
564 else version (OpenBSD)
565 {
566     enum F_DUPFD        = 0;
567     enum F_GETFD        = 1;
568     enum F_SETFD        = 2;
569     enum F_GETFL        = 3;
570     enum F_SETFL        = 4;
571     enum F_GETOWN       = 5;
572     enum F_SETOWN       = 6;
573     enum F_GETLK        = 7;
574     enum F_SETLK        = 8;
575     enum F_SETLKW       = 9;
576     enum F_DUPFD_CLOEXEC= 10;
577     enum F_ISATTY       = 11;
578 
579     enum FD_CLOEXEC     = 1;
580 
581     enum F_RDLCK        = 1;
582     enum F_UNLCK        = 2;
583     enum F_WRLCK        = 3;
584 
585     enum O_CREAT        = 0x0200;
586     enum O_EXCL         = 0x0800;
587     enum O_NOCTTY       = 0x8000;
588     enum O_TRUNC        = 0x0400;
589 
590     enum O_RDONLY       = 0x0000;
591     enum O_WRONLY       = 0x0001;
592     enum O_RDWR         = 0x0002;
593     enum O_ACCMODE      = 0x0003;
594     enum O_SHLOCK       = 0x0010;
595     enum O_EXLOCK       = 0x0020;
596     enum O_ASYNC        = 0x0040;
597     enum O_FSYNC        = 0x0080;
598     enum O_NOFOLLOW     = 0x0100;
599 
600     enum O_NONBLOCK     = 0x0004;
601     enum O_APPEND       = 0x0008;
602     enum O_SYNC         = 0x0080;
603     enum O_DSYNC        = O_SYNC;
604     enum O_RSYNC        = O_SYNC;
605 
606     enum O_CLOEXEC      = 0x10000;
607     enum O_DIRECTORY    = 0x20000;
608 
609     enum LOCK_SH        = 0x01;
610     enum LOCK_EX        = 0x02;
611     enum LOCK_NB        = 0x04;
612     enum LOCK_UN        = 0x08;
613 
614     struct flock
615     {
616         off_t   l_start;
617         off_t   l_len;
618         pid_t   l_pid;
619         short   l_type;
620         short   l_whence;
621     }
622 
623     enum AT_FDCWD            = -100;
624 
625     enum AT_EACCESS          = 0x01;
626     enum AT_SYMLINK_NOFOLLOW = 0x02;
627     enum AT_SYMLINK_FOLLOW   = 0x04;
628     enum AT_REMOVEDIR        = 0x08;
629 }
630 else version (NetBSD)
631 {
632     enum F_DUPFD        = 0;
633     enum F_GETFD        = 1;
634     enum F_SETFD        = 2;
635     enum F_GETFL        = 3;
636     enum F_SETFL        = 4;
637     enum F_GETOWN       = 5;
638     enum F_SETOWN       = 6;
639     enum F_GETLK        = 7;
640     enum F_SETLK        = 8;
641     enum F_SETLKW       = 9;
642     enum F_CLOSEM       = 10;
643     enum F_MAXFD        = 11;
644     enum F_DUPFD_CLOEXEC= 12;
645     enum F_GETNOSIGPIPE = 13;
646     enum F_SETNOSIGPIPE = 14;
647 
648     enum FD_CLOEXEC     = 1;
649 
650     enum F_RDLCK        = 1;
651     enum F_UNLCK        = 2;
652     enum F_WRLCK        = 3;
653 
654     enum O_CREAT        = 0x0200;
655     enum O_EXCL         = 0x0800;
656     enum O_NOCTTY       = 0x8000;
657     enum O_TRUNC        = 0x0400;
658 
659     enum O_RDONLY       = 0x0000;
660     enum O_WRONLY       = 0x0001;
661     enum O_RDWR         = 0x0002;
662     enum O_ACCMODE      = 0x0003;
663 
664     enum O_NONBLOCK     = 0x0004;
665     enum O_APPEND       = 0x0008;
666     enum O_SYNC         = 0x0080;
667     //enum O_DSYNC
668     //enum O_RSYNC
669 
670     struct flock
671     {
672         off_t   l_start;
673         off_t   l_len;
674         pid_t   l_pid;
675         short   l_type;
676         short   l_whence;
677     }
678 }
679 else version (DragonFlyBSD)
680 {
681     enum O_RDONLY       = 0x0000;
682     enum O_WRONLY       = 0x0001;
683     enum O_RDWR         = 0x0002;
684     enum O_ACCMODE      = 0x0003;
685 
686     enum FREAD          = 0x0001;
687     enum FWRITE         = 0x0002;
688     enum O_NONBLOCK     = 0x0000004;
689     enum O_APPEND       = 0x0000008;
690     enum O_SHLOCK       = 0x0000010;
691     enum O_EXLOCK       = 0x0000020;
692     enum O_ASYNC        = 0x0000040;
693     enum O_FSYNC        = 0x0000080;
694     enum O_SYNC         = 0x0000080;
695     enum O_NOFOLLOW     = 0x0000100;
696     enum O_CREAT        = 0x0000200;
697     enum O_TRUNC        = 0x0000400;
698     enum O_EXCL         = 0x0000800;
699     enum O_NOCTTY       = 0x0008000;
700     enum O_DIRECT       = 0x0010000;
701     enum O_CLOEXEC      = 0x0020000;
702     enum O_FBLOCKING    = 0x0040000;
703     enum O_FNONBLOCKING = 0x0080000;
704     enum O_FAPPEND      = 0x0100000;
705     enum O_FOFFSET      = 0x0200000;
706     enum O_FSYNCWRITE   = 0x0400000;
707     enum O_FASYNCWRITE  = 0x0800000;
708     enum O_DIRECTORY    = 0x8000000;
709 
710     enum FAPPEND        = O_APPEND;
711     enum FASYNC         = O_ASYNC;
712     enum FFSYNC         = O_FSYNC;
713     enum FNONBLOCK      = O_NONBLOCK;
714     enum FNDELAY        = O_NONBLOCK;
715     enum O_NDELAY       = O_NONBLOCK;
716     enum FPOSIXSHM      = O_NOFOLLOW;
717 
718     enum FCNTLFLAGS = (FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FPOSIXSHM|O_DIRECT);
719 
720     enum F_DUPFD        = 0;
721     enum F_GETFD        = 1;
722     enum F_SETFD        = 2;
723     enum F_GETFL        = 3;
724     enum F_SETFL        = 4;
725     enum F_GETOWN       = 5;
726     enum F_SETOWN       = 6;
727     enum F_GETLK        = 7;
728 //    enum F_SETLK        = 8;
729     enum F_SETLK        = 8;
730     enum F_SETLKW       = 9;
731     enum F_OGETLK       = F_GETLK;
732     enum F_OSETLK       = F_SETLK;
733     enum F_OSETLKW      = F_SETLKW;
734     enum F_DUP2FD       = 10;
735     //enum F_GETLK        = 11;
736     //enum F_SETLK        = 12;
737     //enum F_SETLKW       = 13;
738     enum F_DUPFD_CLOEXEC = 17;
739     enum F_DUP2FD_CLOEXEC = 18;
740 
741     enum FD_CLOEXEC     = 1;
742 
743     enum F_RDLCK        = 1;
744     enum F_UNLCK        = 2;
745     enum F_WRLCK        = 3;
746 
747     enum LOCK_SH        = 0x01;
748     enum LOCK_EX        = 0x02;
749     enum LOCK_NB        = 0x04;
750     enum LOCK_UN        = 0x08;
751 
752     struct flock
753     {
754         off_t   l_start;
755         off_t   l_len;
756         pid_t   l_pid;
757         short   l_type;
758         short   l_whence;
759     }
760 
761     alias oflock = flock;
762 }
763 else version (Solaris)
764 {
765     enum F_DUPFD = 0;
766     enum F_GETFD = 1;
767     enum F_SETFD = 2;
768     enum F_GETFL = 3;
769     enum F_SETFL = 4;
770 
771     version (D_LP64)
772     {
773         enum F_GETLK = 14;
774         enum F_SETLK = 6;
775         enum F_SETLKW = 7;
776     }
777     else
778     {
779         static if (__USE_FILE_OFFSET64)
780         {
781             enum F_GETLK = 14;
782             enum F_SETLK = 6;
783             enum F_SETLKW = 7;
784         }
785         else
786         {
787             enum F_GETLK = 33;
788             enum F_SETLK = 34;
789             enum F_SETLKW = 35;
790         }
791     }
792 
793     enum F_GETOWN = 23;
794     enum F_SETOWN = 24;
795 
796     enum FD_CLOEXEC = 1;
797 
798     enum F_RDLCK = 1;
799     enum F_UNLCK = 3;
800     enum F_WRLCK = 2;
801     enum F_UNCKSYS = 4;
802 
803     enum O_CREAT = 0x0100;
804     enum O_EXCL = 0x0400;
805     enum O_NOCTTY = 0x0800;
806     enum O_TRUNC = 0x0200;
807 
808     enum O_APPEND = 0x0008;
809     enum O_NONBLOCK = 0x0080;
810     enum O_SYNC = 0x0010;
811     enum O_DSYNC = 0x0040;
812     enum O_RSYNC = 0x8000;
813 
814     enum O_ACCMODE = (O_SEARCH | O_EXEC | 0x3);
815     enum O_RDONLY = 0;
816     enum O_WRONLY = 1;
817     enum O_RDWR = 2;
818     enum O_SEARCH = 0x200000;
819     enum O_EXEC = 0x400000;
820 
821     struct flock
822     {
823         short l_type;
824         short l_whence;
825         off_t l_start;
826         off_t l_len;
827         int l_sysid;
828         pid_t l_pid;
829         c_long[4] l_pad;
830     }
831 
832     static if (__USE_LARGEFILE64)
833     {
834         struct flock64
835         {
836             short       l_type;
837             short       l_whence;
838             off64_t     l_start;
839             off64_t     l_len;
840             int         l_sysid;
841             pid_t       l_pid;
842             c_long[4]   l_pad;
843         }
844     }
845 }
846 else
847 {
848     static assert(false, "Unsupported platform");
849 }
850 
851 /*
852 int creat(const scope char*, mode_t);
853 int fcntl(int, int, ...);
854 int open(const scope char*, int, ...);
855 */
856 version (CRuntime_Glibc)
857 {
858     static if ( __USE_FILE_OFFSET64 )
859     {
860         int   creat64(const scope char*, mode_t);
861         alias creat64 creat;
862 
863         int   open64(const scope char*, int, ...);
864         alias open64 open;
865     }
866     else
867     {
868         int   creat(const scope char*, mode_t);
869         int   open(const scope char*, int, ...);
870     }
871 }
872 else version (Darwin)
873 {
874     int creat(const scope char*, mode_t);
875     int open(const scope char*, int, ...);
876 }
877 else version (FreeBSD)
878 {
879     int creat(const scope char*, mode_t);
880     int open(const scope char*, int, ...);
881 }
882 else version (OpenBSD)
883 {
884     int creat(const scope char*, mode_t);
885     int open(const scope char*, int, ...);
886 }
887 else version (NetBSD)
888 {
889     int creat(const scope char*, mode_t);
890     int open(const scope char*, int, ...);
891 }
892 else version (DragonFlyBSD)
893 {
894     int creat(const scope char*, mode_t);
895     int open(const scope char*, int, ...);
896 }
897 else version (Solaris)
898 {
899     version (D_LP64)
900     {
901         int creat(const scope char*, mode_t);
902         int open(const scope char*, int, ...);
903 
904         static if (__USE_LARGEFILE64)
905         {
906             alias creat creat64;
907             alias open open64;
908         }
909     }
910     else
911     {
912         static if (__USE_LARGEFILE64)
913         {
914             int creat64(const scope char*, mode_t);
915             alias creat64 creat;
916 
917             int open64(const scope char*, int, ...);
918             alias open64 open;
919         }
920         else
921         {
922             int creat(const scope char*, mode_t);
923             int open(const scope char*, int, ...);
924         }
925     }
926 }
927 else version (CRuntime_Bionic)
928 {
929     int   creat(const scope char*, mode_t);
930     int   open(const scope char*, int, ...);
931 }
932 else version (CRuntime_Musl)
933 {
934     int open(const scope char*, int, ...);
935 }
936 else version (CRuntime_UClibc)
937 {
938     static if ( __USE_FILE_OFFSET64 )
939     {
940         int   creat64(const scope char*, mode_t);
941         alias creat64 creat;
942 
943         int   open64(const scope char*, int, ...);
944         alias open64 open;
945     }
946     else
947     {
948         int   creat(const scope char*, mode_t);
949         int   open(const scope char*, int, ...);
950     }
951 }
952 else
953 {
954     static assert(false, "Unsupported platform");
955 }
956 
957 //int creat(const scope char*, mode_t);
958 int fcntl(int, int, ...);
959 //int open(const scope char*, int, ...);
960 
961 // Generic Posix fallocate
962 int posix_fallocate(int, off_t, off_t);
963 
964 //
965 // Advisory Information (ADV)
966 //
967 /*
968 POSIX_FADV_NORMAL
969 POSIX_FADV_SEQUENTIAL
970 POSIX_FADV_RANDOM
971 POSIX_FADV_WILLNEED
972 POSIX_FADV_DONTNEED
973 POSIX_FADV_NOREUSE
974 
975 int posix_fadvise(int, off_t, off_t, int);
976 */