/* Utilities.c from Kernighan & Ritchie */
int strlen(s)          /* p 95: return length of string s */
char *s;
{
int n;
for (n=0; *s != '\r' && *s != '\0'; s++)
n++;
return(n);
}
void reverse(s)         /* p 59: reverse string s in place */
char s[];
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoh (n, s)         /* convert n to hexa characters in s */
char s[];
unsigned n;
{
int i;
char h;
i = 0;
do {           /* generate digits in reverse order */
h = n % 16;        /* get next digit */
s[i++] = (h <= 9) ? h + '0' : h-10 + 'A';
} while ((n /= 16) != 0);     /* delete it */
s[i] = '\0';
reverse(s);
}
int atoi(s)         /* p58 : convert s to an integer */
char s[];
{
int i, n, sign;
for (i=0; s[i]==' ' || s[i]=='\n' || s[i]=='\t'; i++)
;           /* skip the white space */
sign = 1;
if (s[i] == '+' || s[i] == '-')     /* sign */
sign = (s[i++]=='+') ? 1 : -1;
for (n=0; s[i]>='0' && s[i]<='9'; i++)
n= 10*n + s[i] - '0';
return(sign * n);
}
int htoi(s)         /* convert s (hexa) to an integer */
char s[];
{
int i;
unsigned n;
for (i=0; s[i]==' ' || s[i]=='\n' || s[i]=='\t'; i++)
;           /* skip the white space */
for (n=0; (s[i]>='0' && s[i]<='9') || (s[i]>='A' && s[i]<='B'); i++)
n= (s[i]<=9) ? 16*n + s[i] - '0' : 16*n + s[i] - 'A'+10;
return(n);
}