#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

//
// test nxt_token() and kenw_ultoa()
//

char *
nxt_token( char *p, int n ) {
    char *pend;

    pend = p + n;

    while( p<pend ) {		// find SP char
    	if( *p == ' ' )	break;
	p++;
    }
    if( p >= pend )	return 0;
    ++p;

    while( p<pend ) {		// scan past SP characters
    	if( *p == ' ' )	++p;
	else		break;
    }

    if( p >= pend )	return 0;
    else		return p;
}

// 2^32 = 4,294,967,296 (10 digits)
//
// Convert from unsigned long to left-justified ascii null-terminated string,
// and return number of digits (#bytes = ndigits+1).  Return -1 if number
// won't fit in n-1 bytes.
//
int
kenw_ultoa(	unsigned long x, char *p, int n ) {
    int k;
    int r;
    char *pend;
    int ndigits;

    ndigits = 0;

    pend = p + n - 1;
    *pend = '\0';
    --pend;

    while( pend >= p ) {
    	if( x > 0 ) {
    	    r = x%10;
	    *pend = '0' + r;
	    ++ndigits;
	} else {
	    *pend = ' ';
	}
	x = x/10;
    	--pend;
    }
    if( x > 0 )		return -1;

    pend = p + n - ndigits - 1;
    for( k=0; k<(ndigits+1); k++) {
    	*p = *pend;
	p++;
	pend++;
    }
    *p = '\0';
    return ndigits;
}

unsigned long
kenw_atou(	char *p ) {
    unsigned long x = 0;
    int		  y;

    while( *p != '\0' ) {
    	if( *p != ' ' )	{
    	    x *= 10;
    	    y = *p - '0';
	    if( (y<0) || (y>9) )	return 0;
	    x += y;
	}
	p++;
    }
    return x;
}

void
kenw_ulong2hex( unsigned long x, char hexstr[8] ) {
    unsigned int c;

    c = x>>28;
    if( c < 10 )	hexstr[0] = '0' + c;
    else		hexstr[0] = 'a' + c - 10;
    c = x>>24 & 0xf;
    if( c < 10 )	hexstr[1] = '0' + c;
    else		hexstr[1] = 'a' + c - 10;
    c = x>>20 & 0xf;
    if( c < 10 )	hexstr[2] = '0' + c;
    else		hexstr[2] = 'a' + c - 10;
    c = x>>16 & 0xf;
    if( c < 10 )	hexstr[3] = '0' + c;
    else		hexstr[3] = 'a' + c - 10;
    c = x>>12 & 0xf;
    if( c < 10 )	hexstr[4] = '0' + c;
    else		hexstr[4] = 'a' + c - 10;
    c = x>>8 & 0xf;
    if( c < 10 )	hexstr[5] = '0' + c;
    else		hexstr[5] = 'a' + c - 10;
    c = x>>4 & 0xf;
    if( c < 10 )	hexstr[6] = '0' + c;
    else		hexstr[6] = 'a' + c - 10;
    c = x & 0xf;
    if( c < 10 )	hexstr[7] = '0' + c;
    else		hexstr[7] = 'a' + c - 10;
}

int
main( ) {
    unsigned long
    		x = 123;
    char	buf[10];
    char	*p;
    char	msg[] = "this   is  a    test";
    void	*y;
    char	hex[9];
    int		rc;

    rc = kenw_ultoa( x, buf, 10 );
    if( rc < 0 )	printf( "*** kenw_ultoa returned rc = %d when n = %d\n",
	    								rc, 10 );
    else		printf( "buf = <%s>, rc = %d\n", buf, rc );

    rc = kenw_ultoa( x, buf, 2 );
    if( rc < 0 )	printf( "*** kenw_ultoa returned rc = %d when n = %d\n",
    									rc, 2 );
    else		printf( "buf = <%s>, rc = %d\n", buf, rc );

    x = 0;
    x = kenw_atou( buf );
    printf( "x = %d\n", x );

    p = msg;
    do {
    	printf( "===\n" );
    	p = nxt_token( p, strlen(p) );
	if ( p != 0 )	printf( "%s\n", p );
    } while( p != 0 );

    hex[8] = '\0';
    y = (void *)msg;
    printf( " msg(hex) = %p\n", msg );
    kenw_ulong2hex( (unsigned long) y, hex );
    printf( "hex <%s> vs ptr <%p>\n", hex, msg );

    return 0;
}

