Main Page | Class List | Directories | File List | Class Members | File Members

fcntl-module.c

Go to the documentation of this file.
00001 /* Copyright (c) 2001 John E. Davis
00002  * This file is part of the S-Lang library.
00003  *
00004  * You may distribute under the terms of either the GNU General Public
00005  * License or the Perl Artistic License.
00006  */
00007 
00008 #include <stdio.h>
00009 #include <slang.h>
00010 
00011 #include <unistd.h>
00012 #include <string.h>
00013 #include <fcntl.h>
00014 #include <errno.h>
00015 
00016 SLANG_MODULE(fcntl);
00017 
00018 static int check_and_set_errno (int e)
00019 {
00020 #ifdef EINTR
00021    if (e == EINTR)
00022      return 0;
00023 #endif
00024    (void) SLerrno_set_errno (e);
00025    return -1;
00026 }
00027 
00028 static int do_fcntl_2 (SLFile_FD_Type *f, int cmd)
00029 {
00030    int ret;
00031    int fd;
00032 
00033    if (-1 == SLfile_get_fd (f, &fd))
00034      return -1;
00035      
00036    while ((-1 == (ret = fcntl (fd, cmd)))
00037           && (0 == check_and_set_errno (errno)))
00038      ;
00039    
00040    return ret;
00041 }
00042 
00043 static int do_fcntl_3_int (SLFile_FD_Type *f, int cmd, int flags)
00044 {
00045    int ret;
00046    int fd;
00047    
00048       
00049    if (-1 == SLfile_get_fd (f, &fd))
00050      return -1;
00051 
00052    while ((-1 == (ret = fcntl (fd, cmd, flags)))
00053           && (0 == check_and_set_errno (errno)))
00054      ;
00055    
00056    return ret;
00057 }
00058    
00059 static int fcntl_getfd (SLFile_FD_Type *f)
00060 {
00061    return do_fcntl_2 (f, F_GETFD);
00062 }
00063 
00064 static int fcntl_setfd (SLFile_FD_Type *f, int *flags)
00065 {
00066    return do_fcntl_3_int (f, F_SETFD, *flags);
00067 }
00068 
00069 static int fcntl_getfl (SLFile_FD_Type *f)
00070 {   
00071    return do_fcntl_2 (f, F_GETFL);
00072 }
00073 
00074 static int fcntl_setfl (SLFile_FD_Type *f, int *flags)
00075 {
00076    return do_fcntl_3_int (f, F_SETFL, *flags);
00077 }
00078 
00079 #define F SLANG_FILE_FD_TYPE
00080 #define I SLANG_INT_TYPE
00081 static SLang_Intrin_Fun_Type Fcntl_Intrinsics [] =
00082 {
00083    MAKE_INTRINSIC_1("fcntl_getfd", fcntl_getfd, I, F),
00084    MAKE_INTRINSIC_2("fcntl_setfd", fcntl_setfd, I, F, I),
00085    MAKE_INTRINSIC_1("fcntl_getfl", fcntl_getfl, I, F),
00086    MAKE_INTRINSIC_2("fcntl_setfl", fcntl_setfl, I, F, I),
00087 
00088    SLANG_END_INTRIN_FUN_TABLE
00089 };
00090 #undef I
00091 #undef F
00092 
00093 static SLang_IConstant_Type Fcntl_Consts [] =
00094 {
00095    MAKE_ICONSTANT("FD_CLOEXEC", FD_CLOEXEC),
00096    SLANG_END_ICONST_TABLE
00097 };
00098 
00099 int init_fcntl_module_ns (char *ns_name)
00100 {
00101    SLang_NameSpace_Type *ns;
00102    
00103    ns = SLns_create_namespace (ns_name);
00104    if (ns == NULL)
00105      return -1;
00106 
00107    if ((-1 == SLns_add_intrin_fun_table (ns, Fcntl_Intrinsics, "__FCNTL__"))
00108        || (-1 == SLns_add_iconstant_table (ns, Fcntl_Consts, NULL)))
00109      return -1;
00110 
00111    return 0;
00112 }
00113 
00114 /* This function is optional */
00115 void deinit_fcntl_module (void)
00116 {
00117 }

© sourcejam.com 2005-2008