1 /* bug-2684.c
2    A bug in the interface between SDCC and tdlib.
3  */
4 
5 #include <testfwk.h>
6 
7 #pragma disable_warning 85
8 #pragma disable_warning 196
9 
10 /* printf.c
11  *    Dale Schumacher                         399 Beacon Ave.
12  *    (alias: Dalnefre')                      St. Paul, MN  55104
13  *    dal@syntel.UUCP                         United States of America
14  *
15  * Altered to use stdarg, made the core function vfprintf.
16  * Hooked into the stdio package using 'inside information'
17  * Altered sizeof() assumptions, now assumes all integers except chars
18  * will be either
19  *  sizeof(xxx) == sizeof(long) or sizeof(xxx) == sizeof(short)
20  *
21  * -RDB
22  */
23 
24 #include <stdarg.h>
25 #include <string.h>
26 
27 typedef unsigned char uchar;
28 typedef signed long fpos_t;
29 /* when you add or change fields here, be sure to change the initialization
30  * in stdio_init and fopen */
31 struct __stdio_file {
32 	uchar	*bufpos;	/* the next byte to write to or read from */
33 	uchar	*bufread;	/* the end of data returned by last read() */
34 	uchar	*bufwrite;	/* highest address writable by macro */
35 	uchar	*bufstart;	/* the start of the buffer */
36 	uchar	*bufend;	/* the end of the buffer; ie the byte after
37 				   the last malloc()ed byte */
38 	int	fd;		/* the file descriptor associated with the stream */
39 	int	mode;
40 	char	unbuf[8];	/* The buffer for 'unbuffered' streams */
41 	struct __stdio_file * next;
42 };
43 
44 typedef struct __stdio_file FILE;
45 
fputc(int c,FILE * stream)46 int fputc(int c, FILE *stream)
47 {
48 	return (0);
49 }
50 
fflush(FILE * stream)51 int fflush(FILE *stream)
52 {
53 	return (0);
54 }
55 
56 #define _IOLBF		0x01
57 #define _IONBF		0x02
58 #define __MODE_BUF	0x03
59 
__ltostr(long __value,int __radix)60 extern char *__ltostr(long __value, int __radix)
61 {
62 	return ("");
63 }
64 
__ultostr(unsigned long value,int __radix)65 extern char *__ultostr(unsigned long value, int __radix)
66 {
67 	return ("");
68 }
69 
70 #if !defined(__SDCC_pic14) // Pseudo-stack size limit
71 /*
72  * Output the given field in the manner specified by the arguments. Return
73  * the number of characters output.
74  */
prtfld(FILE * op,size_t maxlen,size_t ct,unsigned char * buf,int ljustf,char sign,char pad,int width,int preci,int buffer_mode)75 static int prtfld(FILE * op, size_t maxlen, size_t ct, unsigned char *buf, int ljustf, char sign,
76 		  char pad, int width, int preci, int buffer_mode)
77 {
78 	return 0;
79 }
80 
81 #if !(defined (__SDCC_mcs51) && defined (__SDCC_MODEL_SMALL)) && !defined (__SDCC_pdk14) && !defined (__SDCC_pdk15)
_vfnprintf(FILE * op,size_t maxlen,const char * fmt,va_list ap)82 int _vfnprintf(FILE * op, size_t maxlen, const char *fmt, va_list ap)
83 {
84 	register int i, ljustf, lval, preci, dpoint, width, radix, cnt = 0;
85 	char pad, sign, hash;
86 	register char *ptmp, *add;
87 	unsigned long val;
88 	char tmp[64];
89 	int buffer_mode;
90 
91 	/* This speeds things up a bit for unbuffered */
92 	buffer_mode = (op->mode & __MODE_BUF);
93 	op->mode &= (~__MODE_BUF);
94 	while (*fmt) {
95 		if (*fmt == '%') {
96 			if (buffer_mode == _IONBF)
97 				fflush(op);
98 			ljustf = 0;	/* left justify flag */
99 			sign = '\0';	/* sign char & status */
100 			pad = ' ';	/* justification padding char */
101 			width = -1;	/* min field width */
102 			dpoint = 0;	/* found decimal point */
103 			preci = -1;	/* max data width */
104 			radix = 10;	/* number base */
105 			ptmp = tmp;	/* pointer to area to print */
106 			hash = 0;
107 			lval = (sizeof(int) == sizeof(long));	/* long value flaged */
108 		      fmtnxt:for (i = 0, ++fmt;;
109 			     ++fmt) {
110 				if (*fmt < '0' || *fmt > '9')
111 					break;
112 				i *= 10;
113 				i += (*fmt - '0');
114 				if (dpoint)
115 					preci = i;
116 				else if (!i && (pad == ' ')) {
117 					pad = '0';
118 					goto fmtnxt;
119 				} else
120 					width = i;
121 			}
122 			switch (*fmt) {
123 			case '\0':	/* early EOS */
124 				--fmt;
125 				goto charout;
126 
127 			case '-':	/* left justification */
128 				ljustf = 1;
129 				goto fmtnxt;
130 
131 			case ' ':
132 			case '+':	/* leading sign flag */
133 				sign = *fmt;
134 				goto fmtnxt;
135 
136 			case '#':
137 				hash = 1;
138 				goto fmtnxt;
139 
140 			case '*':	/* parameter width value */
141 				i = va_arg(ap, int);
142 				if (dpoint)
143 					preci = i;
144 				else if ((width = i) < 0) {
145 					ljustf = 1;
146 					width = -i;
147 				}
148 				goto fmtnxt;
149 
150 			case '.':	/* secondary width field */
151 				dpoint = 1;
152 				goto fmtnxt;
153 
154 			case 'l':	/* long data */
155 				lval = 1;
156 				goto fmtnxt;
157 
158 			case 'h':	/* short data */
159 				lval = 0;
160 				goto fmtnxt;
161 
162 			case 'd':	/* Signed decimal */
163 			case 'i':
164 				ptmp = __ltostr((long) ((lval) ?
165 						    va_arg(ap, long) :
166 						    va_arg(ap, int)), 10);
167 				goto printit;
168 
169 			case 'b':	/* Unsigned binary */
170 				radix = 2;
171 				goto usproc;
172 
173 			case 'o':	/* Unsigned octal */
174 				radix = 8;
175 				goto usproc;
176 
177 			case 'p':	/* Pointer */
178 				lval = (sizeof(char *) == sizeof(long));
179 				pad = '0';
180 				width = 5;
181 				preci = 8;
182 				/* fall thru */
183 
184 			case 'X':	/* Unsigned hexadecimal 'ABC' */
185 				radix = 16;
186 				goto usproc;
187 
188 			case 'x':	/* Unsigned hexadecimal 'abc' */
189 				radix = -16;
190 				/* fall thru */
191 
192 			case 'u':	/* Unsigned decimal */
193 			usproc:
194 				val = lval ? va_arg(ap, unsigned long) :
195 				    va_arg(ap, unsigned int);
196 				ptmp = __ultostr(val, radix < 0 ? -radix : radix);
197 				add = "";
198 				if (hash) {
199 					if (radix == 2)
200 						add = "0b";
201 					else if (radix == 8) {
202 						if (val != 0)
203 							add = "0";
204 					} else if (radix == 16)
205 						add = "0x";
206 					else if (radix == -16)
207 						add = "0X";
208 					if (*add) {
209 						pad = '\0';
210 						strcpy(tmp, add);
211 						ptmp = strcat(tmp, ptmp);
212 					}
213 				}
214 				goto printit;
215 
216 			case '!':	/* inline Character */
217 				if ((i = fmt[1]) != 0)
218 					++fmt;
219 				goto Chr;
220 
221 			case 'c':	/* Character */
222 				i = va_arg(ap, int);
223 			      Chr:ptmp[0] =
224 				    i;
225 				ptmp[1] = '\0';
226 				if (hash) {
227 					pad = *ptmp;
228 					goto chrpad;
229 				}
230 				goto nopad;
231 
232 			case 's':	/* String */
233 				ptmp = va_arg(ap, char *);
234 			      nopad:pad =
235 				    ' ';
236 			      chrpad:sign =
237 				    '\0';
238 			      printit:cnt +=
239 				    prtfld(op, maxlen, cnt, (unsigned char *) ptmp,
240 					   ljustf, sign, pad, width, preci,
241 					   buffer_mode);
242 				break;
243 
244 			default:	/* unknown character */
245 				goto charout;
246 			}
247 		} else {
248 		      charout:
249 			/* normal char out */
250 		        if (cnt < maxlen)
251 		                fputc(*fmt, op);
252 			++cnt;
253 			if (*fmt == '\n' && buffer_mode == _IOLBF)
254 				fflush(op);
255 		}
256 		++fmt;
257 	}
258 	op->mode |= buffer_mode;
259 	if (buffer_mode == _IONBF)
260 		fflush(op);
261 	if (buffer_mode == _IOLBF)
262 		op->bufwrite = op->bufstart;
263 	return (cnt);
264 }
265 #endif
266 #endif
267 
testBug(void)268 void testBug(void)
269 {
270 }
271 
272