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 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* fcntl module */ |
| 33 | |
| 34 | #include "allobjects.h" |
| 35 | #include "modsupport.h" |
| 36 | |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 37 | #include <fcntl.h> |
| 38 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 39 | |
| 40 | /* fcntl(fd, opt, [arg]) */ |
| 41 | |
| 42 | static object * |
| 43 | fcntl_fcntl(self, args) |
| 44 | object *self; /* Not used */ |
| 45 | object *args; |
| 46 | { |
| 47 | int fd; |
| 48 | int code; |
| 49 | int arg; |
| 50 | int ret; |
| 51 | char *str; |
| 52 | int len; |
| 53 | char buf[1024]; |
| 54 | |
| 55 | if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { |
| 56 | if (len > sizeof buf) { |
| 57 | err_setstr(ValueError, "fcntl string arg too long"); |
| 58 | return NULL; |
| 59 | } |
| 60 | memcpy(buf, str, len); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 61 | BGN_SAVE |
| 62 | ret = fcntl(fd, code, buf); |
| 63 | END_SAVE |
| 64 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 65 | err_errno(IOError); |
| 66 | return NULL; |
| 67 | } |
| 68 | return newsizedstringobject(buf, len); |
| 69 | } |
| 70 | |
| 71 | err_clear(); |
| 72 | if (getargs(args, "(ii)", &fd, &code)) |
| 73 | arg = 0; |
| 74 | else { |
| 75 | err_clear(); |
| 76 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 77 | return NULL; |
| 78 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 79 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 80 | ret = fcntl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 81 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 82 | if (ret < 0) { |
| 83 | err_errno(IOError); |
| 84 | return NULL; |
| 85 | } |
| 86 | return newintobject((long)ret); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* ioctl(fd, opt, [arg]) */ |
| 91 | |
| 92 | static object * |
| 93 | fcntl_ioctl(self, args) |
| 94 | object *self; /* Not used */ |
| 95 | object *args; |
| 96 | { |
| 97 | int fd; |
| 98 | int code; |
| 99 | int arg; |
| 100 | int ret; |
| 101 | char *str; |
| 102 | int len; |
| 103 | char buf[1024]; |
| 104 | |
| 105 | if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { |
| 106 | if (len > sizeof buf) { |
| 107 | err_setstr(ValueError, "ioctl string arg too long"); |
| 108 | return NULL; |
| 109 | } |
| 110 | memcpy(buf, str, len); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 111 | BGN_SAVE |
| 112 | ret = ioctl(fd, code, buf); |
| 113 | END_SAVE |
| 114 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 115 | err_errno(IOError); |
| 116 | return NULL; |
| 117 | } |
| 118 | return newsizedstringobject(buf, len); |
| 119 | } |
| 120 | |
| 121 | err_clear(); |
| 122 | if (getargs(args, "(ii)", &fd, &code)) |
| 123 | arg = 0; |
| 124 | else { |
| 125 | err_clear(); |
| 126 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 127 | return NULL; |
| 128 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 129 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 130 | ret = ioctl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 131 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 132 | if (ret < 0) { |
| 133 | err_errno(IOError); |
| 134 | return NULL; |
| 135 | } |
| 136 | return newintobject((long)ret); |
| 137 | } |
| 138 | |
| 139 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 140 | /* flock(fd, operation) */ |
| 141 | |
| 142 | static object * |
| 143 | fcntl_flock(self, args) |
| 144 | object *self; /* Not used */ |
| 145 | object *args; |
| 146 | { |
| 147 | int fd; |
| 148 | int code; |
| 149 | int ret; |
| 150 | FILE *f; |
| 151 | |
| 152 | if (!getargs(args, "(ii)", &fd, &code)) |
| 153 | return NULL; |
| 154 | |
| 155 | BGN_SAVE |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 156 | #ifdef HAVE_FLOCK |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 157 | ret = flock(fd, code); |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 158 | #else |
| 159 | |
| 160 | #ifndef LOCK_SH |
| 161 | #define LOCK_SH 1 /* shared lock */ |
| 162 | #define LOCK_EX 2 /* exclusive lock */ |
| 163 | #define LOCK_NB 4 /* don't block when locking */ |
| 164 | #define LOCK_UN 8 /* unlock */ |
| 165 | #endif |
| 166 | { |
| 167 | struct flock l; |
| 168 | if (code == LOCK_UN) |
| 169 | l.l_type = F_UNLCK; |
| 170 | else if (code & LOCK_SH) |
| 171 | l.l_type = F_RDLCK; |
| 172 | else if (code & LOCK_EX) |
| 173 | l.l_type = F_WRLCK; |
| 174 | else { |
| 175 | err_setstr(ValueError, "unrecognized flock argument"); |
| 176 | return NULL; |
| 177 | } |
| 178 | l.l_whence = l.l_start = l.l_len = 0; |
| 179 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 180 | } |
| 181 | #endif /* HAVE_FLOCK */ |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 182 | END_SAVE |
| 183 | if (ret < 0) { |
| 184 | err_errno(IOError); |
| 185 | return NULL; |
| 186 | } |
| 187 | INCREF(None); |
| 188 | return None; |
| 189 | } |
| 190 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 191 | /* lockf(fd, operation) */ |
| 192 | static object * |
| 193 | fcntl_lockf(self, args) |
| 194 | object *self; /* Not used */ |
| 195 | object *args; |
| 196 | { |
| 197 | int fd, code, len = 0, start = 0, whence = 0, ret; |
| 198 | FILE *f; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 200 | if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len, |
| 201 | &start, &whence)) |
| 202 | return NULL; |
| 203 | |
| 204 | BGN_SAVE |
| 205 | #ifndef LOCK_SH |
| 206 | #define LOCK_SH 1 /* shared lock */ |
| 207 | #define LOCK_EX 2 /* exclusive lock */ |
| 208 | #define LOCK_NB 4 /* don't block when locking */ |
| 209 | #define LOCK_UN 8 /* unlock */ |
| 210 | #endif |
| 211 | { |
| 212 | struct flock l; |
| 213 | if (code == LOCK_UN) |
| 214 | l.l_type = F_UNLCK; |
| 215 | else if (code & LOCK_SH) |
| 216 | l.l_type = F_RDLCK; |
| 217 | else if (code & LOCK_EX) |
| 218 | l.l_type = F_WRLCK; |
| 219 | else { |
| 220 | err_setstr(ValueError, "unrecognized flock argument"); |
| 221 | return NULL; |
| 222 | } |
| 223 | l.l_len = len; |
| 224 | l.l_start = start; |
| 225 | l.l_whence = whence; |
| 226 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 227 | } |
| 228 | END_SAVE |
| 229 | if (ret < 0) { |
| 230 | err_errno(IOError); |
| 231 | return NULL; |
| 232 | } |
| 233 | INCREF(None); |
| 234 | return None; |
| 235 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 237 | /* List of functions */ |
| 238 | |
| 239 | static struct methodlist fcntl_methods[] = { |
| 240 | {"fcntl", fcntl_fcntl}, |
| 241 | {"ioctl", fcntl_ioctl}, |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 242 | {"flock", fcntl_flock}, |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 243 | {"lockf", fcntl_lockf, 1}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 244 | {NULL, NULL} /* sentinel */ |
| 245 | }; |
| 246 | |
| 247 | |
| 248 | /* Module initialisation */ |
| 249 | |
| 250 | void |
| 251 | initfcntl() |
| 252 | { |
| 253 | object *m, *d; |
| 254 | |
| 255 | /* Create the module and add the functions */ |
| 256 | m = initmodule("fcntl", fcntl_methods); |
| 257 | |
| 258 | /* Add some symbolic constants to the module */ |
| 259 | d = getmoduledict(m); |
| 260 | |
| 261 | /* Check for errors */ |
| 262 | if (err_occurred()) |
| 263 | fatal("can't initialize module fcntl"); |
| 264 | } |