Calling C functions from Modula-3

In this example, we create an interface for accessing getcwd function from Modula-3. We then wrap a safe interface around the unsafe layer that calls C. (This example only works on POSIX, but a similar example can be written for Win32 platforms. Of course, if we were to only use Modula-3 facilities, the code could easily be ported.)

  1. First, read the manpage on "getcwd" to get some information about its parameter, and what it does.
  2. Interface Ulib contains the <*EXTERNAL*> Modula-3 signature for the "getcwd" function in our project. If we are to call this from client code, we'd have to make that unsafe, and have to deal with C data structures, which is probably not a good idea. So, in the next step, we build a safe wrapper around the C call.
  3. Create an interface and an implementation "Lib". This will be the Modula-3 wrapper for Ulib. The idea here is to create a function, GetCWD() which returns a TEXT containing your current working directory. Lib.i3 should be pretty straightforward. All you do is declare the signature of the function.
  4. Lib.m3 is more subtle. What we need to do is allocate some space for the C buffer, and then pass it to "getcwd", finally copy the contents of the getcwd buffer back into a TEXT and return it. We can either use Cstdlib.malloc for allocating the right buffer, and Cstdlib.free to dispose it after copying the buffer into a TEXT via M3toC.CopyStoT.
  5. Create a safe main module and call Lib.GetCWD() from it.

The good news is that most of the time, we can program in the safe mode, so that the language takes care of things like garbage collection.