brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2001, 2002 Georges Menie (www.menie.org) |
| 3 | stdarg version contributed by Christian Ettinger |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU Lesser General Public License as published by |
| 7 | the Free Software Foundation; either version 2 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 Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | putchar is the only external dependency for this file, |
| 22 | if you have a working putchar, leave it commented out. |
| 23 | If not, uncomment the define below and |
| 24 | replace outbyte(c) by your own function call. |
| 25 | |
| 26 | */ |
| 27 | |
| 28 | #include <stdarg.h> |
| 29 | |
| 30 | int VCOM_putchar(int c); |
| 31 | |
| 32 | int putchar (int c) |
| 33 | { |
| 34 | return VCOM_putchar(c); |
| 35 | } |
| 36 | |
| 37 | static void printchar(char **str, int c) |
| 38 | { |
| 39 | //extern int putchar(int c); |
| 40 | |
| 41 | if (str) { |
| 42 | **str = (char)c; |
| 43 | ++(*str); |
| 44 | } else { |
| 45 | VCOM_putchar(c); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int puts (char * str) |
| 50 | { |
| 51 | int pc = 1; |
| 52 | while (*str != 0) { |
| 53 | VCOM_putchar(*str); |
| 54 | str ++; |
| 55 | pc ++; |
| 56 | } |
| 57 | VCOM_putchar('\n'); |
| 58 | return pc; |
| 59 | } |
| 60 | |
| 61 | #define PAD_RIGHT 1 |
| 62 | #define PAD_ZERO 2 |
| 63 | |
| 64 | static int prints(char **out, const char *string, int width, int pad) |
| 65 | { |
| 66 | register int pc = 0, padchar = ' '; |
| 67 | |
| 68 | if (width > 0) { |
| 69 | register int len = 0; |
| 70 | register const char *ptr; |
| 71 | for (ptr = string; *ptr; ++ptr) ++len; |
| 72 | if (len >= width) width = 0; |
| 73 | else width -= len; |
| 74 | if (pad & PAD_ZERO) padchar = '0'; |
| 75 | } |
| 76 | if (!(pad & PAD_RIGHT)) { |
| 77 | for (; width > 0; --width) { |
| 78 | printchar(out, padchar); |
| 79 | ++pc; |
| 80 | } |
| 81 | } |
| 82 | for (; *string ; ++string) { |
| 83 | printchar(out, *string); |
| 84 | ++pc; |
| 85 | } |
| 86 | for (; width > 0; --width) { |
| 87 | printchar(out, padchar); |
| 88 | ++pc; |
| 89 | } |
| 90 | |
| 91 | return pc; |
| 92 | } |
| 93 | |
| 94 | /* the following should be enough for 32 bit int */ |
| 95 | #define PRINT_BUF_LEN 12 |
| 96 | |
| 97 | static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase) |
| 98 | { |
| 99 | char print_buf[PRINT_BUF_LEN]; |
| 100 | register char *s; |
| 101 | register int t, neg = 0, pc = 0; |
| 102 | register unsigned int u = (unsigned int)i; |
| 103 | |
| 104 | if (i == 0) { |
| 105 | print_buf[0] = '0'; |
| 106 | print_buf[1] = '\0'; |
| 107 | return prints(out, print_buf, width, pad); |
| 108 | } |
| 109 | |
| 110 | if (sg && b == 10 && i < 0) { |
| 111 | neg = 1; |
| 112 | u = (unsigned int) - i; |
| 113 | } |
| 114 | |
| 115 | s = print_buf + PRINT_BUF_LEN - 1; |
| 116 | *s = '\0'; |
| 117 | |
| 118 | while (u) { |
| 119 | t = (unsigned int)u % b; |
| 120 | if (t >= 10) |
| 121 | t += letbase - '0' - 10; |
| 122 | *--s = (char)(t + '0'); |
| 123 | u /= b; |
| 124 | } |
| 125 | |
| 126 | if (neg) { |
| 127 | if (width && (pad & PAD_ZERO)) { |
| 128 | printchar(out, '-'); |
| 129 | ++pc; |
| 130 | --width; |
| 131 | } else { |
| 132 | *--s = '-'; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return pc + prints(out, s, width, pad); |
| 137 | } |
| 138 | |
| 139 | static int print(char **out, const char *format, va_list args) |
| 140 | { |
| 141 | register int width, pad; |
| 142 | register int pc = 0; |
| 143 | char scr[2]; |
| 144 | |
| 145 | for (; *format != 0; ++format) { |
| 146 | if (*format == '%') { |
| 147 | ++format; |
| 148 | width = pad = 0; |
| 149 | if (*format == '\0') break; |
| 150 | if (*format == '%') goto pout; |
| 151 | if (*format == '-') { |
| 152 | ++format; |
| 153 | pad = PAD_RIGHT; |
| 154 | } |
| 155 | while (*format == '0') { |
| 156 | ++format; |
| 157 | pad |= PAD_ZERO; |
| 158 | } |
| 159 | for (; *format >= '0' && *format <= '9'; ++format) { |
| 160 | width *= 10; |
| 161 | width += *format - '0'; |
| 162 | } |
| 163 | if (*format == 's') { |
| 164 | register char *s = (char *)va_arg(args, int); |
| 165 | pc += prints(out, s ? s : "(null)", width, pad); |
| 166 | continue; |
| 167 | } |
| 168 | if (*format == 'd') { |
| 169 | pc += printi(out, va_arg(args, int), 10, 1, width, pad, 'a'); |
| 170 | continue; |
| 171 | } |
| 172 | if (*format == 'x') { |
| 173 | pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'a'); |
| 174 | continue; |
| 175 | } |
| 176 | if (*format == 'X') { |
| 177 | pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'A'); |
| 178 | continue; |
| 179 | } |
| 180 | if (*format == 'u') { |
| 181 | pc += printi(out, va_arg(args, int), 10, 0, width, pad, 'a'); |
| 182 | continue; |
| 183 | } |
| 184 | if (*format == 'c') { |
| 185 | /* char are converted to int then pushed on the stack */ |
| 186 | scr[0] = (char)va_arg(args, int); |
| 187 | scr[1] = '\0'; |
| 188 | pc += prints(out, scr, width, pad); |
| 189 | continue; |
| 190 | } |
| 191 | } else { |
| 192 | pout: |
| 193 | printchar(out, *format); |
| 194 | ++pc; |
| 195 | } |
| 196 | } |
| 197 | if (out) **out = '\0'; |
| 198 | va_end(args); |
| 199 | return pc; |
| 200 | } |
| 201 | |
| 202 | int printf(const char *format, ...) |
| 203 | { |
| 204 | va_list args; |
| 205 | |
| 206 | va_start(args, format); |
| 207 | return print(0, format, args); |
| 208 | } |
| 209 | |
| 210 | int sprintf(char *out, const char *format, ...) |
| 211 | { |
| 212 | va_list args; |
| 213 | |
| 214 | va_start(args, format); |
| 215 | return print(&out, format, args); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | int snprintf(char *buf, unsigned int count, const char *format, ...) |
| 220 | { |
| 221 | va_list args; |
| 222 | |
| 223 | (void) count; |
| 224 | |
| 225 | va_start(args, format); |
| 226 | return print(&buf, format, args); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | #ifdef TEST_PRINTF |
| 231 | int main(void) |
| 232 | { |
| 233 | char *ptr = "Hello world!"; |
| 234 | char *np = 0; |
| 235 | int i = 5; |
| 236 | unsigned int bs = sizeof(int) * 8; |
| 237 | int mi; |
| 238 | char buf[80]; |
| 239 | |
| 240 | mi = (1 << (bs - 1)) + 1; |
| 241 | printf("%s\n", ptr); |
| 242 | printf("printf test\n"); |
| 243 | printf("%s is null pointer\n", np); |
| 244 | printf("%d = 5\n", i); |
| 245 | printf("%d = - max int\n", mi); |
| 246 | printf("char %c = 'a'\n", 'a'); |
| 247 | printf("hex %x = ff\n", 0xff); |
| 248 | printf("hex %02x = 00\n", 0); |
| 249 | printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3); |
| 250 | printf("%d %s(s)%", 0, "message"); |
| 251 | printf("\n"); |
| 252 | printf("%d %s(s) with %%\n", 0, "message"); |
| 253 | sprintf(buf, "justif: \"%-10s\"\n", "left"); |
| 254 | printf("%s", buf); |
| 255 | sprintf(buf, "justif: \"%10s\"\n", "right"); |
| 256 | printf("%s", buf); |
| 257 | sprintf(buf, " 3: %04d zero padded\n", 3); |
| 258 | printf("%s", buf); |
| 259 | sprintf(buf, " 3: %-4d left justif.\n", 3); |
| 260 | printf("%s", buf); |
| 261 | sprintf(buf, " 3: %4d right justif.\n", 3); |
| 262 | printf("%s", buf); |
| 263 | sprintf(buf, "-3: %04d zero padded\n", -3); |
| 264 | printf("%s", buf); |
| 265 | sprintf(buf, "-3: %-4d left justif.\n", -3); |
| 266 | printf("%s", buf); |
| 267 | sprintf(buf, "-3: %4d right justif.\n", -3); |
| 268 | printf("%s", buf); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * if you compile this file with |
| 275 | * gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c |
| 276 | * you will get a normal warning: |
| 277 | * printf.c:214: warning: spurious trailing `%' in format |
| 278 | * this line is testing an invalid % at the end of the format string. |
| 279 | * |
| 280 | * this should display (on 32bit int machine) : |
| 281 | * |
| 282 | * Hello world! |
| 283 | * printf test |
| 284 | * (null) is null pointer |
| 285 | * 5 = 5 |
| 286 | * -2147483647 = - max int |
| 287 | * char a = 'a' |
| 288 | * hex ff = ff |
| 289 | * hex 00 = 00 |
| 290 | * signed -3 = unsigned 4294967293 = hex fffffffd |
| 291 | * 0 message(s) |
| 292 | * 0 message(s) with % |
| 293 | * justif: "left " |
| 294 | * justif: " right" |
| 295 | * 3: 0003 zero padded |
| 296 | * 3: 3 left justif. |
| 297 | * 3: 3 right justif. |
| 298 | * -3: -003 zero padded |
| 299 | * -3: -3 left justif. |
| 300 | * -3: -3 right justif. |
| 301 | */ |
| 302 | |
| 303 | #endif |
| 304 | |
| 305 | |
| 306 | /* To keep linker happy. */ |
| 307 | int write(int i, char* c, int n) |
| 308 | { |
| 309 | (void)i; |
| 310 | (void)n; |
| 311 | (void)c; |
| 312 | return 0; |
| 313 | } |
| 314 | |