Rev 1272 | Rev 1623 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1272 | Rev 1617 | ||
---|---|---|---|
Line 50... | Line 50... | ||
50 | * @param data structure with destination string, counter of used space and total string size. |
50 | * @param data structure with destination string, counter of used space and total string size. |
51 | * @return number of characters to print (not characters really printed!) |
51 | * @return number of characters to print (not characters really printed!) |
52 | */ |
52 | */ |
53 | int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data) |
53 | int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data) |
54 | { |
54 | { |
55 | size_t i,j; |
55 | size_t i; |
56 | i = data->size - data->len; |
56 | i = data->size - data->len; |
57 | j = count; |
- | |
58 | 57 | ||
59 | if (i > 0) { |
58 | if (i == 0) { |
60 | if (i <= j) { |
59 | return count; |
- | 60 | } |
|
- | 61 | ||
61 | if (i == 1) { |
62 | if (i == 1) { |
62 | /* We have only one free byte left in buffer => write there trailing zero */ |
63 | /* We have only one free byte left in buffer => write there trailing zero */ |
63 | data->string[data->size - 1] = 0; |
64 | data->string[data->size - 1] = 0; |
64 | data->len = data->size; |
65 | data->len = data->size; |
65 | } else { |
66 | return count; |
- | 67 | } |
|
- | 68 | ||
- | 69 | if (i <= count) { |
|
66 | /* We have not enought space for whole string with the trailing zero => print only a part of string */ |
70 | /* We have not enought space for whole string with the trailing zero => print only a part of string */ |
67 | memcpy((void *)(data->string + data->len), (void *)str, i - 1); |
71 | memcpy((void *)(data->string + data->len), (void *)str, i - 1); |
68 | data->string[data->size - 1] = 0; |
72 | data->string[data->size - 1] = 0; |
69 | data->len = data->size; |
73 | data->len = data->size; |
- | 74 | return count; |
|
70 | } |
75 | } |
71 | } else { |
76 | |
72 | /* Buffer is big enought to print whole string */ |
77 | /* Buffer is big enought to print whole string */ |
73 | memcpy((void *)(data->string + data->len), (void *)str, j); |
78 | memcpy((void *)(data->string + data->len), (void *)str, count); |
74 | data->len += j; |
79 | data->len += count; |
75 | /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */ |
80 | /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */ |
76 | data->string[data->len] = 0; |
81 | data->string[data->len] = 0; |
77 | } |
- | |
78 | } |
- | |
79 | 82 | ||
80 | return count; |
83 | return count; |
81 | } |
84 | } |
82 | 85 | ||
83 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) |
86 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) |