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