All pastes #458923 Raw Edit

Something

public text v1 · immutable
#458923 ·published 2007-04-26 05:31 UTC
rendered paste body
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RWRITE 2
#define O_APPEND 3

int getc()
{
	int c;
	/*fd, buffer, numBytes*/
	read(0, &c, 1);
	if (c==4) return 0; /*Control_D key*/
	return (c & 0x7F);
}/*end int getc()*/
 
int putc(int c)
{
	/*fd, buffer, numBytes*/
	write(1, &c, 1);
	/*I added this next line*/
	return 1;
}/*end putc(int c)*/

int gets(char* s)
{
	/*fd, buffer, numBytes*/
	return read(0, s, 1024);
}/*end int gets(char* s)*/


prints(char* s)
{
	/*fd, buffer, numBytes*/
	write(1, s, strlen(s));
}/*end prints(char* )*/

/**** YOUR printf(), etc. *****/
int printchar(int x)
{
	putc( x );
	return 0;
}/*end printchar*/

int printint(int x)
{
	if(0 == x)
	{
		/*putc('0');*/
	}/*omit extra zeros*/
	else
	{
		printint( x / 10 );
		putc('0' + (x % 10) );
	}/**/
	return 0;
}/*end printint*/

int printhex(int x)
{
	if(x>15)
	{
		printhex(x/16);
	}/*end if greater than one byte*/
	if((x%16)<10)
	{
		putc('0' + (x%16));
	}/*end if less than 10, print integer*/
	else
	{
		putc('A' - 10 + (x%16));
	}/*end if 10 or greater, do alphabetic stuff*/
	return 0;
}/*end printhex*/

/*the big boy! formatted print!*/
int printf(char* s)
{
	char theChar;
	char argType;
/*	int* bp = getbp();
	originally used getbp()...but during debugging I changed it
	to this and this works...so why not keep it? */
	int* bp = &s;
	/*first arg is at bp+3, so set this to 2 and let
	the rest of the code increment as needed*/
	int stackOffset = 2;
	while(*s)
	{
		/*get char and advance*/
		theChar = *s++;
		if('%' == theChar)
		{
			/*get char and advance*/
			argType = *s++;
			if('c' == argType)
			{
				printchar( *(bp+(++stackOffset)) );
			}/*end if is char, print it*/
			else if('d' == argType)
			{
				printint( *(bp+(++stackOffset)) );
			}/*end int types*/
			else if('x' == argType)
			{
				printhex( *(bp+(++stackOffset)) );
			}/*end hex type*/
			else if('s' == argType)
			{
				prints( *(bp+(++stackOffset)) );
			}/*end string case*/
			else
			{
				prints("myprintf blew up, bad arg type");
			}/*end bad arg type*/
		}/*end 'if is an escape character'*/
		else
		{
			/*putc(*s++);*/
			putc(theChar);
		}/*end 'not an escape character'*/
		
	}/*end while*/
}/*end prints*/

/**************END STRING FUNCTIONS*****************/