-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathms.c
More file actions
36 lines (33 loc) · 662 Bytes
/
ms.c
File metadata and controls
36 lines (33 loc) · 662 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 "ms.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "k.core/x.h"
static char *b;
static size_t l, c;
int mprintf(const char *f, ...) {
va_list a,a2;
int n;
size_t nc;
char *t=0;
va_start(a,f);
va_copy(a2,a);
n=vsnprintf(0,0,f,a2);
va_end(a2);
if(n<0) { va_end(a); return -1; }
if(l+n+1>c) {
nc=c?c*2:32;
while(nc<l+n+1) nc*=2;
t=xrealloc(b,nc);
if(!t) { va_end(a); return -1; }
b=t;
c=nc;
}
vsnprintf(b+l,c-l,f,a);
va_end(a);
l+=n;
return n;
}
const char *mbuffer(void) { return b?b:""; }
void mreset(void) { l=0; if(b) b[0]=0; }
void mfree(void) { xfree(b); b=0; l=c=0; }