Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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
15fcntl - load the C fcntl.h defines
16
17= DESCRIPTION
18
19This module is just a translation of the C <fcntl.h> file.
20
21= NOTE
22
23Only #define symbols get translated; you must still correctly
24pack 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 */
64void
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 O_CREAT
163 /* Document-const: O_CREAT
164 *
165 * Create the file if it doesn't exist
166 */
167 rb_define_const(mFcntl, "O_CREAT", INT2NUM(O_CREAT));
168#endif
169#ifdef O_EXCL
170 /* Document-const: O_EXCL
171 *
172 * Used with O_CREAT, fail if the file exists
173 */
174 rb_define_const(mFcntl, "O_EXCL", INT2NUM(O_EXCL));
175#endif
176#ifdef O_NOCTTY
177 /* Document-const: O_NOCTTY
178 *
179 * Open TTY without it becoming the controlling TTY
180 */
181 rb_define_const(mFcntl, "O_NOCTTY", INT2NUM(O_NOCTTY));
182#endif
183#ifdef O_TRUNC
184 /* Document-const: O_TRUNC
185 *
186 * Truncate the file on open
187 */
188 rb_define_const(mFcntl, "O_TRUNC", INT2NUM(O_TRUNC));
189#endif
190#ifdef O_APPEND
191 /* Document-const: O_APPEND
192 *
193 * Open the file in append mode
194 */
195 rb_define_const(mFcntl, "O_APPEND", INT2NUM(O_APPEND));
196#endif
197#ifdef O_NONBLOCK
198 /* Document-const: O_NONBLOCK
199 *
200 * Open the file in non-blocking mode
201 */
202 rb_define_const(mFcntl, "O_NONBLOCK", INT2NUM(O_NONBLOCK));
203#endif
204#ifdef O_NDELAY
205 /* Document-const: O_NDELAY
206 *
207 * Open the file in non-blocking mode
208 */
209 rb_define_const(mFcntl, "O_NDELAY", INT2NUM(O_NDELAY));
210#endif
211#ifdef O_RDONLY
212 /* Document-const: O_RDONLY
213 *
214 * Open the file in read-only mode
215 */
216 rb_define_const(mFcntl, "O_RDONLY", INT2NUM(O_RDONLY));
217#endif
218#ifdef O_RDWR
219 /* Document-const: O_RDWR
220 *
221 * Open the file in read-write mode
222 */
223 rb_define_const(mFcntl, "O_RDWR", INT2NUM(O_RDWR));
224#endif
225#ifdef O_WRONLY
226 /* Document-const: O_WRONLY
227 *
228 * Open the file in write-only mode.
229 */
230 rb_define_const(mFcntl, "O_WRONLY", INT2NUM(O_WRONLY));
231#endif
232#ifdef O_ACCMODE
233 /* Document-const: O_ACCMODE
234 *
235 * Mask to extract the read/write flags
236 */
237 rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_ACCMODE));
238#else
239 /* Document-const: O_ACCMODE
240 *
241 * Mask to extract the read/write flags
242 */
243 rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_RDONLY | O_WRONLY | O_RDWR));
244#endif
245}
void Init_fcntl(void)
Definition: fcntl.c:65
VALUE rb_define_module(const char *)
Definition: class.c:785
#define O_ACCMODE
Definition: io.c:125
unsigned long VALUE
#define INT2NUM(x)
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2891
#define INT2FIX(i)
#define O_NONBLOCK
Definition: win32.h:611
#define FD_CLOEXEC
Definition: win32.h:610
#define F_DUPFD
Definition: win32.h:602
#define F_GETFD
Definition: win32.h:603
#define F_SETFD
Definition: win32.h:604
#define F_SETFL
Definition: win32.h:608