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@347 | 22 | from __builtins__.file import filestream |
paul@347 | 23 | from __builtins__.types import check_int, check_string |
paul@356 | 24 | from native import ( |
paul@356 | 25 | close as _close, |
paul@356 | 26 | fdopen as _fdopen, |
paul@356 | 27 | read as _read, |
paul@356 | 28 | write as _write |
paul@356 | 29 | ) |
paul@328 | 30 | |
paul@336 | 31 | # Abstractions for system-level files and streams. |
paul@336 | 32 | |
paul@332 | 33 | class sysfile: |
paul@332 | 34 | |
paul@332 | 35 | "A system-level file object." |
paul@332 | 36 | |
paul@332 | 37 | def __init__(self, fd): |
paul@332 | 38 | |
paul@332 | 39 | "Initialise the file with the given 'fd'." |
paul@332 | 40 | |
paul@332 | 41 | self.fd = fd |
paul@332 | 42 | |
paul@332 | 43 | def read(self, n): |
paul@332 | 44 | |
paul@336 | 45 | "Read 'n' bytes from the file, returning a string." |
paul@332 | 46 | |
paul@332 | 47 | return read(self.fd, n) |
paul@332 | 48 | |
paul@332 | 49 | def write(self, s): |
paul@332 | 50 | |
paul@336 | 51 | "Write string 's' to the file." |
paul@336 | 52 | |
paul@352 | 53 | return write(self.fd, s) |
paul@352 | 54 | |
paul@352 | 55 | def close(self): |
paul@352 | 56 | |
paul@352 | 57 | "Close the file." |
paul@352 | 58 | |
paul@352 | 59 | close(self.fd) |
paul@336 | 60 | |
paul@347 | 61 | class sysstream(filestream): |
paul@336 | 62 | |
paul@336 | 63 | "A system-level stream object." |
paul@336 | 64 | |
paul@392 | 65 | def __init__(self, fd, mode="r", encoding=None, bufsize=1024): |
paul@336 | 66 | |
paul@392 | 67 | """ |
paul@392 | 68 | Initialise the stream with the given 'fd', 'mode', 'encoding' and |
paul@392 | 69 | 'bufsize'. |
paul@392 | 70 | """ |
paul@336 | 71 | |
paul@392 | 72 | get_using(filestream.__init__, self)(encoding, bufsize) |
paul@336 | 73 | self.__data__ = fdopen(fd, mode) |
paul@336 | 74 | |
paul@337 | 75 | # Standard streams. |
paul@337 | 76 | |
paul@337 | 77 | stdin = sysstream(0) |
paul@337 | 78 | stdout = sysstream(1, "w") |
paul@337 | 79 | stderr = sysstream(2, "w") |
paul@337 | 80 | |
paul@336 | 81 | # Input/output functions. |
paul@332 | 82 | |
paul@352 | 83 | def close(fd): |
paul@352 | 84 | |
paul@352 | 85 | "Close the file descriptor 'fd'." |
paul@352 | 86 | |
paul@356 | 87 | _close(fd) |
paul@352 | 88 | |
paul@6 | 89 | def closerange(fd_low, fd_high): pass |
paul@6 | 90 | def dup(fd): pass |
paul@6 | 91 | def dup2(old_fd, new_fd): pass |
paul@328 | 92 | def fchdir(fd): pass |
paul@6 | 93 | def fchmod(fd, mode): pass |
paul@6 | 94 | def fchown(fd, uid, gid): pass |
paul@328 | 95 | def fdatasync(fd): pass |
paul@328 | 96 | |
paul@328 | 97 | def fdopen(fd, mode="r"): |
paul@328 | 98 | |
paul@328 | 99 | """ |
paul@328 | 100 | Open a stream for the given file descriptor 'fd', operating in the given |
paul@328 | 101 | 'mode'. |
paul@328 | 102 | """ |
paul@328 | 103 | |
paul@347 | 104 | check_int(fd) |
paul@347 | 105 | check_string(mode) |
paul@356 | 106 | return _fdopen(fd, mode) |
paul@328 | 107 | |
paul@6 | 108 | def fpathconf(fd, name): pass |
paul@6 | 109 | def fstat(fd): pass |
paul@6 | 110 | def fstatvfs(fd): pass |
paul@328 | 111 | def fsync(fd): pass |
paul@6 | 112 | def ftruncate(fd, length): pass |
paul@6 | 113 | def isatty(fd): pass |
paul@328 | 114 | |
paul@328 | 115 | SEEK_CUR = 1 |
paul@328 | 116 | SEEK_END = 2 |
paul@328 | 117 | SEEK_SET = 0 |
paul@328 | 118 | |
paul@6 | 119 | def lseek(fd, pos, how): pass |
paul@6 | 120 | def open(filename, flag, mode=0777): pass |
paul@6 | 121 | def openpty(): pass |
paul@6 | 122 | def pipe(): pass |
paul@6 | 123 | def putenv(key, value): pass |
paul@328 | 124 | |
paul@328 | 125 | def read(fd, n): |
paul@328 | 126 | |
paul@328 | 127 | """ |
paul@328 | 128 | Read using the low-level file descriptor 'fd' the given number of bytes 'n'. |
paul@328 | 129 | """ |
paul@328 | 130 | |
paul@347 | 131 | check_int(fd) |
paul@347 | 132 | check_int(n) |
paul@356 | 133 | return _read(fd, n) |
paul@328 | 134 | |
paul@6 | 135 | def times(): pass |
paul@6 | 136 | def ttyname(fd): pass |
paul@6 | 137 | def umask(new_mask): pass |
paul@6 | 138 | def uname(): pass |
paul@6 | 139 | def unsetenv(key): pass |
paul@328 | 140 | |
paul@328 | 141 | def write(fd, s): |
paul@328 | 142 | |
paul@328 | 143 | "Write using the low-level file descriptor 'fd' the given string 's'." |
paul@328 | 144 | |
paul@347 | 145 | check_int(fd) |
paul@347 | 146 | check_string(s) |
paul@356 | 147 | return _write(fd, s) |
paul@6 | 148 | |
paul@336 | 149 | # Constants. |
paul@336 | 150 | |
paul@6 | 151 | O_APPEND = 1024 |
paul@6 | 152 | O_ASYNC = 8192 |
paul@6 | 153 | O_CREAT = 64 |
paul@6 | 154 | O_DIRECT = 16384 |
paul@6 | 155 | O_DIRECTORY = 65536 |
paul@6 | 156 | O_DSYNC = 4096 |
paul@6 | 157 | O_EXCL = 128 |
paul@6 | 158 | O_LARGEFILE = 32768 |
paul@6 | 159 | O_NDELAY = 2048 |
paul@6 | 160 | O_NOATIME = 262144 |
paul@6 | 161 | O_NOCTTY = 256 |
paul@6 | 162 | O_NOFOLLOW = 131072 |
paul@6 | 163 | O_NONBLOCK = 2048 |
paul@6 | 164 | O_RDONLY = 0 |
paul@6 | 165 | O_RDWR = 2 |
paul@6 | 166 | O_RSYNC = 1052672 |
paul@6 | 167 | O_SYNC = 1052672 |
paul@6 | 168 | O_TRUNC = 512 |
paul@6 | 169 | O_WRONLY = 1 |
paul@6 | 170 | |
paul@6 | 171 | # vim: tabstop=4 expandtab shiftwidth=4 |