Example #1
0
void
_perror_r (struct _reent *ptr,
       const char *s)
{
  char *error;
  int dummy;
  FILE *fp = _stderr_r (ptr);

  CHECK_INIT (ptr, fp);

  _newlib_flockfile_start(fp);
  _fflush_r (ptr, fp);
  if (s != NULL && *s != '\0')
    {
      WRITE_STR (s);
      WRITE_STR (": ");
    }

  if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
    WRITE_STR (error);

#ifdef __SCLE
  WRITE_STR ((fp->_flags & __SCLE) ? "\r\n" : "\n");
#else
  WRITE_STR ("\n");
#endif
  fp->_flags &= ~__SOFF;
  _newlib_flockfile_end(fp);
}
Example #2
0
int
ferror (FILE * fp)
{
  int result;
  CHECK_INIT(_REENT, fp);
  _newlib_flockfile_start (fp);
  result = __sferror (fp);
  _newlib_flockfile_end (fp);
  return result;
}
Example #3
0
wint_t
_fputwc_r (struct _reent *ptr,
	wchar_t wc,
	FILE *fp)
{
  wint_t r;

  _newlib_flockfile_start (fp);
  ORIENT(fp, 1);
  r = __fputwc(ptr, wc, fp);
  _newlib_flockfile_end (fp);
  return r;
}
Example #4
0
/**
 * @brief Maps a stream pointer to a file descriptor.
 *
 * @details Returns the integer file descriptor associated
 * with the stream pointed to by @p f.
 *
 * @return Returns the integer value of the file descriptor
 * associated with @p f. Otherwise, the value -1 is returned
 * and errno set to indicate the error.
 */
int fileno(FILE * f)
{
  int result;
  CHECK_INIT (_REENT, f);
  _newlib_flockfile_start (f);
  if (f->_flags)
    result = __sfileno (f);
  else
    {
      result = -1;
      _REENT->_errno = EBADF;
    }
  _newlib_flockfile_end (f);
  return result;
}
Example #5
0
FILE *
_funopen_r (struct _reent *ptr,
       const void *cookie,
       funread readfn,
       funwrite writefn,
       funseek seekfn,
       funclose closefn)
{
  FILE *fp;
  funcookie *c;

  if (!readfn && !writefn)
    {
      __errno_r(ptr) = EINVAL;
      return NULL;
    }
  if ((fp = __sfp (ptr)) == NULL)
    return NULL;
  if ((c = (funcookie *) _malloc_r (ptr, sizeof *c)) == NULL)
    {
      _newlib_sfp_lock_start ();
      fp->_flags = 0;		/* release */
#ifndef __SINGLE_THREAD__
      __lock_close_recursive (fp->_lock);
#endif
      _newlib_sfp_lock_end ();
      return NULL;
    }

  _newlib_flockfile_start (fp);
  fp->_file = -1;
  c->cookie = (void *) cookie; /* cast away const */
  fp->_cookie = c;
  if (readfn)
    {
      c->readfn = readfn;
      fp->_read = funreader;
      if (writefn)
	{
	  fp->_flags = __SRW;
	  c->writefn = writefn;
	  fp->_write = funwriter;
	}
      else
	{
	  fp->_flags = __SRD;
	  c->writefn = NULL;
	  fp->_write = NULL;
	}
    }
  else
    {
      fp->_flags = __SWR;
      c->writefn = writefn;
      fp->_write = funwriter;
      c->readfn = NULL;
      fp->_read = NULL;
    }
  c->seekfn = seekfn;
  fp->_seek = seekfn ? funseeker : NULL;
#ifdef __LARGE64_FILES
  fp->_seek64 = seekfn ? funseeker64 : NULL;
  fp->_flags |= __SL64;
#endif
  c->closefn = closefn;
  fp->_close = funcloser;
  _newlib_flockfile_end (fp);
  return fp;
}
Example #6
0
FILE *
_fdopen64_r (struct _reent *ptr,
	int fd,
	const char *mode)
{
  register FILE *fp;
  int flags, oflags;
#ifdef HAVE_FCNTL
  int fdflags, fdmode;
#endif

  if ((flags = __sflags (ptr, mode, &oflags)) == 0)
    return 0;

  /* make sure the mode the user wants is a subset of the actual mode */
#ifdef HAVE_FCNTL
  if ((fdflags = _fcntl_r (ptr, fd, F_GETFL, 0)) < 0)
    return 0;
  fdmode = fdflags & O_ACCMODE;
  if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
    {
      ptr->_errno = EBADF;
      return 0;
    }
#endif

  if ((fp = __sfp (ptr)) == 0)
    return 0;

  _newlib_flockfile_start(fp);

  fp->_flags = flags;
  /* POSIX recommends setting the O_APPEND bit on fd to match append
     streams.  Someone may later clear O_APPEND on fileno(fp), but the
     stream must still remain in append mode.  Rely on __sflags
     setting __SAPP properly.  */
#ifdef HAVE_FCNTL
  if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
    _fcntl_r (ptr, fd, F_SETFL, fdflags | O_APPEND);
#endif
  fp->_file = fd;
  fp->_cookie = (void *) fp;

#undef _read
#undef _write
#undef _seek
#undef _close

  fp->_read = __sread;
  fp->_write = __swrite64;
  fp->_seek = __sseek;
  fp->_seek64 = __sseek64;
  fp->_close = __sclose;

#ifdef __SCLE
  /* Explicit given mode results in explicit setting mode on fd */
  if (oflags & O_BINARY)
    setmode(fp->_file, O_BINARY);
  else if (oflags & O_TEXT)
    setmode(fp->_file, O_TEXT);
  if (__stextmode(fp->_file))
    fp->_flags |= __SCLE;
#endif

  fp->_flags |= __SL64;

  _newlib_flockfile_end(fp);
  return fp;
}