chiark / gitweb /
Doxygen-clean
[disorder] / lib / snprintf.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
18
19#define NO_MEMORY_ALLOCATION
20/* because used from log.c */
21
05b75f8d 22#include "common.h"
460b9539 23
460b9539 24#include <stdarg.h>
25#include <stddef.h>
26
27#include "printf.h"
28#include "sink.h"
29
30struct fixedstr_sink {
31 struct sink s;
32 char *buffer;
33 int nbytes;
34 size_t size;
35};
36
37static int fixedstr_write(struct sink *f, const void *buffer, int nbytes) {
38 struct fixedstr_sink *s = (struct fixedstr_sink *)f;
39 int count;
40
41 if((size_t)s->nbytes < s->size) {
42 if((size_t)nbytes > s->size - s->nbytes)
43 count = s->size - s->nbytes;
44 else
45 count = nbytes;
46 memcpy(s->buffer + s->nbytes, buffer, count);
47 }
48 s->nbytes += nbytes;
49 return 0;
50}
51
52int byte_vsnprintf(char buffer[],
53 size_t bufsize,
54 const char *fmt,
55 va_list ap) {
56 struct fixedstr_sink s;
57 int n, m;
58
59 /* We have to make a sink directly here, since we can't safely do memory
60 * allocation here (we might be formatting the error message from a failed
61 * memory allocation) */
62 s.s.write = fixedstr_write;
63 s.buffer = buffer;
64 s.nbytes = 0;
65 s.size = bufsize;
66 n = byte_vsinkprintf(&s.s, fmt, ap);
67 if(bufsize) {
68 /* add the null terminator (even if the printf failed) */
69 m = s.nbytes;
70 if((size_t)m >= bufsize) m = bufsize - 1;
71 buffer[m] = 0;
72 }
73 return n;
74}
75
76int byte_snprintf(char buffer[], size_t bufsize, const char *fmt, ...) {
77 int n;
78 va_list ap;
79
80 va_start(ap, fmt);
81 n = byte_vsnprintf(buffer, bufsize, fmt, ap);
82 va_end(ap);
83 return n;
84}
85
86/*
87Local Variables:
88c-basic-offset:2
89comment-column:40
90End:
91*/