diff --git a/base/io.jl b/base/io.jl index 6dcb9fba85911..c488e911e50c7 100644 --- a/base/io.jl +++ b/base/io.jl @@ -34,6 +34,8 @@ else error("seriously? what is this machine?") end +isreadonly(s) = isreadable(s) && !iswriteable(s) + ## binary I/O ## # all subtypes should implement this @@ -294,6 +296,8 @@ fd(s::IOStream) = int(ccall(:jl_ios_fd, Clong, (Ptr{Void},), s.ios)) close(s::IOStream) = ccall(:ios_close, Void, (Ptr{Void},), s.ios) flush(s::IOStream) = ccall(:ios_flush, Void, (Ptr{Void},), s.ios) isreadonly(s::IOStream) = bool(ccall(:ios_get_readonly, Cint, (Ptr{Void},), s.ios)) +iswriteable(s::IOStream) = !isreadonly(s) +isreadable(s::IOStream) = true truncate(s::IOStream, n::Integer) = (ccall(:ios_trunc, Int32, (Ptr{Void}, Uint), s.ios, n)==0 || diff --git a/base/iobuffer.jl b/base/iobuffer.jl index 189e951db9b84..e733998102b64 100644 --- a/base/iobuffer.jl +++ b/base/iobuffer.jl @@ -73,6 +73,9 @@ end read{T}(from::IOBuffer, ::Type{Ptr{T}}) = convert(Ptr{T}, read(from, Uint)) +isreadable(io::IOBuffer) = io.readable +iswriteable(io::IOBuffer) = io.writable + # TODO: IOBuffer is not iterable, so doesn't really have a length. # This should maybe be sizeof() instead. #length(io::IOBuffer) = (io.seekable ? io.size : nb_available(io)) diff --git a/base/stream.jl b/base/stream.jl index 89ae0428bb2a2..76fe12715c7ec 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -177,6 +177,13 @@ function TTY(fd::RawFD; readable::Bool = false) ret end +# note that uv_is_readable/writeable work for any subtype of +# uv_stream_t, including uv_tty_t and uv_pipe_t +isreadable(io::Union(NamedPipe,PipeServer,TTY)) = + bool(ccall(:uv_is_readable, Cint, (Ptr{Void},), io.handle)) +iswriteable(io::Union(NamedPipe,PipeServer,TTY)) = + bool(ccall(:uv_is_writable, Cint, (Ptr{Void},), io.handle)) + show(io::IO,stream::TTY) = print(io,"TTY(",uv_status_string(stream),", ", nb_available(stream.buffer)," bytes waiting)")