Ruby  1.9.3p392(2013-02-22revision39386)
fcntl.c
Go to the documentation of this file.
1 /************************************************
2 
3  fcntl.c -
4 
5  $Author: nobu $
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 = SYNOPSIS
18 
19  require "fcntl"
20  m = s.fcntl(Fcntl::F_GETFL, 0)
21  f.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK|m)
22 
23 = DESCRIPTION
24 
25 This module is just a translation of the C <fcntl.h> file.
26 
27 = NOTE
28 
29 Only #define symbols get translated; you must still correctly
30 pack up your own arguments to pass as args for locking functions, etc.
31 
32 ************************************************/
33 
34 #include "ruby.h"
35 #include <fcntl.h>
36 
37 /* Fcntl loads the constants defined in the system's <fcntl.h> C header
38  * file, and used with both the fcntl(2) and open(2) POSIX system calls.
39  *
40  * Copyright (C) 1997-2001 Yukihiro Matsumoto
41  *
42  * Documented by mathew <meta@pobox.com>
43  *
44  * = Usage
45  *
46  * To perform a fcntl(2) operation, use IO::fcntl in the core classes.
47  *
48  * To perform an open(2) operation, use IO::sysopen.
49  *
50  * The set of operations and constants available depends upon specific OS
51  * platform. Some values listed below may not be supported on your system.
52  *
53  * The constants supported by Ruby for use with IO::fcntl are:
54  *
55  * - F_DUPFD - duplicate a close-on-exec file handle to a non-close-on-exec
56  * file handle.
57  *
58  * - F_GETFD - read the close-on-exec flag of a file handle.
59  *
60  * - F_SETFD - set the close-on-exec flag of a file handle.
61  *
62  * - FD_CLOEXEC - the value of the close-on-exec flag.
63  *
64  * - F_GETFL - get file descriptor flags.
65  *
66  * - F_SETFL - set file descriptor flags.
67  *
68  * - O_APPEND, O_NONBLOCK, etc (see below) - file descriptor flag
69  * values for the above.
70  *
71  * - F_GETLK - determine whether a given region of a file is locked.
72  *
73  * - F_SETLK - acquire a lock on a region of a file.
74  *
75  * - F_SETLKW - acquire a lock on a region of a file, waiting if necessary.
76  *
77  * - F_RDLCK, F_WRLCK, F_UNLCK - types of lock for the above.
78  *
79  * The constants supported by Ruby for use with IO::sysopen are:
80  *
81  * - O_APPEND - open file in append mode.
82  *
83  * - O_NOCTTY - open tty without it becoming controlling tty.
84  *
85  * - O_CREAT - create file if it doesn't exist.
86  *
87  * - O_EXCL - used with O_CREAT, fail if file exists.
88  *
89  * - O_TRUNC - truncate file on open.
90  *
91  * - O_NONBLOCK / O_NDELAY - open in non-blocking mode.
92  *
93  * - O_RDONLY - open read-only.
94  *
95  * - O_WRONLY - open write-only.
96  *
97  * - O_RDWR - open read-write.
98  *
99  * - O_ACCMODE - mask to extract read/write flags.
100  *
101  * Example:
102  *
103  * require 'fcntl'
104  *
105  * fd = IO::sysopen('/tmp/tempfile',
106  * Fcntl::O_WRONLY | Fcntl::O_EXCL | Fcntl::O_CREAT)
107  * f = IO.open(fd)
108  * f.syswrite("TEMP DATA")
109  * f.close
110  *
111  */
112 void
114 {
115  VALUE mFcntl = rb_define_module("Fcntl");
116 #ifdef F_DUPFD
117  rb_define_const(mFcntl, "F_DUPFD", INT2NUM(F_DUPFD));
118 #endif
119 #ifdef F_GETFD
120  rb_define_const(mFcntl, "F_GETFD", INT2NUM(F_GETFD));
121 #endif
122 #ifdef F_GETLK
123  rb_define_const(mFcntl, "F_GETLK", INT2NUM(F_GETLK));
124 #endif
125 #ifdef F_SETFD
126  rb_define_const(mFcntl, "F_SETFD", INT2NUM(F_SETFD));
127 #endif
128 #ifdef F_GETFL
129  rb_define_const(mFcntl, "F_GETFL", INT2NUM(F_GETFL));
130 #endif
131 #ifdef F_SETFL
132  rb_define_const(mFcntl, "F_SETFL", INT2NUM(F_SETFL));
133 #endif
134 #ifdef F_SETLK
135  rb_define_const(mFcntl, "F_SETLK", INT2NUM(F_SETLK));
136 #endif
137 #ifdef F_SETLKW
138  rb_define_const(mFcntl, "F_SETLKW", INT2NUM(F_SETLKW));
139 #endif
140 #ifdef FD_CLOEXEC
141  rb_define_const(mFcntl, "FD_CLOEXEC", INT2NUM(FD_CLOEXEC));
142 #endif
143 #ifdef F_RDLCK
144  rb_define_const(mFcntl, "F_RDLCK", INT2NUM(F_RDLCK));
145 #endif
146 #ifdef F_UNLCK
147  rb_define_const(mFcntl, "F_UNLCK", INT2NUM(F_UNLCK));
148 #endif
149 #ifdef F_WRLCK
150  rb_define_const(mFcntl, "F_WRLCK", INT2NUM(F_WRLCK));
151 #endif
152 #ifdef O_CREAT
153  rb_define_const(mFcntl, "O_CREAT", INT2NUM(O_CREAT));
154 #endif
155 #ifdef O_EXCL
156  rb_define_const(mFcntl, "O_EXCL", INT2NUM(O_EXCL));
157 #endif
158 #ifdef O_NOCTTY
159  rb_define_const(mFcntl, "O_NOCTTY", INT2NUM(O_NOCTTY));
160 #endif
161 #ifdef O_TRUNC
162  rb_define_const(mFcntl, "O_TRUNC", INT2NUM(O_TRUNC));
163 #endif
164 #ifdef O_APPEND
165  rb_define_const(mFcntl, "O_APPEND", INT2NUM(O_APPEND));
166 #endif
167 #ifdef O_NONBLOCK
168  rb_define_const(mFcntl, "O_NONBLOCK", INT2NUM(O_NONBLOCK));
169 #endif
170 #ifdef O_NDELAY
171  rb_define_const(mFcntl, "O_NDELAY", INT2NUM(O_NDELAY));
172 #endif
173 #ifdef O_RDONLY
174  rb_define_const(mFcntl, "O_RDONLY", INT2NUM(O_RDONLY));
175 #endif
176 #ifdef O_RDWR
177  rb_define_const(mFcntl, "O_RDWR", INT2NUM(O_RDWR));
178 #endif
179 #ifdef O_WRONLY
180  rb_define_const(mFcntl, "O_WRONLY", INT2NUM(O_WRONLY));
181 #endif
182 #ifdef O_ACCMODE
183  rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_ACCMODE));
184 #else
185  rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_RDONLY | O_WRONLY | O_RDWR));
186 #endif
187 }
188