fcntlmodule.c revision 0297512a08698cdee4bebe044a40b08b406398a8
1/*********************************************************** 2Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The 3Netherlands. 4 5 All Rights Reserved 6 7Permission to use, copy, modify, and distribute this software and its 8documentation for any purpose and without fee is hereby granted, 9provided that the above copyright notice appear in all copies and that 10both that copyright notice and this permission notice appear in 11supporting documentation, and that the names of Stichting Mathematisch 12Centrum or CWI not be used in advertising or publicity pertaining to 13distribution of the software without specific, written prior permission. 14 15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO 16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE 18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22 23******************************************************************/ 24 25/* fcntl module */ 26 27#include "allobjects.h" 28#include "modsupport.h" 29 30 31/* fcntl(fd, opt, [arg]) */ 32 33static object * 34fcntl_fcntl(self, args) 35 object *self; /* Not used */ 36 object *args; 37{ 38 int fd; 39 int code; 40 int arg; 41 int ret; 42 char *str; 43 int len; 44 char buf[1024]; 45 46 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { 47 if (len > sizeof buf) { 48 err_setstr(ValueError, "fcntl string arg too long"); 49 return NULL; 50 } 51 memcpy(buf, str, len); 52 if (fcntl(fd, code, buf) < 0) { 53 err_errno(IOError); 54 return NULL; 55 } 56 return newsizedstringobject(buf, len); 57 } 58 59 err_clear(); 60 if (getargs(args, "(ii)", &fd, &code)) 61 arg = 0; 62 else { 63 err_clear(); 64 if (!getargs(args, "(iii)", &fd, &code, &arg)) 65 return NULL; 66 } 67 ret = fcntl(fd, code, arg); 68 if (ret < 0) { 69 err_errno(IOError); 70 return NULL; 71 } 72 return newintobject((long)ret); 73} 74 75 76/* ioctl(fd, opt, [arg]) */ 77 78static object * 79fcntl_ioctl(self, args) 80 object *self; /* Not used */ 81 object *args; 82{ 83 int fd; 84 int code; 85 int arg; 86 int ret; 87 char *str; 88 int len; 89 char buf[1024]; 90 91 if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { 92 if (len > sizeof buf) { 93 err_setstr(ValueError, "ioctl string arg too long"); 94 return NULL; 95 } 96 memcpy(buf, str, len); 97 if (ioctl(fd, code, buf) < 0) { 98 err_errno(IOError); 99 return NULL; 100 } 101 return newsizedstringobject(buf, len); 102 } 103 104 err_clear(); 105 if (getargs(args, "(ii)", &fd, &code)) 106 arg = 0; 107 else { 108 err_clear(); 109 if (!getargs(args, "(iii)", &fd, &code, &arg)) 110 return NULL; 111 } 112 ret = ioctl(fd, code, arg); 113 if (ret < 0) { 114 err_errno(IOError); 115 return NULL; 116 } 117 return newintobject((long)ret); 118} 119 120 121/* List of functions */ 122 123static struct methodlist fcntl_methods[] = { 124 {"fcntl", fcntl_fcntl}, 125 {"ioctl", fcntl_ioctl}, 126 {NULL, NULL} /* sentinel */ 127}; 128 129 130/* Module initialisation */ 131 132void 133initfcntl() 134{ 135 object *m, *d; 136 137 /* Create the module and add the functions */ 138 m = initmodule("fcntl", fcntl_methods); 139 140 /* Add some symbolic constants to the module */ 141 d = getmoduledict(m); 142 143 /* Check for errors */ 144 if (err_occurred()) 145 fatal("can't initialize module fcntl"); 146} 147