VirtualBox

source: kBuild/trunk/src/kash/shfile.c@ 2290

Last change on this file since 2290 was 2290, checked in by bird, 16 years ago

kash: malloc/free/friends gets a psh.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 28.4 KB
Line 
1/* $Id: shfile.c 2290 2009-02-27 04:08:07Z bird $ */
2/** @file
3 *
4 * File management.
5 *
6 * Copyright (c) 2007-2009 knut st. osmundsen <[email protected]>
7 *
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include "shfile.h"
28#include "shinstance.h" /* TRACE2 */
29#include <stdlib.h>
30#include <stdio.h>
31#include <assert.h>
32
33#if K_OS == K_OS_WINDOWS
34# include <limits.h>
35# ifndef PIPE_BUF
36# define PIPE_BUF 512
37# endif
38# include <Windows.h>
39#else
40# include <unistd.h>
41# include <fcntl.h>
42# include <dirent.h>
43#endif
44
45#ifdef DEBUG
46extern FILE *tracefile;
47#endif
48
49
50/*******************************************************************************
51* Defined Constants And Macros *
52*******************************************************************************/
53/** @def SHFILE_IN_USE
54 * Whether the file descriptor table stuff is actually in use or not.
55 */
56#if !defined(SH_PURE_STUB_MODE) \
57 && ( K_OS == K_OS_WINDOWS \
58 || ( !defined(SH_STUB_MODE) \
59 && !defined(SH_FORKED_MODE)) \
60 )
61# define SHFILE_IN_USE
62#endif
63/** The max file table size. */
64#define SHFILE_MAX 1024
65/** The file table growth rate. */
66#define SHFILE_GROW 64
67/** The min native unix file descriptor. */
68#define SHFILE_UNIX_MIN_FD 32
69/** The path buffer size we use. */
70#define SHFILE_MAX_PATH 4096
71
72/** Set errno and return. Doing a trace in debug build. */
73#define RETURN_ERROR(rc, err, msg) \
74 do { \
75 TRACE2((NULL, "%s: " ## msg ## " - returning %d / %d\n", __FUNCTION__, (rc), (err))); \
76 errno = (err); \
77 return (rc); \
78 } while (0)
79
80
81#ifdef SHFILE_IN_USE
82
83/**
84 * Close the specified native handle.
85 *
86 * @param native The native file handle.
87 * @param flags The flags in case they might come in handy later.
88 */
89static void shfile_native_close(intptr_t native, unsigned flags)
90{
91#if K_OS == K_OS_WINDOWS
92 BOOL fRc = CloseHandle((HANDLE)native);
93 assert(fRc); (void)fRc;
94#else
95 int s = errno;
96 close(native);
97 errno = s;
98#endif
99 (void)flags;
100}
101
102/**
103 * Inserts the file into the descriptor table.
104 *
105 * If we're out of memory and cannot extend the table, we'll close the
106 * file, set errno to EMFILE and return -1.
107 *
108 * @returns The file descriptor number. -1 and errno on failure.
109 * @param pfdtab The file descriptor table.
110 * @param native The native file handle.
111 * @param flags The flags the it was created with.
112 * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
113 * @param who Who we're doing this for (for logging purposes).
114 */
115static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned flags, int fdMin, const char *who)
116{
117 shmtxtmp tmp;
118 int fd;
119 int i;
120
121 /*
122 * Fend of bad stuff.
123 */
124 if (fdMin >= SHFILE_MAX)
125 {
126 errno = EMFILE;
127 return -1;
128 }
129
130 shmtx_enter(&pfdtab->mtx, &tmp);
131
132 /*
133 * Search for a fitting unused location.
134 */
135 fd = -1;
136 for (i = 0; (unsigned)i < pfdtab->size; i++)
137 if ( i >= fdMin
138 && pfdtab->tab[i].fd == -1)
139 {
140 fd = i;
141 break;
142 }
143 if (fd == -1)
144 {
145 /*
146 * Grow the descriptor table.
147 */
148 shfile *new_tab;
149 int new_size = pfdtab->size + SHFILE_GROW;
150 while (new_size < fdMin)
151 new_size += SHFILE_GROW;
152 new_tab = sh_realloc(NULL, pfdtab->tab, new_size * sizeof(shfile));
153 if (new_tab)
154 {
155 fd = i = pfdtab->size;
156 if (fd < fdMin)
157 fd = fdMin;
158 for (i = pfdtab->size; i < new_size; i++)
159 {
160 new_tab[i].fd = -1;
161 new_tab[i].flags = 0;
162 new_tab[i].native = -1;
163 }
164
165 pfdtab->tab = new_tab;
166 pfdtab->size = new_size;
167 }
168 }
169
170 /*
171 * Fill in the entry if we've found one.
172 */
173 if (fd != -1)
174 {
175 pfdtab->tab[fd].fd = fd;
176 pfdtab->tab[fd].flags = flags;
177 pfdtab->tab[fd].cloexec = 0;
178 pfdtab->tab[fd].native = native;
179 }
180 else
181 shfile_native_close(native, flags);
182
183 shmtx_leave(&pfdtab->mtx, &tmp);
184
185 if (fd == -1)
186 errno = EMFILE;
187 (void)who;
188 return fd;
189}
190
191/**
192 * Gets a file descriptor and lock the file descriptor table.
193 *
194 * @returns Pointer to the file and table ownership on success,
195 * NULL and errno set to EBADF on failure.
196 * @param pfdtab The file descriptor table.
197 * @param fd The file descriptor number.
198 * @param ptmp See shmtx_enter.
199 */
200static shfile *shfile_get(shfdtab *pfdtab, int fd, shmtxtmp *ptmp)
201{
202 shfile *file = NULL;
203 if ( fd >= 0
204 && (unsigned)fd < pfdtab->size)
205 {
206 shmtx_enter(&pfdtab->mtx, ptmp);
207 if ((unsigned)fd < pfdtab->size
208 && pfdtab->tab[fd].fd != -1)
209 file = &pfdtab->tab[fd];
210 else
211 shmtx_leave(&pfdtab->mtx, ptmp);
212 }
213 if (!file)
214 errno = EBADF;
215 return file;
216}
217
218/**
219 * Puts back a file descriptor and releases the table ownership.
220 *
221 * @param pfdtab The file descriptor table.
222 * @param file The file.
223 * @param ptmp See shmtx_leave.
224 */
225static void shfile_put(shfdtab *pfdtab, shfile *file, shmtxtmp *ptmp)
226{
227 shmtx_leave(&pfdtab->mtx, ptmp);
228 assert(file);
229 (void)file;
230}
231
232/**
233 * Constructs a path from the current directory and the passed in path.
234 *
235 * @returns 0 on success, -1 and errno set to ENAMETOOLONG or EINVAL on failure.
236 *
237 * @param pfdtab The file descriptor table
238 * @param path The path the caller supplied.
239 * @param buf Where to put the path. This is assumed to be SHFILE_MAX_PATH
240 * chars in size.
241 */
242int shfile_make_path(shfdtab *pfdtab, const char *path, char *buf)
243{
244 size_t path_len = strlen(path);
245 if (path_len == 0)
246 {
247 errno = EINVAL;
248 return -1;
249 }
250 if (path_len >= SHFILE_MAX_PATH)
251 {
252 errno = ENAMETOOLONG;
253 return -1;
254 }
255 if ( *path == '/'
256#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
257 || *path == '\\'
258 || ( *path
259 && path[1] == ':'
260 && ( (*path >= 'A' && *path <= 'Z')
261 || (*path >= 'a' && *path <= 'z')))
262#endif
263 )
264 {
265 memcpy(buf, path, path_len + 1);
266 }
267 else
268 {
269 size_t cwd_len;
270 shmtxtmp tmp;
271
272 shmtx_enter(&pfdtab->mtx, &tmp);
273
274 cwd_len = strlen(pfdtab->cwd);
275 memcpy(buf, pfdtab->cwd, cwd_len);
276
277 shmtx_leave(&pfdtab->mtx, &tmp);
278
279 if (cwd_len + path_len + 1 >= SHFILE_MAX_PATH)
280 {
281 errno = ENAMETOOLONG;
282 return -1;
283 }
284 if ( !cwd_len
285 || buf[cwd_len - 1] != '/')
286 buf[cwd_len++] = '/';
287 memcpy(buf + cwd_len, path, path_len + 1);
288 }
289
290#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
291 if (!strcmp(buf, "/dev/null"))
292 strcpy(buf, "NUL");
293#endif
294 return 0;
295}
296
297# if K_OS == K_OS_WINDOWS
298/**
299 * Converts a DOS(/Windows) error code to errno,
300 * assigning it to errno.
301 *
302 * @returns -1
303 * @param err The DOS error.
304 */
305static int shfile_dos2errno(int err)
306{
307 switch (err)
308 {
309 case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
310 case ERROR_ACCESS_DENIED: errno = EACCES; break;
311 case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
312 case ERROR_LOCK_VIOLATION: errno = EACCES; break;
313 case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
314 case ERROR_CANNOT_MAKE: errno = EACCES; break;
315 case ERROR_FAIL_I24: errno = EACCES; break;
316 case ERROR_DRIVE_LOCKED: errno = EACCES; break;
317 case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
318 case ERROR_NOT_LOCKED: errno = EACCES; break;
319 case ERROR_LOCK_FAILED: errno = EACCES; break;
320 case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
321 case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
322 case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
323 case ERROR_INVALID_HANDLE: errno = EBADF; break;
324 case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
325 case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
326 case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
327 case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
328 case ERROR_FILE_EXISTS: errno = EEXIST; break;
329 case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
330 case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
331 case ERROR_INVALID_ACCESS: errno = EINVAL; break;
332 case ERROR_INVALID_DATA: errno = EINVAL; break;
333 case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
334 case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
335 case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
336 case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
337 case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
338 case ERROR_INVALID_DRIVE: errno = ENOENT; break;
339 case ERROR_NO_MORE_FILES: errno = ENOENT; break;
340 case ERROR_BAD_NETPATH: errno = ENOENT; break;
341 case ERROR_BAD_NET_NAME: errno = ENOENT; break;
342 case ERROR_BAD_PATHNAME: errno = ENOENT; break;
343 case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
344 case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
345 case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
346 case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
347 case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
348 case ERROR_NOT_ENOUGH_QUOTA: errno = ENOMEM; break;
349 case ERROR_DISK_FULL: errno = ENOSPC; break;
350 case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
351 case ERROR_BROKEN_PIPE: errno = EPIPE; break;
352 case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
353 default: errno = EINVAL; break;
354 }
355 return -1;
356}
357# endif /* K_OS == K_OS_WINDOWS */
358
359#endif /* SHFILE_IN_USE */
360
361/**
362 * Initializes a file descriptor table.
363 *
364 * @returns 0 on success, -1 and errno on failure.
365 * @param pfdtab The table to initialize.
366 * @param inherit File descriptor table to inherit from. If not specified
367 * we will inherit from the current process as it were.
368 */
369int shfile_init(shfdtab *pfdtab, shfdtab *inherit)
370{
371 int rc;
372
373 pfdtab->cwd = NULL;
374 pfdtab->size = 0;
375 pfdtab->tab = NULL;
376 rc = shmtx_init(&pfdtab->mtx);
377 if (!rc)
378 {
379#ifdef SHFILE_IN_USE
380 char buf[SHFILE_MAX_PATH];
381 if (getcwd(buf, sizeof(buf)))
382 {
383 pfdtab->cwd = strdup(buf);
384 if (!inherit)
385 {
386# if K_OS == K_OS_WINDOWS
387 static const struct
388 {
389 DWORD dwStdHandle;
390 unsigned flags;
391 } aStdHandles[3] =
392 {
393 { STD_INPUT_HANDLE, _O_RDONLY },
394 { STD_OUTPUT_HANDLE, _O_WRONLY },
395 { STD_ERROR_HANDLE, _O_WRONLY }
396 };
397 unsigned i;
398 STARTUPINFO Info;
399
400 rc = 0;
401
402 /* Try pick up the Visual C++ CRT file descriptor info. */
403 __try {
404 GetStartupInfo(&Info);
405 } __except (EXCEPTION_EXECUTE_HANDLER) {
406 memset(&Info, 0, sizeof(Info));
407 }
408 if ( Info.cbReserved2 <= sizeof(int)
409 && (uintptr_t)Info.lpReserved2 >= 0x1000
410 && *(int *)Info.lpReserved2 * (sizeof(int) + sizeof(intptr_t)) + 4 <= Info.cbReserved2
411 && *(int *)Info.lpReserved2 <= 2048
412 && *(int *)Info.lpReserved2 >= 1)
413 {
414 unsigned c = *(int *)Info.lpReserved2;
415 char *aosfile = (char *)Info.lpReserved2 + sizeof(int);
416 intptr_t *aosfhnd = (intptr_t *)(aosfile + c);
417
418 /** @todo process */
419
420 }
421
422 /* Check the three standard handles. */
423 for (i = 0; i < 3; i++)
424 {
425 HANDLE hFile = GetStdHandle(aStdHandles[i].dwStdHandle);
426 if ( hFile != INVALID_HANDLE_VALUE
427 && ( (unsigned)i >= pfdtab->size
428 || pfdtab->tab[i].fd == -1))
429 {
430 int fd2 = shfile_insert(pfdtab, (intptr_t)hFile, aStdHandles[i].flags, i, "shtab_init");
431 assert(fd2 == i); (void)fd2;
432 if (fd2 != i)
433 rc = -1;
434 }
435 }
436# else
437# endif
438 }
439 else
440 {
441 /** @todo */
442 errno = ENOSYS;
443 rc = -1;
444 }
445 }
446 else
447 rc = -1;
448#endif
449 }
450 return rc;
451}
452
453/**
454 * open().
455 */
456int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
457{
458 int fd;
459#ifdef SHFILE_IN_USE
460 char absname[SHFILE_MAX_PATH];
461# if K_OS == K_OS_WINDOWS
462 HANDLE hFile;
463 DWORD dwDesiredAccess;
464 DWORD dwShareMode;
465 DWORD dwCreationDisposition;
466 DWORD dwFlagsAndAttributes;
467 SECURITY_ATTRIBUTES SecurityAttributes;
468
469# ifndef _O_ACCMODE
470# define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
471# endif
472 switch (flags & (_O_ACCMODE | _O_APPEND))
473 {
474 case _O_RDONLY: dwDesiredAccess = GENERIC_READ; break;
475 case _O_RDONLY | _O_APPEND: dwDesiredAccess = GENERIC_READ; break;
476 case _O_WRONLY: dwDesiredAccess = GENERIC_WRITE; break;
477 case _O_WRONLY | _O_APPEND: dwDesiredAccess = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
478 case _O_RDWR: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; break;
479 case _O_RDWR | _O_APPEND: dwDesiredAccess = GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
480
481 default:
482 RETURN_ERROR(-1, EINVAL, "invalid mode");
483 }
484
485 dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
486
487 SecurityAttributes.nLength = sizeof(SecurityAttributes);
488 SecurityAttributes.lpSecurityDescriptor = NULL;
489 SecurityAttributes.bInheritHandle = (flags & _O_NOINHERIT) == 0;
490
491 if (flags & _O_CREAT)
492 {
493 if ((flags & (_O_EXCL | _O_TRUNC)) == (_O_EXCL | _O_TRUNC))
494 RETURN_ERROR(-1, EINVAL, "_O_EXCL | _O_TRUNC");
495
496 if (flags & _O_TRUNC)
497 dwCreationDisposition = CREATE_ALWAYS; /* not 100%, but close enough */
498 else if (flags & _O_EXCL)
499 dwCreationDisposition = CREATE_NEW;
500 else
501 dwCreationDisposition = OPEN_ALWAYS;
502 }
503 else if (flags & _O_TRUNC)
504 dwCreationDisposition = TRUNCATE_EXISTING;
505 else
506 dwCreationDisposition = OPEN_EXISTING;
507
508 if (!(flags & _O_CREAT) || (mode & 0222))
509 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
510 else
511 dwFlagsAndAttributes = FILE_ATTRIBUTE_READONLY;
512
513 fd = shfile_make_path(pfdtab, name, &absname[0]);
514 if (!fd)
515 {
516 SetLastError(0);
517 hFile = CreateFileA(absname,
518 dwDesiredAccess,
519 dwShareMode,
520 &SecurityAttributes,
521 dwCreationDisposition,
522 dwFlagsAndAttributes,
523 NULL /* hTemplateFile */);
524 if (hFile != INVALID_HANDLE_VALUE)
525 fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, -1, "shfile_open");
526 else
527 fd = shfile_dos2errno(GetLastError());
528 }
529
530# else /* K_OS != K_OS_WINDOWS */
531 fd = shfile_make_path(pfdtab, name, &absname[0]);
532 if (!fd)
533 {
534 fd = open(absname, flag, mode);
535 if (fd != -1)
536 {
537 int native = fcntl(fd, F_DUPFD, SHFILE_UNIX_MIN_FD);
538 int s = errno;
539 close(fd);
540 errno = s;
541 if (native != -1)
542 fd = shfile_insert(pfdtab, native, flags, -1, "shfile_open");
543 else
544 fd = -1;
545 }
546 }
547
548# endif /* K_OS != K_OS_WINDOWS */
549
550#elif defined(SH_PURE_STUB_MODE)
551 fd = -1;
552 errno = ENOSYS;
553
554#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
555 fd = open(name, flags, mode);
556#endif
557
558#ifdef DEBUG
559 if (tracefile)
560 TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
561#endif
562 return fd;
563}
564
565int shfile_pipe(shfdtab *pfdtab, int fds[2])
566{
567#ifdef SH_PURE_STUB_MODE
568 return -1;
569
570#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
571# ifdef _MSC_VER
572 return _pipe(fds, PIPE_BUF, O_BINARY);
573# else
574 return pipe(fds);
575# endif
576
577#else
578#endif
579}
580
581int shfile_dup(shfdtab *pfdtab, int fd)
582{
583 int rc;
584#ifdef SH_PURE_STUB_MODE
585 rc = -1;
586
587#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
588 rc = dup(fd);
589
590#else
591#endif
592
593 TRACE2((NULL, "shfile_dup(%d) -> %d [%d]\n", fd, rc, errno));
594 return rc;
595}
596
597/**
598 * close().
599 */
600int shfile_close(shfdtab *pfdtab, unsigned fd)
601{
602 int rc;
603#ifdef SHFILE_IN_USE
604 shmtxtmp tmp;
605 shfile *file = shfile_get(pfdtab, fd, &tmp);
606 if (file)
607 {
608 shfile_native_close(file->native, file->flags);
609
610 file->fd = -1;
611 file->flags = 0;
612 file->native = -1;
613
614 shfile_put(pfdtab, file, &tmp);
615 rc = 0;
616 }
617 else
618 rc = -1;
619
620#elif defined(SH_PURE_STUB_MODE)
621 rc = -1;
622
623#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
624 rc = close(fd);
625#endif
626
627 TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
628 return rc;
629}
630
631/**
632 * read().
633 */
634long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
635{
636 long rc;
637#ifdef SHFILE_IN_USE
638 shmtxtmp tmp;
639 shfile *file = shfile_get(pfdtab, fd, &tmp);
640 if (file)
641 {
642# if K_OS == K_OS_WINDOWS
643 DWORD dwRead = 0;
644 if (ReadFile((HANDLE)file->native, buf, (DWORD)len, &dwRead, NULL))
645 rc = dwRead;
646 else
647 rc = shfile_dos2errno(GetLastError());
648# else
649 rc = read(file->native, buf, len);
650# endif
651
652 shfile_put(pfdtab, file, &tmp);
653 }
654 else
655 rc = -1;
656
657#elif defined(SH_PURE_STUB_MODE)
658 rc = -1;
659
660#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
661# ifdef _MSC_VER
662 rc = read(fd, buf, (unsigned)len);
663# else
664 rc = read(fd, buf, len);
665# endif
666#endif
667 return rc;
668}
669
670/**
671 * write().
672 */
673long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
674{
675 long rc;
676#ifdef SHFILE_IN_USE
677 shmtxtmp tmp;
678 shfile *file = shfile_get(pfdtab, fd, &tmp);
679 if (file)
680 {
681# if K_OS == K_OS_WINDOWS
682 DWORD dwWritten = 0;
683 if (WriteFile((HANDLE)file->native, buf, (DWORD)len, &dwWritten, NULL))
684 rc = dwWritten;
685 else
686 rc = shfile_dos2errno(GetLastError());
687# else
688 rc = write(file->native, buf, len);
689# endif
690
691 shfile_put(pfdtab, file, &tmp);
692 }
693 else
694 rc = -1;
695
696#elif defined(SH_PURE_STUB_MODE)
697 rc = -1;
698
699#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
700# ifdef _MSC_VER
701 rc = write(fd, buf, (unsigned)len);
702# else
703 rc = write(fd, buf, len);
704# endif
705#endif
706 return rc;
707}
708
709/**
710 * lseek().
711 */
712long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
713{
714 long rc;
715#ifdef SHFILE_IN_USE
716 shmtxtmp tmp;
717 shfile *file = shfile_get(pfdtab, fd, &tmp);
718 if (file)
719 {
720# if K_OS == K_OS_WINDOWS
721 assert(SEEK_SET == FILE_BEGIN);
722 assert(SEEK_CUR == FILE_CURRENT);
723 assert(SEEK_END == FILE_END);
724 rc = SetFilePointer((HANDLE)file->native, off, NULL, whench);
725 if (rc == INVALID_SET_FILE_POINTER)
726 rc = shfile_dos2errno(GetLastError());
727# else
728 rc = lseek(file->native, off, whench);
729# endif
730
731 shfile_put(pfdtab, file, &tmp);
732 }
733 else
734 rc = -1;
735
736#elif defined(SH_PURE_STUB_MODE)
737 rc = -1;
738
739#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
740 rc = lseek(fd, off, whench);
741#endif
742
743 return rc;
744}
745
746int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
747{
748 int rc;
749#ifdef SHFILE_IN_USE
750 shmtxtmp tmp;
751 shfile *file = shfile_get(pfdtab, fd, &tmp);
752 if (file)
753 {
754 switch (cmd)
755 {
756 case F_GETFL:
757 rc = file->flags;
758 break;
759
760 case F_SETFL:
761 {
762 unsigned mask = O_NONBLOCK | O_APPEND | O_BINARY | O_TEXT;
763# ifdef O_DIRECT
764 mask |= O_DIRECT;
765# endif
766# ifdef O_ASYNC
767 mask |= O_ASYNC;
768# endif
769# ifdef O_SYNC
770 mask |= O_SYNC;
771# endif
772 if ((file->flags & mask) == (arg & mask))
773 rc = 0;
774 else
775 {
776# if K_OS == K_OS_WINDOWS
777 assert(0);
778 errno = EINVAL;
779 rc = -1;
780# else
781 rc = fcntl(file->native, F_SETFL, arg);
782 if (rc != -1)
783 file->flags = (file->flags & ~mask) | (arg & mask);
784# endif
785 }
786 break;
787 }
788
789 case F_DUPFD:
790 {
791# if K_OS == K_OS_WINDOWS
792 HANDLE hNew = INVALID_HANDLE_VALUE;
793 if (DuplicateHandle(GetCurrentProcess(),
794 (HANDLE)file->native,
795 GetCurrentProcess(),
796 &hNew,
797 0,
798 TRUE /* bInheritHandle */,
799 DUPLICATE_SAME_ACCESS))
800 rc = shfile_insert(pfdtab, (intptr_t)hNew, file->flags, arg, "shfile_fcntl");
801 else
802 rc = shfile_dos2errno(GetLastError());
803# else
804 int nativeNew = fcntl(file->native F_DUPFD, SHFILE_UNIX_MIN_FD);
805 if (nativeNew != -1)
806 rc = shfile_insert(pfdtab, nativeNew, file->flags, arg, "shfile_fcntl");
807# endif
808 break;
809 }
810
811 default:
812 errno = -EINVAL;
813 rc = -1;
814 break;
815 }
816
817 shfile_put(pfdtab, file, &tmp);
818 }
819 else
820 rc = -1;
821
822#elif defined(SH_PURE_STUB_MODE)
823 rc = -1;
824
825#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
826# ifdef _MSC_VER
827 switch (cmd)
828 {
829 /* Just enough F_GETFL/F_SETFL to get along with. */
830 case F_GETFL:
831 errno = 0;
832 rc = _isatty(fd);
833 if (errno == EBADF)
834 rc = -1;
835 break;
836
837 case F_SETFL:
838 errno = 0;
839 rc = _isatty(fd);
840 if (errno != EBADF)
841 {
842 if (!arg)
843 rc = 0;
844 else
845 {
846 errno = EINVAL;
847 rc = -1;
848 }
849 }
850 else
851 rc = -1;
852 break;
853
854 case F_DUPFD:
855 {
856 /* the brute force approach. */
857 int i = 0;
858 int fds[256];
859 for (i = 0; i < 256; i++)
860 {
861 fds[i] = -1;
862 rc = _dup(fd);
863 if (rc >= arg)
864 break;
865 fds[i] = rc;
866 }
867 while (i-- > 0)
868 close(fds[i]);
869 if (rc < arg)
870 {
871 errno = EMFILE;
872 rc = -1;
873 }
874 break;
875 }
876 }
877# else
878 rc = fcntl(fd, cmd, arg);
879# endif
880#endif
881
882#ifdef DEBUG
883 if (tracefile)
884 switch (cmd)
885 {
886 case F_GETFL:
887 TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno));
888 break;
889 case F_SETFL:
890 TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno));
891 break;
892 case F_DUPFD:
893 TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno));
894 break;
895 default:
896 TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno));
897 break;
898 }
899#endif
900 return rc;
901}
902
903int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
904{
905#ifdef SH_PURE_STUB_MODE
906 return -1;
907
908#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
909 return stat(path, pst);
910
911#else
912#endif
913}
914
915int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
916{
917#ifdef SH_PURE_STUB_MODE
918 return -1;
919
920#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
921# ifdef _MSC_VER
922 return stat(link, pst);
923# else
924 return lstat(link, pst);
925# endif
926
927#else
928#endif
929}
930
931int shfile_chdir(shfdtab *pfdtab, const char *path)
932{
933#ifdef SH_PURE_STUB_MODE
934 return -1;
935
936#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
937# ifdef _MSC_VER //???
938 return chdir(path);
939# else
940 return chdir(path);
941# endif
942
943#else
944#endif
945}
946
947char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
948{
949#ifdef SH_PURE_STUB_MODE
950 return NULL;
951
952#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
953 return getcwd(buf, len);
954
955#else
956#endif
957}
958
959int shfile_access(shfdtab *pfdtab, const char *path, int type)
960{
961#ifdef SH_PURE_STUB_MODE
962 return -1;
963
964#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
965# ifdef _MSC_VER
966 type &= ~X_OK;
967 return access(path, type);
968# else
969 return access(path, type);
970# endif
971
972#else
973#endif
974}
975
976int shfile_isatty(shfdtab *pfdtab, int fd)
977{
978 int rc;
979
980#ifdef SH_PURE_STUB_MODE
981 rc = 0;
982#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
983 rc = isatty(fd);
984#else
985#endif
986
987 TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
988 return rc;
989}
990
991
992int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
993{
994 int rc;
995
996#ifdef SH_PURE_STUB_MODE
997 rc = -1;
998
999#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1000# ifdef _MSC_VER
1001 errno = ENOSYS;
1002 rc = -1;
1003# else
1004 rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
1005 | (closeit ? FD_CLOEXEC : 0));
1006# endif
1007
1008#else
1009#endif
1010
1011 TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
1012 return rc;
1013}
1014
1015
1016int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
1017{
1018 int rc;
1019
1020#ifdef SH_PURE_STUB_MODE
1021 rc = -1;
1022
1023#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1024# ifdef _MSC_VER
1025 errno = ENOSYS;
1026 rc = -1;
1027# else
1028 rc = ioctl(fd, request, buf);
1029# endif
1030
1031#else
1032#endif
1033
1034 TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
1035 return rc;
1036}
1037
1038
1039mode_t shfile_get_umask(shfdtab *pfdtab)
1040{
1041#ifdef SH_PURE_STUB_MODE
1042 return 022;
1043
1044#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1045 return 022;
1046
1047#else
1048#endif
1049}
1050
1051void shfile_set_umask(shfdtab *pfdtab, mode_t mask)
1052{
1053 (void)mask;
1054}
1055
1056
1057shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
1058{
1059#ifdef SH_PURE_STUB_MODE
1060 return NULL;
1061
1062#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1063# ifdef _MSC_VER
1064 errno = ENOSYS;
1065 return NULL;
1066# else
1067 return (shdir *)opendir(dir);
1068# endif
1069
1070#else
1071#endif
1072}
1073
1074shdirent *shfile_readdir(struct shdir *pdir)
1075{
1076#ifdef SH_PURE_STUB_MODE
1077 return NULL;
1078
1079#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1080# ifdef _MSC_VER
1081 errno = ENOSYS;
1082 return NULL;
1083# else
1084 struct dirent *pde = readdir((DIR *)pdir);
1085 return pde ? (shdirent *)&pde->d_name[0] : NULL;
1086# endif
1087
1088#else
1089#endif
1090}
1091
1092void shfile_closedir(struct shdir *pdir)
1093{
1094#ifdef SH_PURE_STUB_MODE
1095 return NULL;
1096
1097#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1098# ifdef _MSC_VER
1099 errno = ENOSYS;
1100# else
1101 closedir((DIR *)pdir);
1102# endif
1103
1104#else
1105#endif
1106}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette