1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*
* $Id: printf.c,v 1.2 2001/07/06 20:18:48 doviende Exp $
*
* libarr - a screen management toolkit
*
* Copyright (C) 2000 Stormix Technologies Inc.
*
* License: LGPL
*
* Author: Chris Bond
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <stdarg.h>
#include <stdlib.h>
#include "printf.h"
#include "types.h"
/* int_buffer[] is the integer conversion buffer that will be used in the string
* formatting routines.
*/
static char int_buffer[12];
/* malloc_calc_len() calculates how long a formatted string will be.
*/
size_t
malloc_calc_len(str, args)
const char *str;
va_list args;
{
register char *ptr;
size_t length;
for (length = 0; *str != NUL; ++str) {
if ((*str != '%') || (*++str == '%')) {
++length;
continue;
}
if (*str == 'c') {
/* We need to pop the character off the stack even
* though we don't do anything with it.
*/
(void)va_arg(args, int);
continue;
}
if ((*str == 's') && ((ptr = va_arg(args, char *)) != NULL)) {
for (; *ptr++ != NUL; ++length);
continue;
}
if ((*str == 'l') && (*++str == 'u')) { /* u_long */
u_long long_conv;
++length;
if ((long_conv = (u_long)va_arg(args, u_long)) != 0)
for (; (long_conv /= 10) != 0; ++length);
continue;
}
if (*str == 'p') { /* pointer */
ptr_size_t pointer;
length += 3;
if ((pointer = (ptr_size_t)va_arg(args, ptr_size_t)))
for (; (pointer /= 16) != 0; ++length);
continue;
}
if (*str == 'd') { /* integer */
int number;
++length;
if ((number = (int)va_arg(args, int)) != 0) {
if (number < 0) {
++length; /* '-' */
number -= number * 2;
}
for (; (number /= 10) != 0; ++length);
}
continue;
}
}
return length;
}
/* format_copy() prints a formatted string (from format and args) to `pointer'.
*/
size_t
format_copy(pointer, str, args)
char *pointer, *str;
va_list args;
{
register char *copy;
char *save, chr;
/* `save' is stored so we can calculate how big our string is when we're
* done without having to increment another variable every time we copy.
*/
save = pointer;
for (; *str != NUL; ++str) {
if ((*str != '%') || (*++str == '%')) {
*pointer++ = *str;
continue;
}
if ((*str == 'c') && ((chr = va_arg(args, int)) != NUL)) {
*pointer++ = chr;
continue;
}
if ((*str == 's') && ((copy = va_arg(args, char *)) != NULL)) {
for (; *copy != 0; *pointer++ = *copy++);
continue;
}
if ((*str == 'l') && (*++str == 'u')) {
u_long long_conv;
if ((long_conv = (u_long)va_arg(args, u_long)) != 0) {
copy = (char *)int_buffer;
/* We need to copy this into a temporary storage
* buffer and then copy it *backwards* into the
* buffer that `pointer' points to.
*/
for (; long_conv != 0; long_conv /= 10)
*copy++ = (long_conv % 10) + '0';
for (--copy; copy >= (char *)int_buffer;)
*pointer++ = *copy--;
}
else
*pointer++ = '0';
}
if (*str == 'p') {
ptr_size_t ptr;
*pointer++ = '0';
*pointer++ = 'x';
if ((ptr = (ptr_size_t)va_arg(args, ptr_size_t)) == 0) {
*pointer++ = '0';
continue;
}
copy = (char *)int_buffer;
for (; ptr != 0; ptr /= 16)
if ((ptr % 16) > 9)
*copy++ = (ptr % 8) + ('0' * 2) - 1;
else
*copy++ = (ptr % 16) + '0';
for (--copy; copy >= (char *)int_buffer; --copy)
*pointer++ = *copy;
}
if (*str == 'd') {
int num_conv;
if ((num_conv = (int)va_arg(args, int)) == 0) {
*pointer++ = '0';
continue;
}
if (num_conv < 0) {
*pointer++ = '-';
num_conv = -num_conv;
}
copy = (char *)int_buffer;
for (; num_conv != 0; num_conv /= 10)
*copy++ = (num_conv % 10) + '0';
for (--copy; copy >= (char *)int_buffer; --copy)
*pointer++ = *copy;
}
}
*pointer = 0;
return (size_t)(pointer - save);
}
/* malloc_printf() allocates enough space for the formatted string and copies
* to a buffer that is supplied by the caller.
*/
size_t
malloc_printf(pointer, str)
char **pointer, *str;
...
{
va_list args;
size_t length;
va_start(args, str);
length = malloc_calc_len((const char *)str, args);
if ((*pointer = (char *)malloc(length + 1)) != NULL)
format_copy(*pointer, str, args);
else
length = 0;
va_end(args);
return length;
}
|