MCPcopy Create free account
hub / github.com/HelenOS/helenos / vsnprintf_str_write

Function vsnprintf_str_write

kernel/generic/src/printf/vsnprintf.c:64–116  ·  view source on GitHub ↗

Write string to given buffer. * * Write at most data->size plain characters including trailing zero. * According to C99, snprintf() has to return number of characters that * would have been written if enough space had been available. Hence * the return value is not the number of actually printed characters * but size of the input string. * * @param str Source string to print. * @param si

Source from the content-addressed store, hash-verified

62 *
63 */
64static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data)
65{
66 size_t left = data->size - data->len;
67
68 if (left == 0)
69 return ((int) size);
70
71 if (left == 1) {
72 /*
73 * We have only one free byte left in buffer
74 * -> store trailing zero
75 */
76 data->dst[data->size - 1] = 0;
77 data->len = data->size;
78 return ((int) size);
79 }
80
81 if (left <= size) {
82 /*
83 * We do not have enough space for the whole string
84 * with the trailing zero => print only a part
85 * of string
86 */
87 size_t index = 0;
88
89 while (index < size) {
90 char32_t uc = str_decode(str, &index, size);
91
92 if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK)
93 break;
94 }
95
96 /*
97 * Put trailing zero at end, but not count it
98 * into data->len so it could be rewritten next time
99 */
100 data->dst[data->len] = 0;
101
102 return ((int) size);
103 }
104
105 /* Buffer is big enough to print the whole string */
106 memcpy((void *)(data->dst + data->len), (void *) str, size);
107 data->len += size;
108
109 /*
110 * Put trailing zero at end, but not count it
111 * into data->len so it could be rewritten next time
112 */
113 data->dst[data->len] = 0;
114
115 return ((int) size);
116}
117
118/** Write wide string to given buffer.
119 *

Callers

nothing calls this directly

Calls 3

str_decodeFunction · 0.50
chr_encodeFunction · 0.50
memcpyFunction · 0.50

Tested by

no test coverage detected