/* Output from p2c, the Pascal-to-C translator */
/* From input file "libc.def" */


#include <p2c/p2c.h> 


/* --fixme-- put the HAVE_UIO definition into configure.in in gm2 directory */
#define HAVE_UIO 

#if defined(HAVE_UIO) 
#include <sys/uio.h> 
#endif 

#define libcG 
#include "libc.h" 


/*
   getenv - returns the value of an environment string.
*/

char *libc_getenv (char *p)
{
	return( getenv(p) );
}

/*
   isatty - returns true if the default input channel is the keyboard.
*/

int libc_isatty()
{
	return( !isatty(STDOUT_FILENO) );
}


/*
   exit - returns an exit status to the process callee.
*/

void libc_exit (r)
int r;
{
	exit(r);
}


long libc_write(d, buf, nbytes)
long nbytes;
Anyptr buf;
long d;
{
	return( write(d, buf, nbytes) );
}


long libc_read(d, buf, nbytes)
long nbytes;
Anyptr buf;
long d;
{
	return( read(d, buf, nbytes) );
}


int libc_system (string)
Anyptr string;
{
    return( system( string ) );
}


/*
     abort - generate a fault

     abort() first closes all open files if possible, then sends
     an IOT signal to the process.  This signal usually results
     in termination with a core dump, which may be used for
     debugging.

     It is possible for abort() to return control if is caught or
     ignored, in which case the value returned is that of the
     kill(2V) system call.
*/

void libc_abort ()
{
	abort();
}


/*
   malloc and cfree.

DESCRIPTION
     These routines provide a general-purpose memory allocation
     package.  They maintain a table of free blocks for efficient
     allocation and coalescing of free storage.  When there is no
     suitable space already free, the allocation routines call
     sbrk() (see brk(2)) to get more memory from the system.
 
     Each of the allocation routines returns a pointer to space
     suitably aligned for storage of any type of object. Each
     returns a NULL pointer if the request cannot be completed.
*/


/*
     malloc - memory allocator.

     char *malloc(size)
     unsigned size;

     malloc() returns a pointer to a block of at least size
     bytes, which is appropriately aligned.  If size is zero,
     malloc() returns a non-NULL pointer, but this pointer should
     not be dereferenced.
*/

Anyptr libc_malloc (size)
unsigned long size;
{
	return( malloc(size) );
}


/*
     cfree - memory deallocator.
 
     cfree(ptr)
     char *ptr;

     free() releases a previously allocated block.  Its argument
     is a pointer to a block previously allocated by malloc, cal-
     loc, realloc, malloc, or memalign.
*/

void libc_cfree (ptr)
Anyptr ptr;
{
	cfree(ptr);
}


/*
   getpid - returns the process identification number.
*/

int libc_getpid (void)
{
  return( getpid() );
}


/*
   dup - duplicates the file descriptor, d.
*/

int libc_dup (int d)
{
  return( dup(d) );
}


/*
   close - closes the file descriptor, d.
*/

int libc_close (int d)
{
  return( close(d) );
}


/*
   open - opens the filename with flag and mode.
*/

int libc_open (char *filename, int flag, int mode)
{
  return( open(filename, flag, mode) );
}


/*
   creat - creates a new file.
*/

int libc_creat (char *filename, int mode)
{
  return( creat(filename, mode) );
}


/*
   lseek - seeks to a new position in a file.
*/

int libc_lseek (int fd, int offset, int whence)
{
  return( lseek(fd, offset, whence) );
}


/*
  perror - display errno along with str
*/

void libc_perror (const char *s)
{
  perror(s);
}


/*
   readv - reads in an io vector of bytes.
*/

int libc_readv (int fd, struct iovec *v, int n)
{
  return( readv(fd, v, n) );
}


/*
   writev - writes an io vector of bytes.
*/

int libc_writev (int fd, struct iovec *v, int n)
{
  return( writev(fd, v, n) );
}


/*
   environ - returns the libc variable environ.
*/

char **libc_environ (void)
{
#if ! (defined (VMS) || defined (OS2)) 
extern char **environ;
 return( environ );
#else 
 return( NULL );
#endif 
}


/*
  setenv - adds the variable name to the environment with the
           value value, if name does not already exist.
           If name does exist in the environment, then its
           value is changed to value if overwrite is non-zero;
           if overwrite is zero, then the value of name is not changed.
*/

#if !defined(HAVE_SETENV) && defined(HAVE_PUTENV) 

int setenv(char *var, char *string, int overwrite)
{
  int  exists, varLen, strLen, result;
  char *buf;

  buf    = getenv(var);
  exists = buf != NULL;
  if (exists && !overwrite)
      return -1;

  varLen = strlen(var);
  strLen = strlen(string);
  buf    = malloc(varLen+strLen+2);
  if (buf != NULL) {
      strcpy(buf, var);
      strcpy(buf+varLen, "=");
      strcpy(buf+varLen+1, string);
      result = putenv(buf);
      free(buf);
      return result;
  }

  return -1;
}

#endif 

int libc_setenv (char *name, char *value, int overwrite)
{
  return( setenv(name, value, overwrite) );
}


/*
 *  getcwd - copies the absolute pathname of  the
 *           current  working directory to the array pointed to by buf,
 *           which is of length size.
 *
 *           If the current absolute path name would require  a  buffer
 *           longer  than size elements, NULL is returned, and errno is
 *           set to ERANGE; an application should check for this error,
 *           and allocate a larger buffer if necessary.
 */

char *libc_getcwd (char *buf, int size)
{
  return( (char *)getcwd(buf, size) );
}


/*
   chown - The  owner  of  the  file  specified  by  path or by fd is
           changed.  Only the super-user may change the  owner  of  a
           file.   The  owner  of  a file may change the group of the
           file to any group of which that owner is  a  member.   The
           super-user may change the group arbitrarily.

           If  the owner or group is specified as -1, then that ID is
           not changed.

           On success, zero is returned.  On error, -1  is  returned,
           and errno is set appropriately.
*/

int libc_chown (char *filename, int uid, int gid)
{
  return( chown(filename, uid, gid) );
}


void _M2_libc_init (void)
{
}


/* End. */