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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [sys/] [mmixware/] [open.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1010 ivang
/* open for MMIXware.
2
 
3
   Copyright (C) 2001 Hans-Peter Nilsson
4
 
5
   Permission to use, copy, modify, and distribute this software is
6
   freely granted, provided that the above copyright notice, this notice
7
   and the following disclaimer are preserved with no changes.
8
 
9
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
10
   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12
   PURPOSE.  */
13
 
14
#include <fcntl.h>
15
#include <_ansi.h>
16
#include <sys/types.h>
17
#include <sys/stat.h>
18
#include "sys/syscall.h"
19
#include <errno.h>
20
 
21
/* Let's keep the filehandle array here, since this is a primary
22
   initializer of it.  */
23
unsigned char _MMIX_allocated_filehandle[32] =
24
 {
25
   1,
26
   1,
27
   1,
28
   0, 0, 0, 0, 0,
29
   0, 0, 0, 0, 0, 0, 0, 0,
30
   0, 0, 0, 0, 0, 0, 0, 0,
31
   0, 0, 0, 0, 0, 0, 0, 0
32
 };
33
 
34
int
35
_open (const char *path,
36
       int flags, ...)
37
{
38
  long fileno;
39
  unsigned char mode;
40
  long fffile = 0;
41
  long ret;
42
 
43
  for (fileno = 0;
44
       fileno < (sizeof (_MMIX_allocated_filehandle) /
45
                 sizeof (_MMIX_allocated_filehandle[0]));
46
       fileno++)
47
    if (_MMIX_allocated_filehandle[fileno] == 0)
48
      break;
49
 
50
  if (fileno == (sizeof (_MMIX_allocated_filehandle) /
51
                 sizeof (_MMIX_allocated_filehandle[0])))
52
    {
53
      errno = EMFILE;
54
      return -1;
55
    }
56
 
57
  /* We map this to a fopen call.  The flags parameter is stymied because
58
     we don't support more other than these flags.  */
59
  if (flags & ~(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_APPEND | O_TRUNC))
60
    {
61
      UNIMPLEMENTED (("path: %s, flags: %d", path, flags));
62
      errno = ENOSYS;
63
      return -1;
64
    }
65
 
66
  if ((flags & O_ACCMODE) == O_RDONLY)
67
    mode = BinaryRead;
68
  else if ((flags & (O_WRONLY | O_APPEND)) == (O_WRONLY | O_APPEND))
69
    {
70
      mode = BinaryReadWrite;
71
      fffile = 1;
72
    }
73
  else if ((flags & (O_RDWR | O_APPEND)) == (O_RDWR | O_APPEND))
74
    {
75
      mode = BinaryReadWrite;
76
      fffile = 1;
77
    }
78
  else if ((flags & (O_WRONLY | O_CREAT)) == (O_WRONLY | O_CREAT)
79
           || (flags & (O_WRONLY | O_TRUNC)) == (O_WRONLY | O_TRUNC))
80
    mode = BinaryWrite;
81
  else if ((flags & (O_RDWR | O_CREAT)) == (O_RDWR | O_CREAT))
82
    mode = BinaryReadWrite;
83
  else if (flags & O_RDWR)
84
    mode = BinaryReadWrite;
85
  else
86
    {
87
      errno = EINVAL;
88
      return -1;
89
    }
90
 
91
  ret = TRAP3f (SYS_Fopen, fileno, path, mode);
92
  if (ret < 0)
93
    {
94
      /* It's totally unknown what the error was.  We'll just take our
95
         chances and assume ENOENT.  */
96
      errno = ENOENT;
97
      return -1;
98
    }
99
 
100
  _MMIX_allocated_filehandle[fileno] = 1;
101
 
102
  if (fffile)
103
    {
104
      TRAP2f (SYS_Fseek, fileno, -1);
105
    }
106
 
107
  return fileno;
108
}

powered by: WebSVN 2.1.0

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