Ruby  2.0.0p247(2013-06-27revision41674)
ext/fcntl/fcntl.c
Go to the documentation of this file.
00001 /************************************************
00002 
00003   fcntl.c -
00004 
00005   $Author: akr $
00006   created at: Mon Apr  7 18:53:05 JST 1997
00007 
00008   Copyright (C) 1997-2001 Yukihiro Matsumoto
00009 
00010 ************************************************/
00011 
00012 /************************************************
00013 = NAME
00014 
00015 fcntl - load the C fcntl.h defines
00016 
00017 = DESCRIPTION
00018 
00019 This module is just a translation of the C <fcntl.h> file.
00020 
00021 = NOTE
00022 
00023 Only #define symbols get translated; you must still correctly
00024 pack up your own arguments to pass as args for locking functions, etc.
00025 
00026 ************************************************/
00027 
00028 #include "ruby.h"
00029 #include <fcntl.h>
00030 
00031 /* Fcntl loads the constants defined in the system's <fcntl.h> C header
00032  * file, and used with both the fcntl(2) and open(2) POSIX system calls.
00033  *
00034  * To perform a fcntl(2) operation, use IO::fcntl.
00035  *
00036  * To perform an open(2) operation, use IO::sysopen.
00037  *
00038  * The set of operations and constants available depends upon specific
00039  * operating system.  Some values listed below may not be supported on your
00040  * system.
00041  *
00042  * See your fcntl(2) man page for complete details.
00043  *
00044  * Open /tmp/tempfile as a write-only file that is created if it doesn't
00045  * exist:
00046  *
00047  *   require 'fcntl'
00048  *
00049  *   fd = IO.sysopen('/tmp/tempfile',
00050  *                   Fcntl::O_WRONLY | Fcntl::O_EXCL | Fcntl::O_CREAT)
00051  *   f = IO.open(fd)
00052  *   f.syswrite("TEMP DATA")
00053  *   f.close
00054  *
00055  * Get the flags on file +s+:
00056  *
00057  *   m = s.fcntl(Fcntl::F_GETFL, 0)
00058  *
00059  * Set the non-blocking flag on +f+ in addition to the existing flags in +m+.
00060  *
00061  *   f.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK|m)
00062  *
00063  */
00064 void
00065 Init_fcntl()
00066 {
00067     VALUE mFcntl = rb_define_module("Fcntl");
00068 #ifdef F_DUPFD
00069     /* Document-const: F_DUPFD
00070      *
00071      * Duplicate a file descriptor to the mimimum unused file descriptor
00072      * greater than or equal to the argument.
00073      *
00074      * The close-on-exec flag of the duplicated file descriptor is set.
00075      * (Ruby uses F_DUPFD_CLOEXEC internally if available to avoid race
00076      * condition.  F_SETFD is used if F_DUPFD_CLOEXEC is not available.)
00077      */
00078     rb_define_const(mFcntl, "F_DUPFD", INT2NUM(F_DUPFD));
00079 #endif
00080 #ifdef F_GETFD
00081     /* Document-const: F_GETFD
00082      *
00083      * Read the close-on-exec flag of a file descriptor.
00084      */
00085     rb_define_const(mFcntl, "F_GETFD", INT2NUM(F_GETFD));
00086 #endif
00087 #ifdef F_GETLK
00088     /* Document-const: F_GETLK
00089      *
00090      * Determine whether a given region of a file is locked.  This uses one of
00091      * the F_*LK flags.
00092      */
00093     rb_define_const(mFcntl, "F_GETLK", INT2NUM(F_GETLK));
00094 #endif
00095 #ifdef F_SETFD
00096     /* Document-const: F_SETFD
00097      *
00098      * Set the close-on-exec flag of a file descriptor.
00099      */
00100     rb_define_const(mFcntl, "F_SETFD", INT2NUM(F_SETFD));
00101 #endif
00102 #ifdef F_GETFL
00103     /* Document-const: F_GETFL
00104      *
00105      * Get the file descriptor flags.  This will be one or more of the O_*
00106      * flags.
00107      */
00108     rb_define_const(mFcntl, "F_GETFL", INT2NUM(F_GETFL));
00109 #endif
00110 #ifdef F_SETFL
00111     /* Document-const: F_SETFL
00112      *
00113      * Set the file descriptor flags.  This will be one or more of the O_*
00114      * flags.
00115      */
00116     rb_define_const(mFcntl, "F_SETFL", INT2NUM(F_SETFL));
00117 #endif
00118 #ifdef F_SETLK
00119     /* Document-const: F_SETLK
00120      *
00121      * Acquire a lock on a region of a file.  This uses one of the F_*LCK
00122      * flags.
00123      */
00124     rb_define_const(mFcntl, "F_SETLK", INT2NUM(F_SETLK));
00125 #endif
00126 #ifdef F_SETLKW
00127     /* Document-const: F_SETLKW
00128      *
00129      * Acquire a lock on a region of a file, waiting if necessary.  This uses
00130      * one of the F_*LCK flags
00131      */
00132     rb_define_const(mFcntl, "F_SETLKW", INT2NUM(F_SETLKW));
00133 #endif
00134 #ifdef FD_CLOEXEC
00135     /* Document-const: FD_CLOEXEC
00136      *
00137      * the value of the close-on-exec flag.
00138      */
00139     rb_define_const(mFcntl, "FD_CLOEXEC", INT2NUM(FD_CLOEXEC));
00140 #endif
00141 #ifdef F_RDLCK
00142     /* Document-const: F_RDLCK
00143      *
00144      * Read lock for a region of a file
00145      */
00146     rb_define_const(mFcntl, "F_RDLCK", INT2NUM(F_RDLCK));
00147 #endif
00148 #ifdef F_UNLCK
00149     /* Document-const: F_UNLCK
00150      *
00151      * Remove lock for a region of a file
00152      */
00153     rb_define_const(mFcntl, "F_UNLCK", INT2NUM(F_UNLCK));
00154 #endif
00155 #ifdef F_WRLCK
00156     /* Document-const: F_WRLCK
00157      *
00158      * Write lock for a region of a file
00159      */
00160     rb_define_const(mFcntl, "F_WRLCK", INT2NUM(F_WRLCK));
00161 #endif
00162 #ifdef O_CREAT
00163     /* Document-const: O_CREAT
00164      *
00165      * Create the file if it doesn't exist
00166      */
00167     rb_define_const(mFcntl, "O_CREAT", INT2NUM(O_CREAT));
00168 #endif
00169 #ifdef O_EXCL
00170     /* Document-const: O_EXCL
00171      *
00172      * Used with O_CREAT, fail if the file exists
00173      */
00174     rb_define_const(mFcntl, "O_EXCL", INT2NUM(O_EXCL));
00175 #endif
00176 #ifdef O_NOCTTY
00177     /* Document-const: O_NOCTTY
00178      *
00179      * Open TTY without it becoming the controlling TTY
00180      */
00181     rb_define_const(mFcntl, "O_NOCTTY", INT2NUM(O_NOCTTY));
00182 #endif
00183 #ifdef O_TRUNC
00184     /* Document-const: O_TRUNC
00185      *
00186      * Truncate the file on open
00187      */
00188     rb_define_const(mFcntl, "O_TRUNC", INT2NUM(O_TRUNC));
00189 #endif
00190 #ifdef O_APPEND
00191     /* Document-const: O_APPEND
00192      *
00193      * Open the file in append mode
00194      */
00195     rb_define_const(mFcntl, "O_APPEND", INT2NUM(O_APPEND));
00196 #endif
00197 #ifdef O_NONBLOCK
00198     /* Document-const: O_NONBLOCK
00199      *
00200      * Open the file in non-blocking mode
00201      */
00202     rb_define_const(mFcntl, "O_NONBLOCK", INT2NUM(O_NONBLOCK));
00203 #endif
00204 #ifdef O_NDELAY
00205     /* Document-const: O_NDELAY
00206      *
00207      * Open the file in non-blocking mode
00208      */
00209     rb_define_const(mFcntl, "O_NDELAY", INT2NUM(O_NDELAY));
00210 #endif
00211 #ifdef O_RDONLY
00212     /* Document-const: O_RDONLY
00213      *
00214      * Open the file in read-only mode
00215      */
00216     rb_define_const(mFcntl, "O_RDONLY", INT2NUM(O_RDONLY));
00217 #endif
00218 #ifdef O_RDWR
00219     /* Document-const: O_RDWR
00220      *
00221      * Open the file in read-write mode
00222      */
00223     rb_define_const(mFcntl, "O_RDWR", INT2NUM(O_RDWR));
00224 #endif
00225 #ifdef O_WRONLY
00226     /* Document-const: O_WRONLY
00227      *
00228      * Open the file in write-only mode.
00229      */
00230     rb_define_const(mFcntl, "O_WRONLY", INT2NUM(O_WRONLY));
00231 #endif
00232 #ifdef O_ACCMODE
00233     /* Document-const: O_ACCMODE
00234      *
00235      * Mask to extract the read/write flags
00236      */
00237     rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_ACCMODE));
00238 #else
00239     /* Document-const: O_ACCMODE
00240      *
00241      * Mask to extract the read/write flags
00242      */
00243     rb_define_const(mFcntl, "O_ACCMODE", INT2FIX(O_RDONLY | O_WRONLY | O_RDWR));
00244 #endif
00245 }
00246