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