f72a40 internals: added amgc_dprintf(), a signal-resilient dprintf implementation.

Authored and Committed by midipix 6 years ago
    internals: added amgc_dprintf(), a signal-resilient dprintf implementation.
    
        
file modified
+1 -0
project/common.mk CHANGED
@@ -17,6 +17,7 @@ API_SRCS = \
17
17
src/skin/amgc_skin_default.c \
18
18
19
19
INTERNAL_SRCS = \
20
+ src/internal/$(PACKAGE)_dprintf_impl.c \
20
21
src/internal/$(PACKAGE)_errinfo_impl.c \
21
22
22
23
APP_SRCS = \
file modified
+1 -0
project/headers.mk CHANGED
@@ -4,6 +4,7 @@ API_HEADERS = \
4
4
5
5
INTERNAL_HEADERS = \
6
6
$(PROJECT_DIR)/src/internal/argv/argv.h \
7
+ $(PROJECT_DIR)/src/internal/$(PACKAGE)_dprintf_impl.h \
7
8
$(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \
8
9
$(PROJECT_DIR)/src/internal/$(PACKAGE)_errinfo_impl.h \
9
10
src/internal/apimagic_dprintf_impl.c ADDED
@@ -0,0 +1,56 @@
1
+ #include <stdio.h>
2
+ #include <stdarg.h>
3
+ #include <stdlib.h>
4
+ #include <unistd.h>
5
+ #include <errno.h>
6
+
7
+ int amgc_dprintf(int fd, const char * fmt, ...)
8
+ {
9
+ int ret;
10
+ int cnt;
11
+ int size;
12
+ va_list ap;
13
+ char * ch;
14
+ char * buf;
15
+ char chbuf[2048];
16
+
17
+ va_start(ap,fmt);
18
+
19
+ size = sizeof(chbuf);
20
+ buf = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size)
21
+ ? chbuf : malloc(cnt + 1);
22
+
23
+ va_end(ap);
24
+
25
+ if (buf == chbuf) {
26
+ (void)0;
27
+
28
+ } else if (buf) {
29
+ va_start(ap,fmt);
30
+ vsprintf(buf,fmt,ap);
31
+ va_end(ap);
32
+
33
+ } else {
34
+ return -1;
35
+ }
36
+
37
+ ret = 0;
38
+ ch = buf;
39
+
40
+ for (; cnt && ret>=0; ) {
41
+ ret = write(fd,ch,cnt);
42
+
43
+ while ((ret < 0) && (errno == EINTR))
44
+ ret = write(fd,ch,cnt);
45
+
46
+ ch += ret;
47
+ cnt -= ret;
48
+ }
49
+
50
+ ret = (ret < 0) ? -1 : ch - buf;
51
+
52
+ if (buf != chbuf)
53
+ free(buf);
54
+
55
+ return ret;
56
+ }
src/internal/apimagic_dprintf_impl.h ADDED
@@ -0,0 +1,6 @@
1
+ #ifndef APIMAGIC_DPRINTF_IMPL_H
2
+ #define APIMAGIC_DPRINTF_IMPL_H
3
+
4
+ int amgc_dprintf(int fd, const char * fmt, ...);
5
+
6
+ #endif