-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathdprintf.c
More file actions
36 lines (32 loc) · 741 Bytes
/
Copy pathdprintf.c
File metadata and controls
36 lines (32 loc) · 741 Bytes
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
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
int dprintf(int fd, const char *fmt, ...)
{
char stack_buffer[256];
char *buf = stack_buffer;
int len = sizeof(stack_buffer);
va_list ap;
int ret = -1;
va_start(ap, fmt);
// Method 1 try to just print it to stack if possible
ret = vsnprintf(buf, len, fmt, ap);
if (ret >= len) {
// Method 2 dynamically allocate buffer
len = ret + 1;
buf = malloc(len);
if (buf) {
va_end(ap);
va_start(ap, fmt);
ret = vsnprintf(buf, len, fmt, ap);
} else {
ret = -1; // Malloc failed due to too little memory??? Couldn't be me
}
}
if (ret >= 0 && (write(fd, buf, ret) != ret)) {
ret = -1;
}
if (buf != stack_buffer) free(buf);
va_end(ap);
return ret;
}