sync-samples.h
[Posted September 15, 2011 by corbet]
/*
* Copyright 2011, Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SYNC_SAMPLES_H
#define SYNC_SAMPLES_H
#define USER_ERR 1
#define LIB_ERR 2
#define SYS_ERR 3
#define INTERNAL_ERR 4
static inline ssize_t
full_write(int fd, const char *buf, size_t len)
{
ssize_t written = 0;
size_t to_write = len;
ssize_t ret;
int got_zero = 0;
while (to_write) {
ret = write(fd, buf, to_write);
switch (ret) {
case 0:
/* shouldn't happen, try again and see if
* an error is reported */
if (got_zero)
return written;
got_zero = 1;
continue;
case -1:
if (errno == EINTR)
continue;
return written ? written : -1;
default:
written += ret;
to_write -= ret;
buf += ret;
break;
}
}
return written;
}
#endif /* SYNC_SAMPLES_H */