blob: aad50f5e0244589f5d13e85b91e7cbdf3007b0fb [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 */
9
Eric Andersendae099b2003-10-09 08:35:42 +000010#include "libbb.h"
11
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000012ssize_t FAST_FUNC safe_write(int fd, const void *buf, size_t count)
Eric Andersendae099b2003-10-09 08:35:42 +000013{
14 ssize_t n;
15
Denys Vlasenkoa03ac602017-07-14 14:22:09 +020016 for (;;) {
Eric Andersendae099b2003-10-09 08:35:42 +000017 n = write(fd, buf, count);
Denys Vlasenkoa03ac602017-07-14 14:22:09 +020018 if (n >= 0 || errno != EINTR)
19 break;
20 /* Some callers set errno=0, are upset when they see EINTR.
21 * Returning EINTR is wrong since we retry write(),
22 * the "error" was transient.
23 */
24 errno = 0;
25 /* repeat the write() */
26 }
Eric Andersendae099b2003-10-09 08:35:42 +000027
28 return n;
29}