Exemple #1
0
/**
 * Print a number to the given string, with the given base.
 */
static int __print_int( _print_ctx_t* ctx,
			int i,
			int b,
			int sg,
			int width,
			int pad,
			int letbase,
			int print_limit )
{
	char print_buf[_PRINTFMT_INT_BUF_LEN];
	register char *s;
	register int t, neg = 0, pc = 0;
	register unsigned int u = i;

	if( i == 0 ) {
		print_buf[0] = '0';
		print_buf[1] = '\0';
		return __print_str( ctx, print_buf, width, pad, print_limit, true );
	}

	if( sg && b == 10 && i < 0 ) {
		neg = 1;
		u = -i;
	}

	s = print_buf + _PRINTFMT_INT_BUF_LEN - 1;
	*s = '\0';

	while( u ) {
		t = u % b;
		if( t >= 10 )
			t += letbase - '0' - 10;
		*--s = t + '0';
		u /= b;
	}

	if( neg ) {
		if( width && ( pad & _PRINTFMT_PAD_ZERO ) ) {
			__print_char( ctx, '-' );
			++pc;
			--width;
		} else {
			*--s = '-';
		}
	}

	return pc + __print_str( ctx, s, width, pad, print_limit, true );
}
Exemple #2
0
int main (int argc, char *argv[]) {
  if (argc != 2) {
    __print_sw_title(argv[0]);
    return ERROR;
  }
  else {
    __print_str(argv);
  }

  return OK;
}
Exemple #3
0
/**
 * Print the given arguments, with given format onto string out.
 */
static int __print_fmt( _print_ctx_t* ctx, const char *format, va_list args )
{
	int	width;
	int	pad;
	int	print_limit;
	int	pc = 0;
	char	scr[2];

	for( ; *format != 0; ++format ) {
		if( *format == '%' ) {
			++format;
			width = pad = print_limit = 0;

			if( *format == '\0' ) {
				break;
			}

			if( *format == '%' ) {
				goto out;
			}

			if( *format == '-' ) {
				++format;
				pad = _PRINTFMT_PAD_RIGHT;
			}

			while( *format == '0' ) {
				++format;
				pad |= _PRINTFMT_PAD_ZERO;
			}

			for( ; *format >= '0' && *format <= '9'; ++format ) {
				width *= 10;
				width += *format - '0';
			}

			if( *format == '.' ) {
				++format;
				for( ; *format >= '0' && *format <= '9'; ++format ) {
					print_limit *= 10;
					print_limit += *format - '0';
				}
			}

			if( 0 == print_limit ) {
				print_limit--;
			}

			if( *format == 'l' ) {
				++format;
			}

			if( *format == 's' ) {
				register char *s = (char *) va_arg( args, int );
				pc += __print_str( 	ctx,
							s ? s : "(null)",
							width,
							pad,
							print_limit,
							false );
				continue;
			}

			if( *format == 'd' ) {
				pc += __print_int( ctx, va_arg( args, int ), 10, 1, width, pad, 'a', print_limit );
				continue;
			}

			if( ( *format == 'x' ) || ( *format == 'p' ) ) {
				pc += __print_int( ctx, va_arg( args, int ), 16, 0, width, pad, 'a', print_limit );
				continue;
			}