[go: up one dir, main page]

File: fmt.c

package info (click to toggle)
criterion 2.3.3git1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,832 kB
  • sloc: ansic: 17,852; cpp: 795; python: 72; sh: 27; makefile: 23
file content (105 lines) | stat: -rw-r--r-- 2,652 bytes parent folder | download | duplicates (2)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * The MIT License (MIT)
 *
 * Copyright © 2017 Franklin "Snaipe" Mathieu <http://snai.pe/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>

#include "fmt.h"

int cri_fmt_bprintf(char **buf, size_t *offset, size_t *sz,
        const char *fmt, ...)
{
    va_list vl;

    va_start(vl, fmt);
    int rc = cri_fmt_vbprintf(buf, offset, sz, fmt, vl);
    va_end(vl);
    return rc;
}

int cri_fmt_vbprintf(char **buf, size_t *offset, size_t *sz,
        const char *fmt, va_list ap)
{
    va_list vl;
    size_t offset_start = 0;

    if (!offset)
        offset = &offset_start;

    va_copy(vl, ap);
#ifdef _WIN32
    int size = _vscprintf(fmt, vl);
#else
    int size = vsnprintf(0, 0, fmt, vl);
#endif
    va_end(vl);

    if (size < 0) {
        return -errno;
    }

    size_t nsz = *sz + size;

    char *nb = realloc(*buf, nsz + 1);
    if (!nb) {
        return -errno;
    }
    *buf = nb;

    vsnprintf(&nb[*offset], size + 1, fmt, ap);

    *offset += size;
    *sz = nsz;
    return 0;
}

int cr_asprintf(char **strp, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    int res = cr_vasprintf(strp, fmt, ap);
    va_end(ap);
    return res;
}

int cr_vasprintf(char **strp, const char *fmt, va_list ap)
{
    size_t size = 0;
    char *out = NULL;
    int rc = cri_fmt_vbprintf(&out, NULL, &size, fmt, ap);
    if (rc < 0)
        return rc;
    if (size > (size_t) INT_MAX)
        return -EOVERFLOW;
    *strp = out;
    return size;
}

void cr_asprintf_free(char *buf)
{
    free(buf);
}