source: rtems/cpukit/libcsupport/src/printk.c @ a4afa90

Last change on this file since a4afa90 was a4afa90, checked in by Joel Sherrill <joel.sherrill@…>, on 04/02/07 at 14:36:01

2007-04-02 Joel Sherrill <joel@…>

  • libcsupport/src/printk.c: Add %p support.
  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*-------------------------------------------------------------------------+
2| printk.c v1.1 - PC386 BSP - 1997/08/07
3+--------------------------------------------------------------------------+
4| (C) Copyright 1997 -
5| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
6|
7| http://pandora.ist.utl.pt
8|
9| Instituto Superior Tecnico * Lisboa * PORTUGAL
10+--------------------------------------------------------------------------+
11| Disclaimer:
12|
13| This file is provided "AS IS" without warranty of any kind, either
14| expressed or implied.
15+--------------------------------------------------------------------------+
16| This code is based on code by: Jose Rufino - IST
17|
18|  $Id$
19+--------------------------------------------------------------------------*/
20
21#if HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <stdarg.h>
26#include <stdio.h>
27#include <rtems/bspIo.h>
28
29/*-------------------------------------------------------------------------+
30|         Function: printNum
31|      Description: print number in a given base.
32| Global Variables: None.
33|        Arguments: num - number to print, base - base used to print the number.
34|          Returns: Nothing.
35+--------------------------------------------------------------------------*/
36static void
37printNum(long unsigned int num, int base, int sign, int maxwidth, int lead)
38{
39  long unsigned int n;
40  int count;
41  char toPrint[20];
42
43  if ( (sign == 1) && ((long)num <  0) ) {
44    BSP_output_char('-');
45    num = -num;
46    if (maxwidth) maxwidth--;
47  }
48
49  count = 0;
50  while ((n = num / base) > 0) {
51    toPrint[count++] = (num - (n*base));
52    num = n ;
53  }
54  toPrint[count++] = num;
55
56  for (n=maxwidth ; n > count; n-- )
57    BSP_output_char(lead);
58
59  for (n = 0; n < count; n++){
60    BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]);
61  }
62} /* printNum */
63
64
65/*-------------------------------------------------------------------------+
66|         Function: printk
67|      Description: a simplified version of printf intended for use when the
68                    console is not yet initialized or in ISR's.
69| Global Variables: None.
70|        Arguments: as in printf: fmt - format string, ... - unnamed arguments.
71|          Returns: Nothing.
72+--------------------------------------------------------------------------*/
73void
74vprintk(char *fmt, va_list ap)
75{
76  char     c, *str;
77  int      lflag, base, sign, width, lead;
78  /* unsigned int level; */
79
80  /* _CPU_ISR_Disable(level); */
81
82  for (; *fmt != '\0'; fmt++)
83  {
84    lflag = 0;
85    base  = 0;
86    sign = 0;
87    width = 0;
88    lead = ' ';
89    if (*fmt == '%')
90    {
91      fmt++;
92      if (*fmt == '0' ) {
93        lead = '0';
94        fmt++;
95      }
96      while (*fmt >= '0' && *fmt <= '9' ) {
97        width *= 10;
98        width += (*fmt - '0');
99        fmt++;
100      }
101
102      if ((c = *fmt) == 'l')
103      {
104        lflag = 1;
105        c = *++fmt;
106      }
107      switch (c)
108      {
109        case 'o': case 'O': base = 8; sign = 0; break;
110                case 'i': case 'I':
111        case 'd': case 'D': base = 10; sign = 1; break;
112        case 'u': case 'U': base = 10; sign = 0; break;
113        case 'x': case 'X': base = 16; sign = 0; break;
114        case 'p':           base = 16; sign = 0; break;
115        case 's':
116          for (str = va_arg(ap, char *); *str; str++)
117            BSP_output_char(*str);
118          break;
119        case 'c':
120          BSP_output_char(va_arg(ap, int));
121          break;
122        default:
123          BSP_output_char(c);
124          break;
125      } /* switch*/
126
127      if (base)
128        printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int),
129                 base, sign, width, lead);
130    }
131    else
132    {
133      BSP_output_char(*fmt);
134    }
135  }
136  /* _CPU_ISR_Enable(level); */
137
138} /* vprintk */
139
140void
141printk(char *fmt, ...)
142{
143  va_list  ap;      /* points to each unnamed argument in turn */
144
145  va_start(ap, fmt); /* make ap point to 1st unnamed arg */
146  vprintk(fmt, ap);
147  va_end(ap); /* clean up when done */
148} /* printk */
Note: See TracBrowser for help on using the repository browser.