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