Welcome to mirror list, hosted at ThFree Co, Russian Federation.

open.c « mmixware « sys « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0b9fbafe2f1180744e91cb5fa31750c2846e6f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* open for MMIXware.

   Copyright (C) 2001, 2002 Hans-Peter Nilsson

   Permission to use, copy, modify, and distribute this software is
   freely granted, provided that the above copyright notice, this notice
   and the following disclaimer are preserved with no changes.

   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   PURPOSE.  */

#include <stdlib.h>
#include <fcntl.h>
#include <_ansi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "sys/syscall.h"
#include <errno.h>

/* Let's keep the filehandle array here, since this is a primary
   initializer of it.  */
unsigned char _MMIX_allocated_filehandle[32] =
 {
   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
 };

int
_open (const char *path,
       int flags, ...)
{
  long fileno;
  unsigned char mode;
  long append_contents = 0;
  unsigned long prev_contents_size = 0;
  char *prev_contents = NULL;
  long ret;

  for (fileno = 0;
       fileno < (sizeof (_MMIX_allocated_filehandle) /
		 sizeof (_MMIX_allocated_filehandle[0]));
       fileno++)
    if (_MMIX_allocated_filehandle[fileno] == 0)
      break;

  if (fileno == (sizeof (_MMIX_allocated_filehandle) /
		 sizeof (_MMIX_allocated_filehandle[0])))
    {
      errno = EMFILE;
      return -1;
    }

  /* We map this to a fopen call.  The flags parameter is stymied because
     we don't support other than these flags.  */
  if (flags & ~(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_APPEND | O_TRUNC))
    {
      UNIMPLEMENTED (("path: %s, flags: %d", path, flags));
      errno = ENOSYS;
      return -1;
    }

  if ((flags & O_ACCMODE) == O_RDONLY)
    mode = BinaryRead;
  else if ((flags & (O_WRONLY | O_APPEND)) == (O_WRONLY | O_APPEND))
    {
      mode = BinaryReadWrite;
      append_contents = 1;
    }
  else if ((flags & (O_RDWR | O_APPEND)) == (O_RDWR | O_APPEND))
    {
      mode = BinaryReadWrite;
      append_contents = 1;
    }
  else if ((flags & (O_WRONLY | O_CREAT)) == (O_WRONLY | O_CREAT)
	   || (flags & (O_WRONLY | O_TRUNC)) == (O_WRONLY | O_TRUNC))
    mode = BinaryWrite;
  else if ((flags & (O_RDWR | O_CREAT)) == (O_RDWR | O_CREAT))
    mode = BinaryReadWrite;
  else if (flags & O_RDWR)
    mode = BinaryReadWrite;
  else
    {
      errno = EINVAL;
      return -1;
    }

  if (append_contents)
    {
      /* BinaryReadWrite is equal to "w+", so it truncates the file rather
	 than keeping the contents, as can be imagined if you're looking
	 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
	 contents, then re-opening the file as BinaryReadWrite and write
	 the previous contents.  This seems to work for the needs of
	 simple test-programs.  */
      long openexist = TRAP3f (SYS_Fopen, fileno, path, BinaryRead);
      if (openexist == 0)
	{
	  /* Yes, this file exists, now opened, so let's read it and keep
             the contents.  Better have the memory around for this to
             work.  */
	  long seekval = TRAP2f (SYS_Fseek, fileno, -1);

	  if (seekval == 0)
	    {
	      prev_contents_size = TRAP1f (SYS_Ftell, fileno);

	      /* If the file has non-zero size, we have something to
		 append to.  */
	      if (prev_contents_size != 0)
		{
		  /* Start reading from the beginning.  Ignore the return
		     value from this call: we'll notice if we can't read
		     as much as we want.  */
		  TRAP2f (SYS_Fseek, fileno, 0);

		  prev_contents = malloc (prev_contents_size);
		  if (prev_contents != 0)
		    {
		      /* I don't like the thought of trying to read the
			 whole file all at once, disregarding the size,
			 because the host system might not support that
			 and we'd get funky errors.  Read in 32k at a
			 time.  */
		      char *ptr = prev_contents;
		      unsigned long read_more = prev_contents_size;
		      unsigned long chunk_size = 1 << 15;

		      while (read_more >= chunk_size)
			{
			  long readval
			    = TRAP3f (SYS_Fread, fileno, ptr, chunk_size);

			  if (readval != 0)
			    {
			      free (prev_contents);
			      TRAP1f (SYS_Fclose, fileno);
			      errno = EIO;
			      return -1;
			    }
			  read_more -= chunk_size;
			  ptr += chunk_size;
			}

		      if (read_more != 0)
			{
			  long readval
			    = TRAP3f (SYS_Fread, fileno, ptr, read_more);
			  if (readval != 0)
			    {
			      free (prev_contents);
			      TRAP1f (SYS_Fclose, fileno);
			      errno = EIO;
			      return -1;
			    }
			}
		    }
		  else
		    {
		      /* Malloc of area to copy to failed.  The glibc
			 manpage says its open can return ENOMEM due to
			 kernel memory failures, so let's do that too
			 here.  */
		      errno = ENOMEM;
		      return -1;
		    }
		}
	    }
	  else
	    {
	      /* Seek failed.  Gotta be some I/O error.  */
	      errno = EIO;
	      return -1;
	    }

	  TRAP1f (SYS_Fclose, fileno);
	}
    }

  ret = TRAP3f (SYS_Fopen, fileno, path, mode);
  if (ret < 0)
    {
      /* It's totally unknown what the error was.  We'll just take our
	 chances and assume ENOENT.  */
      errno = ENOENT;
      return -1;
    }

  if (prev_contents_size != 0)
    {
      /* Write out the previous contents, a chunk at a time.  Leave the
	 file pointer at the end of the file.  */
      unsigned long write_more = prev_contents_size;
      unsigned long chunk_size = 1 << 15;
      char *ptr = prev_contents;

      while (write_more >= chunk_size)
	{
	  long writeval
	    = TRAP3f (SYS_Fwrite, fileno, ptr, chunk_size);
	  if (writeval != 0)
	    {
	      free (prev_contents);
	      TRAP1f (SYS_Fclose, fileno);
	      errno = EIO;
	      return -1;
	    }
	  write_more -= chunk_size;
	  ptr += chunk_size;
	}
      if (write_more != 0)
	{
	  long writeval
	    = TRAP3f (SYS_Fwrite, fileno, ptr, write_more);
	  if (writeval != 0)
	    {
	      free (prev_contents);
	      TRAP1f (SYS_Fclose, fileno);
	      errno = EIO;
	      return -1;
	    }
	}

      free (prev_contents);
    }

  _MMIX_allocated_filehandle[fileno] = 1;

  return fileno;
}