1  /* Glibc.c provides access to some libc functions.
       2  
       3  Copyright (C) 2016-2023 Free Software Foundation, Inc.
       4  Contributed by Gaius Mulley <gaius@glam.ac.uk>.
       5  
       6  This file is part of GNU Modula-2.
       7  
       8  GNU Modula-2 is free software; you can redistribute it and/or modify
       9  it under the terms of the GNU General Public License as published by
      10  the Free Software Foundation; either version 3, or (at your option)
      11  any later version.
      12  
      13  GNU Modula-2 is distributed in the hope that it will be useful, but
      14  WITHOUT ANY WARRANTY; without even the implied warranty of
      15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16  General Public License for more details.
      17  
      18  You should have received a copy of the GNU General Public License
      19  along with GNU Modula-2; see the file COPYING3.  If not see
      20  <http://www.gnu.org/licenses/>.  */
      21  
      22  #include "config.h"
      23  #include "system.h"
      24  
      25  #if defined(__cplusplus)
      26  #define EXTERN extern "C"
      27  #else
      28  #define EXTERN
      29  #endif
      30  
      31  EXTERN
      32  int
      33  libc_read (int fd, void *a, int nbytes)
      34  {
      35    return read (fd, a, nbytes);
      36  }
      37  
      38  EXTERN
      39  int
      40  libc_write (int fd, void *a, int nbytes)
      41  {
      42    return write (fd, a, nbytes);
      43  }
      44  
      45  EXTERN
      46  int
      47  libc_close (int fd)
      48  {
      49    return close (fd);
      50  }
      51  
      52  EXTERN
      53  int
      54  libc_exit (int code)
      55  {
      56    exit (code);
      57  }
      58  
      59  EXTERN
      60  void
      61  libc_perror (char *s)
      62  {
      63    perror (s);
      64  }
      65  
      66  EXTERN
      67  int
      68  libc_abort ()
      69  {
      70    abort ();
      71  }
      72  
      73  EXTERN
      74  int
      75  libc_strlen (char *s)
      76  {
      77    return strlen (s);
      78  }
      79  
      80  EXTERN
      81  time_t
      82  libc_time (time_t *buf)
      83  {
      84    return time (buf);
      85  }
      86  
      87  EXTERN
      88  void *
      89  libc_localtime (time_t *epochtime)
      90  {
      91    return localtime (epochtime);
      92  }
      93  
      94  EXTERN
      95  int
      96  libc_printf (char *_format, unsigned int _format_high, ...)
      97  {
      98    va_list arg;
      99    int done;
     100    char format[_format_high + 1];
     101    unsigned int i = 0;
     102    unsigned int j = 0;
     103    char *c;
     104  
     105    do
     106      {
     107        c = index (&_format[i], '\\');
     108        if (c == NULL)
     109          strcpy (&format[j], &_format[i]);
     110        else
     111          {
     112            memcpy (&format[j], &_format[i], (c - _format) - i);
     113            i = c - _format;
     114            j += c - _format;
     115            if (_format[i + 1] == 'n')
     116              format[j] = '\n';
     117            else
     118              format[j] = _format[i + 1];
     119            j++;
     120            i += 2;
     121          }
     122      }
     123    while (c != NULL);
     124  
     125    va_start (arg, _format_high);
     126    done = vfprintf (stdout, format, arg);
     127    va_end (arg);
     128    return done;
     129  }
     130  
     131  EXTERN
     132  int
     133  libc_snprintf (char *dest, size_t length, char *_format, unsigned int _format_high, ...)
     134  {
     135    va_list arg;
     136    int done;
     137    char format[_format_high + 1];
     138    unsigned int i = 0;
     139    unsigned int j = 0;
     140    char *c;
     141  
     142    do
     143      {
     144        c = index (&_format[i], '\\');
     145        if (c == NULL)
     146          strcpy (&format[j], &_format[i]);
     147        else
     148          {
     149            memcpy (&format[j], &_format[i], (c - _format) - i);
     150            i = c - _format;
     151            j += c - _format;
     152            if (_format[i + 1] == 'n')
     153              format[j] = '\n';
     154            else
     155              format[j] = _format[i + 1];
     156            j++;
     157            i += 2;
     158          }
     159      }
     160    while (c != NULL);
     161  
     162    va_start (arg, _format_high);
     163    done = vsnprintf (dest, length, format, arg);
     164    va_end (arg);
     165    return done;
     166  }
     167  
     168  EXTERN
     169  void *
     170  libc_malloc (unsigned int size)
     171  {
     172    return malloc (size);
     173  }
     174  
     175  EXTERN
     176  void
     177  libc_free (void *p)
     178  {
     179    free (p);
     180  }
     181  
     182  EXTERN
     183  char *
     184  libc_strcpy (char *dest, char *src)
     185  {
     186    return strcpy (dest, src);
     187  }
     188  
     189  EXTERN
     190  char *
     191  libc_strncpy (char *dest, char *src, int n)
     192  {
     193    return strncpy (dest, src, n);
     194  }
     195  
     196  EXTERN
     197  int
     198  libc_unlink (char *p)
     199  {
     200    return unlink (p);
     201  }
     202  
     203  EXTERN
     204  int
     205  libc_system (char *command)
     206  {
     207    return system (command);
     208  }
     209  
     210  EXTERN
     211  void *
     212  libc_memcpy (void *dest, void *src, int n)
     213  {
     214    return memcpy (dest, src, n);
     215  }
     216  
     217  EXTERN
     218  char *
     219  libc_getenv (char *name)
     220  {
     221    return getenv (name);
     222  }
     223  
     224  EXTERN
     225  int
     226  libc_putenv (char *name)
     227  {
     228    return putenv (name);
     229  }
     230  
     231  EXTERN
     232  int
     233  libc_creat (char *p, mode_t mode)
     234  {
     235    return creat (p, mode);
     236  }
     237  
     238  EXTERN
     239  int
     240  libc_open (char *p, int flags, mode_t mode)
     241  {
     242    return open (p, flags, mode);
     243  }
     244  
     245  EXTERN
     246  off_t
     247  libc_lseek (int fd, off_t offset, int whence)
     248  {
     249    return lseek (fd, offset, whence);
     250  }
     251  
     252  EXTERN
     253  void *
     254  libc_realloc (void *ptr, size_t size)
     255  {
     256    return realloc (ptr, size);
     257  }
     258  
     259  EXTERN
     260  void *
     261  libc_memset (void *s, int c, size_t n)
     262  {
     263    return memset (s, c, n);
     264  }
     265  
     266  EXTERN
     267  void *
     268  libc_memmove (void *dest, void *src, size_t n)
     269  {
     270    return memmove (dest, src, n);
     271  }
     272  
     273  EXTERN
     274  int
     275  libc_getpid (void)
     276  {
     277    return getpid ();
     278  }
     279  
     280  EXTERN
     281  unsigned int
     282  libc_sleep (unsigned int s)
     283  {
     284    return sleep (s);
     285  }
     286  
     287  EXTERN
     288  int
     289  libc_atexit (void (*function) (void))
     290  {
     291    return atexit (function);
     292  }