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