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"): 55 56 "Initialise the stream with the given 'fd' and 'mode'." 57 58 self.__data__ = fdopen(fd, mode) 59 60 def read(self, n): 61 62 "Read 'n' bytes from the stream." 63 64 _check_int(n) 65 return native._fread(self.__data__, n) 66 67 def write(self, s): 68 69 "Write string 's' to the stream." 70 71 _check_string(s) 72 native._fwrite(self.__data__, s) 73 74 # Input/output functions. 75 76 def close(fd): pass 77 def closerange(fd_low, fd_high): pass 78 def dup(fd): pass 79 def dup2(old_fd, new_fd): pass 80 def fchdir(fd): pass 81 def fchmod(fd, mode): pass 82 def fchown(fd, uid, gid): pass 83 def fdatasync(fd): pass 84 85 def fdopen(fd, mode="r"): 86 87 """ 88 Open a stream for the given file descriptor 'fd', operating in the given 89 'mode'. 90 """ 91 92 _check_fd(fd) 93 _check_string(mode) 94 return native._fdopen(fd, mode) 95 96 def fpathconf(fd, name): pass 97 def fstat(fd): pass 98 def fstatvfs(fd): pass 99 def fsync(fd): pass 100 def ftruncate(fd, length): pass 101 def isatty(fd): pass 102 103 SEEK_CUR = 1 104 SEEK_END = 2 105 SEEK_SET = 0 106 107 def lseek(fd, pos, how): pass 108 def open(filename, flag, mode=0777): pass 109 def openpty(): pass 110 def pipe(): pass 111 def putenv(key, value): pass 112 113 def read(fd, n): 114 115 """ 116 Read using the low-level file descriptor 'fd' the given number of bytes 'n'. 117 """ 118 119 _check_fd(fd) 120 _check_int(n) 121 return native._read(fd, n) 122 123 def times(): pass 124 def ttyname(fd): pass 125 def umask(new_mask): pass 126 def uname(): pass 127 def unsetenv(key): pass 128 129 def write(fd, s): 130 131 "Write using the low-level file descriptor 'fd' the given string 's'." 132 133 _check_fd(fd) 134 _check_string(s) 135 native._write(fd, s) 136 137 # Constants. 138 139 O_APPEND = 1024 140 O_ASYNC = 8192 141 O_CREAT = 64 142 O_DIRECT = 16384 143 O_DIRECTORY = 65536 144 O_DSYNC = 4096 145 O_EXCL = 128 146 O_LARGEFILE = 32768 147 O_NDELAY = 2048 148 O_NOATIME = 262144 149 O_NOCTTY = 256 150 O_NOFOLLOW = 131072 151 O_NONBLOCK = 2048 152 O_RDONLY = 0 153 O_RDWR = 2 154 O_RSYNC = 1052672 155 O_SYNC = 1052672 156 O_TRUNC = 512 157 O_WRONLY = 1 158 159 # Type validation functions. 160 161 def _check_fd(fd): 162 163 "Check the given low-level file descriptor 'fd'." 164 165 if not native._isinstance(fd, int): 166 raise ValueError(fd) 167 168 def _check_int(i): 169 170 "Check the given int 'i'." 171 172 if not native._isinstance(i, int): 173 raise ValueError(i) 174 175 def _check_string(s): 176 177 "Check the given string 's'." 178 179 if not native._isinstance(s, string): 180 raise ValueError(s) 181 182 # vim: tabstop=4 expandtab shiftwidth=4