1 #!/usr/bin/env python 2 3 """ 4 POSIX input/output functions. 5 6 Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import native 23 24 # Abstractions for system-level files and streams. 25 26 class sysfile: 27 28 "A system-level file object." 29 30 def __init__(self, fd): 31 32 "Initialise the file with the given 'fd'." 33 34 self.fd = fd 35 36 def read(self, n): 37 38 "Read 'n' bytes from the file, returning a string." 39 40 _check_int(n) 41 return read(self.fd, n) 42 43 def write(self, s): 44 45 "Write string 's' to the file." 46 47 _check_string(s) 48 write(self.fd, s) 49 50 class sysstream: 51 52 "A system-level stream object." 53 54 def __init__(self, fd, mode="r", bufsize=1024): 55 56 "Initialise the stream with the given 'fd' and 'mode'." 57 58 self.__data__ = fdopen(fd, mode) 59 self.bufsize = bufsize 60 61 def read(self, n=0): 62 63 "Read 'n' bytes from the stream." 64 65 _check_int(n) 66 67 # Read any indicated number of bytes. 68 69 if n > 0: 70 return native._fread(self.__data__, n) 71 72 # Read all remaining bytes. 73 74 else: 75 l = [] 76 77 # Read until end-of-file. 78 79 try: 80 while True: 81 l.append(native._fread(self.__data__, self.bufsize)) 82 83 # Handle end-of-file reads. 84 85 except EOFError: 86 pass 87 88 return "".join(l) 89 90 def write(self, s): 91 92 "Write string 's' to the stream." 93 94 _check_string(s) 95 native._fwrite(self.__data__, s) 96 97 # Standard streams. 98 99 stdin = sysstream(0) 100 stdout = sysstream(1, "w") 101 stderr = sysstream(2, "w") 102 103 # Input/output functions. 104 105 def close(fd): pass 106 def closerange(fd_low, fd_high): pass 107 def dup(fd): pass 108 def dup2(old_fd, new_fd): pass 109 def fchdir(fd): pass 110 def fchmod(fd, mode): pass 111 def fchown(fd, uid, gid): pass 112 def fdatasync(fd): pass 113 114 def fdopen(fd, mode="r"): 115 116 """ 117 Open a stream for the given file descriptor 'fd', operating in the given 118 'mode'. 119 """ 120 121 _check_fd(fd) 122 _check_string(mode) 123 return native._fdopen(fd, mode) 124 125 def fpathconf(fd, name): pass 126 def fstat(fd): pass 127 def fstatvfs(fd): pass 128 def fsync(fd): pass 129 def ftruncate(fd, length): pass 130 def isatty(fd): pass 131 132 SEEK_CUR = 1 133 SEEK_END = 2 134 SEEK_SET = 0 135 136 def lseek(fd, pos, how): pass 137 def open(filename, flag, mode=0777): pass 138 def openpty(): pass 139 def pipe(): pass 140 def putenv(key, value): pass 141 142 def read(fd, n): 143 144 """ 145 Read using the low-level file descriptor 'fd' the given number of bytes 'n'. 146 """ 147 148 _check_fd(fd) 149 _check_int(n) 150 return native._read(fd, n) 151 152 def times(): pass 153 def ttyname(fd): pass 154 def umask(new_mask): pass 155 def uname(): pass 156 def unsetenv(key): pass 157 158 def write(fd, s): 159 160 "Write using the low-level file descriptor 'fd' the given string 's'." 161 162 _check_fd(fd) 163 _check_string(s) 164 native._write(fd, s) 165 166 # Constants. 167 168 O_APPEND = 1024 169 O_ASYNC = 8192 170 O_CREAT = 64 171 O_DIRECT = 16384 172 O_DIRECTORY = 65536 173 O_DSYNC = 4096 174 O_EXCL = 128 175 O_LARGEFILE = 32768 176 O_NDELAY = 2048 177 O_NOATIME = 262144 178 O_NOCTTY = 256 179 O_NOFOLLOW = 131072 180 O_NONBLOCK = 2048 181 O_RDONLY = 0 182 O_RDWR = 2 183 O_RSYNC = 1052672 184 O_SYNC = 1052672 185 O_TRUNC = 512 186 O_WRONLY = 1 187 188 # Type validation functions. 189 190 def _check_fd(fd): 191 192 "Check the given low-level file descriptor 'fd'." 193 194 if not native._isinstance(fd, int): 195 raise ValueError(fd) 196 197 def _check_int(i): 198 199 "Check the given int 'i'." 200 201 if not native._isinstance(i, int): 202 raise ValueError(i) 203 204 def _check_string(s): 205 206 "Check the given string 's'." 207 208 if not native._isinstance(s, string): 209 raise ValueError(s) 210 211 # vim: tabstop=4 expandtab shiftwidth=4