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