summaryrefslogtreecommitdiff
path: root/libc/stdio1/old_printf.c
blob: 81d62acf4bba66147ac23474bde01019547172e1 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*   fprintf.c for limited Linux libc
     Copyright (C) 1996  Joel N. Weber II <nemo@koa.iolani.honolulu.hi.us>
     
     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.
 */
/* Thanks Alan for writing the hard routine for me :-)
 * Alan said that this works "most of the time".  Something tells me I'm making
 * it even worse! */
/* The basic idea here is to make fprintf the core routine.  printf obviously
   can just call fprintf with stdout followed by all of its arguments.
   sprintf() works using the fake file &the_sprintf.  It's marked as fully
   buffered, so that it will only write(2) when &the_sprintf->bufpos ==
   &the_sprintf->bufend, which I doubt will happen since &the_sprintf->bufend
   = 0.  The trick is that sprintf must set &the_sprintf->bufstart =
   &the_sprintf->bufpos = its first argument.  Not as orthagonal (is that
   the right word?) as glibc's facilities for non-files, but this isn't a
   library for people who have unlimited RAM budgets.  (not like the libc
   I use on linux/i586 enjoys an unlimited RAM budget either; I only have
   8 MB

   I'm not sure what the "correct" way to pass the variable arguments
   from one function to the next is.  Rather than pass the arguments
   themselves, I'm passing a pointer to them.  However, the following
   explaination from Alan is probably a polite way of saying it will not
   work on a 386 anyway.
   Joel Weber

   [ I've migrated all of this code over to the ELKS stdarg that I wrote.
     The accepted way to do it is for the wrapper function to accept a
     variable number of arguments, use stdarg to make an argument pointer,
     and then pass the argument pointer on to the core function, as I've
     done here.  This should definitely work on a 386, as the arguments
     are still passed in the stack, and stack order is maintained. -Nat ]
 */

/*
 *	This is NOT stunningly portable, but works
 *	for pretty dumb non ANSI compilers and is
 *	tight.	Adjust the sizes to taste.
 *
 *	Illegal format strings will break it. Only the
 *	following simple subset is supported
 *
 *	%x	-	hex
 *	%d	-	decimal
 *	%s	-	string
 *	%c	-	char
 *
 *	And the h/l length specifiers for %d/%x
 *
 *		Alan Cox.
 */

#include <stdarg.h>
#include "stdio.h"

/* 17 to make sure that there's room for the trailing newline.
   I'm not really sure if this is ideal... */
static char nstring[17]="0123456789ABCDEF";

static unsigned char *
__numout(long i, int base)
{
  static unsigned char out[16];
  int n;
  int flg = 0;
  unsigned long val;

  if (i<0 && base==10)
    {
      flg = 1;
      i = -i;
    }
  val = i;

  for (n = 0; n < 15; n++)
    out[n] = ' ';
  out[15] = '\0';
  n = 14;
  do{
    out[n] = nstring[val % base];
    n--;
    val /= base;
  }
  while(val);
  if(flg) out[n--] = '-';
  return &out[n+1];
}

static int
internal_fprintf(FILE * stream, __const char * fmt, va_list ap)
{
  register int c;
  int count = 0;
  int type, base;

  while(c=*fmt++)
    {
      if(c!='%')
	{
	  putc(c, stream);
	  count++;
	}
      else
	{
	  type=1;
	  do { c=*fmt++; } while( c=='.' || (c>='0' && c<='9'));
	  if( c == 0 ) break;
	  if(c=='h')
	    {
	      c=*fmt++;
	      type = 0;
	    }
	  else if(c=='l')
	    {
	      c=*fmt++;
	      type = 2;
	    }

	  switch(c)
	    {
	    case 'x':
	    case 'o':
	    case 'd':
	      if (c=='x') base=16;
	      if (c=='o') base=8;
	      if (c=='d') base=10;
	      {
		long val=0;
		switch(type)
		  {
		  case 0:
		    val=va_arg(ap, short);
		    break; 
		  case 1:
		    val=va_arg(ap, int);
		    break;
		  case 2:
		    val=va_arg(ap, long);
		    break;
		  }
		fputs((__const char *)__numout(val,base),stream);
	      }
	      break;
	    case 's':
	      {
		char *cp;
		cp=va_arg(ap, char *);
		while(*cp)
		    putc(*cp++, stream);
		break;
	      }
	    case 'c':
	      putc(va_arg(ap, int), stream);
	      break;
	    default:
	      putc(c, stream);
	    }
	}
    }
  return count;
}


int
fprintf(FILE * stream, __const char * fmt, ...)
{
  va_list ap;
  int retval;
  va_start(ap, fmt);
  retval=internal_fprintf(stream, fmt, ap);
  va_end(ap);
  return(retval);
}

int
printf(__const char * fmt, ...)
{
  va_list ap;
  int retval;
  va_start(ap, fmt);
  retval=internal_fprintf(stdout, fmt, ap);
  va_end(ap);
  return(retval);
}

/* This is a strange way of doing sprintf, but it should work */
int sprintf(char * s, __const char * fmt, ...)
{
  static FILE the_sprintf = {
    -1,
    0,
    0,
    0,
    0,
    _IOFBF,
    _MODE_WRITE,
    0, 0,
    0, 0};
  va_list ap;
  int retval;

  va_start(ap, fmt);
  the_sprintf.bufstart = the_sprintf.bufpos = (unsigned char *) s;
  the_sprintf.fc_err = 0;

  retval = internal_fprintf(&the_sprintf, fmt, ap);
  /* null-terminate the string */
  putc('\0', &the_sprintf);

  va_end(ap);
  return retval;
}