Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / pic16 / libc / stdio / printf_small.c
1 /*-----------------------------------------------------------------
2     printf_small.c - source file for reduced version of printf
3
4     Modified for pic16 port, by Vangelis Rokas, 2004 (vrokas@otenet.gr)
5
6     Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
7
8     This library is free software; you can redistribute it and/or modify it
9     under the terms of the GNU Library General Public License as published by the
10     Free Software Foundation; either version 2, or (at your option) any
11     later version.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Library General Public License for more details.
17
18     You should have received a copy of the GNU Library General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22     In other words, you are welcome to use, share and improve this program.
23     You are forbidden to forbid anyone else to use, share and improve
24     what you give them.   Help stamp out software-hoarding!
25
26    As a special exception, if you link this library with other files,
27    some of which are compiled with SDCC, to produce an executable,
28    this library does not by itself cause the resulting executable
29    to be covered by the GNU General Public License.
30    This exception does not however invalidate any other reasons why
31    the executable file might be covered by the GNU General Public License.
32 -------------------------------------------------------------------------*/
33
34 /* This function uses function putchar() to dump a character
35  * to standard output. putchar() is defined in libc18f.lib
36  * as dummy function, which will be linked if no putchar()
37  * function is provided by the user.
38  * The user can write his own putchar() function and link it
39  * with the source *BEFORE* the libc18f.lib library. This way
40  * the linker will link the first function (i.e. the user's function) */
41
42 /* following formats are supported :-
43    format     output type       argument-type
44      %d        decimal             int
45      %ld       decimal             long
46      %hd       decimal             char
47      %x        hexadecimal         int
48      %lx       hexadecimal         long
49      %hx       hexadecimal         char
50      %o        octal               int
51      %lo       octal               long
52      %ho       octal               char
53      %c        character           char
54      %s        character           generic pointer
55      %f        float               float
56 */
57
58 #include <stdarg.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61
62 void
63 printf_small (const char *fmt, ...)
64   __reentrant
65 {
66   char *ch;
67   char radix;
68   char flong;
69   char fstr;
70   char fchar;
71   char ffloat;
72   float flt;
73   char *str;
74   __data char *str1;
75   long val;
76   static char buffer[16];
77   va_list ap;
78
79   ch = fmt;
80   va_start (ap, fmt);
81
82   while (*ch) //for (; *fmt ; fmt++ )
83     {
84       if (*ch == '%')
85         {
86           flong = fstr = fchar = ffloat = 0;
87           radix = 0;
88           ++ch;
89
90           if (*ch == 'l')
91             {
92               flong = 1;
93               ++ch;
94             }
95           else if (*ch == 'h')
96             {
97               fchar = 1;
98               ++ch;
99             }
100
101           if (*ch == 's')
102             fstr = 1;
103           else if (*ch == 'f')
104             ffloat = 1;
105           else if (*ch == 'd')
106             radix = 10;
107           else if (*ch == 'x')
108             radix = 16;
109           else if (*ch == 'c')
110             radix = 0;
111           else if (*ch == 'o')
112             radix = 8;
113
114           if (fstr)
115             {
116               str = va_arg (ap, char *);
117               while (*str)
118                 putchar (*str++);
119             }
120           else if (ffloat)
121             {
122               flt = va_arg (ap, float);
123               x_ftoa (flt, buffer, 32, 6);
124               str1 = buffer;
125               while (*str1)
126                 ++str1;
127               --str1;
128               while (*str1 == '0')
129                 --str1;
130               ++str1;
131               *str1 = 0;
132               str1 = buffer;
133               while (*str1)
134                 putchar (*str1++);
135             }
136           else
137             {
138               if (flong)
139                 val = va_arg (ap, long);
140               else if (fchar)
141                 val = (char) va_arg (ap, int);  // FIXME: SDCC casts char arguments into ints
142               else
143                 {
144                   val = va_arg (ap, int);
145                 }
146
147               if (radix)
148                 {
149                   ltoa (val, buffer, radix);
150
151                   str1 = buffer;
152                   while (*str1)
153                     {
154                       putchar (*str1++);
155                     }
156                 }
157               else
158                 putchar ((char) val);
159             }
160         }
161       else
162         putchar (*ch);
163
164       ++ch;
165     }
166 }