blob: 4be22035cc1e4fe152fd6868219ad5acb85a1413 [file] [log] [blame]
Adam Tkac247b7292008-11-14 13:11:31 +00001/* Copyright (C) 2008 TightVNC Team. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
Adam Tkac8aee1a82009-09-04 12:08:56 +000019#ifdef HAVE_CONFIG_H
20#include <config.h>
Adam Tkac247b7292008-11-14 13:11:31 +000021#endif
22
23#include <os/print.h>
Adam Tkac241e9002008-12-03 14:13:36 +000024
Adam Tkac247b7292008-11-14 13:11:31 +000025#include <stdarg.h>
Adam Tkac241e9002008-12-03 14:13:36 +000026#include <stdlib.h>
27#include <string.h>
Adam Tkac247b7292008-11-14 13:11:31 +000028
29#ifndef HAVE_VSNPRINTF
Adam Tkac241e9002008-12-03 14:13:36 +000030size_t internal_memcpy(char *dest, const char *src, size_t destsize,
31 size_t srcsize) {
32 size_t copied;
Adam Tkac247b7292008-11-14 13:11:31 +000033
Adam Tkac241e9002008-12-03 14:13:36 +000034 copied = ((destsize) < (srcsize)) ? (destsize) : (srcsize);
35 memcpy(dest, src, copied);
36
37 return copied;
38}
39
40int tight_vsnprintf(char *str, size_t n, const char *format, va_list ap) {
41 int written = 0;
42 int tmpint, len;
43 char buf[64]; /* Is it enough? */
44 char *tmpstr;
45
46 if (format == NULL || n < 1)
Adam Tkac247b7292008-11-14 13:11:31 +000047 return 0;
48
Adam Tkac241e9002008-12-03 14:13:36 +000049 while (*format != '\0' && written < n - 1) {
50 if (*format != '%') {
51 if (written < n) {
52 str[written++] = *format++;
53 continue;
54 } else
55 break;
56 }
57
58 format++;
59 switch (*format) {
60 case '\0':
61 str[written++] = '%';
62 continue;
63 case 'd':
64 tmpint = va_arg(ap, int);
65 sprintf(buf, "%d", tmpint);
66 len = strlen(buf);
67 written += internal_memcpy (&str[written], buf,
68 len, n - written);
69 break;
70 case 's':
71 tmpstr = va_arg(ap, char *);
72 len = strlen(tmpstr);
73 written += internal_memcpy (&str[written],
74 tmpstr, len,
75 n - written);
76 break;
77 /* Catch unimplemented stuff */
78 default:
79 fprintf(stderr, "Unimplemented format: %c\n",
80 *format);
81 abort();
82 }
83 format++;
Adam Tkac247b7292008-11-14 13:11:31 +000084 }
85
Adam Tkac247b7292008-11-14 13:11:31 +000086 str[written] = '\0';
Adam Tkac241e9002008-12-03 14:13:36 +000087
88 return written;
Adam Tkac247b7292008-11-14 13:11:31 +000089}
90#endif /* HAVE_VSNPRINTF */
91
Adam Tkac3422fbd2008-12-03 14:18:19 +000092#ifndef HAVE_SNPRINTF
93int tight_snprintf(char *str, size_t n, const char *format, ...) {
94 va_list ap;
95 int written;
96
97 va_start(ap, format);
98 written = vsnprintf(str, n, format, ap);
99 va_end(ap);
100
101 return written;
102}
103#endif /* HAVE_SNPRINTF */
104