This source file includes following definitions.
- xstrcat
- xvasprintf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19
20 #include "xvasprintf.h"
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 #include "xalloc.h"
28
29
30 #include "xsize.h"
31
32 static char *
33 xstrcat (size_t argcount, va_list args)
34 {
35 char *result;
36 va_list ap;
37 size_t totalsize;
38 size_t i;
39 char *p;
40
41
42 totalsize = 0;
43 va_copy (ap, args);
44 for (i = argcount; i > 0; i--)
45 {
46 const char *next = va_arg (ap, const char *);
47 totalsize = xsum (totalsize, strlen (next));
48 }
49 va_end (ap);
50
51
52
53
54 if (totalsize == SIZE_MAX || totalsize > INT_MAX)
55 {
56 errno = EOVERFLOW;
57 return NULL;
58 }
59
60
61 result = XNMALLOC (totalsize + 1, char);
62 p = result;
63 for (i = argcount; i > 0; i--)
64 {
65 const char *next = va_arg (args, const char *);
66 size_t len = strlen (next);
67 memcpy (p, next, len);
68 p += len;
69 }
70 *p = '\0';
71
72 return result;
73 }
74
75 char *
76 xvasprintf (const char *format, va_list args)
77 {
78 char *result;
79
80
81
82
83 {
84 size_t argcount = 0;
85 const char *f;
86
87 for (f = format;;)
88 {
89 if (*f == '\0')
90
91 return xstrcat (argcount, args);
92 if (*f != '%')
93 break;
94 f++;
95 if (*f != 's')
96 break;
97 f++;
98 argcount++;
99 }
100 }
101
102 if (vasprintf (&result, format, args) < 0)
103 {
104 if (errno == ENOMEM)
105 xalloc_die ();
106 return NULL;
107 }
108
109 return result;
110 }