2 A small Example


#include "mozart.h"                                  // 1
                                                     // 2
OZ_BI_define(BIgetenv,1,1)                           // 3
{                                                    // 4
  OZ_declareAtom(0,envVarName);                      // 5
                                                     // 6
  char *envValue = getenv(envVarName);               // 7
                                                     // 8
  if (envValue == 0) /* not defined in environment */// 9
    return OZ_FAILED;                                //10
                                                     //11
  OZ_RETURN_ATOM(envValue);                          //12
OZ_BI_end                                          //13
                                                     //14
                                                     //15
static OZ_C_proc_interface oz_interface[] = {        //16
  {"getenv",1,1,BIgetenv},                           //17
  {0,0,0,0}                                          //18
};                                                   //19
                                                     //20
OZ_C_proc_interface *oz_init_module() {              //21
  return oz_interface;                               //22
}                                                    //23

Program 2.1: File getenv.cc: a C++ program to provide a getenv for Oz


Suppose we want to provide an Oz native module Goodies containing a single procedure {Goodies.getenv VarA ValueA} as an interface to the C library function getenv(3): it constrains ValueA to an atom, which is the value of the environment variable VarA, where VarA is an atom. Thus {Goodies.getenv 'HOME' X} will constrain X to an atom representing the path to our home directory. To realize this we have to perform the following steps:

  1. Write a piece of C code.

  2. Create an object file from the C code.

  3. Create a dynamic library from the object file(s).

  4. Link the library into Oz.

These steps are explained in more detail below.


Michael Mehl, Tobias Müller, Christian Schulte and Ralf Scheidhauer
Version 1.4.0 (20080702)