Adam Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 1 | /* 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 Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
Adam Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | #include <os/print.h> |
Adam Tkac | 241e900 | 2008-12-03 14:13:36 +0000 | [diff] [blame] | 24 | |
Adam Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 25 | #include <stdarg.h> |
Adam Tkac | 241e900 | 2008-12-03 14:13:36 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Adam Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 28 | |
| 29 | #ifndef HAVE_VSNPRINTF |
Adam Tkac | 241e900 | 2008-12-03 14:13:36 +0000 | [diff] [blame] | 30 | size_t internal_memcpy(char *dest, const char *src, size_t destsize, |
| 31 | size_t srcsize) { |
| 32 | size_t copied; |
Adam Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 33 | |
Adam Tkac | 241e900 | 2008-12-03 14:13:36 +0000 | [diff] [blame] | 34 | copied = ((destsize) < (srcsize)) ? (destsize) : (srcsize); |
| 35 | memcpy(dest, src, copied); |
| 36 | |
| 37 | return copied; |
| 38 | } |
| 39 | |
| 40 | int 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 Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 47 | return 0; |
| 48 | |
Adam Tkac | 241e900 | 2008-12-03 14:13:36 +0000 | [diff] [blame] | 49 | 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 Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Adam Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 86 | str[written] = '\0'; |
Adam Tkac | 241e900 | 2008-12-03 14:13:36 +0000 | [diff] [blame] | 87 | |
| 88 | return written; |
Adam Tkac | 247b729 | 2008-11-14 13:11:31 +0000 | [diff] [blame] | 89 | } |
| 90 | #endif /* HAVE_VSNPRINTF */ |
| 91 | |
Adam Tkac | 3422fbd | 2008-12-03 14:18:19 +0000 | [diff] [blame] | 92 | #ifndef HAVE_SNPRINTF |
| 93 | int 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 | |