Move all sources to 'src/'
[lttng-ust.git] / src / snprintf / snprintf.c
1 /* $OpenBSD: snprintf.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
2 /*
3 * SPDX-License-Identifier: BSD-3-Clause
4 *
5 * Copyright (C) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Chris Torek.
10 */
11
12 #include <limits.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdarg.h>
17 #include "local.h"
18 #include "ust-snprintf.h"
19
20 #define DUMMY_LEN 1
21
22 int ust_safe_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
23 {
24 int ret;
25 char dummy[DUMMY_LEN];
26 LTTNG_UST_LFILE f;
27 struct __lttng_ust_sfileext fext;
28
29 /* While snprintf(3) specifies size_t stdio uses an int internally */
30 if (n > INT_MAX)
31 n = INT_MAX;
32 /* Stdio internals do not deal correctly with zero length buffer */
33 if (n == 0) {
34 str = dummy;
35 n = DUMMY_LEN;
36 }
37 _FILEEXT_SETUP(&f, &fext);
38 f._file = -1;
39 f._flags = __SWR | __SSTR;
40 f._bf._base = f._p = (unsigned char *)str;
41 f._bf._size = f._w = n - 1;
42 ret = ust_safe_vfprintf(&f, fmt, ap);
43 *f._p = '\0';
44 return (ret);
45 }
46
47 int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
48 {
49 va_list ap;
50 int ret;
51
52 va_start(ap, fmt);
53 ret = ust_safe_vsnprintf(str, n, fmt, ap);
54 va_end(ap);
55
56 return ret;
57 }
This page took 0.02978 seconds and 4 git commands to generate.