home *** CD-ROM | disk | FTP | other *** search
- /* Miscellaneous machine independent utilities */
-
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include "global.h"
- #include "misc.h"
-
- /* Convert hex-ascii to integer */
- int htoi(char *s)
- {
- int i = 0;
- char c;
-
- while((c = *s++) != '\0'){
- if(c == 'x')
- continue; /* allow 0x notation */
- if('0' <= c && c <= '9')
- i = (i * 16) + (c - '0');
- else if('a' <= c && c <= 'f')
- i = (i * 16) + (c - 'a' + 10);
- else if('A' <= c && c <= 'F')
- i = (i * 16) + (c - 'A' + 10);
- else
- break;
- }
- return i;
- }
- /* replace terminating end of line marker(s) with null */
- void rip(register char *s)
- {
- register char *cp;
-
- if((cp = strchr(s,'\r')) != NULLCHAR)
- *cp = '\0';
- if((cp = strchr(s,'\n')) != NULLCHAR)
- *cp = '\0';
- }
-
- /* Case-insensitive string comparison */
- int strnicmp(register char *a, register char *b, register int n)
- {
- char a1, b1;
-
- while (n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0')
- {
- if (a1 == b1)
- continue; /* No need to convert */
- a1 = tolower(a1);
- b1 = tolower(b1);
- if (a1 == b1)
- continue; /* Now they match */
- if (a1 > b1)
- return(1);
- if (a1 < b1)
- return(-1);
- }
-
- return(0);
- }
- /* Copy a string into a dynamically allocated piece of memory */
- char *strdup(char *s)
- {
- char *p;
-
- if ((p = malloc(strlen(s) + 1)) == NULL)
- return(NULL);
-
- strcpy(p, s);
-
- return(p);
- }
- /* Put a long in host order into a char array in network order */
- char *put32(register char *cp, int32 x)
- {
- *cp++ = uchar(x >> 24);
- *cp++ = uchar(x >> 16);
- *cp++ = uchar(x >> 8);
- *cp++ = uchar(x);
-
- return(cp);
- }
-
- /* Put a short in host order into a char array in network order */
- char *put16(register char *cp, int16 x)
- {
- *cp++ = uchar(x >> 8);
- *cp++ = uchar(x);
-
- return(cp);
- }
-
- /* Machine independant, alignment insensitive network-to-host short conversion */
- int16 get16(register char *cp)
- {
- register int16 x;
-
- x = uchar(*cp++);
- x <<= 8;
- x |= uchar(*cp);
-
- return(x);
- }
-
- /* Machine independant, alignment insensitive network-to-host short conversion */
- int32 get32(register char *cp)
- {
- register int32 x;
-
- x = uchar(*cp++);
- x <<= 8;
- x |= uchar(*cp++);
- x <<= 8;
- x |= uchar(*cp++);
- x <<= 8;
- x |= uchar(*cp);
-
- return(x);
- }
-