Exemplo n.º 1
0
MOZCE_SHUNT_API int open(const char *pathname, int flags, int mode)
{
#ifdef API_LOGGING
        mozce_printf("open called\n");
#endif
    
    _initfds();
    
    
    char modestr[10];
    *modestr = '\0';
    
    mode2binstr(mode, modestr);
    if (*modestr == '\0')
        return -1;
    
    
    FILE* file = fopen(pathname, modestr);
    
    int fd = -1;
    
    if (file)
    {
        fd = _getnewfd();
        
        _fdtab[fd].fd = fd;
        _fdtab[fd].file = file;
        
        fflush(file);
        fread(NULL, 0, 0, file);
    }
    
    return fd;
}
Exemplo n.º 2
0
Arquivo: io.c Projeto: mmcx/cegcc
int
_open_r(struct _reent *reent, const char *path, int flags, int mode)
{
  wchar_t wpath[MAX_PATH];
  char pathbuf[MAX_PATH];
  HANDLE hnd = NULL;
  DWORD fileaccess;
  DWORD fileshare; 
  DWORD filecreate;
  DWORD fileattrib;
  void *cxt;
  int fd;

  WCETRACE(WCE_IO, "open(%s, %x, %o)", path, flags, mode);

  _initfds();

  if (!strncmp("fifo", path, 4)) {
	  fd = _assignfd(IO_FILE_TYPE_FIFO, NULL, 0);

	  if (fd < 0) {
		  errno = ENMFILE;
		  return(-1);
	  }

	  _fdtab[fd].devops = _fifo_devops;
	  _fdtab[fd].cxt = cxt = _fifo_alloc();
	  if ((_fdtab[fd].fd = _fdtab[fd].devops->open_r(reent, path, flags, mode, cxt)) == -1) {
		  WCETRACE(WCE_IO, "FIFO open fails, errno %d", errno);
		  _fdtab[fd].fd = -1;
		  return(-1);
	  }
  }
  else if(!strncmp(path, "nul", 3) ||
		  !strncmp(path, "nul:", 4) ||
		  !strncmp(path, "null:", 5) ||
		  !strncmp(path, "/dev/nul", 8) ||
		  !strncmp(path, "/dev/null", 9))
  {
	  fd = _assignfd(IO_FILE_TYPE_NULL, (HANDLE) -1, 0);
	  if (fd < 0) {
		  errno = ENMFILE;
		  return(-1);
	  }
	  _fdtab[fd].devops = NULL;
	  _fdtab[fd].cxt = NULL;
  } else {
    if (strlen(path) >= MAX_PATH) {
      WCETRACE(WCE_IO, "open fails, invalid path\n");
      return(-1);
    }

    fixpath(path, pathbuf);
    mbstowcs(wpath, pathbuf, strlen(pathbuf) + 1);

    fileshare = FILE_SHARE_READ|FILE_SHARE_WRITE;
    fileattrib = FILE_ATTRIBUTE_NORMAL;

    switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
    case O_RDONLY:              /* read access */
      fileaccess = GENERIC_READ;
      break;
    case O_WRONLY:              /* write access */
      fileaccess = GENERIC_WRITE;
      break;
    case O_RDWR:                /* read and write access */
      fileaccess = GENERIC_READ | GENERIC_WRITE;
      break;
    default:                    /* error, bad flags */
      errno = EINVAL;
      return -1;
    }

    switch (flags & (O_CREAT | O_EXCL | O_TRUNC)) {
    case 0:
    case O_EXCL:                /* ignore EXCL w/o CREAT */
      filecreate = OPEN_EXISTING;
      break;
    case O_CREAT:
      filecreate = OPEN_ALWAYS;
      break;
    case O_CREAT | O_EXCL:
    case O_CREAT | O_TRUNC | O_EXCL:
      filecreate = CREATE_NEW;
      break;

    case O_TRUNC:
    case O_TRUNC | O_EXCL:      /* ignore EXCL w/o CREAT */
      filecreate = TRUNCATE_EXISTING;
      break;
    case O_CREAT | O_TRUNC:
      filecreate = CREATE_ALWAYS;
      break;
    default:
      /* this can't happen ... all cases are covered */
      errno = EINVAL;
      return(-1);
    }

    if ((hnd = _CreateFileW(wpath, fileaccess, fileshare, NULL, filecreate,
                           fileattrib, NULL)) == INVALID_HANDLE_VALUE) {
      errno = _winerr2errno(GetLastError());
      WCETRACE(WCE_IO, "_CreateFile(%s): errno=%d oserr=%d\n", pathbuf, errno, GetLastError());
      return(-1);
    }

    fd = _assignfd(IO_FILE_TYPE_FILE, hnd, 0);

    if (fd < 0) {
      errno = ENMFILE;
      return(-1);
    }
    _fdtab[fd].devops = NULL;
    _fdtab[fd].cxt = NULL;

    if (flags & O_APPEND) {
      _SetFilePointer(hnd, 0, NULL, FILE_END);
    }
  }

  WCETRACE(WCE_IO, "open returns %d fd %d cxt %p (hnd %x)", fd, _fdtab[fd].fd, cxt, hnd);
  return fd;
}