OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uC-libc/] [stdio2/] [printf.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * This file based on printf.c from 'Dlibs' on the atari ST  (RdeBath)
3
 *
4
 *
5
 *    Dale Schumacher                         399 Beacon Ave.
6
 *    (alias: Dalnefre')                      St. Paul, MN  55104
7
 *    dal@syntel.UUCP                         United States of America
8
 *  "It's not reality that's important, but how you perceive things."
9
 */
10
 
11
/* Altered to use stdarg, made the core function vfprintf.
12
 * Hooked into the stdio package using 'inside information'
13
 * Altered sizeof() assumptions, now assumes all integers except chars
14
 * will be either
15
 *  sizeof(xxx) == sizeof(long) or sizeof(xxx) == sizeof(short)
16
 *
17
 * -RDB
18
 */
19
 
20
#include <sys/types.h>
21
#include <fcntl.h>
22
#include <string.h>
23
#include <stdlib.h>
24
 
25
#ifdef __STDC__
26
#include <stdarg.h>
27
#define va_strt      va_start
28
#else
29
#include <varargs.h>
30
#define va_strt(p,i) va_start(p)
31
#endif
32
 
33
#include "stdio.h"
34
 
35
#ifdef L_printf
36
 
37
#ifdef __STDC__
38
int printf(const char * fmt, ...)
39
#else
40
int printf(fmt, va_alist)
41
__const char *fmt;
42
va_dcl
43
#endif
44
{
45
  va_list ptr;
46
  int rv;
47
 
48
  va_strt(ptr, fmt);
49
  rv = vfprintf(stdout,fmt,ptr);
50
  va_end(ptr);
51
  return rv;
52
}
53
#endif
54
 
55
#ifdef L_sprintf
56
#ifdef __STDC__
57
int sprintf(char * sp, const char * fmt, ...)
58
#else
59
int sprintf(sp, fmt, va_alist)
60
char * sp;
61
__const char *fmt;
62
va_dcl
63
#endif
64
{
65
static FILE  string[1] =
66
{
67
   {0, 0, (char*)(unsigned) -1, 0, (char*) (unsigned) -1, -1,
68
    _IOFBF | __MODE_WRITE}
69
};
70
 
71
  va_list ptr;
72
  int rv;
73
  va_strt(ptr, fmt);
74
  string->bufpos = sp;
75
  rv = vfprintf(string,fmt,ptr);
76
  va_end(ptr);
77
  *(string->bufpos) = 0;
78
  return rv;
79
}
80
#endif
81
 
82
#ifdef L_fprintf
83
#ifdef __STDC__
84
int fprintf(FILE * fp, const char * fmt, ...)
85
#else
86
int fprintf(fp, fmt, va_alist)
87
FILE * fp;
88
__const char *fmt;
89
va_dcl
90
#endif
91
{
92
  va_list ptr;
93
  int rv;
94
  va_strt(ptr, fmt);
95
  rv = vfprintf(fp,fmt,ptr);
96
  va_end(ptr);
97
  return rv;
98
}
99
#endif
100
 
101
#ifdef L_vprintf
102
int vprintf(fmt, ap)
103
__const char *fmt;
104
va_list ap;
105
{
106
  return vfprintf(stdout,fmt,ap);
107
}
108
#endif
109
 
110
#ifdef L_vsprintf
111
int vsprintf(sp, fmt, ap)
112
char * sp;
113
__const char *fmt;
114
va_list ap;
115
{
116
static FILE  string[1] =
117
{
118
   {0, 0, (char*)(unsigned) -1, 0, (char*) (unsigned) -1, -1,
119
    _IOFBF | __MODE_WRITE}
120
};
121
 
122
  int rv;
123
  string->bufpos = sp;
124
  rv = vfprintf(string,fmt,ap);
125
  *(string->bufpos) = 0;
126
  return rv;
127
}
128
#endif
129
 
130
#ifdef L_vfprintf
131
 
132
#if FLOATS
133
int _vfprintf_fp_ref = 1;
134
#else
135
int _vfprintf_fp_ref = 0;
136
#endif
137
 
138
static int
139
prtfld(op, buf, ljustf, sign, pad, width, preci, buffer_mode)
140
register FILE *op;
141
register unsigned char *buf;
142
int   ljustf;
143
register char sign;
144
char  pad;
145
register int width;
146
int   preci;
147
int   buffer_mode;
148
/*
149
 * Output the given field in the manner specified by the arguments. Return
150
 * the number of characters output.
151
 */
152
{
153
   register int cnt = 0, len;
154
   register unsigned char ch;
155
 
156
   len = strlen(buf);
157
 
158
   if (*buf == '-')
159
      sign = *buf++;
160
   else if (sign)
161
      len++;
162
 
163
   if ((preci != -1) && (len > preci))  /* limit max data width */
164
      len = preci;
165
 
166
   if (width < len)             /* flexible field width or width overflow */
167
      width = len;
168
 
169
   /*
170
    * at this point: width = total field width len   = actual data width
171
    * (including possible sign character)
172
    */
173
   cnt = width;
174
   width -= len;
175
 
176
   while (width || len)
177
   {
178
      if (!ljustf && width)     /* left padding */
179
      {
180
         if (len && sign && (pad == '0'))
181
            goto showsign;
182
         ch = pad;
183
         --width;
184
      }
185
      else if (len)
186
      {
187
         if (sign)
188
         {
189
          showsign:ch = sign;   /* sign */
190
            sign = '\0';
191
         }
192
         else
193
            ch = *buf++;        /* main field */
194
         --len;
195
      }
196
      else
197
      {
198
         ch = pad;              /* right padding */
199
         --width;
200
      }
201
      putc(ch, op);
202
      if( ch == '\n' && buffer_mode == _IOLBF ) fflush(op);
203
   }
204
 
205
   return (cnt);
206
}
207
 
208
int
209
vfprintf(op, fmt, ap)
210
FILE *op;
211
register __const char *fmt;
212
register va_list ap;
213
{
214
   register int i, cnt = 0, ljustf, lval;
215
   int   preci, dpoint, width;
216
   char  pad, sign, radix, hash;
217
   register char *ptmp;
218
   char  tmp[64], *ltostr(), *ultostr();
219
   int buffer_mode;
220
 
221
   /* This speeds things up a bit for unbuffered */
222
   buffer_mode = (op->mode&__MODE_BUF);
223
   op->mode &= (~__MODE_BUF);
224
 
225
   while (*fmt)
226
   {
227
      if (*fmt == '%')
228
      {
229
         if( buffer_mode == _IONBF ) fflush(op);
230
         ljustf = 0;             /* left justify flag */
231
         sign = '\0';           /* sign char & status */
232
         pad = ' ';             /* justification padding char */
233
         width = -1;            /* min field width */
234
         dpoint = 0;             /* found decimal point */
235
         preci = -1;            /* max data width */
236
         radix = 10;            /* number base */
237
         ptmp = tmp;            /* pointer to area to print */
238
         hash = 0;
239
         lval = (sizeof(int)==sizeof(long));    /* long value flaged */
240
       fmtnxt:
241
         i = 0;
242
         for(;;)
243
         {
244
            ++fmt;
245
            if(*fmt < '0' || *fmt > '9' ) break;
246
            i = (i * 10) + (*fmt - '0');
247
            if (dpoint)
248
               preci = i;
249
            else if (!i && (pad == ' '))
250
            {
251
               pad = '0';
252
               goto fmtnxt;
253
            }
254
            else
255
               width = i;
256
         }
257
 
258
         switch (*fmt)
259
         {
260
         case '\0':             /* early EOS */
261
            --fmt;
262
            goto charout;
263
 
264
         case '-':              /* left justification */
265
            ljustf = 1;
266
            goto fmtnxt;
267
 
268
         case ' ':
269
         case '+':              /* leading sign flag */
270
            sign = *fmt;
271
            goto fmtnxt;
272
 
273
         case '*':              /* parameter width value */
274
            i = va_arg(ap, int);
275
            if (dpoint)
276
               preci = i;
277
            else
278
               width = i;
279
            goto fmtnxt;
280
 
281
         case '.':              /* secondary width field */
282
            dpoint = 1;
283
            goto fmtnxt;
284
 
285
         case 'l':              /* long data */
286
            lval = 1;
287
            goto fmtnxt;
288
 
289
         case 'h':              /* short data */
290
            lval = 0;
291
            goto fmtnxt;
292
 
293
         case 'd':              /* Signed decimal */
294
         case 'i':
295
            ptmp = ltostr((long) ((lval)
296
                         ? va_arg(ap, long)
297
                         : va_arg(ap, short)),
298
                 10, 0);
299
            goto printit;
300
 
301
         case 'b':              /* Unsigned binary */
302
            radix = 2;
303
            goto usproc;
304
 
305
         case 'o':              /* Unsigned octal */
306
            radix = 8;
307
            goto usproc;
308
 
309
         case 'p':              /* Pointer */
310
            lval = (sizeof(char*) == sizeof(long));
311
            pad = '0';
312
            width = 6;
313
            preci = 8;
314
            /* fall thru */
315
 
316
         case 'x':              /* Unsigned hexadecimal */
317
         case 'X':
318
            radix = 16;
319
            /* fall thru */
320
 
321
         case 'u':              /* Unsigned decimal */
322
          usproc:
323
            ptmp = ultostr((unsigned long) ((lval)
324
                                   ? va_arg(ap, unsigned long)
325
                                   : va_arg(ap, unsigned short)),
326
                  radix, (*fmt == 'X') ? 1 : 0);
327
            if( hash && radix == 8 ) { width = strlen(ptmp)+1; pad='0'; }
328
            goto printit;
329
 
330
         case '#':
331
            hash=1;
332
            goto fmtnxt;
333
 
334
         case 'c':              /* Character */
335
            ptmp[0] = va_arg(ap, int);
336
            ptmp[1] = '\0';
337
            goto nopad;
338
 
339
         case 's':              /* String */
340
            ptmp = va_arg(ap, char*);
341
          nopad:
342
            sign = '\0';
343
            pad = ' ';
344
          printit:
345
            cnt += prtfld(op, ptmp, ljustf,
346
                           sign, pad, width, preci, buffer_mode);
347
            break;
348
 
349
#if FLOATS
350
         case 'e':              /* float */
351
         case 'f':
352
         case 'g':
353
         case 'E':
354
         case 'G':
355
         fprintf(stderr, "LIBM:PRINTF");
356
            gcvt(va_arg(ap, double), preci, ptmp);
357
            preci = -1;
358
            goto printit;
359
#else
360
         case 'e':              /* float */
361
         case 'f':
362
         case 'g':
363
         case 'E':
364
         case 'G':
365
                fprintf(stderr, "LIBC:PRINTF");
366
                exit(-1);
367
#endif
368
 
369
         default:               /* unknown character */
370
            goto charout;
371
         }
372
      }
373
      else
374
      {
375
       charout:
376
         putc(*fmt, op);        /* normal char out */
377
         ++cnt;
378
         if( *fmt == '\n' && buffer_mode == _IOLBF ) fflush(op);
379
      }
380
      ++fmt;
381
   }
382
   op->mode |= buffer_mode;
383
   if( buffer_mode == _IONBF ) fflush(op);
384
   if( buffer_mode == _IOLBF ) op->bufwrite = op->bufstart;
385
   return (cnt);
386
}
387
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.