blob: 12bb438b7b9102ecfb514913059137944d22d4e2 [file] [log] [blame]
Eric Andersendae099b2003-10-09 08:35:42 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersendae099b2003-10-09 08:35:42 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersendae099b2003-10-09 08:35:42 +00008 */
Eric Andersendae099b2003-10-09 08:35:42 +00009#include "libbb.h"
10
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000011ssize_t FAST_FUNC safe_write(int fd, const void *buf, size_t count)
Eric Andersendae099b2003-10-09 08:35:42 +000012{
13 ssize_t n;
14
Denys Vlasenkoa03ac602017-07-14 14:22:09 +020015 for (;;) {
Eric Andersendae099b2003-10-09 08:35:42 +000016 n = write(fd, buf, count);
Denys Vlasenkoa03ac602017-07-14 14:22:09 +020017 if (n >= 0 || errno != EINTR)
18 break;
19 /* Some callers set errno=0, are upset when they see EINTR.
20 * Returning EINTR is wrong since we retry write(),
21 * the "error" was transient.
22 */
23 errno = 0;
24 /* repeat the write() */
25 }
Eric Andersendae099b2003-10-09 08:35:42 +000026
27 return n;
28}