File: printf_small.c

package info (click to toggle)
sdcc 3.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 60,316 kB
  • ctags: 69,276
  • sloc: ansic: 418,778; cpp: 48,792; makefile: 46,787; sh: 28,453; asm: 11,918; perl: 8,387; yacc: 6,640; lisp: 1,630; lex: 746; python: 660; awk: 495; sed: 89
file content (162 lines) | stat: -rw-r--r-- 4,787 bytes parent folder | download | duplicates (7)
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
/*-------------------------------------------------------------------------
   printf_small.c - source file for reduced version of printf

   Copyright (C) 1999, Sandeep Dutta <sandeep.dutta AT ieee.org>
   Modified for pic16 port, by Vangelis Rokas, 2004 <vrokas AT otenet.gr>

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, 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 General Public License for more details.

   You should have received a copy of the GNU General Public License 
   along with this library; see the file COPYING. If not, write to the
   Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.

   As a special exception, if you link this library with other files,
   some of which are compiled with SDCC, to produce an executable,
   this library does not by itself cause the resulting executable to
   be covered by the GNU General Public License. This exception does
   not however invalidate any other reasons why the executable file
   might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/

/* This function uses function putchar() to dump a character
 * to standard output. putchar() is defined in libc18f.lib
 * as dummy function, which will be linked if no putchar()
 * function is provided by the user.
 * The user can write his own putchar() function and link it
 * with the source *BEFORE* the libc18f.lib library. This way
 * the linker will link the first function (i.e. the user's function) */

/* following formats are supported :-
   format     output type       argument-type
     %d        decimal             int
     %ld       decimal             long
     %hd       decimal             char
     %x        hexadecimal         int
     %lx       hexadecimal         long
     %hx       hexadecimal         char
     %o        octal               int
     %lo       octal               long
     %ho       octal               char
     %c        character           char
     %s        character           generic pointer
     %f        float               float
*/

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

void
printf_small (const char *fmt, ...)
  __reentrant
{
  const char *ch;
  char radix;
  char flong;
  char fstr;
  char fchar;
  char ffloat;
  float flt;
  char *str;
  __data char *str1;
  long val;
  static char buffer[16];
  va_list ap;

  ch = fmt;
  va_start (ap, fmt);

  while (*ch) //for (; *fmt ; fmt++ )
    {
      if (*ch == '%')
        {
          flong = fstr = fchar = ffloat = 0;
          radix = 0;
          ++ch;

          if (*ch == 'l')
            {
              flong = 1;
              ++ch;
            }
          else if (*ch == 'h')
            {
              fchar = 1;
              ++ch;
            }

          if (*ch == 's')
            fstr = 1;
          else if (*ch == 'f')
            ffloat = 1;
          else if (*ch == 'd')
            radix = 10;
          else if (*ch == 'x')
            radix = 16;
          else if (*ch == 'c')
            radix = 0;
          else if (*ch == 'o')
            radix = 8;

          if (fstr)
            {
              str = va_arg (ap, char *);
              while (*str)
                putchar (*str++);
            }
          else if (ffloat)
            {
              flt = va_arg (ap, float);
              x_ftoa (flt, buffer, 32, 6);
              str1 = buffer;
              while (*str1)
                ++str1;
              --str1;
              while (*str1 == '0')
                --str1;
              ++str1;
              *str1 = 0;
              str1 = buffer;
              while (*str1)
                putchar (*str1++);
            }
          else
            {
              if (flong)
                val = va_arg (ap, long);
              else if (fchar)
                val = (char) va_arg (ap, int);  // FIXME: SDCC casts char arguments into ints
              else
                {
                  val = va_arg (ap, int);
                }

              if (radix)
                {
                  ltoa (val, buffer, radix);

                  str1 = buffer;
                  while (*str1)
                    {
                      putchar (*str1++);
                    }
                }
              else
                putchar ((char) val);
            }
        }
      else
        putchar (*ch);

      ++ch;
    }
}