Merge: Newstreams
[nit.git] / lib / standard / file.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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>
6 #
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
13 # another product.
14
15 # File manipulations (create, read, write, etc.)
16 module file
17
18 intrude import stream
19 intrude import ropes
20 import string_search
21 import time
22
23 in "C Header" `{
24 #include <dirent.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <poll.h>
31 #include <errno.h>
32 `}
33
34 # File Abstract Stream
35 abstract class FStream
36 super IOS
37 # The path of the file.
38 var path: nullable String = null
39
40 # The FILE *.
41 private var file: nullable NativeFile = null
42
43 # The status of a file. see POSIX stat(2).
44 fun file_stat: FileStat do return _file.file_stat
45
46 # File descriptor of this file
47 fun fd: Int do return _file.fileno
48
49 # Sets the buffering mode for the current FStream
50 #
51 # If the buf_size is <= 0, its value will be 512 by default
52 #
53 # The mode is any of the buffer_mode enumeration in `Sys`:
54 # - buffer_mode_full
55 # - buffer_mode_line
56 # - buffer_mode_none
57 fun set_buffering_mode(buf_size, mode: Int) do
58 if buf_size <= 0 then buf_size = 512
59 if _file.set_buffering_type(buf_size, mode) != 0 then
60 last_error = new IOError("Error while changing buffering type for FStream, returned error {sys.errno.strerror}")
61 end
62 end
63 end
64
65 # File input stream
66 class IFStream
67 super FStream
68 super BufferedIStream
69 super PollableIStream
70 # Misc
71
72 # Open the same file again.
73 # The original path is reused, therefore the reopened file can be a different file.
74 fun reopen
75 do
76 if not eof and not _file.address_is_null then close
77 last_error = null
78 _file = new NativeFile.io_open_read(path.to_cstring)
79 if _file.address_is_null then
80 last_error = new IOError("Error: Opening file at '{path.as(not null)}' failed with '{sys.errno.strerror}'")
81 end_reached = true
82 return
83 end
84 end_reached = false
85 _buffer_pos = 0
86 _buffer.clear
87 end
88
89 redef fun close
90 do
91 if _file.address_is_null then return
92 var i = _file.io_close
93 _buffer.clear
94 end_reached = true
95 end
96
97 redef fun fill_buffer
98 do
99 var nb = _file.io_read(_buffer.items, _buffer.capacity)
100 if nb <= 0 then
101 end_reached = true
102 nb = 0
103 end
104 _buffer.length = nb
105 _buffer_pos = 0
106 end
107
108 # End of file?
109 redef var end_reached: Bool = false
110
111 # Open the file at `path` for reading.
112 init open(path: String)
113 do
114 self.path = path
115 prepare_buffer(10)
116 _file = new NativeFile.io_open_read(path.to_cstring)
117 if _file.address_is_null then
118 last_error = new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
119 end_reached = true
120 end
121 end
122
123 init from_fd(fd: Int) do
124 self.path = ""
125 prepare_buffer(1)
126 _file = fd.fd_to_stream(read_only)
127 if _file.address_is_null then
128 last_error = new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
129 end_reached = true
130 end
131 end
132 end
133
134 # File output stream
135 class OFStream
136 super FStream
137 super OStream
138
139 redef fun write(s)
140 do
141 if last_error != null then return
142 if not _is_writable then
143 last_error = new IOError("Cannot write to non-writable stream")
144 return
145 end
146 if s isa FlatText then
147 write_native(s.to_cstring, s.length)
148 else
149 for i in s.substrings do write_native(i.to_cstring, i.length)
150 end
151 _file.flush
152 end
153
154 redef fun close
155 do
156 if _file.address_is_null then
157 if last_error != null then return
158 last_error = new IOError("Cannot close unopened write stream")
159 _is_writable = false
160 return
161 end
162 var i = _file.io_close
163 if i != 0 then
164 last_error = new IOError("Close failed due to error {sys.errno.strerror}")
165 end
166 _is_writable = false
167 end
168 redef var is_writable = false
169
170 # Write `len` bytes from `native`.
171 private fun write_native(native: NativeString, len: Int)
172 do
173 if last_error != null then return
174 if not _is_writable then
175 last_error = new IOError("Cannot write to non-writable stream")
176 return
177 end
178 if _file.address_is_null then
179 last_error = new IOError("Writing on a null stream")
180 _is_writable = false
181 return
182 end
183 var err = _file.io_write(native, len)
184 if err != len then
185 # Big problem
186 last_error = new IOError("Problem in writing : {err} {len} \n")
187 end
188 end
189
190 # Open the file at `path` for writing.
191 init open(path: String)
192 do
193 _file = new NativeFile.io_open_write(path.to_cstring)
194 self.path = path
195 _is_writable = true
196 if _file.address_is_null then
197 last_error = new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
198 is_writable = false
199 end
200 end
201
202 # Creates a new File stream from a file descriptor
203 init from_fd(fd: Int) do
204 self.path = ""
205 _file = fd.fd_to_stream(wipe_write)
206 _is_writable = true
207 if _file.address_is_null then
208 last_error = new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
209 _is_writable = false
210 end
211 end
212 end
213
214 redef class Int
215 # Creates a file stream from a file descriptor `fd` using the file access `mode`.
216 #
217 # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
218 private fun fd_to_stream(mode: NativeString): NativeFile is extern "file_int_fdtostream"
219 end
220
221 # Constant for read-only file streams
222 private fun read_only: NativeString do return "r".to_cstring
223
224 # Constant for write-only file streams
225 #
226 # If a stream is opened on a file with this method,
227 # it will wipe the previous file if any.
228 # Else, it will create the file.
229 private fun wipe_write: NativeString do return "w".to_cstring
230
231 ###############################################################################
232
233 # Standard input stream.
234 class Stdin
235 super IFStream
236
237 init do
238 _file = new NativeFile.native_stdin
239 path = "/dev/stdin"
240 prepare_buffer(1)
241 end
242
243 redef fun poll_in: Bool is extern "file_stdin_poll_in"
244 end
245
246 # Standard output stream.
247 class Stdout
248 super OFStream
249 init do
250 _file = new NativeFile.native_stdout
251 path = "/dev/stdout"
252 _is_writable = true
253 end
254 end
255
256 # Standard error stream.
257 class Stderr
258 super OFStream
259 init do
260 _file = new NativeFile.native_stderr
261 path = "/dev/stderr"
262 _is_writable = true
263 end
264 end
265
266 ###############################################################################
267
268 redef class Streamable
269 # Like `write_to` but take care of creating the file
270 fun write_to_file(filepath: String)
271 do
272 var stream = new OFStream.open(filepath)
273 write_to(stream)
274 stream.close
275 end
276 end
277
278 redef class String
279 # return true if a file with this names exists
280 fun file_exists: Bool do return to_cstring.file_exists
281
282 # The status of a file. see POSIX stat(2).
283 fun file_stat: FileStat do return to_cstring.file_stat
284
285 # The status of a file or of a symlink. see POSIX lstat(2).
286 fun file_lstat: FileStat do return to_cstring.file_lstat
287
288 # Remove a file, return true if success
289 fun file_delete: Bool do return to_cstring.file_delete
290
291 # Copy content of file at `self` to `dest`
292 fun file_copy_to(dest: String)
293 do
294 var input = new IFStream.open(self)
295 var output = new OFStream.open(dest)
296
297 while not input.eof do
298 var buffer = input.read(1024)
299 output.write buffer
300 end
301
302 input.close
303 output.close
304 end
305
306 # Remove the trailing extension `ext`.
307 #
308 # `ext` usually starts with a dot but could be anything.
309 #
310 # assert "file.txt".strip_extension(".txt") == "file"
311 # assert "file.txt".strip_extension("le.txt") == "fi"
312 # assert "file.txt".strip_extension("xt") == "file.t"
313 #
314 # if `ext` is not present, `self` is returned unmodified.
315 #
316 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
317 fun strip_extension(ext: String): String
318 do
319 if has_suffix(ext) then
320 return substring(0, length - ext.length)
321 end
322 return self
323 end
324
325 # Extract the basename of a path and remove the extension
326 #
327 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
328 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
329 # assert "path/to".basename(".ext") == "to"
330 # assert "path/to/".basename(".ext") == "to"
331 # assert "path".basename("") == "path"
332 # assert "/path".basename("") == "path"
333 # assert "/".basename("") == "/"
334 # assert "".basename("") == ""
335 fun basename(ext: String): String
336 do
337 var l = length - 1 # Index of the last char
338 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
339 if l == 0 then return "/"
340 var pos = chars.last_index_of_from('/', l)
341 var n = self
342 if pos >= 0 then
343 n = substring(pos+1, l-pos)
344 end
345 return n.strip_extension(ext)
346 end
347
348 # Extract the dirname of a path
349 #
350 # assert "/path/to/a_file.ext".dirname == "/path/to"
351 # assert "path/to/a_file.ext".dirname == "path/to"
352 # assert "path/to".dirname == "path"
353 # assert "path/to/".dirname == "path"
354 # assert "path".dirname == "."
355 # assert "/path".dirname == "/"
356 # assert "/".dirname == "/"
357 # assert "".dirname == "."
358 fun dirname: String
359 do
360 var l = length - 1 # Index of the last char
361 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
362 var pos = chars.last_index_of_from('/', l)
363 if pos > 0 then
364 return substring(0, pos)
365 else if pos == 0 then
366 return "/"
367 else
368 return "."
369 end
370 end
371
372 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
373 fun realpath: String do
374 var cs = to_cstring.file_realpath
375 var res = cs.to_s_with_copy
376 # cs.free_malloc # FIXME memory leak
377 return res
378 end
379
380 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
381 # ".." are not resolved if they start the path
382 # starting "/" is not removed
383 # trainling "/" is removed
384 #
385 # Note that the method only wonrk on the string:
386 # * no I/O access is performed
387 # * the validity of the path is not checked
388 #
389 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
390 # assert "../dir/file".simplify_path == "../dir/file"
391 # assert "dir/../../".simplify_path == ".."
392 # assert "dir/..".simplify_path == "."
393 # assert "//absolute//path/".simplify_path == "/absolute/path"
394 # assert "//absolute//../".simplify_path == "/"
395 fun simplify_path: String
396 do
397 var a = self.split_with("/")
398 var a2 = new Array[String]
399 for x in a do
400 if x == "." then continue
401 if x == "" and not a2.is_empty then continue
402 if x == ".." and not a2.is_empty and a2.last != ".." then
403 a2.pop
404 continue
405 end
406 a2.push(x)
407 end
408 if a2.is_empty then return "."
409 if a2.length == 1 and a2.first == "" then return "/"
410 return a2.join("/")
411 end
412
413 # Correctly join two path using the directory separator.
414 #
415 # Using a standard "{self}/{path}" does not work in the following cases:
416 #
417 # * `self` is empty.
418 # * `path` ends with `'/'`.
419 # * `path` starts with `'/'`.
420 #
421 # This method ensures that the join is valid.
422 #
423 # assert "hello".join_path("world") == "hello/world"
424 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
425 # assert "".join_path("world") == "world"
426 # assert "hello".join_path("/world") == "/world"
427 # assert "hello/".join_path("world") == "hello/world"
428 # assert "hello/".join_path("/world") == "/world"
429 #
430 # Note: You may want to use `simplify_path` on the result.
431 #
432 # Note: This method works only with POSIX paths.
433 fun join_path(path: String): String
434 do
435 if path.is_empty then return self
436 if self.is_empty then return path
437 if path.chars[0] == '/' then return path
438 if self.last == '/' then return "{self}{path}"
439 return "{self}/{path}"
440 end
441
442 # Convert the path (`self`) to a program name.
443 #
444 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
445 # used as a program name. In order to do that, prepend `./` if needed.
446 #
447 # assert "foo".to_program_name == "./foo"
448 # assert "/foo".to_program_name == "/foo"
449 # assert "".to_program_name == "./" # At least, your shell will detect the error.
450 fun to_program_name: String do
451 if self.has_prefix("/") then
452 return self
453 else
454 return "./{self}"
455 end
456 end
457
458 # Alias for `join_path`
459 #
460 # assert "hello" / "world" == "hello/world"
461 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
462 # assert "" / "world" == "world"
463 # assert "/hello" / "/world" == "/world"
464 #
465 # This operator is quite useful for chaining changes of path.
466 # The next one being relative to the previous one.
467 #
468 # var a = "foo"
469 # var b = "/bar"
470 # var c = "baz/foobar"
471 # assert a/b/c == "/bar/baz/foobar"
472 fun /(path: String): String do return join_path(path)
473
474 # Returns the relative path needed to go from `self` to `dest`.
475 #
476 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
477 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
478 #
479 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
480 #
481 # In some cases, the result is still independent of the current directory:
482 #
483 # assert "foo/bar".relpath("..") == "../../.."
484 #
485 # In other cases, parts of the current directory may be exhibited:
486 #
487 # var p = "../foo/bar".relpath("baz")
488 # var c = getcwd.basename("")
489 # assert p == "../../{c}/baz"
490 #
491 # For path resolution independent of the current directory (eg. for paths in URL),
492 # or to use an other starting directory than the current directory,
493 # just force absolute paths:
494 #
495 # var start = "/a/b/c/d"
496 # var p2 = (start/"../foo/bar").relpath(start/"baz")
497 # assert p2 == "../../d/baz"
498 #
499 #
500 # Neither `self` or `dest` has to be real paths or to exist in directories since
501 # the resolution is only done with string manipulations and without any access to
502 # the underlying file system.
503 #
504 # If `self` and `dest` are the same directory, the empty string is returned:
505 #
506 # assert "foo".relpath("foo") == ""
507 # assert "foo/../bar".relpath("bar") == ""
508 #
509 # The empty string and "." designate both the current directory:
510 #
511 # assert "".relpath("foo/bar") == "foo/bar"
512 # assert ".".relpath("foo/bar") == "foo/bar"
513 # assert "foo/bar".relpath("") == "../.."
514 # assert "/" + "/".relpath(".") == getcwd
515 fun relpath(dest: String): String
516 do
517 var cwd = getcwd
518 var from = (cwd/self).simplify_path.split("/")
519 if from.last.is_empty then from.pop # case for the root directory
520 var to = (cwd/dest).simplify_path.split("/")
521 if to.last.is_empty then to.pop # case for the root directory
522
523 # Remove common prefixes
524 while not from.is_empty and not to.is_empty and from.first == to.first do
525 from.shift
526 to.shift
527 end
528
529 # Result is going up in `from` with ".." then going down following `to`
530 var from_len = from.length
531 if from_len == 0 then return to.join("/")
532 var up = "../"*(from_len-1) + ".."
533 if to.is_empty then return up
534 var res = up + "/" + to.join("/")
535 return res
536 end
537
538 # Create a directory (and all intermediate directories if needed)
539 fun mkdir
540 do
541 var dirs = self.split_with("/")
542 var path = new FlatBuffer
543 if dirs.is_empty then return
544 if dirs[0].is_empty then
545 # it was a starting /
546 path.add('/')
547 end
548 for d in dirs do
549 if d.is_empty then continue
550 path.append(d)
551 path.add('/')
552 path.to_s.to_cstring.file_mkdir
553 end
554 end
555
556 # Delete a directory and all of its content, return `true` on success
557 #
558 # Does not go through symbolic links and may get stuck in a cycle if there
559 # is a cycle in the filesystem.
560 fun rmdir: Bool
561 do
562 var ok = true
563 for file in self.files do
564 var file_path = self.join_path(file)
565 var stat = file_path.file_lstat
566 if stat.is_dir then
567 ok = file_path.rmdir and ok
568 else
569 ok = file_path.file_delete and ok
570 end
571 stat.free
572 end
573
574 # Delete the directory itself
575 if ok then to_cstring.rmdir
576
577 return ok
578 end
579
580 # Change the current working directory
581 #
582 # "/etc".chdir
583 # assert getcwd == "/etc"
584 # "..".chdir
585 # assert getcwd == "/"
586 #
587 # TODO: errno
588 fun chdir do to_cstring.file_chdir
589
590 # Return right-most extension (without the dot)
591 #
592 # Only the last extension is returned.
593 # There is no special case for combined extensions.
594 #
595 # assert "file.txt".file_extension == "txt"
596 # assert "file.tar.gz".file_extension == "gz"
597 #
598 # For file without extension, `null` is returned.
599 # Hoever, for trailing dot, `""` is returned.
600 #
601 # assert "file".file_extension == null
602 # assert "file.".file_extension == ""
603 #
604 # The starting dot of hidden files is never considered.
605 #
606 # assert ".file.txt".file_extension == "txt"
607 # assert ".file".file_extension == null
608 fun file_extension: nullable String
609 do
610 var last_slash = chars.last_index_of('.')
611 if last_slash > 0 then
612 return substring( last_slash+1, length )
613 else
614 return null
615 end
616 end
617
618 # returns files contained within the directory represented by self
619 fun files: Array[String] is extern import Array[String], Array[String].add, NativeString.to_s, String.to_cstring `{
620 char *dir_path;
621 DIR *dir;
622
623 dir_path = String_to_cstring( recv );
624 if ((dir = opendir(dir_path)) == NULL)
625 {
626 perror( dir_path );
627 exit( 1 );
628 }
629 else
630 {
631 Array_of_String results;
632 String file_name;
633 struct dirent *de;
634
635 results = new_Array_of_String();
636
637 while ( ( de = readdir( dir ) ) != NULL )
638 if ( strcmp( de->d_name, ".." ) != 0 &&
639 strcmp( de->d_name, "." ) != 0 )
640 {
641 file_name = NativeString_to_s( strdup( de->d_name ) );
642 Array_of_String_add( results, file_name );
643 }
644
645 closedir( dir );
646 return results;
647 }
648 `}
649 end
650
651 redef class NativeString
652 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
653 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
654 private fun file_lstat: FileStat `{
655 struct stat* stat_element;
656 int res;
657 stat_element = malloc(sizeof(struct stat));
658 res = lstat(recv, stat_element);
659 if (res == -1) return NULL;
660 return stat_element;
661 `}
662 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
663 private fun rmdir: Bool `{ return rmdir(recv); `}
664 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
665 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
666 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
667 end
668
669 # This class is system dependent ... must reify the vfs
670 extern class FileStat `{ struct stat * `}
671 # Returns the permission bits of file
672 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
673 # Returns the last access time
674 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
675 # Returns the last status change time
676 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
677 # Returns the last modification time
678 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
679 # Returns the size
680 fun size: Int is extern "file_FileStat_FileStat_size_0"
681
682 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
683 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
684 # Returns true if it is a directory
685 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
686 # Returns true if it is a character device
687 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
688 # Returns true if it is a block device
689 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
690 # Returns true if the type is fifo
691 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
692 # Returns true if the type is a link
693 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
694 # Returns true if the type is a socket
695 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
696 end
697
698 # Instance of this class are standard FILE * pointers
699 private extern class NativeFile `{ FILE* `}
700 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
701 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
702 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
703 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
704 fun fileno: Int `{ return fileno(recv); `}
705 # Flushes the buffer, forcing the write operation
706 fun flush: Int is extern "fflush"
707 # Used to specify how the buffering will be handled for the current stream.
708 fun set_buffering_type(buf_length: Int, mode: Int): Int is extern "file_NativeFile_NativeFile_set_buffering_type_0"
709
710 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
711 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
712 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
713 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
714 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
715 end
716
717 redef class Sys
718
719 init do
720 if stdout isa FStream then stdout.as(FStream).set_buffering_mode(256, buffer_mode_line)
721 end
722
723 # Standard input
724 var stdin: PollableIStream = new Stdin is protected writable
725
726 # Standard output
727 var stdout: OStream = new Stdout is protected writable
728
729 # Standard output for errors
730 var stderr: OStream = new Stderr is protected writable
731
732 # Enumeration for buffer mode full (flushes when buffer is full)
733 fun buffer_mode_full: Int is extern "file_Sys_Sys_buffer_mode_full_0"
734 # Enumeration for buffer mode line (flushes when a `\n` is encountered)
735 fun buffer_mode_line: Int is extern "file_Sys_Sys_buffer_mode_line_0"
736 # Enumeration for buffer mode none (flushes ASAP when something is written)
737 fun buffer_mode_none: Int is extern "file_Sys_Sys_buffer_mode_none_0"
738
739 # returns first available stream to read or write to
740 # return null on interruption (possibly a signal)
741 protected fun poll( streams : Sequence[FStream] ) : nullable FStream
742 do
743 var in_fds = new Array[Int]
744 var out_fds = new Array[Int]
745 var fd_to_stream = new HashMap[Int,FStream]
746 for s in streams do
747 var fd = s.fd
748 if s isa IFStream then in_fds.add( fd )
749 if s isa OFStream then out_fds.add( fd )
750
751 fd_to_stream[fd] = s
752 end
753
754 var polled_fd = intern_poll( in_fds, out_fds )
755
756 if polled_fd == null then
757 return null
758 else
759 return fd_to_stream[polled_fd]
760 end
761 end
762
763 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) `{
764 int in_len, out_len, total_len;
765 struct pollfd *c_fds;
766 sigset_t sigmask;
767 int i;
768 int first_polled_fd = -1;
769 int result;
770
771 in_len = Array_of_Int_length( in_fds );
772 out_len = Array_of_Int_length( out_fds );
773 total_len = in_len + out_len;
774 c_fds = malloc( sizeof(struct pollfd) * total_len );
775
776 /* input streams */
777 for ( i=0; i<in_len; i ++ ) {
778 int fd;
779 fd = Array_of_Int__index( in_fds, i );
780
781 c_fds[i].fd = fd;
782 c_fds[i].events = POLLIN;
783 }
784
785 /* output streams */
786 for ( i=0; i<out_len; i ++ ) {
787 int fd;
788 fd = Array_of_Int__index( out_fds, i );
789
790 c_fds[i].fd = fd;
791 c_fds[i].events = POLLOUT;
792 }
793
794 /* poll all fds, unlimited timeout */
795 result = poll( c_fds, total_len, -1 );
796
797 if ( result > 0 ) {
798 /* analyse results */
799 for ( i=0; i<total_len; i++ )
800 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
801 c_fds[i].revents & POLLHUP ) /* closed */
802 {
803 first_polled_fd = c_fds[i].fd;
804 break;
805 }
806
807 return Int_as_nullable( first_polled_fd );
808 }
809 else if ( result < 0 )
810 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
811
812 return null_Int();
813 `}
814
815 end
816
817 # Print `objects` on the standard output (`stdout`).
818 protected fun printn(objects: Object...)
819 do
820 sys.stdout.write(objects.to_s)
821 end
822
823 # Print an `object` on the standard output (`stdout`) and add a newline.
824 protected fun print(object: Object)
825 do
826 sys.stdout.write(object.to_s)
827 sys.stdout.write("\n")
828 end
829
830 # Read a character from the standard input (`stdin`).
831 protected fun getc: Char
832 do
833 return sys.stdin.read_char.ascii
834 end
835
836 # Read a line from the standard input (`stdin`).
837 protected fun gets: String
838 do
839 return sys.stdin.read_line
840 end
841
842 # Return the working (current) directory
843 protected fun getcwd: String do return file_getcwd.to_s
844 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"