Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF 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 | |
| 33 | static object * |
| 34 | fcntl_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); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 52 | BGN_SAVE |
| 53 | ret = fcntl(fd, code, buf); |
| 54 | END_SAVE |
| 55 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 56 | err_errno(IOError); |
| 57 | return NULL; |
| 58 | } |
| 59 | return newsizedstringobject(buf, len); |
| 60 | } |
| 61 | |
| 62 | err_clear(); |
| 63 | if (getargs(args, "(ii)", &fd, &code)) |
| 64 | arg = 0; |
| 65 | else { |
| 66 | err_clear(); |
| 67 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 68 | return NULL; |
| 69 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 70 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 71 | ret = fcntl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 72 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 73 | if (ret < 0) { |
| 74 | err_errno(IOError); |
| 75 | return NULL; |
| 76 | } |
| 77 | return newintobject((long)ret); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /* ioctl(fd, opt, [arg]) */ |
| 82 | |
| 83 | static object * |
| 84 | fcntl_ioctl(self, args) |
| 85 | object *self; /* Not used */ |
| 86 | object *args; |
| 87 | { |
| 88 | int fd; |
| 89 | int code; |
| 90 | int arg; |
| 91 | int ret; |
| 92 | char *str; |
| 93 | int len; |
| 94 | char buf[1024]; |
| 95 | |
| 96 | if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { |
| 97 | if (len > sizeof buf) { |
| 98 | err_setstr(ValueError, "ioctl string arg too long"); |
| 99 | return NULL; |
| 100 | } |
| 101 | memcpy(buf, str, len); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 102 | BGN_SAVE |
| 103 | ret = ioctl(fd, code, buf); |
| 104 | END_SAVE |
| 105 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 106 | err_errno(IOError); |
| 107 | return NULL; |
| 108 | } |
| 109 | return newsizedstringobject(buf, len); |
| 110 | } |
| 111 | |
| 112 | err_clear(); |
| 113 | if (getargs(args, "(ii)", &fd, &code)) |
| 114 | arg = 0; |
| 115 | else { |
| 116 | err_clear(); |
| 117 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 118 | return NULL; |
| 119 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 120 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 121 | ret = ioctl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 122 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 123 | if (ret < 0) { |
| 124 | err_errno(IOError); |
| 125 | return NULL; |
| 126 | } |
| 127 | return newintobject((long)ret); |
| 128 | } |
| 129 | |
| 130 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 131 | /* flock(fd, operation) */ |
| 132 | |
| 133 | static object * |
| 134 | fcntl_flock(self, args) |
| 135 | object *self; /* Not used */ |
| 136 | object *args; |
| 137 | { |
| 138 | int fd; |
| 139 | int code; |
| 140 | int ret; |
| 141 | FILE *f; |
| 142 | |
| 143 | if (!getargs(args, "(ii)", &fd, &code)) |
| 144 | return NULL; |
| 145 | |
| 146 | BGN_SAVE |
| 147 | ret = flock(fd, code); |
| 148 | END_SAVE |
| 149 | if (ret < 0) { |
| 150 | err_errno(IOError); |
| 151 | return NULL; |
| 152 | } |
| 153 | INCREF(None); |
| 154 | return None; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 159 | /* List of functions */ |
| 160 | |
| 161 | static struct methodlist fcntl_methods[] = { |
| 162 | {"fcntl", fcntl_fcntl}, |
| 163 | {"ioctl", fcntl_ioctl}, |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 164 | {"flock", fcntl_flock}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 165 | {NULL, NULL} /* sentinel */ |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | /* Module initialisation */ |
| 170 | |
| 171 | void |
| 172 | initfcntl() |
| 173 | { |
| 174 | object *m, *d; |
| 175 | |
| 176 | /* Create the module and add the functions */ |
| 177 | m = initmodule("fcntl", fcntl_methods); |
| 178 | |
| 179 | /* Add some symbolic constants to the module */ |
| 180 | d = getmoduledict(m); |
| 181 | |
| 182 | /* Check for errors */ |
| 183 | if (err_occurred()) |
| 184 | fatal("can't initialize module fcntl"); |
| 185 | } |