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