File: printf.c

package info (click to toggle)
quik 2.1-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 364 kB
  • ctags: 553
  • sloc: ansic: 3,791; asm: 475; makefile: 103; perl: 74; sh: 44
file content (122 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download | duplicates (4)
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
117
118
119
120
121
122
/* Dumb printing routines
   
   Copyright (C) 1996 Pete A. Zaitcev
   
   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 <stdarg.h>
#include "quik.h"
#include "prom.h"

static int printn(long, int,int (*outchar)(int c, void *),void *);
extern int qvprintf(const char *,int (*outchar)(int c, void *), void *,va_list);

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

static inline int myputchar(int c, void *dummy)
{
	return putchar(c);
}

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

    va_start (x1, fmt);
    qvprintf (fmt, myputchar, NULL, x1);
    va_end (x1);
}

static int putbyte(int c, void *buffer)
{

    char **buf=(char **)buffer;

    *(buf[0]++)=c;

    return c;
}

int qsprintf (char *buf, char *fmt,...)
{
	int result;
    va_list x1;

	va_start (x1, fmt);
	result=qvprintf (fmt, putbyte, &buf,x1);
	va_end (x1);

	return result;
}

int qvprintf (const char *fmt, int (*outchar)(int c,void *buffer), 
	      void *buffer,va_list adx)
{
	int c,len=0;
    char *s;

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

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

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

	return len;
}