1.1 --- a/lib/posix/io.py Tue Dec 06 17:49:39 2016 +0100
1.2 +++ b/lib/posix/io.py Tue Dec 06 17:55:55 2016 +0100
1.3 @@ -1,31 +1,86 @@
1.4 #!/usr/bin/env python
1.5
1.6 +"""
1.7 +POSIX input/output functions.
1.8 +
1.9 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +import native
1.26 +
1.27 def close(fd): pass
1.28 def closerange(fd_low, fd_high): pass
1.29 def dup(fd): pass
1.30 def dup2(old_fd, new_fd): pass
1.31 -def fchdir(fildes): pass
1.32 +def fchdir(fd): pass
1.33 def fchmod(fd, mode): pass
1.34 def fchown(fd, uid, gid): pass
1.35 -def fdatasync(fildes): pass
1.36 -def fdopen(fd, mode='r', bufsize=None): pass
1.37 +def fdatasync(fd): pass
1.38 +
1.39 +def fdopen(fd, mode="r"):
1.40 +
1.41 + """
1.42 + Open a stream for the given file descriptor 'fd', operating in the given
1.43 + 'mode'.
1.44 + """
1.45 +
1.46 + _check_fd(fd)
1.47 + _check_string(mode)
1.48 + return native._fdopen(fd, mode)
1.49 +
1.50 def fpathconf(fd, name): pass
1.51 def fstat(fd): pass
1.52 def fstatvfs(fd): pass
1.53 -def fsync(fildes): pass
1.54 +def fsync(fd): pass
1.55 def ftruncate(fd, length): pass
1.56 def isatty(fd): pass
1.57 +
1.58 +SEEK_CUR = 1
1.59 +SEEK_END = 2
1.60 +SEEK_SET = 0
1.61 +
1.62 def lseek(fd, pos, how): pass
1.63 def open(filename, flag, mode=0777): pass
1.64 def openpty(): pass
1.65 def pipe(): pass
1.66 def putenv(key, value): pass
1.67 +
1.68 +def read(fd, n):
1.69 +
1.70 + """
1.71 + Read using the low-level file descriptor 'fd' the given number of bytes 'n'.
1.72 + """
1.73 +
1.74 + _check_fd(fd)
1.75 + _check_int(n)
1.76 + return native._read(fd, n)
1.77 +
1.78 def times(): pass
1.79 def ttyname(fd): pass
1.80 def umask(new_mask): pass
1.81 def uname(): pass
1.82 def unsetenv(key): pass
1.83 -def write(fd, string): pass
1.84 +
1.85 +def write(fd, s):
1.86 +
1.87 + "Write using the low-level file descriptor 'fd' the given string 's'."
1.88 +
1.89 + _check_fd(fd)
1.90 + _check_string(s)
1.91 + native._write(fd, s)
1.92
1.93 O_APPEND = 1024
1.94 O_ASYNC = 8192
1.95 @@ -47,8 +102,25 @@
1.96 O_TRUNC = 512
1.97 O_WRONLY = 1
1.98
1.99 -SEEK_CUR = 1
1.100 -SEEK_END = 2
1.101 -SEEK_SET = 0
1.102 +def _check_fd(fd):
1.103 +
1.104 + "Check the given low-level file descriptor 'fd'."
1.105 +
1.106 + if not native._isinstance(fd, int):
1.107 + raise ValueError(fd)
1.108 +
1.109 +def _check_int(i):
1.110 +
1.111 + "Check the given int 'i'."
1.112 +
1.113 + if not native._isinstance(i, int):
1.114 + raise ValueError(i)
1.115 +
1.116 +def _check_string(s):
1.117 +
1.118 + "Check the given string 's'."
1.119 +
1.120 + if not native._isinstance(s, string):
1.121 + raise ValueError(s)
1.122
1.123 # vim: tabstop=4 expandtab shiftwidth=4