/* Copyright Motorola, Inc. 1993, 1994 ALL RIGHTS RESERVED You are hereby granted a copyright license to use, modify, and distribute the SOFTWARE so long as this entire notice is retained without alteration in any modified and/or redistributed versions, and that such modified versions are clearly identified as such. No licenses are granted by implication, estoppel or otherwise under any patents or trademarks of Motorola, Inc. The SOFTWARE is provided on an "AS IS" basis and without warranty. To the maximum extent permitted by applicable law, MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED, INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE AND ANY WARRANTY AGAINST INFRINGEMENT WITH REGARD TO THE SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF) AND ANY ACCOMPANYING WRITTEN MATERIALS. To the maximum extent permitted by applicable law, IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE. Motorola assumes no responsibility for the maintenance and support of the SOFTWARE. */ char *qBase; int qSize; int (*qCompare)(); int std_compare(); void std_swap(); void qsort1(); int (*q1Compare)(); void (*q1Swap)(); int strlen(s) char *s; { /* this is our counter so that we can keep track of the number of characters in the string */ int i; /* so lets clear the counter to zero */ i = 0; /* now lets loop until we find a \0 on the the string. We need to increment the pointer each time and the counter each time */ while (*s != '\0') { s ++; i ++; } /* all set!!! now lets return the value to the calling program */ return i; } char *strcpy(s1,s2) char *s1, *s2; { /* this is so that we can return the proper value to the caller when we exit */ char *retvalue; /* it just so happens that s1 is the proper return value */ retvalue = s1; /* now lets loop on s2 until *s2 is equal to '\0' and in this loop we need to copy the value from s2 into s1 and increment both pointers. */ while( *s2 != '\0' ) { *s1=*s2; s1 ++; s2 ++; } /* since we terminated the loop on detecting the NULL char without copying the character we need to place '\0' on s1 so that we have a proper C-string in s1 */ *s1 = '\0'; /* now lets return to the caller what he/she needs */ return retvalue; } char *strrchr(s,c) char *s; int c; { while( (*s != c) && (*s !='\0') ) s++; if(*s == '\0') return 0; return s; } char *strncpy(s1,s2,n) char *s1; char *s2; int n; { /* this is so that we can return the proper value to the caller when we exit */ char *retvalue; /* it just so happens that s1 is the proper return value */ retvalue = s1; /* now lets loop on s2 until *s2 is equal to '\0' and in this loop we need to copy the value from s2 into s1 and increment both pointers. If we have reached the end of s2 then we need to pad s1 until we have reached n characters. NOTICE that we do not increment s2 if we have a '\0' character. This means that we always increment s1 and only substitute '\0' when we have reached it in s2. Since s2 is not incremented at that point or after that point we will be padding the s1 correctly...*/ while( n > 0 ) { if (*s2 != '\0') { *s1 = *s2; s2 ++; } else *s1 = '\0'; s1 ++; n --; } /* since we terminated the loop on detecting the NULL char without copying the character we need to place '\0' on s1 so that we have a proper C-string in s1 */ *s1 = '\0'; /* now lets return to the caller what he/she needs */ return retvalue; } char *strcat(s1,s2) char *s1, *s2; { /* this is to reserve the appropriate return value to the caller */ char *retvalue; /* this value is s1 */ retvalue = s1; /* so lets first find the end of the s1 string we do this by looping until we find th '\0' character at the end of the string */ while( (*s1 != '\0') )/*&& (*s1 != '\c') && (*s1 != '\r') && (*s1 != '\n') ) */ s1++; /* now that we have that lets copy the contents of s2 in to that string. */ strcpy(s1,s2); /* now we can return the original string to the user */ return retvalue; } int strcmp(s1,s2) char *s1, *s2; { /* first thing we need to do is get to a point where the is no equality between the two strings. And if we find that one string is shorter than the other we need to exit also */ while((*s1 == *s2) && (*s1 !='\0') && (*s2 != '\0') ) { s1 ++; s2 ++; } /* now we can evaluate the current characters and respond accordingly */ if(*s1 > *s2) return 1; if(*s1 < *s2) return -1; /* they must be equal at this point */ return 0; } int strncmp(s1,s2,n) char *s1, *s2; int n; { /* first thing we need to do is get to a point where the is no equality between the two strings. And if we find that one string is shorter than the other we need to exit also */ while((*s1 == *s2) && (*s1 !='\0') && (*s2 != '\0') && n > 0 ) { s1 ++; s2 ++; n--;} /* now we can evaluate the current characters and respond accordingly */ if(n == 0) return 0; if(*s1 > *s2) return 1; if(*s1 < *s2) return -1; /* they must be equal at this point */ return 0; } void bzero(s1,n) char* s1; int n; { int count; for(count=0;count s2) { while(n>0) { *s2 = *s1; s2++; s1++; n--; } return retvalue; } else { s1 = (char *)(s1 + n); s2 = (char *)(s2 + n); while(n>0) { *s2 = *s1; s2--; s1--; n--; }; return retvalue; }; } char *memcpy(s1,s2,n) char *s1; char *s2; int n; { char *a; char *b; a = s1; b = s2; for(;n > 0; --n, ++a,++b) *a =*b; return s1; } void *bsearch(key,base,n,size,compare) void *key; void *base; int n; int size; int (*compare)(); { int i = 0, j; int k; void *guess; while (i < n) { j = (i + n - 1) >> 1; guess = (char *) base + j * size; if ((k = (*compare)(key, guess)) == 0) return(guess); if (k < 0) n = j; else i = j + 1; } return 0; } void std_swap(i,j) int i; int j; { int size = qSize; char *p; char *q; char temp; size = qSize; p = qBase + i * size; q = qBase + j * size; for(;size>0;size--) { /* swap the current pair*/ temp = *p; *p=*q; *q = temp; /* now move to the next pair */ p++; q++; }; } void _qsort(n,compare,swap) int n; int (*compare)(); void (*swap)(); { q1Compare = compare; q1Swap = swap; qsort1(0, n); } void qsort1(first,last) int first; int last; { int i; /* "static" to save stack space */ int j; while (last - first > 1) { i = first; j = last; for (;;) { while (++i < last && (*q1Compare)(i, first) < 0) ; while (--j > first && (*q1Compare)(j, first) > 0) ; if (i >= j) break; (*q1Swap)(i, j); } if (j == first) { ++first; continue; } (*q1Swap)(first, j); if (j - first < last - (j + 1)) { qsort1(first, j); first = j + 1; /* qsort1(j + 1, last); */ } else { qsort1(j + 1, last); last = j; /* qsort1(first, j); */ } } } void qsort(base,n,size,compare) void *base; int n; int size; int (*compare)(); { qBase = base; qSize = size; qCompare = compare; _qsort(n, std_compare, std_swap); } int std_compare(i,j) int i; int j; { return((*qCompare)(qBase + i * qSize, qBase + j * qSize)); }