summaryrefslogtreecommitdiff
path: root/newlib/addon/newlib/libc/sys/hurd/writer.c
blob: b8b4cc8db61f67e242c2104fec65e0d327cb9b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <reent.h>
#include <unistd.h>
#include <errno.h>

#include "fd.h"

_ssize_t
write (int fdi, const void *buf, size_t cnt)
{
  if (fdi < 0 || fdi >= _fd_size)
    {
      errno = EBADF;
      return -1;
    }

  ss_mutex_lock (&_fd_lock);

  struct _fd *fd = _fds[fdi];
  if (! fd || ! fd->ops->pwrite)
    {
      errno = EBADF;
      ss_mutex_unlock (&_fd_lock);
      return -1;
    }

  if (cnt == 0)
    {
      ss_mutex_unlock (&_fd_lock);
      return 0;
    }

  ss_mutex_lock (&fd->lock);
  ss_mutex_unlock (&_fd_lock);

  _ssize_t len = fd->ops->pwrite (fd, buf, cnt, fd->pos);
  fd->pos += len;

  ss_mutex_unlock (&fd->lock);

  return len;
}


_ssize_t
_DEFUN (_write_r, (ptr, fd, buf, cnt),
     struct _reent *ptr _AND
     int fd _AND
     _CONST _PTR buf _AND
     size_t cnt)
{
  return write (fd, buf, cnt);
}