source: trunk/sys/dietlibc/iprintf.c @ 232

Last change on this file since 232 was 1, checked in by alain, 7 years ago

First import

File size: 4.2 KB
Line 
1/*
2   This file is part of MutekP.
3 
4   MutekP is free software; you can redistribute it and/or modify it
5   under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8 
9   MutekP is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   General Public License for more details.
13 
14   You should have received a copy of the GNU General Public License
15   along with MutekP; if not, write to the Free Software Foundation,
16   Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18   UPMC / LIP6 / SOC (c) 2007
19*/
20
21/*
22 * stdio of the ghost of libc
23 * Developped by Denis Hommais and Frédéric Pétrot
24 * $Log: iprintf.c,v $
25 * Revision 1.2  2003/07/01 13:40:46  fred
26   Supports the %u and doesnt core anymore on an unrecognized char
27   following a %
28 * Revision 1.1.1.1  2002/02/28 12:58:56  disydent
29   Creation of Disydent CVS Tree
30 * Revision 1.1  2001/11/22 15:07:36  fred
31   Adding the iprintf function that does the formatting, and removes all
32   the old stuff hanging around.
33 * $Id: iprintf.c,v 1.2 2003/07/01 13:40:46 fred Exp $
34   Made up this file to centralize the printf behavior
35 */
36
37
38#include <stdarg.h>
39#include <stdio.h>
40
41/*
42 * Handling of the printf internals
43 * Addr is either the buffer or the tty addrs
44 * inc is 0 for a tty and 1 for a buffer
45 * Other arguments are self explanatory
46 */
47#define SIZE_OF_BUF 16
48int iprintf (char *addr, int inc, const char *fmt, va_list ap)
49{
50   register char *tmp;
51   int val, i = 0, count = 0;
52   char buf[SIZE_OF_BUF], *xdigit;
53
54   while (*fmt)
55   {
56      while ((*fmt != '%') && (*fmt))
57      {
58         *addr = *fmt++;
59         addr += inc;
60         count++;
61      }
62      if (*fmt)
63      {
64         fmt++;
65         switch (*fmt)
66         {
67         case '%':
68            *addr = '%';
69            addr += inc;
70            count++;
71            goto next;
72         case 'c':
73            *addr = va_arg (ap, int);
74            addr += inc;
75            count++;
76            goto next;
77         case 'd':
78         case 'i':
79            val = va_arg (ap, int);
80            if (val < 0)
81            {
82               val = -val;
83               *addr = '-';
84               addr += inc;
85               count++;
86            }
87            tmp = buf + SIZE_OF_BUF;
88            *--tmp = '\0';
89            do
90            {
91               *--tmp = val % 10 + '0';
92               val /= 10;
93            }
94            while (val);
95            break;
96         case 'u':
97            val = va_arg (ap, unsigned int);
98            tmp = buf + SIZE_OF_BUF;
99            *--tmp = '\0';
100            do
101            {
102               *--tmp = val % 10 + '0';
103               val /= 10;
104            }
105            while (val);
106            break;
107         case 'o':
108            val = va_arg (ap, int);
109            tmp = buf + SIZE_OF_BUF;
110            *--tmp = '\0';
111            do
112            {
113               *--tmp = (val & 7) + '0';
114               val = (unsigned int) val >> 3;
115            }
116            while (val);
117            break;
118         case 's':
119            tmp = va_arg (ap, char *);
120            if (!tmp)
121               tmp = "empty str";
122            break;
123         case 'p':
124         case 'x':
125         case 'X':
126            val = va_arg (ap, int);
127            tmp = buf + SIZE_OF_BUF;
128            *--tmp = '\0';
129            if (*fmt != 'X')
130               xdigit = "0123456789abcdef";
131            else
132               xdigit = "0123456789ABCDEF";
133            do
134            {
135               *--tmp = xdigit[val & 15];
136               val = (unsigned int) val >> 4;
137               i++;
138            }
139            while (val);
140            if (*fmt == 'p')
141               while (i < 8)
142               {
143                  *--tmp = xdigit[0];
144                  i++;
145               }
146            break;
147         default:
148            *addr = *fmt;
149            count++;
150            goto next;
151         }
152         while (*tmp)
153         {
154            *addr = *tmp++;
155            addr += inc;
156            count++;
157         }
158       next:
159         fmt++;
160      }
161   }
162   va_end (ap);
163   return count;
164}
165
Note: See TracBrowser for help on using the repository browser.