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