summaryrefslogtreecommitdiffstats
path: root/include/writeall.h
blob: 20207076d8012600bc0934094de76ef62f1ad8ef (plain) (blame)
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
#ifndef UTIL_LINUX_WRITEALL_H
#define UTIL_LINUX_WRITEALL_H

#include <string.h>
#include <unistd.h>
#include <errno.h>

static inline int write_all(int fd, const void *buf, size_t count)
{
	while(count) {
		ssize_t tmp;

		errno = 0;
		tmp = write(fd, buf, count);
		if (tmp > 0) {
			count -= tmp;
			if (count)
				buf += tmp;
		} else if (errno != EINTR && errno != EAGAIN)
			return -1;
		if (errno == EAGAIN)	/* Try later, *sigh* */
			usleep(10000);
	}
	return 0;
}

#endif /* UTIL_LINUX_WRITEALL_H */