OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc2/] [newlib/] [libc/] [sys/] [mmixware/] [open.c] - Diff between revs 207 and 520

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 520
/* open for MMIXware.
/* open for MMIXware.
 
 
   Copyright (C) 2001, 2002 Hans-Peter Nilsson
   Copyright (C) 2001, 2002 Hans-Peter Nilsson
 
 
   Permission to use, copy, modify, and distribute this software is
   Permission to use, copy, modify, and distribute this software is
   freely granted, provided that the above copyright notice, this notice
   freely granted, provided that the above copyright notice, this notice
   and the following disclaimer are preserved with no changes.
   and the following disclaimer are preserved with no changes.
 
 
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   PURPOSE.  */
   PURPOSE.  */
 
 
#include <stdlib.h>
#include <stdlib.h>
#include <fcntl.h>
#include <fcntl.h>
#include <_ansi.h>
#include <_ansi.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include "sys/syscall.h"
#include "sys/syscall.h"
#include <errno.h>
#include <errno.h>
 
 
/* Let's keep the filehandle array here, since this is a primary
/* Let's keep the filehandle array here, since this is a primary
   initializer of it.  */
   initializer of it.  */
unsigned char _MMIX_allocated_filehandle[32] =
unsigned char _MMIX_allocated_filehandle[32] =
 {
 {
   1,
   1,
   1,
   1,
   1,
   1,
   0, 0, 0, 0, 0,
   0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0
   0, 0, 0, 0, 0, 0, 0, 0
 };
 };
 
 
int
int
_open (const char *path,
_open (const char *path,
       int flags, ...)
       int flags, ...)
{
{
  long fileno;
  long fileno;
  unsigned char mode;
  unsigned char mode;
  long append_contents = 0;
  long append_contents = 0;
  unsigned long prev_contents_size = 0;
  unsigned long prev_contents_size = 0;
  char *prev_contents = NULL;
  char *prev_contents = NULL;
  long ret;
  long ret;
 
 
  for (fileno = 0;
  for (fileno = 0;
       fileno < (sizeof (_MMIX_allocated_filehandle) /
       fileno < (sizeof (_MMIX_allocated_filehandle) /
                 sizeof (_MMIX_allocated_filehandle[0]));
                 sizeof (_MMIX_allocated_filehandle[0]));
       fileno++)
       fileno++)
    if (_MMIX_allocated_filehandle[fileno] == 0)
    if (_MMIX_allocated_filehandle[fileno] == 0)
      break;
      break;
 
 
  if (fileno == (sizeof (_MMIX_allocated_filehandle) /
  if (fileno == (sizeof (_MMIX_allocated_filehandle) /
                 sizeof (_MMIX_allocated_filehandle[0])))
                 sizeof (_MMIX_allocated_filehandle[0])))
    {
    {
      errno = EMFILE;
      errno = EMFILE;
      return -1;
      return -1;
    }
    }
 
 
  /* We map this to a fopen call.  The flags parameter is stymied because
  /* We map this to a fopen call.  The flags parameter is stymied because
     we don't support other than these flags.  */
     we don't support other than these flags.  */
  if (flags & ~(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_APPEND | O_TRUNC))
  if (flags & ~(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_APPEND | O_TRUNC))
    {
    {
      UNIMPLEMENTED (("path: %s, flags: %d", path, flags));
      UNIMPLEMENTED (("path: %s, flags: %d", path, flags));
      errno = ENOSYS;
      errno = ENOSYS;
      return -1;
      return -1;
    }
    }
 
 
  if ((flags & O_ACCMODE) == O_RDONLY)
  if ((flags & O_ACCMODE) == O_RDONLY)
    mode = BinaryRead;
    mode = BinaryRead;
  else if ((flags & (O_WRONLY | O_APPEND)) == (O_WRONLY | O_APPEND))
  else if ((flags & (O_WRONLY | O_APPEND)) == (O_WRONLY | O_APPEND))
    {
    {
      mode = BinaryReadWrite;
      mode = BinaryReadWrite;
      append_contents = 1;
      append_contents = 1;
    }
    }
  else if ((flags & (O_RDWR | O_APPEND)) == (O_RDWR | O_APPEND))
  else if ((flags & (O_RDWR | O_APPEND)) == (O_RDWR | O_APPEND))
    {
    {
      mode = BinaryReadWrite;
      mode = BinaryReadWrite;
      append_contents = 1;
      append_contents = 1;
    }
    }
  else if ((flags & (O_WRONLY | O_CREAT)) == (O_WRONLY | O_CREAT)
  else if ((flags & (O_WRONLY | O_CREAT)) == (O_WRONLY | O_CREAT)
           || (flags & (O_WRONLY | O_TRUNC)) == (O_WRONLY | O_TRUNC))
           || (flags & (O_WRONLY | O_TRUNC)) == (O_WRONLY | O_TRUNC))
    mode = BinaryWrite;
    mode = BinaryWrite;
  else if ((flags & (O_RDWR | O_CREAT)) == (O_RDWR | O_CREAT))
  else if ((flags & (O_RDWR | O_CREAT)) == (O_RDWR | O_CREAT))
    mode = BinaryReadWrite;
    mode = BinaryReadWrite;
  else if (flags & O_RDWR)
  else if (flags & O_RDWR)
    mode = BinaryReadWrite;
    mode = BinaryReadWrite;
  else
  else
    {
    {
      errno = EINVAL;
      errno = EINVAL;
      return -1;
      return -1;
    }
    }
 
 
  if (append_contents)
  if (append_contents)
    {
    {
      /* BinaryReadWrite is equal to "w+", so it truncates the file rather
      /* BinaryReadWrite is equal to "w+", so it truncates the file rather
         than keeping the contents, as can be imagined if you're looking
         than keeping the contents, as can be imagined if you're looking
         for append functionality.  The only way we can keep the contents
         for append functionality.  The only way we can keep the contents
         so we can append to it, is by first reading in and saving the
         so we can append to it, is by first reading in and saving the
         contents, then re-opening the file as BinaryReadWrite and write
         contents, then re-opening the file as BinaryReadWrite and write
         the previous contents.  This seems to work for the needs of
         the previous contents.  This seems to work for the needs of
         simple test-programs.  */
         simple test-programs.  */
      long openexist = TRAP3f (SYS_Fopen, fileno, path, BinaryRead);
      long openexist = TRAP3f (SYS_Fopen, fileno, path, BinaryRead);
      if (openexist == 0)
      if (openexist == 0)
        {
        {
          /* Yes, this file exists, now opened, so let's read it and keep
          /* Yes, this file exists, now opened, so let's read it and keep
             the contents.  Better have the memory around for this to
             the contents.  Better have the memory around for this to
             work.  */
             work.  */
          long seekval = TRAP2f (SYS_Fseek, fileno, -1);
          long seekval = TRAP2f (SYS_Fseek, fileno, -1);
 
 
          if (seekval == 0)
          if (seekval == 0)
            {
            {
              prev_contents_size = TRAP1f (SYS_Ftell, fileno);
              prev_contents_size = TRAP1f (SYS_Ftell, fileno);
 
 
              /* If the file has non-zero size, we have something to
              /* If the file has non-zero size, we have something to
                 append to.  */
                 append to.  */
              if (prev_contents_size != 0)
              if (prev_contents_size != 0)
                {
                {
                  /* Start reading from the beginning.  Ignore the return
                  /* Start reading from the beginning.  Ignore the return
                     value from this call: we'll notice if we can't read
                     value from this call: we'll notice if we can't read
                     as much as we want.  */
                     as much as we want.  */
                  TRAP2f (SYS_Fseek, fileno, 0);
                  TRAP2f (SYS_Fseek, fileno, 0);
 
 
                  prev_contents = malloc (prev_contents_size);
                  prev_contents = malloc (prev_contents_size);
                  if (prev_contents != 0)
                  if (prev_contents != 0)
                    {
                    {
                      /* I don't like the thought of trying to read the
                      /* I don't like the thought of trying to read the
                         whole file all at once, disregarding the size,
                         whole file all at once, disregarding the size,
                         because the host system might not support that
                         because the host system might not support that
                         and we'd get funky errors.  Read in 32k at a
                         and we'd get funky errors.  Read in 32k at a
                         time.  */
                         time.  */
                      char *ptr = prev_contents;
                      char *ptr = prev_contents;
                      unsigned long read_more = prev_contents_size;
                      unsigned long read_more = prev_contents_size;
                      unsigned long chunk_size = 1 << 15;
                      unsigned long chunk_size = 1 << 15;
 
 
                      while (read_more >= chunk_size)
                      while (read_more >= chunk_size)
                        {
                        {
                          long readval
                          long readval
                            = TRAP3f (SYS_Fread, fileno, ptr, chunk_size);
                            = TRAP3f (SYS_Fread, fileno, ptr, chunk_size);
 
 
                          if (readval != 0)
                          if (readval != 0)
                            {
                            {
                              free (prev_contents);
                              free (prev_contents);
                              TRAP1f (SYS_Fclose, fileno);
                              TRAP1f (SYS_Fclose, fileno);
                              errno = EIO;
                              errno = EIO;
                              return -1;
                              return -1;
                            }
                            }
                          read_more -= chunk_size;
                          read_more -= chunk_size;
                          ptr += chunk_size;
                          ptr += chunk_size;
                        }
                        }
 
 
                      if (read_more != 0)
                      if (read_more != 0)
                        {
                        {
                          long readval
                          long readval
                            = TRAP3f (SYS_Fread, fileno, ptr, read_more);
                            = TRAP3f (SYS_Fread, fileno, ptr, read_more);
                          if (readval != 0)
                          if (readval != 0)
                            {
                            {
                              free (prev_contents);
                              free (prev_contents);
                              TRAP1f (SYS_Fclose, fileno);
                              TRAP1f (SYS_Fclose, fileno);
                              errno = EIO;
                              errno = EIO;
                              return -1;
                              return -1;
                            }
                            }
                        }
                        }
                    }
                    }
                  else
                  else
                    {
                    {
                      /* Malloc of area to copy to failed.  The glibc
                      /* Malloc of area to copy to failed.  The glibc
                         manpage says its open can return ENOMEM due to
                         manpage says its open can return ENOMEM due to
                         kernel memory failures, so let's do that too
                         kernel memory failures, so let's do that too
                         here.  */
                         here.  */
                      errno = ENOMEM;
                      errno = ENOMEM;
                      return -1;
                      return -1;
                    }
                    }
                }
                }
            }
            }
          else
          else
            {
            {
              /* Seek failed.  Gotta be some I/O error.  */
              /* Seek failed.  Gotta be some I/O error.  */
              errno = EIO;
              errno = EIO;
              return -1;
              return -1;
            }
            }
 
 
          TRAP1f (SYS_Fclose, fileno);
          TRAP1f (SYS_Fclose, fileno);
        }
        }
    }
    }
 
 
  ret = TRAP3f (SYS_Fopen, fileno, path, mode);
  ret = TRAP3f (SYS_Fopen, fileno, path, mode);
  if (ret < 0)
  if (ret < 0)
    {
    {
      /* It's totally unknown what the error was.  We'll just take our
      /* It's totally unknown what the error was.  We'll just take our
         chances and assume ENOENT.  */
         chances and assume ENOENT.  */
      errno = ENOENT;
      errno = ENOENT;
      return -1;
      return -1;
    }
    }
 
 
  if (prev_contents_size != 0)
  if (prev_contents_size != 0)
    {
    {
      /* Write out the previous contents, a chunk at a time.  Leave the
      /* Write out the previous contents, a chunk at a time.  Leave the
         file pointer at the end of the file.  */
         file pointer at the end of the file.  */
      unsigned long write_more = prev_contents_size;
      unsigned long write_more = prev_contents_size;
      unsigned long chunk_size = 1 << 15;
      unsigned long chunk_size = 1 << 15;
      char *ptr = prev_contents;
      char *ptr = prev_contents;
 
 
      while (write_more >= chunk_size)
      while (write_more >= chunk_size)
        {
        {
          long writeval
          long writeval
            = TRAP3f (SYS_Fwrite, fileno, ptr, chunk_size);
            = TRAP3f (SYS_Fwrite, fileno, ptr, chunk_size);
          if (writeval != 0)
          if (writeval != 0)
            {
            {
              free (prev_contents);
              free (prev_contents);
              TRAP1f (SYS_Fclose, fileno);
              TRAP1f (SYS_Fclose, fileno);
              errno = EIO;
              errno = EIO;
              return -1;
              return -1;
            }
            }
          write_more -= chunk_size;
          write_more -= chunk_size;
          ptr += chunk_size;
          ptr += chunk_size;
        }
        }
      if (write_more != 0)
      if (write_more != 0)
        {
        {
          long writeval
          long writeval
            = TRAP3f (SYS_Fwrite, fileno, ptr, write_more);
            = TRAP3f (SYS_Fwrite, fileno, ptr, write_more);
          if (writeval != 0)
          if (writeval != 0)
            {
            {
              free (prev_contents);
              free (prev_contents);
              TRAP1f (SYS_Fclose, fileno);
              TRAP1f (SYS_Fclose, fileno);
              errno = EIO;
              errno = EIO;
              return -1;
              return -1;
            }
            }
        }
        }
 
 
      free (prev_contents);
      free (prev_contents);
    }
    }
 
 
  _MMIX_allocated_filehandle[fileno] = 1;
  _MMIX_allocated_filehandle[fileno] = 1;
 
 
  return fileno;
  return fileno;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.