This is an appendix to Understanding Unix/Linux Programming written for people interested in Ada programming on POSIX systems.

Focus on File Systems: Writing pwd

Section 4.4.2, pages 123-127

mkdir

You create a directory with procedure Create_Directory:

package POSIX.Files is
   [...]
   procedure Create_Directory
     (Pathname   : in    POSIX.Pathname;
      Permission : in    POSIX.Permissions.Permission_Set);
   [...]
end POSIX.Files;

rmdir

Similarly you remove a directory with procedure Remove_Directory:

package POSIX.Files is
   [...]
   procedure Remove_Directory (Pathname : in    POSIX.Pathname);
   [...]
end POSIX.Files;

rm

You remove a file with procedure Unlink:

package POSIX.Files is
   [...]
   procedure Unlink (Pathname : in    POSIX.Pathname);
   [...]
end POSIX.Files;

ln

You create a new link to file with procedure Link:

package POSIX.Files is
   [...]
   procedure Link
     (Old_Pathname : in    POSIX.Pathname;
      New_Pathname : in    POSIX.Pathname);
   [...]
end POSIX.Files;

mv

You change the name or location of a file with procedure Rename:

package POSIX.Files is
   [...]
   procedure Rename
     (Old_Pathname : in    POSIX.Pathname;
      New_Pathname : in    POSIX.Pathname);
   [...]
end POSIX.Files;

cd

You change the working directory of your shell with the command cd. Similarly you can change the working directory of a running process by calling procedure Change_Working_Directory:

package POSIX.Process_Environment is
   [...]
   procedure Change_Working_Directory
     (Directory_Name   : in    POSIX.Pathname);
   [...]
end POSIX.Process_Environment;

Section 4.5.2, pages 128-131

We can use function Get_Working_Directory to get the current working directory of our process:

package POSIX.Process_Environment is
   [...]
   function Get_Working_Directory return POSIX.Pathname;
   [...]
end POSIX.Process_Environment;

This makes it very simple to implement the command pwd. Here is a first draft: spwd.adb

We compile it and compare it with the system version:

% gnatmake -P spwd
[...]
% pwd; /bin/pwd; ./spwd
/home/sparre/temp/Posix_in_Ada/04
/home/sparre/Undervisning/CEUS/CA+OS/Posix_in_Ada/04
/home/sparre/Undervisning/CEUS/CA+OS/Posix_in_Ada/04
% which pwd
pwd: shell built-in command

We see how the shell built-in version shows one thing (the sequence of directory names I have cded through; including symbolic links), while the system program and our own program agree and show the actual path to the directory.

Section 4.6, page 131

Modern unixes (for example Linux and OpenSolaris) can be configured to join several disks as one big file system, so we are not really stuck with the third approach.

Section 4.6.3, pages 133-134

System Calls for Symbolic Links

The POSIX/C functions symlink and readlink do not have corresponding operations in POSIX/Ada. If you want to work on symbolic links, you have to import these functions from POSIX/C instead of using native POSIX/Ada functions.

-- 

Go to next chapter.

Collected examples:
posix-in-ada.zip
Based on:
Understanding Unix/Linux Programming, Bruce Molay, ISBN 0-13-008396-8.
Main page:
http://edb.jacob-sparre.dk/Posix_in_Ada/
Written by:
Jacob Sparre Andersen.
Latest update:
19th of April, 2008