Switch to GPL v3
[disorder] / lib / snprintf.c
1 /*
2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
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 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #define NO_MEMORY_ALLOCATION
20 /* because used from log.c */
21
22 #include "common.h"
23
24 #include <stdarg.h>
25 #include <stddef.h>
26
27 #include "printf.h"
28 #include "sink.h"
29
30 struct fixedstr_sink {
31 struct sink s;
32 char *buffer;
33 int nbytes;
34 size_t size;
35 };
36
37 static 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
52 int 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
76 int 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 /*
87 Local Variables:
88 c-basic-offset:2
89 comment-column:40
90 End:
91 */