1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
5 # Copyright 2008 Jean-Sébastien Gélinas <calestar@gmail.com>
7 # This file is free software, which comes along with NIT. This software is
8 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
11 # is kept unaltered, and a notification of the changes is added.
12 # You are allowed to redistribute it and sell it, alone or is a part of
15 # File manipulations (create, read, write, etc.)
27 #include <sys/types.h>
35 # File Abstract Stream
36 abstract class FStream
38 # The path of the file.
39 var path
: nullable String = null
42 private var file
: nullable NativeFile = null
44 # The status of a file. see POSIX stat(2).
45 fun file_stat
: NativeFileStat do return _file
.file_stat
47 # File descriptor of this file
48 fun fd
: Int do return _file
.fileno
50 # Sets the buffering mode for the current FStream
52 # If the buf_size is <= 0, its value will be 512 by default
54 # The mode is any of the buffer_mode enumeration in `Sys`:
58 fun set_buffering_mode
(buf_size
, mode
: Int) do
59 if buf_size
<= 0 then buf_size
= 512
60 if _file
.set_buffering_type
(buf_size
, mode
) != 0 then
61 last_error
= new IOError("Error while changing buffering type for FStream, returned error {sys.errno.strerror}")
73 # Open the same file again.
74 # The original path is reused, therefore the reopened file can be a different file.
77 if not eof
and not _file
.address_is_null
then close
79 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
80 if _file
.address_is_null
then
81 last_error
= new IOError("Error: Opening file at '{path.as(not null)}' failed with '{sys.errno.strerror}'")
92 if _file
.address_is_null
then return
93 var i
= _file
.io_close
100 var nb
= _file
.io_read
(_buffer
.items
, _buffer
.capacity
)
110 redef var end_reached
: Bool = false
112 # Open the file at `path` for reading.
113 init open
(path
: String)
117 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
118 if _file
.address_is_null
then
119 last_error
= new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
124 init from_fd
(fd
: Int) do
127 _file
= fd
.fd_to_stream
(read_only
)
128 if _file
.address_is_null
then
129 last_error
= new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
142 if last_error
!= null then return
143 if not _is_writable
then
144 last_error
= new IOError("Cannot write to non-writable stream")
147 if s
isa FlatText then
148 write_native
(s
.to_cstring
, s
.length
)
150 for i
in s
.substrings
do write_native
(i
.to_cstring
, i
.length
)
157 if _file
.address_is_null
then
158 if last_error
!= null then return
159 last_error
= new IOError("Cannot close unopened write stream")
163 var i
= _file
.io_close
165 last_error
= new IOError("Close failed due to error {sys.errno.strerror}")
169 redef var is_writable
= false
171 # Write `len` bytes from `native`.
172 private fun write_native
(native
: NativeString, len
: Int)
174 if last_error
!= null then return
175 if not _is_writable
then
176 last_error
= new IOError("Cannot write to non-writable stream")
179 if _file
.address_is_null
then
180 last_error
= new IOError("Writing on a null stream")
184 var err
= _file
.io_write
(native
, len
)
187 last_error
= new IOError("Problem in writing : {err} {len} \n")
191 # Open the file at `path` for writing.
192 init open
(path
: String)
194 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
197 if _file
.address_is_null
then
198 last_error
= new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
203 # Creates a new File stream from a file descriptor
204 init from_fd
(fd
: Int) do
206 _file
= fd
.fd_to_stream
(wipe_write
)
208 if _file
.address_is_null
then
209 last_error
= new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
216 # Creates a file stream from a file descriptor `fd` using the file access `mode`.
218 # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
219 private fun fd_to_stream
(mode
: NativeString): NativeFile is extern "file_int_fdtostream"
222 # Constant for read-only file streams
223 private fun read_only
: NativeString do return "r".to_cstring
225 # Constant for write-only file streams
227 # If a stream is opened on a file with this method,
228 # it will wipe the previous file if any.
229 # Else, it will create the file.
230 private fun wipe_write
: NativeString do return "w".to_cstring
232 ###############################################################################
234 # Standard input stream.
239 _file
= new NativeFile.native_stdin
244 redef fun poll_in
: Bool is extern "file_stdin_poll_in"
247 # Standard output stream.
251 _file
= new NativeFile.native_stdout
257 # Standard error stream.
261 _file
= new NativeFile.native_stderr
267 ###############################################################################
269 redef class Streamable
270 # Like `write_to` but take care of creating the file
271 fun write_to_file
(filepath
: String)
273 var stream
= new OFStream.open
(filepath
)
279 # Utility class to access file system services
281 # Usually created with `Text::to_path`.
284 private var path
: String
287 redef fun to_s
do return path
289 # Name of the file name at `to_s`
292 # var path = "/tmp/somefile".to_path
293 # assert path.filename == "somefile"
295 var filename
: String = path
.basename
("") is lazy
297 # Does the file at `path` exists?
298 fun exists
: Bool do return stat
!= null
300 # Information on the file at `self` following symbolic links
302 # Returns `null` if there is no file at `self`.
305 # var p = "/tmp/".to_path
307 # if stat != null then # Does `p` exist?
308 # print "It's size is {stat.size}"
309 # if stat.is_dir then print "It's a directory"
312 fun stat
: nullable FileStat
314 var stat
= path
.to_cstring
.file_stat
315 if stat
.address_is_null
then return null
316 return new FileStat(stat
)
319 # Information on the file or link at `self`
321 # Do not follow symbolic links.
322 fun link_stat
: nullable FileStat
324 var stat
= path
.to_cstring
.file_lstat
325 if stat
.address_is_null
then return null
326 return new FileStat(stat
)
329 # Delete a file from the file system, return `true` on success
332 fun delete
: Bool do return path
.to_cstring
.file_delete
334 # Copy content of file at `path` to `dest`
340 var output
= dest
.open_wo
342 while not input
.eof
do
343 var buffer
= input
.read
(1024)
351 # Open this file for reading
353 # Require: `exists and not link_stat.is_dir`
354 fun open_ro
: IFStream
356 # TODO manage streams error when they are merged
357 return new IFStream.open
(path
)
360 # Open this file for writing
362 # Require: `not exists or not stat.is_dir`
363 fun open_wo
: OFStream
365 # TODO manage streams error when they are merged
366 return new OFStream.open
(path
)
369 # Lists the name of the files contained within the directory at `path`
371 # Require: `exists and is_dir`
372 fun files
: Array[Path]
374 var files
= new Array[Path]
375 for filename
in path
.files
do
376 files
.add
new Path(path
/ filename
)
381 # Delete a directory and all of its content, return `true` on success
383 # Does not go through symbolic links and may get stuck in a cycle if there
384 # is a cycle in the file system.
388 for file
in self.files
do
389 var stat
= file
.link_stat
391 ok
= file
.rmdir
and ok
393 ok
= file
.delete
and ok
397 # Delete the directory itself
398 if ok
then path
.to_cstring
.rmdir
403 redef fun ==(other
) do return other
isa Path and path
.simplify_path
== other
.path
.simplify_path
404 redef fun hash
do return path
.simplify_path
.hash
407 # Information on a file
409 # Created by `Path::stat` and `Path::link_stat`.
411 # The information within this class is gathered when the instance is initialized
412 # it will not be updated if the targeted file is modified.
418 # The low-level status of a file
421 private var stat
: NativeFileStat
423 private var finalized
= false
427 if not finalized
then
433 # Returns the last access time in seconds since Epoch
434 fun last_access_time
: Int
440 # Returns the last modification time in seconds since Epoch
441 fun last_modification_time
: Int
447 # Size of the file at `path`
454 # Is this a regular file and not a device file, pipe, socket, etc.?
461 # Is this a directory?
468 # Is this a symbolic link?
475 # FIXME Make the following POSIX only? or implement in some other way on Windows
477 # Returns the last status change time in seconds since Epoch
478 fun last_status_change_time
: Int
484 # Returns the permission bits of file
491 # Is this a character device?
498 # Is this a block device?
505 # Is this a FIFO pipe?
512 # Is this a UNIX socket
521 # Access file system related services on the path at `self`
522 fun to_path
: Path do return new Path(to_s
)
526 # return true if a file with this names exists
527 fun file_exists
: Bool do return to_cstring
.file_exists
529 # The status of a file. see POSIX stat(2).
530 fun file_stat
: NativeFileStat do return to_cstring
.file_stat
532 # The status of a file or of a symlink. see POSIX lstat(2).
533 fun file_lstat
: NativeFileStat do return to_cstring
.file_lstat
535 # Remove a file, return true if success
536 fun file_delete
: Bool do return to_cstring
.file_delete
538 # Copy content of file at `self` to `dest`
539 fun file_copy_to
(dest
: String) do to_path
.copy
(dest
.to_path
)
541 # Remove the trailing extension `ext`.
543 # `ext` usually starts with a dot but could be anything.
545 # assert "file.txt".strip_extension(".txt") == "file"
546 # assert "file.txt".strip_extension("le.txt") == "fi"
547 # assert "file.txt".strip_extension("xt") == "file.t"
549 # if `ext` is not present, `self` is returned unmodified.
551 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
552 fun strip_extension
(ext
: String): String
554 if has_suffix
(ext
) then
555 return substring
(0, length
- ext
.length
)
560 # Extract the basename of a path and remove the extension
562 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
563 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
564 # assert "path/to".basename(".ext") == "to"
565 # assert "path/to/".basename(".ext") == "to"
566 # assert "path".basename("") == "path"
567 # assert "/path".basename("") == "path"
568 # assert "/".basename("") == "/"
569 # assert "".basename("") == ""
570 fun basename
(ext
: String): String
572 var l
= length
- 1 # Index of the last char
573 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
574 if l
== 0 then return "/"
575 var pos
= chars
.last_index_of_from
('/', l
)
578 n
= substring
(pos
+1, l-pos
)
580 return n
.strip_extension
(ext
)
583 # Extract the dirname of a path
585 # assert "/path/to/a_file.ext".dirname == "/path/to"
586 # assert "path/to/a_file.ext".dirname == "path/to"
587 # assert "path/to".dirname == "path"
588 # assert "path/to/".dirname == "path"
589 # assert "path".dirname == "."
590 # assert "/path".dirname == "/"
591 # assert "/".dirname == "/"
592 # assert "".dirname == "."
595 var l
= length
- 1 # Index of the last char
596 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
597 var pos
= chars
.last_index_of_from
('/', l
)
599 return substring
(0, pos
)
600 else if pos
== 0 then
607 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
608 fun realpath
: String do
609 var cs
= to_cstring
.file_realpath
610 var res
= cs
.to_s_with_copy
611 # cs.free_malloc # FIXME memory leak
615 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
616 # ".." are not resolved if they start the path
617 # starting "/" is not removed
618 # trainling "/" is removed
620 # Note that the method only wonrk on the string:
622 # * no I/O access is performed
623 # * the validity of the path is not checked
626 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
627 # assert "../dir/file".simplify_path == "../dir/file"
628 # assert "dir/../../".simplify_path == ".."
629 # assert "dir/..".simplify_path == "."
630 # assert "//absolute//path/".simplify_path == "/absolute/path"
631 # assert "//absolute//../".simplify_path == "/"
633 fun simplify_path
: String
635 var a
= self.split_with
("/")
636 var a2
= new Array[String]
638 if x
== "." then continue
639 if x
== "" and not a2
.is_empty
then continue
640 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
646 if a2
.is_empty
then return "."
647 if a2
.length
== 1 and a2
.first
== "" then return "/"
651 # Correctly join two path using the directory separator.
653 # Using a standard "{self}/{path}" does not work in the following cases:
656 # * `path` ends with `'/'`.
657 # * `path` starts with `'/'`.
659 # This method ensures that the join is valid.
661 # assert "hello".join_path("world") == "hello/world"
662 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
663 # assert "".join_path("world") == "world"
664 # assert "hello".join_path("/world") == "/world"
665 # assert "hello/".join_path("world") == "hello/world"
666 # assert "hello/".join_path("/world") == "/world"
668 # Note: You may want to use `simplify_path` on the result.
670 # Note: This method works only with POSIX paths.
671 fun join_path
(path
: String): String
673 if path
.is_empty
then return self
674 if self.is_empty
then return path
675 if path
.chars
[0] == '/' then return path
676 if self.last
== '/' then return "{self}{path}"
677 return "{self}/{path}"
680 # Convert the path (`self`) to a program name.
682 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
683 # used as a program name. In order to do that, prepend `./` if needed.
685 # assert "foo".to_program_name == "./foo"
686 # assert "/foo".to_program_name == "/foo"
687 # assert "".to_program_name == "./" # At least, your shell will detect the error.
688 fun to_program_name
: String do
689 if self.has_prefix
("/") then
696 # Alias for `join_path`
698 # assert "hello" / "world" == "hello/world"
699 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
700 # assert "" / "world" == "world"
701 # assert "/hello" / "/world" == "/world"
703 # This operator is quite useful for chaining changes of path.
704 # The next one being relative to the previous one.
708 # var c = "baz/foobar"
709 # assert a/b/c == "/bar/baz/foobar"
710 fun /(path
: String): String do return join_path
(path
)
712 # Returns the relative path needed to go from `self` to `dest`.
714 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
715 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
717 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
719 # In some cases, the result is still independent of the current directory:
721 # assert "foo/bar".relpath("..") == "../../.."
723 # In other cases, parts of the current directory may be exhibited:
725 # var p = "../foo/bar".relpath("baz")
726 # var c = getcwd.basename("")
727 # assert p == "../../{c}/baz"
729 # For path resolution independent of the current directory (eg. for paths in URL),
730 # or to use an other starting directory than the current directory,
731 # just force absolute paths:
733 # var start = "/a/b/c/d"
734 # var p2 = (start/"../foo/bar").relpath(start/"baz")
735 # assert p2 == "../../d/baz"
738 # Neither `self` or `dest` has to be real paths or to exist in directories since
739 # the resolution is only done with string manipulations and without any access to
740 # the underlying file system.
742 # If `self` and `dest` are the same directory, the empty string is returned:
744 # assert "foo".relpath("foo") == ""
745 # assert "foo/../bar".relpath("bar") == ""
747 # The empty string and "." designate both the current directory:
749 # assert "".relpath("foo/bar") == "foo/bar"
750 # assert ".".relpath("foo/bar") == "foo/bar"
751 # assert "foo/bar".relpath("") == "../.."
752 # assert "/" + "/".relpath(".") == getcwd
753 fun relpath
(dest
: String): String
756 var from
= (cwd
/self).simplify_path
.split
("/")
757 if from
.last
.is_empty
then from
.pop
# case for the root directory
758 var to
= (cwd
/dest
).simplify_path
.split
("/")
759 if to
.last
.is_empty
then to
.pop
# case for the root directory
761 # Remove common prefixes
762 while not from
.is_empty
and not to
.is_empty
and from
.first
== to
.first
do
767 # Result is going up in `from` with ".." then going down following `to`
768 var from_len
= from
.length
769 if from_len
== 0 then return to
.join
("/")
770 var up
= "../"*(from_len-1
) + ".."
771 if to
.is_empty
then return up
772 var res
= up
+ "/" + to
.join
("/")
776 # Create a directory (and all intermediate directories if needed)
779 var dirs
= self.split_with
("/")
780 var path
= new FlatBuffer
781 if dirs
.is_empty
then return
782 if dirs
[0].is_empty
then
783 # it was a starting /
787 if d
.is_empty
then continue
790 path
.to_s
.to_cstring
.file_mkdir
794 # Delete a directory and all of its content, return `true` on success
796 # Does not go through symbolic links and may get stuck in a cycle if there
797 # is a cycle in the filesystem.
798 fun rmdir
: Bool do return to_path
.rmdir
800 # Change the current working directory
803 # assert getcwd == "/etc"
805 # assert getcwd == "/"
808 fun chdir
do to_cstring
.file_chdir
810 # Return right-most extension (without the dot)
812 # Only the last extension is returned.
813 # There is no special case for combined extensions.
815 # assert "file.txt".file_extension == "txt"
816 # assert "file.tar.gz".file_extension == "gz"
818 # For file without extension, `null` is returned.
819 # Hoever, for trailing dot, `""` is returned.
821 # assert "file".file_extension == null
822 # assert "file.".file_extension == ""
824 # The starting dot of hidden files is never considered.
826 # assert ".file.txt".file_extension == "txt"
827 # assert ".file".file_extension == null
828 fun file_extension
: nullable String
830 var last_slash
= chars
.last_index_of
('.')
831 if last_slash
> 0 then
832 return substring
( last_slash
+1, length
)
838 # returns files contained within the directory represented by self
839 fun files
: Array[String] is extern import Array[String], Array[String].add
, NativeString.to_s
, String.to_cstring
`{
843 dir_path = String_to_cstring( recv );
844 if ((dir = opendir(dir_path)) == NULL)
851 Array_of_String results;
855 results = new_Array_of_String();
857 while ( ( de = readdir( dir ) ) != NULL )
858 if ( strcmp( de->d_name, ".." ) != 0 &&
859 strcmp( de->d_name, "." ) != 0 )
861 file_name = NativeString_to_s( strdup( de->d_name ) );
862 Array_of_String_add( results, file_name );
871 redef class NativeString
872 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
873 private fun file_stat
: NativeFileStat is extern "string_NativeString_NativeString_file_stat_0"
874 private fun file_lstat
: NativeFileStat `{
875 struct stat* stat_element;
877 stat_element = malloc(sizeof(struct stat));
878 res = lstat(recv, stat_element);
879 if (res == -1) return NULL;
882 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
883 private fun rmdir
: Bool `{ return rmdir(recv); `}
884 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
885 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
886 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
889 # This class is system dependent ... must reify the vfs
890 extern class NativeFileStat `{ struct stat * `}
891 # Returns the permission bits of file
892 fun mode
: Int is extern "file_FileStat_FileStat_mode_0"
893 # Returns the last access time
894 fun atime
: Int is extern "file_FileStat_FileStat_atime_0"
895 # Returns the last status change time
896 fun ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
897 # Returns the last modification time
898 fun mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
900 fun size
: Int is extern "file_FileStat_FileStat_size_0"
902 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
903 fun is_reg
: Bool `{ return S_ISREG(recv->st_mode); `}
904 # Returns true if it is a directory
905 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
906 # Returns true if it is a character device
907 fun is_chr
: Bool `{ return S_ISCHR(recv->st_mode); `}
908 # Returns true if it is a block device
909 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
910 # Returns true if the type is fifo
911 fun is_fifo
: Bool `{ return S_ISFIFO(recv->st_mode); `}
912 # Returns true if the type is a link
913 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
914 # Returns true if the type is a socket
915 fun is_sock
: Bool `{ return S_ISSOCK(recv->st_mode); `}
918 # Instance of this class are standard FILE * pointers
919 private extern class NativeFile `{ FILE* `}
920 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
921 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
922 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
923 fun file_stat
: NativeFileStat is extern "file_NativeFile_NativeFile_file_stat_0"
924 fun fileno
: Int `{ return fileno(recv); `}
925 # Flushes the buffer, forcing the write operation
926 fun flush: Int is extern "fflush"
927 # Used to specify how the buffering will be handled for the current stream.
928 fun set_buffering_type(buf_length: Int, mode: Int): Int is extern "file_NativeFile_NativeFile_set_buffering_type_0"
930 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
931 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
932 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
933 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
934 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
940 if stdout isa FStream then stdout.as(FStream).set_buffering_mode(256, buffer_mode_line)
944 var stdin: PollableIStream = new Stdin is protected writable
947 var stdout: OStream = new Stdout is protected writable
949 # Standard output for errors
950 var stderr: OStream = new Stderr is protected writable
952 # Enumeration for buffer mode full (flushes when buffer is full)
953 fun buffer_mode_full: Int is extern "file_Sys_Sys_buffer_mode_full_0"
954 # Enumeration for buffer mode line (flushes when a `\n
` is encountered)
955 fun buffer_mode_line: Int is extern "file_Sys_Sys_buffer_mode_line_0"
956 # Enumeration for buffer mode none (flushes ASAP when something is written)
957 fun buffer_mode_none: Int is extern "file_Sys_Sys_buffer_mode_none_0"
959 # returns first available stream to read or write to
960 # return null on interruption (possibly a signal)
961 protected fun poll( streams : Sequence[FStream] ) : nullable FStream
963 var in_fds = new Array[Int]
964 var out_fds = new Array[Int]
965 var fd_to_stream = new HashMap[Int,FStream]
968 if s isa IFStream then in_fds.add( fd )
969 if s isa OFStream then out_fds.add( fd )
974 var polled_fd = intern_poll( in_fds, out_fds )
976 if polled_fd == null then
979 return fd_to_stream[polled_fd]
983 private fun intern_poll(in_fds: Array[Int], out_fds: Array[Int]) : nullable Int is extern import Array[Int].length, Array[Int].[], Int.as(nullable Int) `{
984 int in_len
, out_len
, total_len
;
985 struct pollfd
*c_fds
;
988 int first_polled_fd
= -1;
991 in_len
= Array_of_Int_length( in_fds
);
992 out_len
= Array_of_Int_length( out_fds
);
993 total_len
= in_len
+ out_len
;
994 c_fds
= malloc
( sizeof
(struct pollfd
) * total_len
);
997 for ( i
=0; i
<in_len
; i
++ ) {
999 fd
= Array_of_Int__index( in_fds
, i
);
1002 c_fds
[i
].events
= POLLIN;
1005 /* output streams
*/
1006 for ( i
=0; i
<out_len
; i
++ ) {
1008 fd
= Array_of_Int__index( out_fds
, i
);
1011 c_fds
[i
].events
= POLLOUT;
1014 /* poll all fds
, unlimited timeout
*/
1015 result
= poll
( c_fds
, total_len
, -1 );
1018 /* analyse results
*/
1019 for ( i
=0; i
<total_len
; i
++ )
1020 if ( c_fds
[i
].revents
& c_fds
[i
].events
|| /* awaited event
*/
1021 c_fds
[i
].revents
& POLLHUP ) /* closed
*/
1023 first_polled_fd
= c_fds
[i
].fd
;
1027 return Int_as_nullable( first_polled_fd
);
1029 else if ( result
< 0 )
1030 fprintf
( stderr
, "Error in Stream:poll: %s\n", strerror
( errno
) );
1037 # Print `objects
` on the standard output (`stdout
`).
1038 protected fun printn(objects: Object...)
1040 sys.stdout.write(objects.to_s)
1043 # Print an `object
` on the standard output (`stdout
`) and add a newline.
1044 protected fun print(object: Object)
1046 sys.stdout.write(object.to_s)
1047 sys.stdout.write("\n")
1050 # Read a character from the standard input (`stdin
`).
1051 protected fun getc: Char
1053 return sys.stdin.read_char.ascii
1056 # Read a line from the standard input (`stdin
`).
1057 protected fun gets: String
1059 return sys.stdin.read_line
1062 # Return the working (current) directory
1063 protected fun getcwd: String do return file_getcwd.to_s
1064 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"