Blame src/internal/apimagic_dprintf_impl.c
|
|
f72a40 |
#include <stdio.h>
|
|
|
f72a40 |
#include <stdarg.h>
|
|
|
f72a40 |
#include <stdlib.h>
|
|
|
f72a40 |
#include <unistd.h>
|
|
|
f72a40 |
#include <errno.h>
|
|
|
f72a40 |
|
|
|
f72a40 |
int amgc_dprintf(int fd, const char * fmt, ...)
|
|
|
f72a40 |
{
|
|
|
f72a40 |
int ret;
|
|
|
f72a40 |
int cnt;
|
|
|
f72a40 |
int size;
|
|
|
f72a40 |
va_list ap;
|
|
|
f72a40 |
char * ch;
|
|
|
f72a40 |
char * buf;
|
|
|
f72a40 |
char chbuf[2048];
|
|
|
f72a40 |
|
|
|
f72a40 |
va_start(ap,fmt);
|
|
|
f72a40 |
|
|
|
f72a40 |
size = sizeof(chbuf);
|
|
|
f72a40 |
buf = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size)
|
|
|
f72a40 |
? chbuf : malloc(cnt + 1);
|
|
|
f72a40 |
|
|
|
f72a40 |
va_end(ap);
|
|
|
f72a40 |
|
|
|
f72a40 |
if (buf == chbuf) {
|
|
|
f72a40 |
(void)0;
|
|
|
f72a40 |
|
|
|
f72a40 |
} else if (buf) {
|
|
|
f72a40 |
va_start(ap,fmt);
|
|
|
f72a40 |
vsprintf(buf,fmt,ap);
|
|
|
f72a40 |
va_end(ap);
|
|
|
f72a40 |
|
|
|
f72a40 |
} else {
|
|
|
f72a40 |
return -1;
|
|
|
f72a40 |
}
|
|
|
f72a40 |
|
|
|
f72a40 |
ret = 0;
|
|
|
f72a40 |
ch = buf;
|
|
|
f72a40 |
|
|
|
f72a40 |
for (; cnt && ret>=0; ) {
|
|
|
f72a40 |
ret = write(fd,ch,cnt);
|
|
|
f72a40 |
|
|
|
f72a40 |
while ((ret < 0) && (errno == EINTR))
|
|
|
f72a40 |
ret = write(fd,ch,cnt);
|
|
|
f72a40 |
|
|
|
f72a40 |
ch += ret;
|
|
|
f72a40 |
cnt -= ret;
|
|
|
f72a40 |
}
|
|
|
f72a40 |
|
|
|
f72a40 |
ret = (ret < 0) ? -1 : ch - buf;
|
|
|
f72a40 |
|
|
|
f72a40 |
if (buf != chbuf)
|
|
|
f72a40 |
free(buf);
|
|
|
f72a40 |
|
|
|
f72a40 |
return ret;
|
|
|
f72a40 |
}
|