~ubuntu-branches/ubuntu/hardy/silo/hardy-updates

« back to all changes in this revision

Viewing changes to common/printf.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2007-10-25 09:28:08 UTC
  • mfrom: (15.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071025092808-1yhj12t7s4zqsfu5
Tags: 1.4.13a+git20070930-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build with -fno-stack-protector.
  - Change silo.postinst to automatically update the boot block without
    invoking siloconfig and keep asking questions on upgrades.
  - Convert silo.conf to use /dev/disk/by-uuid.
  - Ubuntu maintainer foobar.
  - Fix debian/rules call to dh_installdocs.
  - Drop the requirement of gcc-4.1 and start using default gcc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Dumb printing routines
 
2
   
 
3
   Copyright (C) 1996 Pete A. Zaitcev
 
4
                 1997 Jakub Jelinek
 
5
                 2001 Ben Collins
 
6
   
 
7
   
 
8
   This program is free software; you can redistribute it and/or modify
 
9
   it under the terms of the GNU General Public License as published by
 
10
   the Free Software Foundation; either version 2 of the License, or
 
11
   (at your option) any later version.
 
12
   
 
13
   This program is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with this program; if not, write to the Free Software
 
20
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 
21
   USA.  */
 
22
 
 
23
#include "promlib.h"
 
24
 
 
25
/*
 
26
 * This part is rewritten by Igor Timkin <ivt@msu.su>. Than I
 
27
 * rewritten it again but kept the MISS style, originated from
 
28
 * Wladimir Butenko. --P3
 
29
 */
 
30
#define OBSIZE      200
 
31
 
 
32
#ifndef NULL
 
33
#define NULL (void *)0
 
34
#endif
 
35
 
 
36
static void putchar_1 (char c)
 
37
{
 
38
    /* P3: This buffer can be static while xprintf flushes it on exit. */
 
39
    static char buff[OBSIZE + 1];
 
40
    static int ox;
 
41
 
 
42
    if ((buff[ox] = c) == 0) {
 
43
        prom_puts (buff, ox);
 
44
        ox = 0;
 
45
    } else {
 
46
        if (++ox >= OBSIZE) {
 
47
            buff[ox] = 0;
 
48
            prom_puts (buff, ox);
 
49
            ox = 0;
 
50
        }
 
51
    }
 
52
}
 
53
 
 
54
int putchar (int __c)
 
55
{
 
56
    char c = (char) __c;
 
57
    if (c == '\n')
 
58
        putchar_1 ('\r');
 
59
    putchar_1 (c);
 
60
 
 
61
    return __c;
 
62
}
 
63
 
 
64
/*
 
65
 * Print an unsigned integer in base b, avoiding recursion.
 
66
 */
 
67
static int printn (long long n, int b)
 
68
{
 
69
    static char prbuf[33];
 
70
    register char *cp;
 
71
    int count = 0;
 
72
 
 
73
    if (b == 10 && n < 0) {
 
74
        putchar ('-');
 
75
        count++;
 
76
        n = -n;
 
77
    }
 
78
    cp = prbuf;
 
79
    do
 
80
        *cp++ = "0123456789ABCDEF"[(unsigned int) (((unsigned long)n) % b)];
 
81
    while ((n = ((unsigned long long)n) / b & 0x0FFFFFFFFFFFFFFFULL));
 
82
    do {
 
83
        putchar (*--cp);
 
84
        count++;
 
85
    } while (cp > prbuf);
 
86
 
 
87
    return count;
 
88
}
 
89
 
 
90
int vprintf (char *fmt, va_list adx)
 
91
{
 
92
    register int c;
 
93
    char *s;
 
94
    int count = 0;
 
95
 
 
96
    for (;;) {
 
97
        while ((c = *fmt++) != '%') {
 
98
            if (c == '\0') {
 
99
                putchar (0);
 
100
                return count;
 
101
            }
 
102
            putchar (c);
 
103
        }
 
104
        c = *fmt++;
 
105
        if (c == 'd' || c == 'o' || c == 'x' || c == 'X') {
 
106
            count += printn ((long long) va_arg (adx, unsigned),
 
107
                             c == 'o' ? 8 : (c == 'd' ? 10 : 16));
 
108
        } else if (c == 'c') {
 
109
            putchar (va_arg (adx, unsigned));
 
110
            count++;
 
111
        } else if (c == 's') {
 
112
            if ((s = va_arg (adx, char *)) == NULL)
 
113
                s = (char *)"(null)";
 
114
            while ((c = *s++)) {
 
115
                putchar (c);
 
116
                count++;
 
117
            }
 
118
        } else if (c == 'l' || c == 'O') {
 
119
            count += printn ((long long) va_arg (adx, long), c == 'l' ? 10 : 8);
 
120
        } else if (c == 'L') {
 
121
            int hex = 0;
 
122
            if (*fmt == 'x') {
 
123
                fmt++;
 
124
                hex = 1;
 
125
            }
 
126
            count += printn ((long long) va_arg (adx, long long), hex ? 16 : 10);
 
127
        } else {
 
128
            /* This is basically what libc's printf does */
 
129
            putchar('%'); putchar(c);
 
130
            count += 2;
 
131
        }
 
132
    }
 
133
 
 
134
    return count;
 
135
}
 
136
 
 
137
/*
 
138
 * Scaled down version of C Library printf.
 
139
 * Only %c %s %d (==%u) %o %x %X %l %O are recognized.
 
140
 */
 
141
 
 
142
void prom_printf (char *fmt,...)
 
143
{
 
144
    va_list x1;
 
145
 
 
146
    va_start (x1, fmt);
 
147
    vprintf (fmt, x1);
 
148
    va_end (x1);
 
149
}