/* * No copyright is claimed. This code is in the public domain; do with * it what you wish. * * Written by Karel Zak * Petr Uzel */ #include #include #include /* * The usleep function was marked obsolete in POSIX.1-2001 and was removed * in POSIX.1-2008. It was replaced with nanosleep() that provides more * advantages (like no interaction with signals and other timer functions). */ static inline int xusleep(useconds_t usec) { struct timespec waittime = { .tv_sec = usec / 1000000L, .tv_nsec = (usec % 1000000L) * 1000 }; return nanosleep(&waittime, NULL); } 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 = (const void *) ((const char *) buf + tmp); } else if (errno != EINTR && errno != EAGAIN) return -1; if (errno == EAGAIN) /* Try later, *sigh* */ xusleep(250000); } return 0; }