|
|
|
|
|
12. OPERATING SYSTEM ACCESS
Seed7 provides a portable access to the services provided by an operating system. This interface is oriented towards POSIX and UNIX. 12.1 Directory accessA portable access to the contents of directories in the file system is provided. For example: After the declaration
var array string: dir_array is 0 times "";
the following statement
dir_array := read_dir(".");
reads the current working directory and stores it into the string-array 'dir_array'. The components of the directory can now be accessed via indexing:
for index range 1 to length(dir_array) do
writeln(dir_array[index]);
end for;
Note that the strings contain only the name of the file. Additional information must be obtained by other calls. Other directories can be read by using their name in the 'read_dir' call. Basing on this mechanism another mechanism is constructed to read the contents of a directory as file. This is shown in the following example
...
include "dir.s7i";
var file: dir_file is STD_NULL;
var string: file_name is "";
...
dir_file := open_dir(".");
file_name := getln(dir_file);
while file_name <> "" do
writeln(file_name);
file_name := getln(dir_file);
end while;
This is useful in programs that accept a list of filenames as input. Using the 'open_dir' mechanism it is possible to read the filenames directly from a directory without large changes in the program. 12.2 Other directory operationsIn most operating systems each process has a current working directory. With the following statement
my_dir := getcwd();
the full path of the current working directory is assigned to the string variable 'my_dir'. To change the current working directory the next statement can be used
chdir("/usr/bin");
A new directory can be created with
mkdir("my_dir");
12.3 File operations
There are the following file operations accessible
remove("file");
rename("old_name", "new_name");
copy("from", "to");
|
|