[go: up one dir, main page]

File: printf.c

package info (click to toggle)
silo 0.8.5-2.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,016 kB
  • ctags: 2,061
  • sloc: ansic: 10,060; asm: 2,319; makefile: 351; perl: 74; sh: 3
file content (116 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download
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
/* Dumb printing routines
   
   Copyright (C) 1996 Pete A. Zaitcev
   Copyright (C) 1997 Jakub Jelinek
   
   This program 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 of the License, or
   (at your option) any later version.
   
   This program 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 program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "promlib.h"

/*
 * This part is rewritten by Igor Timkin <ivt@msu.su>. Than I
 * rewritten it again but kept the MISS style, originated from
 * Wladimir Butenko. --P3
 */
#define OBSIZE      200

static void putchar_1 (char c)
{
    /* P3: This buffer can be static while xprintf flushes it on exit. */
    static char buff[OBSIZE + 1];
    static int ox;

    if ((buff[ox] = c) == 0) {
	prom_puts (buff, ox);
	ox = 0;
    } else {
	if (++ox >= OBSIZE) {
	    buff[ox] = 0;
	    prom_puts (buff, ox);
	    ox = 0;
	}
    }
}

void putchar (char c)
{
    if (c == '\n')
	putchar_1 ('\r');
    putchar_1 (c);
}

/*
 * Print an unsigned integer in base b, avoiding recursion.
 */
static void printn (long n, int b)
{
    char prbuf[24];
    register char *cp;

    if (b == 10 && n < 0) {
	putchar ('-');
	n = -n;
    }
    cp = prbuf;
    do
	*cp++ = "0123456789ABCDEF"[(unsigned int) (((unsigned long)n) % b)];
    while (n = ((unsigned long)n) / b & 0x0FFFFFFF);
    do
	putchar (*--cp);
    while (cp > prbuf);
}

void vprintf (char *fmt, va_list adx)
{
    register c;
    char *s;

    for (;;) {
	while ((c = *fmt++) != '%') {
	    if (c == '\0') {
		putchar (0);
		return;
	    }
	    putchar (c);
	}
	c = *fmt++;
	if (c == 'd' || c == 'o' || c == 'x' || c == 'X') {
	    printn ((long) va_arg (adx, unsigned),
		    c == 'o' ? 8 : (c == 'd' ? 10 : 16));
	} else if (c == 'c') {
	    putchar (va_arg (adx, unsigned));
	} else if (c == 's') {
	    s = va_arg (adx, char *);
	    while (c = *s++)
		putchar (c);
	} else if (c == 'l' || c == 'O') {
	    printn ((long) va_arg (adx, long), c == 'l' ? 10 : 8);
	}
    }
}

/*
 * Scaled down version of C Library printf.
 * Only %c %s %u %d (==%u) %o %x %D %O are recognized.
 */

void prom_printf (char *fmt,...)
{
    va_list x1;

    va_start (x1, fmt);
    vprintf (fmt, x1);
    va_end (x1);
}