Parent: [288ae8] (diff)

Download this file

UnixLib.def.m2cc    488 lines (416 with data), 22.8 kB

#ifdef __XDS__
DEFINITION MODULE ["C"] UnixLib; (* XDS *)
#endif
#ifdef __GM2__
DEFINITION MODULE FOR "C" UnixLib; (* GNU M2 *)
#endif
#ifdef __MOCKA__
FOREIGN MODULE UnixLib; (* Mocka  *)
#endif

  (*========================================================================*)
  (* WICHTIG: BITTE NUR DIE DATEI UnixLib.def.m2cc EDITIEREN !!!            *)
  (*========================================================================*)
  (* Interface to UNIX functions                                            *)
  (*                                                                        *)
  (* Es sind 3 Versionen enthalten die mit                                  *)
  (*                                                                        *)
  (*   m2cc -D __{Parameter}__ < UnixLib.def.m2cc > UnixLib.def             *)
  (*                                                                        *)
  (* mit Parameter = {XDS|GM2} erzeugt werden koennen.                      *)
  (*                                                                        *)
  (*   XDS   : Parameter werden fuer den XDS Modula-2 Compilter gesetzt     *)
  (*   GM2   : Parameter werden fuer den GNU Modula-2 Compilter gesetzt     *)
  (*   MOCKA : Parameter werden fuer den MOCKA        Compilter gesetzt     *)
  (*                                                                        *)
  (* There are there versions contained for XDS, GNU and Mocak complier.    *)
  (* You can create them by the m2cc command line shown.                    *)
  (*------------------------------------------------------------------------*)
  (* Last change:                                                           *)
  (*                                                                        *)
  (* 09.11.14, MRi: Changed lseek return value from INTERGER to LONGINT     *)
  (* 09.01.15, MRi: Added getenv function                                   *)
  (* 25.03.16, MRi: Added pread function                                    *)
  (* 07.07.16, MRi: changed definition of time(timeT) function              *)
  (* 18.01.18, MRi: made the size of whole numbers dependent from the       *)
  (*                compiler used, enhanced documentation, commented out    *)
  (*                unused stuff                                            *)
  (*                Adjusted the sizes for the Stat Record for 64 bit Linux *)
  (*                Added open64 and lseek64                                *)
  (*------------------------------------------------------------------------*)
  (* Offene Punkte                                                          *)
  (*                                                                        *)
  (* - Doku verbessern (OK)                                                 *)
  (* - Alle {SHOT|LONG|}INTEGER, INTEGER, {SHOT|LONG|}CARDINAL und CARDINAL *)
  (*   Werte parametrisieren und mit Praeprozessordirektiven absichern      *)
  (*   damit die Groessen korrekt sind. (OK)                                *)
  (* - 64 bit Dateilaengen fuer 32 bit Linux implementieren                 *)
  (* - Stat-Puffer fuer 32 bit System neu definieren (liefert nur noch      *)
  (*   Mist)                                                                *)
  (*------------------------------------------------------------------------*)
  (* Implementation : Michael Riedl                                         *)
  (* Licence        : GNU Lesser General Public License (LGPL)              *)
  (*------------------------------------------------------------------------*)

  (* $Id: UnixLib.def.m2cc,v 1.7 2018/06/08 21:39:31 mriedl Exp mriedl $ *)

FROM SYSTEM IMPORT ADDRESS;
            IMPORT SYSTEM;

#ifdef __XDS__
<* ALIGNMENT = "4" *>

TYPE  CARDINAL8  = SYSTEM.CARD8;
      CARDINAL32 = SYSTEM.CARD32;
      CARDINAL64 = SYSTEM.CARD64;
      INTEGER8   = SYSTEM.INT8;
      INTEGER32  = SYSTEM.INT32;
      INTEGER64  = SYSTEM.INT64;
#endif
#ifdef __GM2__
TYPE  CARDINAL8  = SYSTEM.CARDINAL8;
      CARDINAL32 = SYSTEM.CARDINAL32;
      CARDINAL64 = SYSTEM.CARDINAL64;
      INTEGER8   = SYSTEM.INTEGER8;
      INTEGER32  = SYSTEM.INTEGER32;
      INTEGER64  = SYSTEM.INTEGER64;
#endif

TYPE  FilePtr32 = INTEGER32;
      FilePtr64 = INTEGER64;

TYPE  SIGNED    = INTEGER32;
      UNSIGNED  = CARDINAL32;

#ifdef __GM2__
(* Linux 64 bit *)

TYPE  devT      = CARDINAL64;
      inoT      = CARDINAL64;
      umodeT    = CARDINAL32;
      nlinkT    = CARDINAL64;
      uidT      = CARDINAL32;
      gidT      = CARDINAL32;
      offT      = INTEGER64;
      blksizeT  = CARDINAL64;
      blkcntT   = CARDINAL64;
      timeT     = INTEGER64;
      clockT    = INTEGER32;
#endif

#ifdef __XDS__
(* Linux 32 bit *)

TYPE  devT      = CARDINAL64;
      inoT      = CARDINAL32;
      umodeT    = CARDINAL32;
      nlinkT    = CARDINAL32;
      uidT      = CARDINAL32;
      gidT      = CARDINAL32;
      offT      = INTEGER32;
      blksizeT  = CARDINAL32;
      blkcntT   = CARDINAL32;
      timeT     = INTEGER32;
      clockT    = INTEGER32;
#endif

      modeT     = CARDINAL32;

      Stat      = RECORD
                    stDev    : devT;     (* ID of device containing file    *)
                    stIno    : inoT;     (* inode number                    *)
                    stMode   : umodeT;   (* protection                      *)
                    stNlink  : nlinkT;   (* number of hard links            *)
                    stUid    : uidT;     (* user ID of owner                *)
                    stGid    : gidT;     (* group ID of owner               *)
                    stRdev   : devT;     (* device ID (if special file)     *)
                    stSize   : offT;     (* total size, in bytes            *)
                    stBlksize: blksizeT; (* blocksize for filesystem I/O    *)
                    stBlocks : blkcntT;  (* number of 512B blocks allocated *)
                    stAtime  : timeT;    (* time of last access             *)
                    stMtime  : timeT;    (* time of last modification       *)
                    stCtime  : timeT;    (* time of last status change      *)
                  END;

      tms       = RECORD
                    utime  : clockT; (* user   time *)
                    stime  : clockT; (* system time *)
                    cutime : clockT; (* user   time of children *)
                    cstime : clockT; (* system time of children *)
                  END;
(*
 * Veraltete Definition
 *
 *       timeT     = LONGINT;
 *       clockT    = LONGINT;
 *       uidT      = SHORTCARD;
 *       gidT      = SHORTCARD;
 *       devT      = SHORTCARD;
 *       inoT      = LONGCARD;
 *       modeT     = SHORTCARD;
 *       umodeT    = SHORTCARD;
 *       nlinkT    = SHORTCARD;
 *       offT      = LONGINT;
 * 
 *       Stat      = RECORD
 *                     stDev    : devT;
 *                     pad1     : SHORTCARD;
 *                     stIno    : inoT;
 *                     stMode   : umodeT;
 *                     stNlink  : nlinkT;
 *                     stUid    : uidT;
 *                     stGid    : gidT;
 *                     stRdev   : devT;
 *                     pad2     : SHORTCARD;
 *                     stSize   : offT;
 *                     stBlksize: LONGCARD;
 *                     stBlocks : LONGCARD;
 *                     stAtime  : timeT;
 *                     unused1  : LONGCARD;
 *                     stMtime  : timeT;
 *                     unused2  : LONGCARD;
 *                     stCtime  : timeT;
 *                     unused3  : LONGCARD;
 *                     unused4  : LONGCARD;
 *                     unused5 :  LONGCARD;
 *                   END;
 *) 

CONST (* flags for open *)

      oLARGEFILE = 0100000B; (* open large file > (2**31-1) *)
      oTRUNC     =   01000B; (* open with truncation *)
      oAPPEND    =   02000B; (* append, i.e writes at the end *)
      oCREAT     =    0100B; (* create new file *)
      oRDWR      =      02B; (* open for reading and writing *)
      oWRONLY    =      01B; (* open for writing only *)
      oRDONLY    =       0B; (* open for reading only *)

      (* file access permisson flags (for create and umask) *)

      pXUSID   = 04000B;    (* set user ID on execution *)
      pXGRID   = 02000B;    (* set group ID on execution *)
      pSTEXT   = 01000B;    (* save text image after execution *)
      pROWNER  =  0400B;    (* read by owner *)
      pWOWNER  =  0200B;    (* write by owner *)
      pXOWNER  =  0100B;    (* execute by owner *)
      pRGROUP  =   040B;    (* read by group *)
      pWGROUP  =   020B;    (* write by group *)
      pXGROUP  =   010B;    (* execute by group *)
      pROTHERS =    04B;    (* read by others *)
      pWOTHERS =    02B;    (* write by others *)
      pXOTHERS =    01B;    (* execute by others *)
      pEMPTY   =     0B;    (* no flag set *)
    
      (* file access check flags (for access) *)
 
      cREAD    = 04H;       (* check if readable *)
      cWRITE   = 02H;       (* check if writable *)
      cEXEC    = 01H;       (* check if executable *)
      cEXISTS  =  0H;       (* check existance *)

      (* flags for open64 *)

      sIRUSR   = 0400B; (* Read permission bit for the owner of the file  *)
      sIWUSR   = 0200B; (* Write permission bit for the owner of the file *)
      sIXUSR   = 0100B; (* Execute (for ordinary files) or search (for    *)
                        (* directories) permission bit for the owner ...  *)

PROCEDURE open(path  : ADDRESS;
               oflag : SIGNED) : UNSIGNED;

          (*---------------------------------------------------------------*)
          (* Given a pathname for a file, open returns a file descriptor,  *)
          (* a small, nonnegative integer for use in subsequent system     *)
          (* calls (read(2),write(2),lseek(2), etc.). The file descriptor  *)
          (* returned by a successful call will be the lowest-numbered     *)
          (* file descriptor not currently open for the process.           *)
          (* On error, -1 is returned, and errno is set appropriately.     *)
          (*---------------------------------------------------------------*)

PROCEDURE open64(path  : ADDRESS;
                 oflag : SIGNED;
                 omode : modeT) : UNSIGNED;

          (*---------------------------------------------------------------*)
          (* Same as open but for file systems with large file support     *)
          (* oflag should contain O_LARGEFILE.                             *)
          (* omode must be added here due to the large file supprt, it can *)
          (* be a combination of sIRUSR, sIWUSR and sIXUSR                 *)
          (*---------------------------------------------------------------*)

PROCEDURE close (fildes : SIGNED) : SIGNED;

          (*---------------------------------------------------------------*)
          (* closes a file descriptor, so that it no longer refers to any  *)
          (* file and may be reused. Returns zero on success. On error,    *)
          (* -1 is returned, and errno is set appropriately.               *)
          (*---------------------------------------------------------------*)

PROCEDURE read(fildes : SIGNED;
               buf    : ADDRESS;
               nbyte  : UNSIGNED) : SIGNED;

          (*---------------------------------------------------------------*)
          (* Attempts to read up to nbyte bytes from file descriptor       *)
          (* fildes into the buffer starting at buf.                       *)
          (*                                                               *)
          (* On files that support seeking, the read operation commences   *)
          (* at the current file offset, and the file offset is            *)
          (* incremented by the number of bytes read. If the current file  *)
          (* offset is at or past the end of file, no bytes are read,      *)
          (* and read returns zero.                                        *)
          (*                                                               *)
          (* On success, the number of bytes read is returned (zero        *)
          (* indicates end of file), and the file position is advanced by  *)
          (* this number.                                                  *)
          (* On error, -1 is returned, and errno is set appropriately.     *)
          (*---------------------------------------------------------------*)

PROCEDURE write(fd    : SIGNED;
                buf   : ADDRESS;
                nbyte : UNSIGNED) : SIGNED;

          (*---------------------------------------------------------------*)
          (* Writes up to nbyte bytes from the buffer pointed buf to the   *)
          (* file referred to by the file descriptor fd.                   *)
          (*                                                               *)
          (* The number of bytes written may be less than count if, for    *)
          (* example, there is  insufficient  space on the underlying      *)
          (* physical medium.                                              *)
          (*                                                               *)
          (* On success, the number of bytes written is returned (zero     *)
          (* indicates noth‐ ing was written).                             *)
          (* On error, -1 is returned, and errno is set appropriately.     *)
          (*---------------------------------------------------------------*)

PROCEDURE unlink (path : ADDRESS) : SIGNED;

          (*---------------------------------------------------------------*)
          (* Deletes a name from the filesystem. If that name was the last *)
          (* link to a file and no processes have the file open the file   *)
          (* is deleted and the space it was using is made available for   *)
          (* reuse.                                                        *)
          (* On success, zero is returned.                                 *)
          (* On error, -1 is returned, and errno is set appropriately.     *)
          (*---------------------------------------------------------------*)

PROCEDURE fstat(fd      : SIGNED;
                VAR buf : Stat) : SIGNED;

          (*---------------------------------------------------------------*)
          (* These functions return information about a file. See          *)
          (* definition of Stat record and appropriate manpage.            *)
          (*---------------------------------------------------------------*)

PROCEDURE chmod(Name  : ADDRESS; (* Dateiname *)
                modus : SIGNED) : SIGNED;

          (*---------------------------------------------------------------*)
          (* Change the permissions of a file with name "Name".            *)
          (* modus is the integer representation of the permissions        *)
          (* wanted, please refer to man 2 chmod for more details.         *)
          (*---------------------------------------------------------------*)

PROCEDURE lseek(fd    : SIGNED;
                Pos   : INTEGER32;
                modus : SIGNED) : INTEGER32;

          (*---------------------------------------------------------------*)
          (* Function repositions the offset of the open file associated   *)
          (* with the file descriptor fd to the argument Pos according to  *)
          (* the directive modus. For modus please refer to the manpage    *)
          (* of lseek.                                                     *)
          (*---------------------------------------------------------------*)

PROCEDURE lseek64(fd    : SIGNED;
                  Pos   : INTEGER64;
                  modus : SIGNED) : INTEGER64;

          (*---------------------------------------------------------------*)
          (* Same as lseek for large files ( > 2**32-1)                    *)
          (*---------------------------------------------------------------*)

PROCEDURE sync();

          (*---------------------------------------------------------------*)
          (* Force changed blocks to disk, update the super block.         *)
          (*---------------------------------------------------------------*)

TYPE  Lock = SIGNED;

CONST LockShared    = 1; (* Shared lock. More than one process may hold a *)
                         (* shared lock for a given file at a given time  *)
      LockExclusive = 2; (* Exclusive lock. Only one process may hold an  *)
                         (* exclusive lock for a given file at a given    *)
                         (* time.                                         *)
      LockUnlock    = 8; (* Unlock.                                       *)
      LockNoblock   = 4; (* Don't block when locking. May be specified    *)
                         (* (by or-ing) along with one of the other oper- *)
                         (* tions.                                        *)  

PROCEDURE flock(f    : SIGNED;         (* Kanalnummer               *)
                cmd  : Lock) : SIGNED; (* Obige LockXx - Konstanten *)

          (*---------------------------------------------------------------*)
          (* Apply or remove an advisory lock on an open file              *)
          (* Returns -1 in case of an error/R"uckgabewert -1 im Fehlerfall *)
          (*---------------------------------------------------------------*)

TYPE  tm    = RECORD
                tm_sec   : SIGNED;
                tm_min   : SIGNED;
                tm_hour  : SIGNED;
                tm_mday  : SIGNED;
                tm_mon   : SIGNED;
                tm_year  : SIGNED;
                tm_wday  : SIGNED;
                tm_yday  : SIGNED;
                tm_isdst : SIGNED;
              END;
      tmPtr = POINTER TO tm;

PROCEDURE time(VAR t : timeT) : timeT;

          (*---------------------------------------------------------------*)
          (* returns the time as the number of seconds since the Epoch,    *)
          (* 1970-01-01 00:00:00 +0000 (UTC).                              *)
          (*---------------------------------------------------------------*)

PROCEDURE times(VAR buffer: tms);

          (*---------------------------------------------------------------*)
          (* Stores the current process times in the struct tms that buf   *)
          (* points to.                                                    *)
          (*---------------------------------------------------------------*)

(*
 * PROCEDURE ErrorStr(VAR Fehlerflag : ARRAY OF CHAR);
 *
 *           (* Interface zu strerror(errno) *)
 *)

TYPE       uid_t  = CARDINAL8;
           StrPtr = POINTER TO ARRAY [0..1023] OF CHAR; (* Formale L"ange *)

PROCEDURE geteuid() : uid_t;

          (*---------------------------------------------------------------*)
          (* Returns the effective user ID of the calling process.         *)
          (*---------------------------------------------------------------*)

PROCEDURE getlogin() : StrPtr;

          (*---------------------------------------------------------------*)
          (* Returns a pointer to a string containing the name of the user *)
          (* logged in on the controlling terminal of the process, or a    *)
          (* NULL pointer if this information cannot be determined. The    *)
          (* string is statically allocated and might be overwritten on    *)
          (* subsequent calls to this function                             *)
          (*---------------------------------------------------------------*)

PROCEDURE getenv(name : StrPtr) : StrPtr;

          (*---------------------------------------------------------------*)
          (* Searches the environment list to find the environment         *)
          (* variable name and returns a pointer to the corresponding      *)
          (* value string.                                                 *)
          (*---------------------------------------------------------------*)

PROCEDURE pread(fd      : SIGNED; 
                buf     : ADDRESS; 
                nbyte   : UNSIGNED;
                offest  : SIGNED) : SIGNED;

          (*---------------------------------------------------------------*)
          (* Reads up to nbyte bytes from file descriptor fd at offset     *)
          (* offset (from the start of the file) into the buffer starting  *)
          (* at  buf. The  file offset is not changed.                     *)
          (*---------------------------------------------------------------*)

PROCEDURE malloc(size : UNSIGNED) : ADDRESS;

          (*---------------------------------------------------------------*)
          (* function allocates size bytes and returns a pointer to the    *)
          (* allocated memory. The memory is not initialized. If size      *)
          (* is 0, then malloc() returns either NULL, or a unique pointer  *)
          (* value that can later be successfully passed to free().        *)
          (*---------------------------------------------------------------*)

PROCEDURE free(ptr : ADDRESS);
 
          (*---------------------------------------------------------------*)
          (* function frees the memory space pointed to by ptr, which must *)
          (* have been returned by a previous call to malloc().            *)
          (* Otherwise, or if free(ptr) has already been called before,    *)
          (* undefined behavior occurs. If ptr is NULL, no operation is    *)
          (* performed.                                                    *)
          (*---------------------------------------------------------------*)

PROCEDURE system (command : ADDRESS) : SIGNED;

          (*---------------------------------------------------------------*)
          (* Executes a command specified in string "command" by calling   *)
          (* "/bin/sh -c 'command'" and returns after the command has been *)
          (* completed.                                                    *)
          (*---------------------------------------------------------------*)
 
(*
 *  Unused ...
 *
 *  TYPE   modeT     = CARDINAL32; (* ??? *)
 *
 *  PROCEDURE umask (cmask : modeT) : modeT;
 *  
 *  PROCEDURE access (path  : ADDRESS; amode : SIGNED) : SIGNED;
 *  
 *  PROCEDURE creat (path  : ADDRESS; cmode : SIGNED) : SIGNED;
 *  
 *  PROCEDURE sbrk (incr : SIGNED): ADDRESS;
 *  
 *  PROCEDURE stat (path: ADDRESS; VAR buf: Stat) : SIGNED;
 *  
 *  PROCEDURE exit (n: SIGNED);
 *  
 *  PROCEDURE abort ();
 *  
 *  PROCEDURE putchar(Zeichen : CHAR);
 *  
 *  PROCEDURE getchar() : CHAR;
 *  
 *  PROCEDURE gmtime   (VAR t : timeT) : tmPtr;
 *  
 *  PROCEDURE localtime(VAR t : timeT) : tmPtr;
 *
 *)

END UnixLib.