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