Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
fcntl.c
Go to the documentation of this file.
1 /************************************************
2 
3  fcntl.c -
4 
5  $Author$
6  created at: Mon Apr 7 18:53:05 JST 1997
7 
8  Copyright (C) 1997-2001 Yukihiro Matsumoto
9 
10 ************************************************/
11 
12 /************************************************
13 = NAME
14 
15 fcntl - load the C fcntl.h defines
16 
17 = DESCRIPTION
18 
19 This module is just a translation of the C <fcntl.h> file.
20 
21 = NOTE
22 
23 Only #define symbols get translated; you must still correctly
24 pack up your own arguments to pass as args for locking functions, etc.
25 
26 ************************************************/
27 
28 #include "ruby.h"
29 #include <fcntl.h>
30 
31 /* Fcntl loads the constants defined in the system's <fcntl.h> C header
32  * file, and used with both the fcntl(2) and open(2) POSIX system calls.
33  *
34  * To perform a fcntl(2) operation, use IO::fcntl.
35  *
36  * To perform an open(2) operation, use IO::sysopen.
37  *
38  * The set of operations and constants available depends upon specific
39  * operating system. Some values listed below may not be supported on your
40  * system.
41  *
42  * See your fcntl(2) man page for complete details.
43  *
44  * Open /tmp/tempfile as a write-only file that is created if it doesn't
45  * exist:
46  *
47  * require 'fcntl'
48  *
49  * fd = IO.sysopen('/tmp/tempfile',
50  * Fcntl::O_WRONLY | Fcntl::O_EXCL | Fcntl::O_CREAT)
51  * f = IO.open(fd)
52  * f.syswrite("TEMP DATA")
53  * f.close
54  *
55  * Get the flags on file +s+:
56  *
57  * m = s.fcntl(Fcntl::F_GETFL, 0)
58  *
59  * Set the non-blocking flag on +f+ in addition to the existing flags in +m+.
60  *
61  * f.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK|m)
62  *
63  */
64 void
66 {
67  VALUE mFcntl = rb_define_module("Fcntl");
68 #ifdef F_DUPFD
69  /* Document-const: F_DUPFD
70  *
71  * Duplicate a file descriptor to the minimum unused file descriptor
72  * greater than or equal to the argument.
73  *
74  * The close-on-exec flag of the duplicated file descriptor is set.
75  * (Ruby uses F_DUPFD_CLOEXEC internally if available to avoid race
76  * condition. F_SETFD is used if F_DUPFD_CLOEXEC is not available.)
77  */
78  rb_define_const(mFcntl, "F_DUPFD", INT2NUM(F_DUPFD));
79 #endif
80 #ifdef F_GETFD
81  /* Document-const: F_GETFD
82  *
83  * Read the close-on-exec flag of a file descriptor.
84  */
85  rb_define_const(mFcntl, "F_GETFD", INT2NUM(F_GETFD));
86 #endif
87 #ifdef F_GETLK
88  /* Document-const: F_GETLK
89  *
90  * Determine whether a given region of a file is locked. This uses one of
91  * the F_*LK flags.
92  */
93  rb_define_const(mFcntl, "F_GETLK", INT2NUM(F_GETLK));
94 #endif
95 #ifdef F_SETFD
96  /* Document-const: F_SETFD
97  *
98  * Set the close-on-exec flag of a file descriptor.
99  */
100  rb_define_const(mFcntl, "F_SETFD", INT2NUM(F_SETFD));
101 #endif
102 #ifdef F_GETFL
103  /* Document-const: F_GETFL
104  *
105  * Get the file descriptor flags. This will be one or more of the O_*
106  * flags.
107  */
108  rb_define_const(mFcntl, "F_GETFL", INT2NUM(F_GETFL));
109 #endif
110 #ifdef F_SETFL
111  /* Document-const: F_SETFL
112  *
113  * Set the file descriptor flags. This will be one or more of the O_*
114  * flags.
115  */
116  rb_define_const(mFcntl, "F_SETFL", INT2NUM(F_SETFL));
117 #endif
118 #ifdef F_SETLK
119  /* Document-const: F_SETLK
120  *
121  * Acquire a lock on a region of a file. This uses one of the F_*LCK
122  * flags.
123  */
124  rb_define_const(mFcntl, "F_SETLK", INT2NUM(F_SETLK));
125 #endif
126 #ifdef F_SETLKW
127  /* Document-const: F_SETLKW
128  *
129  * Acquire a lock on a region of a file, waiting if necessary. This uses
130  * one of the F_*LCK flags
131  */
132  rb_define_const(mFcntl, "F_SETLKW", INT2NUM(F_SETLKW));
133 #endif
134 #ifdef FD_CLOEXEC
135  /* Document-const: FD_CLOEXEC
136  *
137  * the value of the close-on-exec flag.
138  */
139  rb_define_const(mFcntl, "FD_CLOEXEC", INT2NUM(FD_CLOEXEC));
140 #endif
141 #ifdef F_RDLCK
142  /* Document-const: F_RDLCK
143  *
144  * Read lock for a region of a file
145  */
146  rb_define_const(mFcntl, "F_RDLCK", INT2NUM(F_RDLCK));
147 #endif
148 #ifdef F_UNLCK
149  /* Document-const: F_UNLCK
150  *
151  * Remove lock for a region of a file
152  */
153  rb_define_const(mFcntl, "F_UNLCK", INT2NUM(F_UNLCK));
154 #endif
155 #ifdef F_WRLCK
156  /* Document-const: F_WRLCK
157  *
158  * Write lock for a region of a file
159  */
160  rb_define_const(mFcntl, "F_WRLCK", INT2NUM(F_WRLCK));
161 #endif
162 #ifdef F_SETPIPE_SZ
163  /* Document-const: F_SETPIPE_SZ
164  *
165  * Change the capacity of the pipe referred to by fd to be at least arg bytes.
166  */
167  rb_define_const(mFcntl, "F_SETPIPE_SZ", INT2NUM(F_SETPIPE_SZ));
168 #endif
169 #ifdef F_GETPIPE_SZ
170  /* Document-const: F_GETPIPE_SZ
171  *
172  * Return (as the function result) the capacity of the pipe referred to by fd.
173  */
174  rb_define_const(mFcntl, "F_GETPIPE_SZ", INT2NUM(F_GETPIPE_SZ));
175 #endif
176 #ifdef O_CREAT
177  /* Document-const: O_CREAT
178  *
179  * Create the file if it doesn't exist
180  */
181  rb_define_const(mFcntl, "O_CREAT", INT2NUM(O_CREAT));
182 #endif
183 #ifdef O_EXCL
184  /* Document-const: O_EXCL
185  *
186  * Used with O_CREAT, fail if the file exists
187  */
188  rb_define_const(mFcntl, "O_EXCL", INT2NUM(O_EXCL));
189 #endif
190 #ifdef O_NOCTTY
191  /* Document-const: O_NOCTTY
192  *
193  * Open TTY without it becoming the controlling TTY
194  */
195  rb_define_const(mFcntl, "O_NOCTTY", INT2NUM(O_NOCTTY));
196 #endif
197 #ifdef O_TRUNC
198  /* Document-const: O_TRUNC
199  *
200  * Truncate the file on open
201  */
202  rb_define_const(mFcntl, "O_TRUNC", INT2NUM(O_TRUNC));
203 #endif
204 #ifdef O_APPEND
205  /* Document-const: O_APPEND
206  *
207  * Open the file in append mode
208  */
209  rb_define_const(mFcntl, "O_APPEND", INT2NUM(O_APPEND));
210 #endif
211 #ifdef O_NONBLOCK
212  /* Document-const: O_NONBLOCK
213  *
214  * Open the file in non-blocking mode
215  */
216  rb_define_const(mFcntl, "O_NONBLOCK", INT2NUM(O_NONBLOCK));
217 #endif
218 #ifdef O_NDELAY
219  /* Document-const: O_NDELAY
220  *
221  * Open the file in non-blocking mode
222  */
223  rb_define_const(mFcntl, "O_NDELAY", INT2NUM(O_NDELAY));
224 #endif
225 #ifdef O_RDONLY
226  /* Document-const: O_RDONLY
227  *
228  * Open the file in read-only mode
229  */
230  rb_define_const(mFcntl, "O_RDONLY", INT2NUM(O_RDONLY));
231 #endif
232 #ifdef O_RDWR
233  /* Document-const: O_RDWR
234  *
235  * Open the file in read-write mode
236  */
237  rb_define_const(mFcntl, "O_RDWR", INT2NUM(O_RDWR));
238 #endif
239 #ifdef O_WRONLY
240  /* Document-const: O_WRONLY
241  *
242  * Open the file in write-only mode.
243  */
244  rb_define_const(mFcntl, "O_WRONLY", INT2NUM(O_WRONLY));
245 #endif
246 #ifdef O_ACCMODE
247  /* Document-const: O_ACCMODE
248  *
249  * Mask to extract the read/write flags
250  */
251  rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_ACCMODE));
252 #else
253  /* Document-const: O_ACCMODE
254  *
255  * Mask to extract the read/write flags
256  */
257  rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_RDONLY | O_WRONLY | O_RDWR));
258 #endif
259 }
O_NONBLOCK
#define O_NONBLOCK
Definition: win32.h:575
INT2FIX
#define INT2FIX
Definition: long.h:48
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:887
F_DUPFD
#define F_DUPFD
Definition: win32.h:566
INT2NUM
#define INT2NUM
Definition: int.h:43
F_GETFD
#define F_GETFD
Definition: win32.h:567
Init_fcntl
void Init_fcntl(void)
Definition: fcntl.c:65
ruby.h
O_ACCMODE
#define O_ACCMODE
Definition: io.c:146
FD_CLOEXEC
#define FD_CLOEXEC
Definition: win32.h:574
VALUE
unsigned long VALUE
Definition: value.h:38
F_SETFD
#define F_SETFD
Definition: win32.h:568
F_SETFL
#define F_SETFL
Definition: win32.h:572
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:3168