source: trunk/kernel/libk/iprintk.c.old @ 50

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

First import

File size: 4.3 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   revision 1.2.1 2007/11/11 18:54:23 ghassan
23   rename iprintf to __iprintk, it's a copy of iprintf called on system level.
24   
25   stdio of the ghost of libc
26   Developped by Denis Hommais and Frédéric Pétrot
27   $Log: iprintf.c,v $
28   Revision 1.2  2003/07/01 13:40:46  fred
29   Supports the %u and doesnt core anymore on an unrecognized char
30   following a %
31 
32   Revision 1.1.1.1  2002/02/28 12:58:56  disydent
33   Creation of Disydent CVS Tree
34 
35   Revision 1.1  2001/11/22 15:07:36  fred
36   Adding the iprintf function that does the formatting, and removes all
37   the old stuff hanging around.
38 
39   $Id: iprintf.c,v 1.2 2003/07/01 13:40:46 fred Exp $
40   Made up this file to centralize the printf behavior
41*/
42
43#include <stdarg.h>
44#include <libk.h>
45
46/* Handling of the printf internals
47 * Addr is either the buffer or the tty addrs
48 * inc is 0 for a tty and 1 for a buffer
49 * Other arguments are self explanatory
50 */
51
52#define SIZE_OF_BUF 16
53int iprintk (char *addr, int inc, const char *fmt, va_list ap)
54{
55   register char *tmp;
56   int val, i = 0, count = 0;
57   char buf[SIZE_OF_BUF], *xdigit;
58
59   while (*fmt)
60   {
61      while ((*fmt != '%') && (*fmt))
62      {
63        *addr = *fmt++;
64         addr += inc;
65         count++;
66      }
67      if (*fmt)
68      {
69         fmt++;
70         switch (*fmt)
71         {
72         case '%':
73            *addr = '%';
74            addr += inc;
75            count++;
76            goto next;
77         case 'c':
78            *addr = va_arg (ap, int);
79            addr += inc;
80            count++;
81            goto next;
82         case 'd':
83         case 'i':
84            val = va_arg (ap, int);
85            if (val < 0)
86            {
87               val = -val;
88               *addr = '-';
89               addr += inc;
90               count++;
91            }
92            tmp = buf + SIZE_OF_BUF;
93            *--tmp = '\0';
94            do
95            {
96               *--tmp = val % 10 + '0';
97               val /= 10;
98            }
99            while (val);
100            break;
101         case 'u':
102            val = va_arg (ap, unsigned int);
103            tmp = buf + SIZE_OF_BUF;
104            *--tmp = '\0';
105            do
106            {
107               *--tmp = val % 10 + '0';
108               val /= 10;
109            }
110            while (val);
111            break;
112         case 'o':
113            val = va_arg (ap, int);
114            tmp = buf + SIZE_OF_BUF;
115            *--tmp = '\0';
116            do
117            {
118               *--tmp = (val & 7) + '0';
119               val = (unsigned int) val >> 3;
120            }
121            while (val);
122            break;
123         case 's':
124            tmp = va_arg (ap, char *);
125            if (!tmp)
126               tmp = "empty str";
127            break;
128         case 'p':
129         case 'x':
130         case 'X':
131            val = va_arg (ap, int);
132            tmp = buf + SIZE_OF_BUF;
133            *--tmp = '\0';
134            if (*fmt != 'X')
135               xdigit = "0123456789abcdef";
136            else
137               xdigit = "0123456789ABCDEF";
138            do
139            {
140               *--tmp = xdigit[val & 15];
141               val = (unsigned int) val >> 4;
142               i++;
143            }
144            while (val);
145            if (*fmt == 'p')
146               while (i < 8)
147               {
148                  *--tmp = xdigit[0];
149                  i++;
150               }
151            break;
152         default:
153            *addr = *fmt;
154            count++;
155            goto next;
156         }
157         while (*tmp)
158         {
159            *addr = *tmp++;
160            addr += inc;
161            count++;
162         }
163       next:
164         fmt++;
165      }
166   }
167   va_end (ap);
168   return count;
169}
Note: See TracBrowser for help on using the repository browser.