Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

#ifndef __MINGW32__
#include <sys/types.h>
#include <sys/uio.h>
#include <errno.h>
#include "buffer.h"
#ifndef __unlikely
#ifdef __GNUC__
#define __unlikely(x) __builtin_expect((x),0)
#define __unlikely(x) (x)
extern ssize_t buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie);
int buffer_putflush(buffer* b,const char* x,size_t len) {
/* Since we know we are going to flush anyway, let's see if we can
* optimize a bit */
if (!b->p) /* if the buffer is empty, just call buffer_stubborn directly */
return buffer_stubborn(b->op,b->fd,x,len,b);
#ifndef __MINGW32__
if (b->op==write) {
struct iovec v[2];
ssize_t w;
size_t cl=b->p+len;
while ((w=writev(b->fd,v,2))<0) {
if (errno == EINTR) continue;
return -1;
if (__unlikely((size_t)w!=cl)) {
/* partial write. ugh. */
if ((size_t)w<v[0].iov_len) {
if (buffer_stubborn(b->op,b->fd,v[0].iov_base+w,v[0].iov_len-w,b) ||
buffer_stubborn(b->op,b->fd,v[1].iov_base,v[0].iov_len,b)) return -1;
} else {
return buffer_stubborn(b->op,b->fd,v[1].iov_base+w,v[1].iov_len-w,b);
return 0;
if (buffer_put(b,x,len)<0) return -1;
if (buffer_flush(b)<0) return -1;
return 0;