paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@328 | 3 | """ |
paul@328 | 4 | POSIX input/output functions. |
paul@328 | 5 | |
paul@328 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@328 | 7 | |
paul@328 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@328 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@328 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@328 | 11 | version. |
paul@328 | 12 | |
paul@328 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@328 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@328 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@328 | 16 | details. |
paul@328 | 17 | |
paul@328 | 18 | You should have received a copy of the GNU General Public License along with |
paul@328 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@328 | 20 | """ |
paul@328 | 21 | |
paul@328 | 22 | import native |
paul@328 | 23 | |
paul@332 | 24 | class sysfile: |
paul@332 | 25 | |
paul@332 | 26 | "A system-level file object." |
paul@332 | 27 | |
paul@332 | 28 | def __init__(self, fd): |
paul@332 | 29 | |
paul@332 | 30 | "Initialise the file with the given 'fd'." |
paul@332 | 31 | |
paul@332 | 32 | self.fd = fd |
paul@332 | 33 | |
paul@332 | 34 | def read(self, n): |
paul@332 | 35 | |
paul@332 | 36 | "Read 'n' bytes from the file." |
paul@332 | 37 | |
paul@332 | 38 | return read(self.fd, n) |
paul@332 | 39 | |
paul@332 | 40 | def write(self, s): |
paul@332 | 41 | |
paul@332 | 42 | "Write 's' to the file." |
paul@332 | 43 | |
paul@332 | 44 | write(self.fd, str(s)) |
paul@332 | 45 | |
paul@6 | 46 | def close(fd): pass |
paul@6 | 47 | def closerange(fd_low, fd_high): pass |
paul@6 | 48 | def dup(fd): pass |
paul@6 | 49 | def dup2(old_fd, new_fd): pass |
paul@328 | 50 | def fchdir(fd): pass |
paul@6 | 51 | def fchmod(fd, mode): pass |
paul@6 | 52 | def fchown(fd, uid, gid): pass |
paul@328 | 53 | def fdatasync(fd): pass |
paul@328 | 54 | |
paul@328 | 55 | def fdopen(fd, mode="r"): |
paul@328 | 56 | |
paul@328 | 57 | """ |
paul@328 | 58 | Open a stream for the given file descriptor 'fd', operating in the given |
paul@328 | 59 | 'mode'. |
paul@328 | 60 | """ |
paul@328 | 61 | |
paul@328 | 62 | _check_fd(fd) |
paul@328 | 63 | _check_string(mode) |
paul@328 | 64 | return native._fdopen(fd, mode) |
paul@328 | 65 | |
paul@6 | 66 | def fpathconf(fd, name): pass |
paul@6 | 67 | def fstat(fd): pass |
paul@6 | 68 | def fstatvfs(fd): pass |
paul@328 | 69 | def fsync(fd): pass |
paul@6 | 70 | def ftruncate(fd, length): pass |
paul@6 | 71 | def isatty(fd): pass |
paul@328 | 72 | |
paul@328 | 73 | SEEK_CUR = 1 |
paul@328 | 74 | SEEK_END = 2 |
paul@328 | 75 | SEEK_SET = 0 |
paul@328 | 76 | |
paul@6 | 77 | def lseek(fd, pos, how): pass |
paul@6 | 78 | def open(filename, flag, mode=0777): pass |
paul@6 | 79 | def openpty(): pass |
paul@6 | 80 | def pipe(): pass |
paul@6 | 81 | def putenv(key, value): pass |
paul@328 | 82 | |
paul@328 | 83 | def read(fd, n): |
paul@328 | 84 | |
paul@328 | 85 | """ |
paul@328 | 86 | Read using the low-level file descriptor 'fd' the given number of bytes 'n'. |
paul@328 | 87 | """ |
paul@328 | 88 | |
paul@328 | 89 | _check_fd(fd) |
paul@328 | 90 | _check_int(n) |
paul@328 | 91 | return native._read(fd, n) |
paul@328 | 92 | |
paul@6 | 93 | def times(): pass |
paul@6 | 94 | def ttyname(fd): pass |
paul@6 | 95 | def umask(new_mask): pass |
paul@6 | 96 | def uname(): pass |
paul@6 | 97 | def unsetenv(key): pass |
paul@328 | 98 | |
paul@328 | 99 | def write(fd, s): |
paul@328 | 100 | |
paul@328 | 101 | "Write using the low-level file descriptor 'fd' the given string 's'." |
paul@328 | 102 | |
paul@328 | 103 | _check_fd(fd) |
paul@328 | 104 | _check_string(s) |
paul@328 | 105 | native._write(fd, s) |
paul@6 | 106 | |
paul@6 | 107 | O_APPEND = 1024 |
paul@6 | 108 | O_ASYNC = 8192 |
paul@6 | 109 | O_CREAT = 64 |
paul@6 | 110 | O_DIRECT = 16384 |
paul@6 | 111 | O_DIRECTORY = 65536 |
paul@6 | 112 | O_DSYNC = 4096 |
paul@6 | 113 | O_EXCL = 128 |
paul@6 | 114 | O_LARGEFILE = 32768 |
paul@6 | 115 | O_NDELAY = 2048 |
paul@6 | 116 | O_NOATIME = 262144 |
paul@6 | 117 | O_NOCTTY = 256 |
paul@6 | 118 | O_NOFOLLOW = 131072 |
paul@6 | 119 | O_NONBLOCK = 2048 |
paul@6 | 120 | O_RDONLY = 0 |
paul@6 | 121 | O_RDWR = 2 |
paul@6 | 122 | O_RSYNC = 1052672 |
paul@6 | 123 | O_SYNC = 1052672 |
paul@6 | 124 | O_TRUNC = 512 |
paul@6 | 125 | O_WRONLY = 1 |
paul@6 | 126 | |
paul@328 | 127 | def _check_fd(fd): |
paul@328 | 128 | |
paul@328 | 129 | "Check the given low-level file descriptor 'fd'." |
paul@328 | 130 | |
paul@328 | 131 | if not native._isinstance(fd, int): |
paul@328 | 132 | raise ValueError(fd) |
paul@328 | 133 | |
paul@328 | 134 | def _check_int(i): |
paul@328 | 135 | |
paul@328 | 136 | "Check the given int 'i'." |
paul@328 | 137 | |
paul@328 | 138 | if not native._isinstance(i, int): |
paul@328 | 139 | raise ValueError(i) |
paul@328 | 140 | |
paul@328 | 141 | def _check_string(s): |
paul@328 | 142 | |
paul@328 | 143 | "Check the given string 's'." |
paul@328 | 144 | |
paul@328 | 145 | if not native._isinstance(s, string): |
paul@328 | 146 | raise ValueError(s) |
paul@6 | 147 | |
paul@6 | 148 | # vim: tabstop=4 expandtab shiftwidth=4 |