summaryrefslogtreecommitdiff
path: root/src/libstddjb/openreadnclose.c
blob: 40edea998cc853401a4bd538eade59a8354c3678 (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
/* ISC license. */

#include <errno.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/djbunix.h>

static int readnclose (int fd, char *s, unsigned int n)
{
  register int r = allread(fd, s, n) ;
  register int e = errno ;
  fd_close(fd) ;
  if ((r > 0) && (r < (int)n)) e = EPIPE ;
  errno = e ;
  return r ;
}

int openreadnclose (char const *file, char *s, unsigned int n)
{
  register int fd = open_readb(file) ;
  return fd < 0 ? fd : readnclose(fd, s, n) ;
}

int openreadnclose_nb (char const *file, char *s, unsigned int n)
{
  register int fd = open_read(file) ;
  return fd < 0 ? fd : readnclose(fd, s, n) ;
}