File: buffer_put.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (48 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <string.h>
#include <unistd.h>
#ifndef __MINGW32__
#include <sys/uio.h>
#endif
#include "buffer.h"

extern ssize_t buffer_stubborn(ssize_t (*op)(int fd,const char* buf,size_t len,void* cookie),int fd,const char* buf, size_t len,void* cookie);

#ifndef __unlikely
#ifdef __GNUC__
#define __unlikely(x) __builtin_expect((x),0)
#else
#define __unlikely(x) (x)
#endif
#endif

int buffer_put(buffer* b,const char* buf,size_t len) {
  if (__unlikely(len>b->a-b->p)) {	/* doesn't fit */
#ifndef __MINGW32__
    if (b->op==(ssize_t (*)())write) {
      /* if it's write, we can substitute writev */
      struct iovec v[2];
      ssize_t r;
      v[0].iov_base=b->x; v[0].iov_len=b->p;
      v[1].iov_base=(char*)buf; v[1].iov_len=len;
      r=writev(b->fd,v,2);
      if (r<0) return -1;
      if ((size_t)r>=b->p) {
	r-=b->p;
	b->p=0;
	buf+=r;
	len-=r;
	if (!len) return 0; // wrote everything
	// fall through
      } /* else fall through */
    }
#endif
    if (buffer_flush(b)==-1) return -1;
    if (len>b->a) {
      if (buffer_stubborn((ssize_t (*)(int fd,const char* buf,size_t len,void* cookie))b->op,b->fd,buf,len,b)<0) return -1;
      return 0;
    }
  }
  memcpy(b->x+b->p, buf, len);
  b->p+=len;
  return 0;
}