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