Ejemplo n.º 1
0
ssize_t read(int d,void *buf,size_t nbytes)
{ StdFileDes *sfd = _lx_fhfromfd(d);

  if (sfd) {
    long r;
    __chkabort();
    if((r=Read(sfd->lx_fh,buf,nbytes))!=EOF)
      return r;
    __seterrno();
  }

  return EOF;
}
Ejemplo n.º 2
0
off_t lseek(int d,off_t offset,int whence)
{ StdFileDes *sfd = _lx_fhfromfd(d);

  if (sfd) {
    long r,file=sfd->lx_fh;
    __chkabort();
    if (Seek(file,offset,whence==SEEK_SET?OFFSET_BEGINNING:
                         whence==SEEK_END?OFFSET_END:OFFSET_CURRENT)!=EOF)
      if ((r=Seek(file,0,OFFSET_CURRENT))!=EOF)
        return r;
    __seterrno();
  }

  return EOF;
}
Ejemplo n.º 3
0
ssize_t write(int d,const void *buf,size_t nbytes)
{ StdFileDes *sfd = _lx_fhfromfd(d);

  if (sfd) {
    long r;
    __chkabort();
    switch((sfd->lx_oflags&O_APPEND)!=0) {
      case 1:
        if(!sfd->lx_isatty&&(Seek(sfd->lx_fh,0,OFFSET_END)==EOF))
          break;
      default:
        if((r=Write(sfd->lx_fh,(char *)buf,nbytes))!=EOF)
          return r;
    }
    __seterrno();
  }

  return EOF;
}
Ejemplo n.º 4
0
__stdargs int
__open(const char *name, int mode, ...)
{
  struct UFB *ufb;
  int         fd;
  int         flags;
  char        newfile = TRUE;
  BPTR        file;

  /*
   * Set up __closefunc (which is used at cleanup)
   */
  __closefunc = __close;

  /*
   * Check for the break signals
   */
  __chkabort();

  /*
   * find first free ufb
   */
  ufb = __allocufb(&fd);
  if (ufb == NULL)
    return -1; /* errno is set by the __allocufb() */

  /*
   * malloc space for the name & copy it
   */
  if ((ufb->ufbfn = malloc(strlen(name)+1)) == NULL) {
    SET_OSERR(ERROR_NO_FREE_STORE);
    errno = ENOMEM;
    return -1;
  }
  strcpy(ufb->ufbfn, name);
  /*
   * Translate mode to ufb flags
   */
  switch (mode & (O_WRONLY | O_RDWR)) {
  case O_RDONLY:
    if (mode & (O_APPEND | O_CREAT | O_TRUNC | O_EXCL)) {
      errno = EINVAL;
      return -1;
    }
    flags = UFB_RA;
    break;
  case O_WRONLY:
    flags = UFB_WA;
    break;
  case O_RDWR:
    flags = UFB_RA | UFB_WA;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  if (mode & O_APPEND)
    flags |= UFB_APP;
  if (mode & O_XLATE)
    flags |= UFB_XLAT;
  if (mode & O_TEMP)
    flags |= UFB_TEMP;
  if (mode & O_CREAT) {
    BPTR lock;
    if (lock = Lock((char *)name, SHARED_LOCK)) {
      if (mode & O_EXCL) {
	UnLock(lock);
	errno = EEXIST;
	free(ufb->ufbfn);
	return -1;
      }

      if (mode & O_TRUNC)
	newfile = FALSE;
      else
	mode &= ~O_CREAT;

      UnLock(lock);
    }
  }
  if (mode & O_CREAT) {
    if ((file = Open((char *)name, MODE_NEWFILE)) == NULL)
      goto osfail;

    if (newfile) {
      va_list va;
      int cmode;

      va_start(va, mode);

      cmode = va_arg(va, int) & ~getumask();
      
      chmod((char *)name, cmode); /* hope this doesn't fail :-) */
    }
  }
  else {
    if ((file = Open((char *)name,
Ejemplo n.º 5
0
int
__write(int fd, const void *buffer, unsigned int length)
{
  struct UFB *ufb;
  int         count, totcount;
  char       *ptr;

  /*
   * Check for the break signals
   */
  __chkabort();
  /*
   * find the ufb *
   */
  if ((ufb = __chkufb(fd)) == NULL) {
    errno = EINVAL;
    return -1;
  }
  /*
   * Check if write is allowed
   */
  if (!(ufb->ufbflg & UFB_WA)) {
    _OSERR = ERROR_WRITE_PROTECTED;
    errno = EIO;
    return -1;
  }

  /*
   * Seek to end of the file if necessary
   */
  if (ufb->ufbflg & UFB_APP)
    __lseek(fd, 0, 2);

  /*
   * Check if translation is not needed
   */
  if (!(ufb->ufbflg & UFB_XLAT) ||
      (ptr = memchr(buffer, 0x0A, length)) == NULL) {
    if (ufb->ufbflg & UFB_SOCK) {
      if ((count = send(fd, (char *)buffer, length, 0)) < 0)
	return -1;
    }
    else {
      if ((count = Write(ufb->ufbfh, (void *)buffer, length)) == -1)
	goto osfail;
    }
    return count;
  }

  totcount = length;

  /*
   * Translate, ie., append CR before each LF
   */
  do {
    count = ptr - (char *)buffer;
    if (ufb->ufbflg & UFB_SOCK) {
      if (send(fd, (char *)buffer, count, 0) < 0)
	return -1;
      if (send(fd, "\015"/* CR */, 1, 0) < 0)
	return -1;
    }
    else {
      if (Write(ufb->ufbfh, (void *)buffer, count) == -1)
	goto osfail;
      if (Write(ufb->ufbfh, "\015"/* CR */, 1) == -1)
	goto osfail;
    }
    length -= count;
    
    buffer = ptr;
  } while ((ptr = memchr((char *)buffer + 1, 0x0A, length)) != NULL);
  
  if (ufb->ufbflg & UFB_SOCK) {
    if ((count = send(fd, (char *)buffer, length, 0)) < 0)
      return -1;
  }
  else {
    if (Write(ufb->ufbfh, (void *)buffer, length) == -1)
      goto osfail;
  }

  return totcount;

osfail:
  errno = __io2errno(_OSERR = IoErr());
  return -1;
}
Ejemplo n.º 6
0
int poll( struct pollfd *pfds, unsigned int nfds, int timeout )
{
	unsigned int i;
	int maxfd = -1, ret;
	fd_set rset,wset,xset;
	struct timeval timeout_tv, *tvp = NULL;

	if (timeout >= 0)
	{
		timeout_tv.tv_sec = (timeout / 1000);
		timeout_tv.tv_usec = (timeout % 1000) * 1000;
		tvp = &timeout_tv;
	}

	if(pfds==NULL||nfds<1)
	{
	  if(pfds==NULL&&nfds<1&&timeout>=0)
	  {
	    ret = WaitSelect(0,NULL,NULL,NULL,tvp,NULL);
	    __chkabort();
	    return(ret);
	  }

	  errno=EINVAL;
	  return -1;
	}

	FD_ZERO (&rset);
	FD_ZERO (&wset);
	FD_ZERO (&xset);

	for (i = 0; i < nfds; i++)
	{
		pfds[i].revents = 0;

		if (pfds[i].events == 0)
			continue;

		if (pfds[i].fd > maxfd)
			maxfd = pfds[i].fd;

		if (pfds[i].events & POLLIN)
			FD_SET (pfds[i].fd, &rset);

		if (pfds[i].events & POLLOUT)
			FD_SET (pfds[i].fd, &wset);

		if (pfds[i].events & POLLERR)
			FD_SET (pfds[i].fd, &xset);
	}

	ret = WaitSelect (maxfd + 1, &rset, &wset, &xset, tvp, NULL);

	__chkabort ( ) ;

	if(ret == -1)
		return ret;

	for (i = 0; i < nfds; i++)
	{
		if (pfds[i].events == 0)
			continue;

		if (FD_ISSET (pfds[i].fd, &rset))
			pfds[i].revents |= POLLIN;

		if (FD_ISSET (pfds[i].fd, &wset))
			pfds[i].revents |= POLLOUT;

		if (FD_ISSET (pfds[i].fd, &xset))
			pfds[i].revents |= POLLERR;
	}

	return ret;
}