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 | |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 32 | |
| 33 | /* fcntl(fd, opt, [arg]) */ |
| 34 | |
| 35 | static object * |
| 36 | fcntl_fcntl(self, args) |
| 37 | object *self; /* Not used */ |
| 38 | object *args; |
| 39 | { |
| 40 | int fd; |
| 41 | int code; |
| 42 | int arg; |
| 43 | int ret; |
| 44 | char *str; |
| 45 | int len; |
| 46 | char buf[1024]; |
| 47 | |
| 48 | if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { |
| 49 | if (len > sizeof buf) { |
| 50 | err_setstr(ValueError, "fcntl string arg too long"); |
| 51 | return NULL; |
| 52 | } |
| 53 | memcpy(buf, str, len); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 54 | BGN_SAVE |
| 55 | ret = fcntl(fd, code, buf); |
| 56 | END_SAVE |
| 57 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 58 | err_errno(IOError); |
| 59 | return NULL; |
| 60 | } |
| 61 | return newsizedstringobject(buf, len); |
| 62 | } |
| 63 | |
| 64 | err_clear(); |
| 65 | if (getargs(args, "(ii)", &fd, &code)) |
| 66 | arg = 0; |
| 67 | else { |
| 68 | err_clear(); |
| 69 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 70 | return NULL; |
| 71 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 72 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 73 | ret = fcntl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 74 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 75 | if (ret < 0) { |
| 76 | err_errno(IOError); |
| 77 | return NULL; |
| 78 | } |
| 79 | return newintobject((long)ret); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* ioctl(fd, opt, [arg]) */ |
| 84 | |
| 85 | static object * |
| 86 | fcntl_ioctl(self, args) |
| 87 | object *self; /* Not used */ |
| 88 | object *args; |
| 89 | { |
| 90 | int fd; |
| 91 | int code; |
| 92 | int arg; |
| 93 | int ret; |
| 94 | char *str; |
| 95 | int len; |
| 96 | char buf[1024]; |
| 97 | |
| 98 | if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { |
| 99 | if (len > sizeof buf) { |
| 100 | err_setstr(ValueError, "ioctl string arg too long"); |
| 101 | return NULL; |
| 102 | } |
| 103 | memcpy(buf, str, len); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 104 | BGN_SAVE |
| 105 | ret = ioctl(fd, code, buf); |
| 106 | END_SAVE |
| 107 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 108 | err_errno(IOError); |
| 109 | return NULL; |
| 110 | } |
| 111 | return newsizedstringobject(buf, len); |
| 112 | } |
| 113 | |
| 114 | err_clear(); |
| 115 | if (getargs(args, "(ii)", &fd, &code)) |
| 116 | arg = 0; |
| 117 | else { |
| 118 | err_clear(); |
| 119 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 120 | return NULL; |
| 121 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 122 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 123 | ret = ioctl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 124 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 125 | if (ret < 0) { |
| 126 | err_errno(IOError); |
| 127 | return NULL; |
| 128 | } |
| 129 | return newintobject((long)ret); |
| 130 | } |
| 131 | |
| 132 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 133 | /* flock(fd, operation) */ |
| 134 | |
| 135 | static object * |
| 136 | fcntl_flock(self, args) |
| 137 | object *self; /* Not used */ |
| 138 | object *args; |
| 139 | { |
| 140 | int fd; |
| 141 | int code; |
| 142 | int ret; |
| 143 | FILE *f; |
| 144 | |
| 145 | if (!getargs(args, "(ii)", &fd, &code)) |
| 146 | return NULL; |
| 147 | |
| 148 | BGN_SAVE |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 149 | #ifdef HAVE_FLOCK |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 150 | ret = flock(fd, code); |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 151 | #else |
| 152 | |
| 153 | #ifndef LOCK_SH |
| 154 | #define LOCK_SH 1 /* shared lock */ |
| 155 | #define LOCK_EX 2 /* exclusive lock */ |
| 156 | #define LOCK_NB 4 /* don't block when locking */ |
| 157 | #define LOCK_UN 8 /* unlock */ |
| 158 | #endif |
| 159 | { |
| 160 | struct flock l; |
| 161 | if (code == LOCK_UN) |
| 162 | l.l_type = F_UNLCK; |
| 163 | else if (code & LOCK_SH) |
| 164 | l.l_type = F_RDLCK; |
| 165 | else if (code & LOCK_EX) |
| 166 | l.l_type = F_WRLCK; |
| 167 | else { |
| 168 | err_setstr(ValueError, "unrecognized flock argument"); |
| 169 | return NULL; |
| 170 | } |
| 171 | l.l_whence = l.l_start = l.l_len = 0; |
| 172 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 173 | } |
| 174 | #endif /* HAVE_FLOCK */ |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 175 | END_SAVE |
| 176 | if (ret < 0) { |
| 177 | err_errno(IOError); |
| 178 | return NULL; |
| 179 | } |
| 180 | INCREF(None); |
| 181 | return None; |
| 182 | } |
| 183 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 184 | /* lockf(fd, operation) */ |
| 185 | static object * |
| 186 | fcntl_lockf(self, args) |
| 187 | object *self; /* Not used */ |
| 188 | object *args; |
| 189 | { |
| 190 | int fd, code, len = 0, start = 0, whence = 0, ret; |
| 191 | FILE *f; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 193 | if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len, |
| 194 | &start, &whence)) |
| 195 | return NULL; |
| 196 | |
| 197 | BGN_SAVE |
| 198 | #ifndef LOCK_SH |
| 199 | #define LOCK_SH 1 /* shared lock */ |
| 200 | #define LOCK_EX 2 /* exclusive lock */ |
| 201 | #define LOCK_NB 4 /* don't block when locking */ |
| 202 | #define LOCK_UN 8 /* unlock */ |
| 203 | #endif |
| 204 | { |
| 205 | struct flock l; |
| 206 | if (code == LOCK_UN) |
| 207 | l.l_type = F_UNLCK; |
| 208 | else if (code & LOCK_SH) |
| 209 | l.l_type = F_RDLCK; |
| 210 | else if (code & LOCK_EX) |
| 211 | l.l_type = F_WRLCK; |
| 212 | else { |
| 213 | err_setstr(ValueError, "unrecognized flock argument"); |
| 214 | return NULL; |
| 215 | } |
| 216 | l.l_len = len; |
| 217 | l.l_start = start; |
| 218 | l.l_whence = whence; |
| 219 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 220 | } |
| 221 | END_SAVE |
| 222 | if (ret < 0) { |
| 223 | err_errno(IOError); |
| 224 | return NULL; |
| 225 | } |
| 226 | INCREF(None); |
| 227 | return None; |
| 228 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 230 | /* List of functions */ |
| 231 | |
| 232 | static struct methodlist fcntl_methods[] = { |
| 233 | {"fcntl", fcntl_fcntl}, |
| 234 | {"ioctl", fcntl_ioctl}, |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 235 | {"flock", fcntl_flock}, |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 236 | {"lockf", fcntl_lockf, 1}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 237 | {NULL, NULL} /* sentinel */ |
| 238 | }; |
| 239 | |
| 240 | |
| 241 | /* Module initialisation */ |
| 242 | |
| 243 | void |
| 244 | initfcntl() |
| 245 | { |
| 246 | object *m, *d; |
| 247 | |
| 248 | /* Create the module and add the functions */ |
| 249 | m = initmodule("fcntl", fcntl_methods); |
| 250 | |
| 251 | /* Add some symbolic constants to the module */ |
| 252 | d = getmoduledict(m); |
| 253 | |
| 254 | /* Check for errors */ |
| 255 | if (err_occurred()) |
| 256 | fatal("can't initialize module fcntl"); |
| 257 | } |