]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /** @file\r |
2 | EFI versions of NetBSD system calls.\r | |
3 | \r | |
4 | Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials are licensed and made available under\r | |
6 | the terms and conditions of the BSD License that accompanies this distribution.\r | |
7 | The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | #include <Uefi.h>\r | |
15 | #include <Library/UefiLib.h>\r | |
16 | #include <Library/BaseLib.h>\r | |
17 | #include <Library/MemoryAllocationLib.h>\r | |
18 | #include <Library/ShellLib.h>\r | |
19 | \r | |
20 | #include <LibConfig.h>\r | |
21 | #include <sys/EfiCdefs.h>\r | |
22 | \r | |
23 | #include <sys/ansi.h>\r | |
24 | #include <errno.h>\r | |
25 | #include <stdarg.h>\r | |
26 | #include <string.h>\r | |
27 | #include <wchar.h>\r | |
28 | #include <sys/fcntl.h>\r | |
29 | #include <sys/stat.h>\r | |
30 | #include <sys/syslimits.h>\r | |
31 | #include "SysEfi.h"\r | |
32 | #include <MainData.h>\r | |
33 | #include <extern.h> // Library/include/extern.h: Private to implementation\r | |
34 | #include <Efi/Console.h>\r | |
35 | \r | |
36 | /* Macros only used in this file. */\r | |
37 | // Parameters for the ValidateFD function.\r | |
38 | #define VALID_OPEN 1\r | |
39 | #define VALID_CLOSED 0\r | |
40 | #define VALID_DONT_CARE -1\r | |
41 | \r | |
42 | \r | |
43 | /* EFI versions of BSD system calls used in stdio */\r | |
44 | \r | |
45 | /* Normalize path so that forward slashes are replaced with backslashes.\r | |
46 | Backslashes are required for UEFI.\r | |
47 | */\r | |
48 | static void\r | |
49 | NormalizePath( const CHAR16 *path)\r | |
50 | {\r | |
51 | CHAR16 *temp;\r | |
52 | \r | |
53 | for( temp = (CHAR16 *)path; *temp; ++temp) {\r | |
54 | if(*temp == L'/') {\r | |
55 | *temp = L'\\';\r | |
56 | }\r | |
57 | }\r | |
58 | }\r | |
59 | \r | |
60 | /* Validate that fd refers to a valid file descriptor.\r | |
61 | IsOpen is interpreted as follows:\r | |
62 | - Positive fd must be OPEN\r | |
63 | - Zero fd must be CLOSED\r | |
64 | - Negative fd may be OPEN or CLOSED\r | |
65 | \r | |
66 | @retval TRUE fd is VALID\r | |
67 | @retval FALSE fd is INVALID\r | |
68 | */\r | |
69 | static BOOLEAN\r | |
70 | ValidateFD( int fd, int IsOpen)\r | |
71 | {\r | |
72 | BOOLEAN retval = FALSE;\r | |
73 | \r | |
74 | if((fd >= 0) && (fd < OPEN_MAX)) {\r | |
75 | retval = TRUE;\r | |
76 | if(IsOpen >= 0) {\r | |
77 | retval = (BOOLEAN)(gMD->fdarray[fd].State != 0); // TRUE if OPEN\r | |
78 | if(IsOpen == VALID_CLOSED) {\r | |
79 | retval = (BOOLEAN)!retval; // We want TRUE if CLOSED\r | |
80 | }\r | |
81 | }\r | |
82 | }\r | |
83 | return retval;\r | |
84 | }\r | |
85 | \r | |
86 | /* Find and reserve a free File Descriptor.\r | |
87 | \r | |
88 | Returns the first free File Descriptor greater than or equal to the,\r | |
89 | already validated, fd specified by Minfd.\r | |
90 | \r | |
91 | @return Returns -1 if there are no free FDs. Otherwise returns the\r | |
92 | found fd.\r | |
93 | */\r | |
94 | static int\r | |
95 | FindFreeFD( int MinFd )\r | |
96 | {\r | |
97 | struct __filedes *Mfd;\r | |
98 | int i;\r | |
99 | int fd = -1;\r | |
100 | \r | |
101 | Mfd = gMD->fdarray;\r | |
102 | \r | |
103 | // Get an available fd\r | |
104 | for(i=MinFd; i < OPEN_MAX; ++i) {\r | |
105 | if(Mfd[i].State == 0) {\r | |
106 | Mfd[i].State = S_ISYSTEM; // Temporarily mark this fd as reserved\r | |
107 | fd = i;\r | |
108 | break;\r | |
109 | }\r | |
110 | }\r | |
111 | return fd;\r | |
112 | }\r | |
113 | \r | |
114 | /** The isatty() function tests whether fildes, an open file descriptor,\r | |
115 | is associated with a terminal device.\r | |
116 | \r | |
117 | @retval 1 fildes is associated with a terminal.\r | |
118 | @retval 0 fildes is not associated with a terminal. errno is set to\r | |
119 | EBADF if fildes is not a valid open FD.\r | |
120 | **/\r | |
121 | int\r | |
122 | isatty (int fildes)\r | |
123 | {\r | |
124 | int retval = 0;\r | |
125 | EFI_FILE_HANDLE FileHandle;\r | |
126 | \r | |
127 | if(ValidateFD( fildes, VALID_OPEN)) {\r | |
128 | FileHandle = gMD->fdarray[fildes].FileHandle;\r | |
129 | retval = (FileHandle >= &gMD->StdIo[0].Abstraction) &&\r | |
130 | (FileHandle <= &gMD->StdIo[2].Abstraction);\r | |
131 | }\r | |
132 | else {\r | |
133 | errno = EBADF;\r | |
134 | }\r | |
135 | return retval;\r | |
136 | }\r | |
137 | \r | |
138 | static BOOLEAN\r | |
139 | IsDupFd( int fd)\r | |
140 | {\r | |
141 | EFI_FILE_HANDLE FileHandle;\r | |
142 | int i;\r | |
143 | BOOLEAN Ret = FALSE;\r | |
144 | \r | |
145 | if(ValidateFD( fd, VALID_OPEN )) {\r | |
146 | FileHandle = gMD->fdarray[fd].FileHandle;\r | |
147 | for(i=0; i < OPEN_MAX; ++i) {\r | |
148 | if(i == fd) continue;\r | |
149 | if(gMD->fdarray[i].State != 0) { // TRUE if fd is OPEN\r | |
150 | if(gMD->fdarray[i].FileHandle == FileHandle) {\r | |
151 | Ret = TRUE;\r | |
152 | break;\r | |
153 | }\r | |
154 | }\r | |
155 | }\r | |
156 | }\r | |
157 | return Ret;\r | |
158 | }\r | |
159 | \r | |
160 | static int\r | |
161 | _closeX (int fd, int NewState)\r | |
162 | {\r | |
163 | struct __filedes *Mfd;\r | |
164 | RETURN_STATUS Status;\r | |
165 | int retval = 0;\r | |
166 | \r | |
167 | Status = EFIerrno = RETURN_SUCCESS; // In case of error before the EFI call.\r | |
168 | \r | |
169 | // Verify my pointers and get my FD.\r | |
170 | if(ValidateFD( fd, VALID_OPEN )) {\r | |
171 | Mfd = &gMD->fdarray[fd];\r | |
172 | // Check if there are duplicates using this FileHandle\r | |
173 | if(! IsDupFd(fd)) {\r | |
174 | // Only do the close if no one else is using the FileHandle\r | |
175 | if(isatty(fd)) {\r | |
176 | Status = Mfd->FileHandle->Close( Mfd->FileHandle);\r | |
177 | }\r | |
178 | else {\r | |
179 | Status = ShellCloseFile( (SHELL_FILE_HANDLE *)&Mfd->FileHandle);\r | |
180 | }\r | |
181 | }\r | |
182 | Mfd->State = NewState; // Close this FD or reserve it\r | |
183 | if(Status != RETURN_SUCCESS) {\r | |
184 | errno = EFI2errno(Status);\r | |
185 | EFIerrno = Status;\r | |
186 | retval = -1;\r | |
187 | }\r | |
188 | }\r | |
189 | else {\r | |
190 | // Bad FD\r | |
191 | errno = EBADF;\r | |
192 | retval = -1;\r | |
193 | }\r | |
194 | return retval;\r | |
195 | }\r | |
196 | \r | |
197 | /** The close() function shall deallocate the file descriptor indicated by fd.\r | |
198 | To deallocate means to make the file descriptor available for return by\r | |
199 | subsequent calls to open() or other functions that allocate file\r | |
200 | descriptors. All outstanding record locks owned by the process on the file\r | |
201 | associated with the file descriptor shall be removed (that is, unlocked).\r | |
202 | \r | |
203 | @return Upon successful completion, 0 shall be returned; otherwise,\r | |
204 | -1 shall be returned and errno set to indicate the error.\r | |
205 | **/\r | |
206 | int\r | |
207 | close (int fd)\r | |
208 | {\r | |
209 | //Print(L"Closing fd %d\n", fd);\r | |
210 | return _closeX(fd, 0);\r | |
211 | }\r | |
212 | \r | |
213 | /* Wide character version of unlink */\r | |
214 | int\r | |
215 | Uunlink (const wchar_t *Path)\r | |
216 | {\r | |
217 | EFI_FILE_HANDLE FileHandle;\r | |
218 | RETURN_STATUS Status;\r | |
219 | \r | |
220 | EFIerrno = RETURN_SUCCESS;\r | |
221 | \r | |
222 | NormalizePath( Path);\r | |
223 | // We can only delete open files.\r | |
224 | Status = ShellOpenFileByName( Path, (SHELL_FILE_HANDLE *)&FileHandle, 3, 0);\r | |
225 | if(Status != RETURN_SUCCESS) {\r | |
226 | errno = EFI2errno(Status);\r | |
227 | EFIerrno = Status;\r | |
228 | return -1;\r | |
229 | }\r | |
230 | Status = ShellDeleteFile( (SHELL_FILE_HANDLE *)&FileHandle);\r | |
231 | if(Status != RETURN_SUCCESS) {\r | |
232 | errno = EFI2errno(Status);\r | |
233 | EFIerrno = Status;\r | |
234 | return -1;\r | |
235 | }\r | |
236 | return 0;\r | |
237 | }\r | |
238 | \r | |
239 | /**\r | |
240 | **/\r | |
241 | int\r | |
242 | unlink (const char *path)\r | |
243 | {\r | |
244 | // Convert path from MBCS to WCS\r | |
245 | (void)AsciiStrToUnicodeStr( path, gMD->UString);\r | |
246 | \r | |
247 | return Uunlink(gMD->UString);\r | |
248 | }\r | |
249 | \r | |
250 | /** The fcntl() function shall perform the operations described below on open\r | |
251 | files. The fildes argument is a file descriptor.\r | |
252 | \r | |
253 | The available values for cmd are defined in <fcntl.h> and are as follows:\r | |
254 | - F_DUPFD - Return a new file descriptor which shall be the lowest\r | |
255 | numbered available (that is, not already open) file\r | |
256 | descriptor greater than or equal to the third argument, arg,\r | |
257 | taken as an integer of type int. The new file descriptor\r | |
258 | shall refer to the same open file description as the original\r | |
259 | file descriptor, and shall share any locks. The FD_CLOEXEC\r | |
260 | flag associated with the new file descriptor shall be cleared\r | |
261 | to keep the file open across calls to one of the exec functions.\r | |
262 | - F_GETFD - Get the file descriptor flags defined in <fcntl.h> that are\r | |
263 | associated with the file descriptor fildes. File descriptor\r | |
264 | flags are associated with a single file descriptor and do not\r | |
265 | affect other file descriptors that refer to the same file.\r | |
266 | - F_SETFD - Set the file descriptor flags defined in <fcntl.h>, that are\r | |
267 | associated with fildes, to the third argument, arg, taken\r | |
268 | as type int. If the FD_CLOEXEC flag in the third argument\r | |
269 | is 0, the file shall remain open across the exec\r | |
270 | functions; otherwise, the file shall be closed upon\r | |
271 | successful execution of one of the exec functions.\r | |
272 | - F_GETFL - Get the file status flags and file access modes, defined in\r | |
273 | <fcntl.h>, for the file description associated with fildes.\r | |
274 | The file access modes can be extracted from the return\r | |
275 | value using the mask O_ACCMODE, which is defined in\r | |
276 | <fcntl.h>. File status flags and file access modes are\r | |
277 | associated with the file description and do not affect\r | |
278 | other file descriptors that refer to the same file with\r | |
279 | different open file descriptions.\r | |
280 | - F_SETFL - Set the file status flags, defined in <fcntl.h>, for the file\r | |
281 | description associated with fildes from the corresponding\r | |
282 | bits in the third argument, arg, taken as type int. Bits\r | |
283 | corresponding to the file access mode and the file creation\r | |
284 | flags, as defined in <fcntl.h>, that are set in arg shall\r | |
285 | be ignored. If any bits in arg other than those mentioned\r | |
286 | here are changed by the application, the result is unspecified.\r | |
287 | - F_GETOWN - If fildes refers to a socket, get the process or process group\r | |
288 | ID specified to receive SIGURG signals when out-of-band\r | |
289 | data is available. Positive values indicate a process ID;\r | |
290 | negative values, other than -1, indicate a process group\r | |
291 | ID. If fildes does not refer to a socket, the results are\r | |
292 | unspecified.\r | |
293 | - F_SETOWN - If fildes refers to a socket, set the process or process\r | |
294 | group ID specified to receive SIGURG signals when\r | |
295 | out-of-band data is available, using the value of the third\r | |
296 | argument, arg, taken as type int. Positive values indicate\r | |
297 | a process ID; negative values, other than -1, indicate a\r | |
298 | process group ID. If fildes does not refer to a socket, the\r | |
299 | results are unspecified.\r | |
300 | \r | |
301 | The fcntl() function shall fail if:\r | |
302 | \r | |
303 | [EBADF] The fildes argument is not a valid open file descriptor.\r | |
304 | [EINVAL] The cmd argument is invalid, or the cmd argument is F_DUPFD\r | |
305 | and arg is negative or greater than or equal to {OPEN_MAX}.\r | |
306 | [EMFILE] The argument cmd is F_DUPFD and {OPEN_MAX} file descriptors\r | |
307 | are currently open in the calling process, or no file\r | |
308 | descriptors greater than or equal to arg are available.\r | |
309 | [EOVERFLOW] One of the values to be returned cannot be represented correctly.\r | |
310 | \r | |
311 | @return Upon successful completion, the value returned shall depend on\r | |
312 | cmd as follows:\r | |
313 | - F_DUPFD - A new file descriptor.\r | |
314 | - F_GETFD - Value of flags defined in <fcntl.h>. The return value\r | |
315 | shall not be negative.\r | |
316 | - F_SETFD - Value other than -1.\r | |
317 | - F_GETFL - Value of file status flags and access modes. The return\r | |
318 | value is not negative.\r | |
319 | - F_SETFL - Value other than -1.\r | |
320 | - F_GETOWN - Value of the socket owner process or process group;\r | |
321 | this will not be -1.\r | |
322 | - F_SETOWN - Value other than -1.\r | |
323 | Otherwise, -1 shall be returned and errno set to indicate the error.\r | |
324 | \r | |
325 | **/\r | |
326 | int\r | |
327 | fcntl (int fildes, int cmd, ...)\r | |
328 | {\r | |
329 | va_list p3;\r | |
330 | struct __filedes *MyFd;\r | |
331 | int retval = -1;\r | |
332 | int temp;\r | |
333 | \r | |
334 | //Print(L"%a( %d, %d, ...)\n", __func__, fildes, cmd);\r | |
335 | va_start(p3, cmd);\r | |
336 | \r | |
337 | if(ValidateFD( fildes, VALID_OPEN )) {\r | |
338 | MyFd = &gMD->fdarray[fildes];\r | |
339 | \r | |
340 | switch(cmd) {\r | |
341 | case F_DUPFD:\r | |
342 | temp = va_arg(p3, int);\r | |
343 | if(ValidateFD( temp, VALID_DONT_CARE )) {\r | |
344 | temp = FindFreeFD( temp );\r | |
345 | if(temp < 0) {\r | |
346 | errno = EMFILE;\r | |
347 | break;\r | |
348 | }\r | |
349 | /* temp is now a valid fd reserved for further use\r | |
350 | so copy fd into temp.\r | |
351 | */\r | |
352 | (void)memcpy(&gMD->fdarray[temp], MyFd, sizeof(struct __filedes));\r | |
353 | retval = temp;\r | |
354 | }\r | |
355 | else {\r | |
356 | errno = EINVAL;\r | |
357 | }\r | |
358 | break;\r | |
359 | //case F_SETFD:\r | |
360 | case F_SETFL:\r | |
361 | retval = MyFd->Oflags; // Get original value\r | |
362 | temp = va_arg(p3, int);\r | |
363 | temp &= O_SETMASK; // Only certain bits can be set\r | |
364 | temp |= retval & O_SETMASK;\r | |
365 | MyFd->Oflags = temp; // Set new value\r | |
366 | break;\r | |
367 | //case F_SETFL:\r | |
368 | case F_SETFD:\r | |
369 | retval = MyFd->State;\r | |
370 | break;\r | |
371 | case F_SETOWN:\r | |
372 | retval = MyFd->SocProc;\r | |
373 | MyFd->SocProc = va_arg(p3, int);\r | |
374 | break;\r | |
375 | case F_GETFD:\r | |
376 | //retval = MyFd->Oflags;\r | |
377 | retval = MyFd->State;\r | |
378 | break;\r | |
379 | case F_GETFL:\r | |
380 | //retval = MyFd->State;\r | |
381 | retval = MyFd->Oflags;\r | |
382 | break;\r | |
383 | case F_GETOWN:\r | |
384 | retval = MyFd->SocProc;\r | |
385 | break;\r | |
386 | default:\r | |
387 | errno = EINVAL;\r | |
388 | break;\r | |
389 | }\r | |
390 | }\r | |
391 | else {\r | |
392 | // Bad FD\r | |
393 | errno = EBADF;\r | |
394 | }\r | |
395 | va_end(p3);\r | |
396 | return retval;;\r | |
397 | }\r | |
398 | \r | |
399 | /** The dup() function provides an alternative interface to the\r | |
400 | service provided by fcntl() using the F_DUPFD command. The call:\r | |
401 | - fid = dup(fildes);\r | |
402 | shall be equivalent to:\r | |
403 | - fid = fcntl(fildes, F_DUPFD, 0);\r | |
404 | \r | |
405 | @return Upon successful completion a non-negative integer, namely the\r | |
406 | file descriptor, shall be returned; otherwise, -1 shall be\r | |
407 | returned and errno set to indicate the error.\r | |
408 | **/\r | |
409 | int\r | |
410 | dup (int fildes)\r | |
411 | {\r | |
412 | return fcntl(fildes, F_DUPFD, 0);\r | |
413 | }\r | |
414 | \r | |
415 | /** The dup2() function provides an alternative interface to the\r | |
416 | service provided by fcntl() using the F_DUPFD command. The call:\r | |
417 | - fid = dup2(fildes, fildes2);\r | |
418 | shall be equivalent to:\r | |
419 | - close(fildes2);\r | |
420 | - fid = fcntl(fildes, F_DUPFD, fildes2);\r | |
421 | except for the following:\r | |
422 | - If fildes2 is less than 0 or greater than or equal to {OPEN_MAX},\r | |
423 | dup2() shall return -1 with errno set to [EBADF].\r | |
424 | - If fildes is a valid file descriptor and is equal to fildes2, dup2()\r | |
425 | shall return fildes2 without closing it.\r | |
426 | - If fildes is not a valid file descriptor, dup2() shall return -1 and\r | |
427 | shall not close fildes2.\r | |
428 | - The value returned shall be equal to the value of fildes2 upon\r | |
429 | successful completion, or -1 upon failure.\r | |
430 | \r | |
431 | @return Upon successful completion a non-negative integer, namely\r | |
432 | fildes2, shall be returned; otherwise, -1 shall be\r | |
433 | returned and errno set to EBADF indicate the error.\r | |
434 | **/\r | |
435 | int\r | |
436 | dup2 (int fildes, int fildes2)\r | |
437 | {\r | |
438 | int retval = -1;\r | |
439 | \r | |
440 | if(ValidateFD( fildes, VALID_OPEN)) {\r | |
441 | retval = fildes2;\r | |
442 | if( fildes != fildes2) {\r | |
443 | if(ValidateFD( fildes2, VALID_DONT_CARE)) {\r | |
444 | gMD->fdarray[fildes2].State = S_ISYSTEM; // Mark the file closed, but reserved\r | |
445 | (void)memcpy(&gMD->fdarray[fildes2], // Duplicate fildes into fildes2\r | |
446 | &gMD->fdarray[fildes], sizeof(struct __filedes));\r | |
447 | }\r | |
448 | else {\r | |
449 | errno = EBADF;\r | |
450 | retval = -1;\r | |
451 | }\r | |
452 | }\r | |
453 | }\r | |
454 | else {\r | |
455 | errno = EBADF;\r | |
456 | }\r | |
457 | return retval;\r | |
458 | }\r | |
459 | \r | |
460 | /** Reposition a file's read/write offset.\r | |
461 | \r | |
462 | The lseek() function repositions the offset of the file descriptor fildes\r | |
463 | to the argument offset according to the directive how. The argument\r | |
464 | fildes must be an open file descriptor. lseek() repositions the file\r | |
465 | pointer fildes as follows:\r | |
466 | \r | |
467 | If how is SEEK_SET, the offset is set to offset bytes.\r | |
468 | \r | |
469 | If how is SEEK_CUR, the offset is set to its current location\r | |
470 | plus offset bytes.\r | |
471 | \r | |
472 | If how is SEEK_END, the offset is set to the size of the file\r | |
473 | plus offset bytes.\r | |
474 | \r | |
475 | The lseek() function allows the file offset to be set beyond the end of\r | |
476 | the existing end-of-file of the file. If data is later written at this\r | |
477 | point, subsequent reads of the data in the gap return bytes of zeros\r | |
478 | (until data is actually written into the gap).\r | |
479 | \r | |
480 | Some devices are incapable of seeking. The value of the pointer associ-\r | |
481 | ated with such a device is undefined.\r | |
482 | \r | |
483 | @return Upon successful completion, lseek() returns the resulting offset\r | |
484 | location as measured in bytes from the beginning of the file.\r | |
485 | Otherwise, a value of -1 is returned and errno is set to\r | |
486 | indicate the error.\r | |
487 | **/\r | |
488 | __off_t\r | |
489 | lseek (int fildes, __off_t offset, int how)\r | |
490 | {\r | |
491 | __off_t CurPos = -1;\r | |
492 | RETURN_STATUS Status = RETURN_SUCCESS;\r | |
493 | EFI_FILE_HANDLE FileHandle;\r | |
494 | \r | |
495 | EFIerrno = RETURN_SUCCESS; // In case of error without an EFI call\r | |
496 | \r | |
497 | if( how == SEEK_SET || how == SEEK_CUR || how == SEEK_END) {\r | |
498 | if(ValidateFD( fildes, VALID_OPEN)) {\r | |
499 | // Both of our parameters have been verified as valid\r | |
500 | FileHandle = gMD->fdarray[fildes].FileHandle;\r | |
501 | CurPos = 0;\r | |
502 | if(isatty(fildes)) {\r | |
503 | Status = FileHandle->SetPosition( FileHandle, offset);\r | |
504 | CurPos = offset;\r | |
505 | }\r | |
506 | else {\r | |
507 | if(how != SEEK_SET) {\r | |
508 | // We are doing a relative seek\r | |
509 | if(how == SEEK_END) {\r | |
510 | // seeking relative to EOF, so position there first.\r | |
511 | Status = ShellSetFilePosition( (SHELL_FILE_HANDLE)FileHandle, 0xFFFFFFFFFFFFFFFFULL);\r | |
512 | }\r | |
513 | if(Status == RETURN_SUCCESS) {\r | |
514 | // Now, determine our current position.\r | |
515 | Status = ShellGetFilePosition( (SHELL_FILE_HANDLE)FileHandle, (UINT64 *)&CurPos);\r | |
516 | }\r | |
517 | }\r | |
518 | if(Status == RETURN_SUCCESS) {\r | |
519 | /* CurPos now indicates the point we are seeking from, so seek... */\r | |
520 | Status = ShellSetFilePosition( (SHELL_FILE_HANDLE)FileHandle, (UINT64)(CurPos + offset));\r | |
521 | if(Status == RETURN_SUCCESS) {\r | |
522 | // Now, determine our final position.\r | |
523 | Status = ShellGetFilePosition( (SHELL_FILE_HANDLE)FileHandle, (UINT64 *)&CurPos);\r | |
524 | }\r | |
525 | }\r | |
526 | if(Status != RETURN_SUCCESS) {\r | |
527 | EFIerrno = Status;\r | |
528 | CurPos = -1;\r | |
529 | if(Status == EFI_UNSUPPORTED) {\r | |
530 | errno = EISDIR;\r | |
531 | }\r | |
532 | else {\r | |
533 | errno = EFI2errno(Status);\r | |
534 | }\r | |
535 | }\r | |
536 | }\r | |
537 | }\r | |
538 | else {\r | |
539 | errno = EBADF; // Bad File Descriptor\r | |
540 | }\r | |
541 | }\r | |
542 | else {\r | |
543 | errno = EINVAL; // Invalid how argument\r | |
544 | }\r | |
545 | return CurPos;\r | |
546 | }\r | |
547 | \r | |
548 | /** The directory path is created with the access permissions specified by\r | |
549 | perms.\r | |
550 | \r | |
551 | The directory is closed after it is created.\r | |
552 | \r | |
553 | @retval 0 The directory was created successfully.\r | |
554 | @retval -1 An error occurred and an error code is stored in errno.\r | |
555 | **/\r | |
556 | int\r | |
557 | mkdir (const char *path, __mode_t perms)\r | |
558 | {\r | |
559 | EFI_FILE_HANDLE FileHandle;\r | |
560 | RETURN_STATUS Status;\r | |
561 | EFI_FILE_INFO *FileInfo;\r | |
562 | \r | |
563 | // Convert name from MBCS to WCS\r | |
564 | (void)AsciiStrToUnicodeStr( path, gMD->UString);\r | |
565 | NormalizePath( gMD->UString);\r | |
566 | \r | |
567 | //Print(L"%a( \"%s\", 0x%8X)\n", __func__, gMD->UString, perms);\r | |
568 | Status = ShellCreateDirectory( gMD->UString, (SHELL_FILE_HANDLE *)&FileHandle);\r | |
569 | if(Status == RETURN_SUCCESS) {\r | |
570 | FileInfo = ShellGetFileInfo( FileHandle);\r | |
571 | if(FileInfo != NULL) {\r | |
572 | FileInfo->Attribute = Omode2EFI(perms);\r | |
573 | Status = ShellSetFileInfo( FileHandle, FileInfo);\r | |
574 | FreePool(FileInfo);\r | |
575 | if(Status == RETURN_SUCCESS) {\r | |
576 | (void)ShellCloseFile((SHELL_FILE_HANDLE *)&FileHandle);\r | |
577 | return 0;\r | |
578 | }\r | |
579 | }\r | |
580 | }\r | |
581 | errno = EFI2errno(Status);\r | |
582 | EFIerrno = Status;\r | |
583 | \r | |
584 | return -1;\r | |
585 | }\r | |
586 | \r | |
587 | /** Open a file.\r | |
588 | \r | |
589 | The EFI ShellOpenFileByName() function is used to perform the low-level\r | |
590 | file open operation. The primary task of open() is to translate from the\r | |
591 | flags used in the <stdio.h> environment to those used by the EFI function.\r | |
592 | \r | |
593 | The only valid flag combinations for ShellOpenFileByName() are:\r | |
594 | - Read\r | |
595 | - Read/Write\r | |
596 | - Create/Read/Write\r | |
597 | \r | |
598 | The mode value is saved in the FD to indicate permissions for further operations.\r | |
599 | \r | |
600 | O_RDONLY -- flags = EFI_FILE_MODE_READ -- this is always done\r | |
601 | O_WRONLY -- flags |= EFI_FILE_MODE_WRITE\r | |
602 | O_RDWR -- flags |= EFI_FILE_MODE_WRITE -- READ is already set\r | |
603 | \r | |
604 | O_NONBLOCK -- ignored\r | |
605 | O_APPEND -- Seek to EOF before every write\r | |
606 | O_CREAT -- flags |= EFI_FILE_MODE_CREATE\r | |
607 | O_TRUNC -- delete first then create new\r | |
608 | O_EXCL -- if O_CREAT is also set, open will fail if the file already exists.\r | |
609 | **/\r | |
610 | int\r | |
611 | open (const char *name, int oflags, int mode)\r | |
612 | {\r | |
613 | EFI_FILE_HANDLE FileHandle;\r | |
614 | struct __filedes *Mfd;\r | |
615 | RETURN_STATUS Status;\r | |
616 | UINT64 OpenMode;\r | |
617 | UINT64 Attributes;\r | |
618 | int fd = -1;\r | |
619 | UINT32 NewState;\r | |
620 | \r | |
621 | EFIerrno = RETURN_SUCCESS;\r | |
622 | Mfd = gMD->fdarray;\r | |
623 | \r | |
624 | // Convert name from MBCS to WCS\r | |
625 | (void)AsciiStrToUnicodeStr( name, gMD->UString);\r | |
626 | NormalizePath( gMD->UString);\r | |
627 | \r | |
628 | // Convert oflags to Attributes\r | |
629 | OpenMode = Oflags2EFI(oflags);\r | |
630 | if(OpenMode == 0) {\r | |
631 | errno = EINVAL;\r | |
632 | return -1;\r | |
633 | }\r | |
634 | \r | |
635 | //Attributes = Omode2EFI(mode);\r | |
636 | Attributes = 0;\r | |
637 | \r | |
638 | // Could add a test to see if the file name begins with a period.\r | |
639 | // If it does, then add the HIDDEN flag to Attributes.\r | |
640 | \r | |
641 | // Get an available fd\r | |
642 | fd = FindFreeFD( 0 );\r | |
643 | \r | |
644 | if( fd < 0 ) {\r | |
645 | // All available FDs are in use\r | |
646 | errno = EMFILE;\r | |
647 | return -1;\r | |
648 | }\r | |
649 | \r | |
650 | Status = ConOpen( NULL, &FileHandle, gMD->UString, OpenMode, Attributes);\r | |
651 | if(Status == RETURN_NO_MAPPING) {\r | |
652 | // Not a console device, how about a regular file device?\r | |
653 | \r | |
654 | /* Do we care if the file already exists?\r | |
655 | If O_TRUNC, then delete the file. It will be created anew subsequently.\r | |
656 | If O_EXCL, then error if the file exists and O_CREAT is set.\r | |
657 | \r | |
658 | !!!!!!!!! Change this to use ShellSetFileInfo() to actually truncate the file\r | |
659 | !!!!!!!!! instead of deleting and re-creating it.\r | |
660 | */\r | |
661 | if((oflags & O_TRUNC) || ((oflags & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {\r | |
662 | Status = ShellIsFile( gMD->UString );\r | |
663 | if(Status == RETURN_SUCCESS) {\r | |
664 | // The file exists\r | |
665 | if(oflags & O_TRUNC) {\r | |
666 | // We do a truncate by deleting the existing file and creating a new one.\r | |
667 | if(Uunlink(gMD->UString) != 0) {\r | |
668 | Mfd[fd].State = 0; // Release our reservation on this FD\r | |
669 | return -1; // errno and EFIerrno are already set.\r | |
670 | }\r | |
671 | }\r | |
672 | else if(oflags & (O_EXCL | O_CREAT)) {\r | |
673 | errno = EEXIST;\r | |
674 | EFIerrno = Status;\r | |
675 | Mfd[fd].State = 0; // Release our reservation on this FD\r | |
676 | return -1;\r | |
677 | }\r | |
678 | }\r | |
679 | }\r | |
680 | // Call the EFI Shell's Open function\r | |
681 | Status = ShellOpenFileByName( gMD->UString, (SHELL_FILE_HANDLE *)&FileHandle, OpenMode, Attributes);\r | |
682 | if(RETURN_ERROR(Status)) {\r | |
683 | Mfd[fd].State = 0; // Release our reservation on this FD\r | |
684 | // Set errno based upon Status\r | |
685 | errno = EFI2errno(Status);\r | |
686 | EFIerrno = Status;\r | |
687 | return -1;\r | |
688 | }\r | |
689 | // Successfully got a regular File\r | |
690 | NewState = S_IFREG;\r | |
691 | }\r | |
692 | else if(Status != RETURN_SUCCESS) {\r | |
693 | // Set errno based upon Status\r | |
694 | errno = EFI2errno(Status);\r | |
695 | EFIerrno = Status;\r | |
696 | return -1;\r | |
697 | }\r | |
698 | else {\r | |
699 | // Succesfully got a Console stream\r | |
700 | NewState = S_IFREG | _S_ITTY | _S_IFCHR;\r | |
701 | }\r | |
702 | \r | |
703 | // Update the info in the fd\r | |
704 | Mfd[fd].FileHandle = FileHandle;\r | |
705 | Mfd[fd].Oflags = oflags;\r | |
706 | Mfd[fd].Omode = mode;\r | |
707 | \r | |
708 | // Re-use OpenMode in order to build our final State value\r | |
709 | OpenMode = ( mode & S_ACC_READ ) ? S_ACC_READ : 0;\r | |
710 | OpenMode |= ( mode & S_ACC_WRITE ) ? S_ACC_WRITE : 0;\r | |
711 | \r | |
712 | Mfd[fd].State = NewState | (UINT32)OpenMode;\r | |
713 | \r | |
714 | // return the fd of our now open file\r | |
715 | return fd;\r | |
716 | }\r | |
717 | \r | |
718 | /** The rename() function changes the name of a file.\r | |
719 | The old argument points to the pathname of the file to be renamed. The new\r | |
720 | argument points to the new pathname of the file.\r | |
721 | \r | |
722 | If the old argument points to the pathname of a file that is not a\r | |
723 | directory, the new argument shall not point to the pathname of a\r | |
724 | directory. If the file named by the new argument exists, it shall be\r | |
725 | removed and old renamed to new. Write access permission is required for\r | |
726 | both the directory containing old and the directory containing new.\r | |
727 | \r | |
728 | If the old argument points to the pathname of a directory, the new\r | |
729 | argument shall not point to the pathname of a file that is not a\r | |
730 | directory. If the directory named by the new argument exists, it shall be\r | |
731 | removed and old renamed to new.\r | |
732 | \r | |
733 | The new pathname shall not contain a path prefix that names old. Write\r | |
734 | access permission is required for the directory containing old and the\r | |
735 | directory containing new. If the old argument points to the pathname of a\r | |
736 | directory, write access permission may be required for the directory named\r | |
737 | by old, and, if it exists, the directory named by new.\r | |
738 | \r | |
739 | If the rename() function fails for any reason other than [EIO], any file\r | |
740 | named by new shall be unaffected.\r | |
741 | \r | |
742 | @return Upon successful completion, rename() shall return 0; otherwise,\r | |
743 | -1 shall be returned, errno shall be set to indicate the error,\r | |
744 | and neither the file named by old nor the file named by new\r | |
745 | shall be changed or created.\r | |
746 | **/\r | |
747 | int\r | |
748 | rename (const char *old, const char *new)\r | |
749 | {\r | |
750 | // UINT64 InfoSize;\r | |
751 | // RETURN_STATUS Status;\r | |
752 | // EFI_FILE_INFO *NewFileInfo = NULL;\r | |
753 | // EFI_FILE_INFO *OldFileInfo;\r | |
754 | // char *Newfn;\r | |
755 | // int OldFd;\r | |
756 | \r | |
757 | //// Open old file\r | |
758 | // OldFd = open(old, O_RDONLY, 0);\r | |
759 | // if(OldFd >= 0) {\r | |
760 | // NewFileInfo = malloc(sizeof(EFI_FILE_INFO) + PATH_MAX);\r | |
761 | // if(NewFileInfo != NULL) {\r | |
762 | // OldFileInfo = ShellGetFileInfo( FileHandle);\r | |
763 | // if(OldFileInfo != NULL) {\r | |
764 | // // Copy the Old file info into our new buffer, and free the old.\r | |
765 | // memcpy(OldFileInfo, NewFileInfo, sizeof(EFI_FILE_INFO));\r | |
766 | // FreePool(OldFileInfo);\r | |
767 | // // Strip off all but the file name portion of new\r | |
768 | // NewFn = strrchr(new, '/');\r | |
769 | // if(NewFn == NULL) {\r | |
770 | // NewFn = strrchr(new '\\');\r | |
771 | // if(NewFn == NULL) {\r | |
772 | // NewFn = new;\r | |
773 | // }\r | |
774 | // }\r | |
775 | // // Convert new name from MBCS to WCS\r | |
776 | // (void)AsciiStrToUnicodeStr( NewFn, gMD->UString);\r | |
777 | // // Copy the new file name into our new file info buffer\r | |
778 | // wcsncpy(NewFileInfo->FileName, gMD->UString, wcslen(gMD->UString)+1);\r | |
779 | // // Apply the new file name\r | |
780 | // Status = ShellSetFileInfo(FileHandle);\r | |
781 | // if(Status == EFI_SUCCESS) {\r | |
782 | // // File has been successfully renamed. We are DONE!\r | |
783 | // return 0;\r | |
784 | // }\r | |
785 | // errno = EFI2errno( Status );\r | |
786 | // EFIerrno = Status;\r | |
787 | // }\r | |
788 | // else {\r | |
789 | // errno = EIO;\r | |
790 | // }\r | |
791 | // }\r | |
792 | // else {\r | |
793 | // errno = ENOMEM;\r | |
794 | // }\r | |
795 | // }\r | |
796 | return -1;\r | |
797 | }\r | |
798 | \r | |
799 | /**\r | |
800 | **/\r | |
801 | int\r | |
802 | rmdir (const char *path)\r | |
803 | {\r | |
804 | EFI_FILE_HANDLE FileHandle;\r | |
805 | RETURN_STATUS Status;\r | |
806 | EFI_FILE_INFO *FileInfo = NULL;\r | |
807 | int Count = 0;\r | |
808 | BOOLEAN NoFile = FALSE;\r | |
809 | \r | |
810 | errno = 0; // Make it easier to see if we have an error later\r | |
811 | \r | |
812 | // Convert name from MBCS to WCS\r | |
813 | (void)AsciiStrToUnicodeStr( path, gMD->UString);\r | |
814 | NormalizePath( gMD->UString);\r | |
815 | \r | |
816 | //Print(L"%a( \"%s\")\n", __func__, gMD->UString);\r | |
817 | Status = ShellOpenFileByName( gMD->UString, (SHELL_FILE_HANDLE *)&FileHandle,\r | |
818 | (EFI_FILE_MODE_READ || EFI_FILE_MODE_WRITE), 0);\r | |
819 | if(Status == RETURN_SUCCESS) {\r | |
820 | FileInfo = ShellGetFileInfo( (SHELL_FILE_HANDLE)FileHandle);\r | |
821 | if(FileInfo != NULL) {\r | |
822 | if((FileInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r | |
823 | errno = ENOTDIR;\r | |
824 | }\r | |
825 | else {\r | |
826 | // See if the directory has any entries other than ".." and ".".\r | |
827 | FreePool(FileInfo); // Free up the buffer from ShellGetFileInfo()\r | |
828 | Status = ShellFindFirstFile( (SHELL_FILE_HANDLE)FileHandle, &FileInfo);\r | |
829 | if(Status == RETURN_SUCCESS) {\r | |
830 | ++Count;\r | |
831 | while(Count < 3) {\r | |
832 | Status = ShellFindNextFile( (SHELL_FILE_HANDLE)FileHandle, FileInfo, &NoFile);\r | |
833 | if(Status == RETURN_SUCCESS) {\r | |
834 | if(NoFile) {\r | |
835 | break;\r | |
836 | }\r | |
837 | ++Count;\r | |
838 | }\r | |
839 | else {\r | |
840 | Count = 99;\r | |
841 | }\r | |
842 | }\r | |
843 | FreePool(FileInfo); // Free buffer from ShellFindFirstFile()\r | |
844 | if(Count < 3) {\r | |
845 | // Directory is empty\r | |
846 | Status = ShellDeleteFile( (SHELL_FILE_HANDLE *)&FileHandle);\r | |
847 | if(Status == RETURN_SUCCESS) {\r | |
848 | EFIerrno = RETURN_SUCCESS;\r | |
849 | return 0;\r | |
850 | /* ######## SUCCESSFUL RETURN ######## */\r | |
851 | }\r | |
852 | }\r | |
853 | else {\r | |
854 | if(Count == 99) {\r | |
855 | errno = EIO;\r | |
856 | }\r | |
857 | else {\r | |
858 | errno = ENOTEMPTY;\r | |
859 | }\r | |
860 | }\r | |
861 | }\r | |
862 | }\r | |
863 | }\r | |
864 | else {\r | |
865 | errno = EIO;\r | |
866 | }\r | |
867 | }\r | |
868 | EFIerrno = Status;\r | |
869 | if(errno == 0) {\r | |
870 | errno = EFI2errno( Status );\r | |
871 | }\r | |
872 | return -1;\r | |
873 | }\r | |
874 | \r | |
875 | /* Internal File Info. worker function for stat and fstat. */\r | |
876 | static\r | |
877 | EFI_STATUS\r | |
878 | _EFI_FileInfo( EFI_FILE_INFO *FileInfo, struct stat *statbuf)\r | |
879 | {\r | |
880 | UINT64 Attributes;\r | |
881 | RETURN_STATUS Status;\r | |
882 | mode_t newmode;\r | |
883 | \r | |
884 | if(FileInfo != NULL) {\r | |
885 | // Got the info, now populate statbuf with it\r | |
886 | statbuf->st_blksize = S_BLKSIZE;\r | |
887 | statbuf->st_size = FileInfo->Size;\r | |
888 | statbuf->st_physsize = FileInfo->PhysicalSize;\r | |
889 | statbuf->st_birthtime = Efi2Time( &FileInfo->CreateTime);\r | |
890 | statbuf->st_atime = Efi2Time( &FileInfo->LastAccessTime);\r | |
891 | statbuf->st_mtime = Efi2Time( &FileInfo->ModificationTime);\r | |
892 | Attributes = FileInfo->Attribute;\r | |
893 | newmode = (mode_t)(Attributes << S_EFISHIFT) | S_ACC_READ;\r | |
894 | if((Attributes & EFI_FILE_DIRECTORY) == 0) {\r | |
895 | newmode |= _S_IFREG;\r | |
896 | if((Attributes & EFI_FILE_READ_ONLY) == 0) {\r | |
897 | statbuf->st_mode |= S_ACC_WRITE;\r | |
898 | }\r | |
899 | }\r | |
900 | else {\r | |
901 | newmode |= _S_IFDIR;\r | |
902 | }\r | |
903 | statbuf->st_mode = newmode;\r | |
904 | Status = RETURN_SUCCESS;\r | |
905 | }\r | |
906 | else {\r | |
907 | Status = RETURN_DEVICE_ERROR;\r | |
908 | }\r | |
909 | return Status;\r | |
910 | }\r | |
911 | \r | |
912 | /** The fstat() function obtains information about an open file associated\r | |
913 | with the file descriptor fildes, and shall write it to the area pointed to\r | |
914 | by buf.\r | |
915 | \r | |
916 | The buf argument is a pointer to a stat structure, as defined\r | |
917 | in <sys/stat.h>, into which information is placed concerning the file.\r | |
918 | \r | |
919 | The structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atime,\r | |
920 | st_ctime, and st_mtime shall have meaningful values. The value of the\r | |
921 | member st_nlink shall be set to the number of links to the file.\r | |
922 | \r | |
923 | The fstat() function shall update any time-related fields before writing\r | |
924 | into the stat structure.\r | |
925 | \r | |
926 | The fstat() function is implemented using the ShellGetFileInfo()\r | |
927 | function.\r | |
928 | \r | |
929 | The stat structure members which don't have direct analogs to EFI file\r | |
930 | information are filled in as follows:\r | |
931 | - st_mode Populated with information from fildes\r | |
932 | - st_ino Set to zero. (inode)\r | |
933 | - st_dev Set to zero.\r | |
934 | - st_uid Set to zero.\r | |
935 | - st_gid Set to zero.\r | |
936 | - st_nlink Set to one.\r | |
937 | \r | |
938 | @param[in] fildes File descriptor as returned from open().\r | |
939 | @param[out] statbuf Buffer in which the file status is put.\r | |
940 | \r | |
941 | @retval 0 Successful Completion.\r | |
942 | @retval -1 An error has occurred and errno has been set to\r | |
943 | identify the error.\r | |
944 | **/\r | |
945 | int\r | |
946 | fstat (int fildes, struct stat *statbuf)\r | |
947 | {\r | |
948 | EFI_FILE_HANDLE FileHandle;\r | |
949 | RETURN_STATUS Status = RETURN_SUCCESS;\r | |
950 | EFI_FILE_INFO *FileInfo = NULL;\r | |
951 | UINTN FinfoSize = sizeof(EFI_FILE_INFO);\r | |
952 | \r | |
953 | if(ValidateFD( fildes, VALID_OPEN)) {\r | |
954 | FileHandle = gMD->fdarray[fildes].FileHandle;\r | |
955 | if(isatty(fildes)) {\r | |
956 | FileInfo = AllocateZeroPool(FinfoSize);\r | |
957 | if(FileInfo != NULL) {\r | |
958 | Status = FileHandle->GetInfo( FileHandle, 0, &FinfoSize, FileInfo);\r | |
959 | }\r | |
960 | else {\r | |
961 | Status = RETURN_OUT_OF_RESOURCES;\r | |
962 | }\r | |
963 | }\r | |
964 | else {\r | |
965 | FileInfo = ShellGetFileInfo( FileHandle);\r | |
966 | }\r | |
967 | Status = _EFI_FileInfo( FileInfo, statbuf);\r | |
968 | }\r | |
969 | errno = EFI2errno(Status);\r | |
970 | EFIerrno = Status;\r | |
971 | \r | |
972 | if(FileInfo != NULL) {\r | |
973 | FreePool(FileInfo); // Release the buffer allocated by the GetInfo function\r | |
974 | }\r | |
975 | \r | |
976 | return errno? -1 : 0;\r | |
977 | }\r | |
978 | \r | |
979 | /** Obtains information about the file pointed to by path.\r | |
980 | \r | |
981 | Opens the file pointed to by path, calls _EFI_FileInfo with the file's handle,\r | |
982 | then closes the file.\r | |
983 | \r | |
984 | @retval 0 Successful Completion.\r | |
985 | @retval -1 An error has occurred and errno has been set to\r | |
986 | identify the error.\r | |
987 | **/\r | |
988 | int\r | |
989 | stat (const char *path, void *statbuf)\r | |
990 | {\r | |
991 | EFI_FILE_HANDLE FileHandle;\r | |
992 | RETURN_STATUS Status;\r | |
993 | EFI_FILE_INFO *FileInfo;\r | |
994 | \r | |
995 | errno = 0; // Make it easier to see if we have an error later\r | |
996 | \r | |
997 | // Convert name from MBCS to WCS\r | |
998 | (void)AsciiStrToUnicodeStr( path, gMD->UString);\r | |
999 | NormalizePath( gMD->UString);\r | |
1000 | \r | |
1001 | Status = ShellOpenFileByName( gMD->UString, (SHELL_FILE_HANDLE *)&FileHandle, EFI_FILE_MODE_READ, 0ULL);\r | |
1002 | if(Status == RETURN_SUCCESS) {\r | |
1003 | FileInfo = ShellGetFileInfo( FileHandle);\r | |
1004 | Status = _EFI_FileInfo( FileInfo, (struct stat *)statbuf);\r | |
1005 | (void)ShellCloseFile( (SHELL_FILE_HANDLE *)&FileHandle);\r | |
1006 | }\r | |
1007 | errno = EFI2errno(Status);\r | |
1008 | EFIerrno = Status;\r | |
1009 | \r | |
1010 | return errno? -1 : 0;\r | |
1011 | }\r | |
1012 | \r | |
1013 | /** Same as stat since EFI doesn't have symbolic links. **/\r | |
1014 | int\r | |
1015 | lstat (const char *path, struct stat *statbuf)\r | |
1016 | {\r | |
1017 | return stat(path, statbuf);\r | |
1018 | }\r | |
1019 | \r | |
1020 | /** Read from a file.\r | |
1021 | \r | |
1022 | The read() function shall attempt to read nbyte bytes from the file\r | |
1023 | associated with the open file descriptor, fildes, into the buffer pointed\r | |
1024 | to by buf.\r | |
1025 | \r | |
1026 | Before any action described below is taken, and if nbyte is zero, the\r | |
1027 | read() function may detect and return errors as described below. In the\r | |
1028 | absence of errors, or if error detection is not performed, the read()\r | |
1029 | function shall return zero and have no other results.\r | |
1030 | \r | |
1031 | On files that support seeking (for example, a regular file), the read()\r | |
1032 | shall start at a position in the file given by the file offset associated\r | |
1033 | with fildes. The file offset shall be incremented by the number of bytes\r | |
1034 | actually read.\r | |
1035 | \r | |
1036 | Files that do not support seeking - for example, terminals - always read\r | |
1037 | from the current position. The value of a file offset associated with\r | |
1038 | such a file is undefined.\r | |
1039 | \r | |
1040 | No data transfer shall occur past the current end-of-file. If the\r | |
1041 | starting position is at or after the end-of-file, 0 shall be returned.\r | |
1042 | \r | |
1043 | The read() function reads data previously written to a file. If any\r | |
1044 | portion of a regular file prior to the end-of-file has not been written,\r | |
1045 | read() shall return bytes with value 0. For example, lseek() allows the\r | |
1046 | file offset to be set beyond the end of existing data in the file. If data\r | |
1047 | is later written at this point, subsequent reads in the gap between the\r | |
1048 | previous end of data and the newly written data shall return bytes with\r | |
1049 | value 0 until data is written into the gap.\r | |
1050 | \r | |
1051 | Upon successful completion, where nbyte is greater than 0, read() shall\r | |
1052 | mark for update the st_atime field of the file, and shall return the\r | |
1053 | number of bytes read. This number shall never be greater than nbyte. The\r | |
1054 | value returned may be less than nbyte if the number of bytes left in the\r | |
1055 | file is less than nbyte, if the read() request was interrupted by a\r | |
1056 | signal, or if the file is a pipe or FIFO or special file and has fewer\r | |
1057 | than nbyte bytes immediately available for reading. For example, a read()\r | |
1058 | from a file associated with a terminal may return one typed line of data.\r | |
1059 | \r | |
1060 | If fildes does not refer to a directory, the function reads the requested\r | |
1061 |