-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.c
60 lines (49 loc) · 1.48 KB
/
test.c
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
49
50
51
52
53
54
55
56
57
58
59
60
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <assert.h>
#define SIZE 100
int message_format(char* start, const char *fmt, ...);
int main(int argc, char** argv){
char* start = malloc(5);
free(start);
int a = 3;
message_format(start, "asdf%d\n", a);
// printf("%lf, %d",ceil((double)strlen(abc)/SIZE), (int) ceil((double)strlen(abc)/SIZE));
// char* str = malloc(40);
// printf("sizeof: %lu, strlen: %lu, str:%s\n", sizeof(str), strlen(str), str);
//for(int i=0;i<9;i++) {
// strcat(str, "abc");
// printf("sizeof: %lu, strlen: %lu, str:%s\n", sizeof(str), strlen(str), str);
//}
// printf("%lu", snprintf(NULL, 0, "%s", abc));
return 0;
}
int message_format(char* start, const char *fmt, ...) {
va_list argptr;
char *vafmt = NULL;
va_start(argptr,fmt);
int source_len = vasprintf(&vafmt, fmt, argptr);
va_end(argptr);
printf("Source_len: %d", source_len);
printf("source = %s", vafmt);
// u32 multiplier = (u32) ceil((double) source_len/EXTEND_MESSAGE_SIZE);
char* new_space = malloc(0 + source_len);
if(!new_space) {exit(1);}
else {*new_space = '\0';}
if (start) {strcpy(new_space, start);}
free(start);
// message->content = new_space;
// message->size += source_len;
// va_list argptr;
start = new_space;
va_start(argptr,fmt);
char* source = malloc(source_len);
snprintf(source, source_len, fmt, argptr);
va_end(argptr);
strcat(start, source);
return 0;
}