source: rtems/cpukit/libcsupport/src/vprintk.c @ eebcc5b

4.104.115
Last change on this file since eebcc5b was 7c411bd, checked in by Joel Sherrill <joel.sherrill@…>, on 09/14/09 at 14:48:38

2009-09-14 Sebastian Huber <Sebastian.Huber@…>

  • score/src/wkspace.c: Removed work space area consistency checks.
  • libblock/include/rtems/ide_part_table.h: Functions are now deprecated.
  • libcsupport/include/rtems/libcsupport.h, libcsupport/src/calloc.c, libcsupport/src/malloc_boundary.c, libcsupport/src/malloc_initialize.c, libcsupport/src/malloc_report_statistics_plugin.c, libcsupport/src/malloc_statistics_helpers.c, libcsupport/src/malloc_walk.c, libcsupport/src/realloc.c, rtems/inline/rtems/rtems/region.inl: Update for heap API changes.

2009-09-14 Christian Mauderer <christian.mauderer@…>

  • libcsupport/src/vprintk.c: Fixed warnings. Print nothing in case the pointer to the string is NULL.
  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * (C) Copyright 1997 -
3 * - NavIST Group - Real-Time Distributed Systems and Industrial Automation
4 *
5 * http://pandora.ist.utl.pt
6 *
7 * Instituto Superior Tecnico * Lisboa * PORTUGAL
8 *
9 * Disclaimer:
10 *
11 * This file is provided "AS IS" without warranty of any kind, either
12 * expressed or implied.
13 *
14 * This code is based on code by: Jose Rufino - IST
15 *
16 *  $Id$
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <stdarg.h>
24#include <stdio.h>
25#include <rtems/bspIo.h>
26
27static void printNum(
28  long num,
29  int base,
30  int sign,
31  int maxwidth,
32  int lead
33);
34
35/*
36 *  vprintk
37 *
38 *  A simplified version of printf intended for use when the
39 *  console is not yet initialized or in ISR's.
40 *
41 * Arguments:
42 *    as in printf: fmt - format string, ... - unnamed arguments.
43 */
44void vprintk(
45  const char *fmt,
46  va_list     ap
47)
48{
49  char     c;
50  int      lflag, base, sign, width, lead, minus;
51
52  for (; *fmt != '\0'; fmt++) {
53    lflag = 0;
54    base  = 0;
55    sign = 0;
56    width = 0;
57    minus = 0;
58    lead = ' ';
59    if (*fmt != '%') {
60      BSP_output_char(*fmt);
61      continue;
62    }
63    fmt++;
64    if (*fmt == '0' ) {
65      lead = '0';
66      fmt++;
67    }
68    if (*fmt == '-' ) {
69      minus = 1;
70      fmt++;
71    }
72    while (*fmt >= '0' && *fmt <= '9' ) {
73      width *= 10;
74      width += (*fmt - '0');
75      fmt++;
76    }
77
78    if ((c = *fmt) == 'l') {
79      lflag = 1;
80      c = *++fmt;
81    }
82    if ( c == 'c' ) {
83      char chr = (char) va_arg(ap, int);
84      BSP_output_char(chr);
85      continue;
86    }
87    if ( c == 's' ) {
88      int i, len;
89      char *s, *str;
90
91      str = va_arg(ap, char *);
92
93      if ( str == NULL ) {
94        str = "";
95      }
96
97      /* calculate length of string */
98      for ( len=0, s=str ; *s ; len++, s++ )
99        ;
100
101      /* leading spaces */
102      if ( !minus )
103        for ( i=len ; i<width ; i++ )
104          BSP_output_char(' ');
105
106      /* no width option */
107      if (width == 0) {
108          width = len;
109      }
110
111      /* output the string */
112      for ( i=0 ; i<width && *str ; str++ )
113        BSP_output_char(*str);
114
115      /* trailing spaces */
116      if ( minus )
117        for ( i=len ; i<width ; i++ )
118          BSP_output_char(' ');
119
120      continue;
121    }
122
123    /* must be a numeric format or something unsupported */
124    if ( c == 'o' || c == 'O' ) {
125      base = 8; sign = 0;
126    } else if ( c == 'i' || c == 'I' ||
127                c == 'd' || c == 'D' ) {
128      base = 10; sign = 1;
129    } else if ( c == 'u' || c == 'U' ) {
130      base = 10; sign = 0;
131    } else if ( c == 'x' || c == 'X' ) {
132      base = 16; sign = 0;
133    } else if ( c == 'p' ) {
134      base = 16; sign = 0; lflag = 1;
135    } else {
136      BSP_output_char(c);
137      continue;
138    }
139
140    printNum(
141      lflag ? va_arg(ap, long) : (long) va_arg(ap, int),
142      base,
143      sign,
144      width,
145      lead
146    );
147  }
148}
149
150/*
151 * printNum - print number in a given base.
152 * Arguments
153 *    num - number to print
154 *    base - base used to print the number.
155 */
156static void printNum(
157  long num,
158  int base,
159  int sign,
160  int maxwidth,
161  int lead
162)
163{
164  long n;
165  int count;
166  char toPrint[20];
167
168  if ( (sign == 1) && ((long)num <  0) ) {
169    BSP_output_char('-');
170    num = -num;
171    if (maxwidth) maxwidth--;
172  }
173
174  count = 0;
175  while ((n = num / base) > 0) {
176    toPrint[count++] = (char) (num - (n*base));
177    num = n;
178  }
179  toPrint[count++] = (char) num;
180
181  for (n=maxwidth ; n > count; n-- )
182    BSP_output_char((char) lead);
183
184  for (n = 0; n < count; n++) {
185    BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]);
186  }
187}
Note: See TracBrowser for help on using the repository browser.