[go: up one dir, main page]

File: xsnprintf.c

package info (click to toggle)
foot 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,492 kB
  • sloc: ansic: 21,242; python: 170; sh: 26; makefile: 7
file content (36 lines) | stat: -rw-r--r-- 926 bytes parent folder | download
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 "xsnprintf.h"

#include <assert.h>
#include <limits.h>
#include <stdio.h>

size_t
xvsnprintf(char *buf, size_t n, const char *format, va_list ap)
{
    assert(n <= INT_MAX);
    int len = vsnprintf(buf, n, format, ap);

    /*
     * ISO C11 ยง7.21.6.5 states:
     * "The snprintf function returns the number of characters that
     * would have been written had n been sufficiently large, not
     * counting the terminating null character, or a negative value
     * if an encoding error occurred. Thus, the null-terminated output
     * has been completely written if and only if the returned value
     * is nonnegative and less than n."
     */
    assert(len >= 0);
    assert(len < (int)n);

    return (size_t)len;
}

size_t
xsnprintf(char *buf, size_t n, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    size_t len = xvsnprintf(buf, n, format, ap);
    va_end(ap);
    return len;
}