Path: utzoo!utgpu!water!watmath!clyde!att!rutgers!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm From: tholm@uvicctr.UUCP (Terrence W. Holm) Newsgroups: comp.os.minix Subject: memcpy(3) & friends Message-ID: <496@uvicctr.UUCP> Date: 14 Sep 88 01:15:27 GMT Reply-To: tholm@uvicctr.UUCP (Terrence W. Holm) Organization: University of Victoria, Victoria B.C. Canada Lines: 282 EFTH MINIX report #40 - September 1988 - memcpy(3) & friends There follows an implementation of memcpy(3), memccpy(3), memchr(3), memcmp(3) and memset(3) for MINIX. Please consider this public domain software. "man" pages are included. ---------------------------------------------------------- echo x - memccpy.3 gres '^X' '' > memccpy.3 << '/' XSUBROUTINES X memccpy(3) - memory copy, until specified character X XINVOCATION X #include X X char *memccpy( to, from, chr, count ) X char *to; X char *from; X int chr; X int count; X XEXPLANATION X characters are copied from the memory pointed to by X to the memory pointed to by . If the character X is encountered then the copy stops, after is copied. X XRESULTS X NULL : was not found. X o/w : Points to the character after in vector. / echo x - memccpy.c gres '^X' '' > memccpy.c << '/' X/* memccpy(3) X * X * Author: Terrence W. Holm Sep. 1988 X */ X X#define NULL (char *) 0 X X Xchar *memccpy( to, from, chr, count ) X char *to; X char *from; X int chr; X int count; X X { X while( --count >= 0 ) X if ( (*to++ = *from++) == chr ) X return( from ); X X return( NULL ); X } / echo x - memchr.3 gres '^X' '' > memchr.3 << '/' XSUBROUTINES X memchr(3) - memory search X XINVOCATION X #include X X char *memchr( vector, chr, count ) X char *vector; X int chr; X int count; X XEXPLANATION X Memchr(3) looks for the character within the first X characters of . X XRESULTS X NULL : was not found. X o/w : Points to in . X XREFERENCES X strchr(3) / echo x - memchr.c gres '^X' '' > memchr.c << '/' X/* memchr(3) X * X * Author: Terrence W. Holm Sep. 1988 X */ X X#define NULL (char *) 0 X X Xchar *memchr( vector, chr, count ) X char *vector; X int chr; X int count; X X { X while( --count >= 0 ) X if ( *vector++ == chr ) X return( vector - 1 ); X X return( NULL ); X } / echo x - memcmp.3 gres '^X' '' > memcmp.3 << '/' XSUBROUTINES X memcmp(3) - memory compare X XINVOCATION X int memcmp( vector1, vector2, count ) X char *vector1; X char *vector2; X int count; X XEXPLANATION X Memcmp(3) uses character comparison to determine if X the data pointed to by is less, equal or X greater than the data pointed to by . Up to X characters are compared. X XRESULTS X <0 : is less than X =0 : is equal to X >0 : is greater than X XREFERENCES X bcmp(3), strcmp(3) / echo x - memcmp.c gres '^X' '' > memcmp.c << '/' X/* memcmp(3) X * X * Author: Terrence W. Holm Sep. 1988 X */ X X Xint memcmp( vector1, vector2, count ) X char *vector1; X char *vector2; X int count; X X { X register int cmp; X X if ( vector1 == vector2 ) X return( 0 ); X X while( --count >= 0 ) X if ( cmp = *vector1++ - *vector2++ ) X return( cmp ); X X return( 0 ); X } / echo x - memcpy.3 gres '^X' '' > memcpy.3 << '/' XSUBROUTINES X memcpy(3) - memory copy X XINVOCATION X #include X X char *memcpy( to, from, count ) X char *to; X char *from; X int count; X XEXPLANATION X characters are copied from the memory pointed X to by to the memory pointed to by . If the X location is within the vector, then X the copy is done backwards to save the vector X from being trampled. X XRESULTS X Returns . X XREFERENCES X bcopy(3), strcpy(3) / echo x - memcpy.c gres '^X' '' > memcpy.c << '/' X/* memcpy(3) X * X * Author: Terrence W. Holm Sep. 1988 X */ X X Xchar *memcpy( to, from, count ) X char *to; X char *from; X int count; X X { X int word_count = count / sizeof(int); X int byte_count = count & ( sizeof(int) - 1 ); X char *temp_to = to; X X if ( to > from && to < from + count ) X { X /* Must copy backwards */ X from += count; X to += count; X X while( --byte_count >= 0 ) X *--to = *--from; X X while( --word_count >= 0 ) X *--((int *) to) = *--((int *) from); X } X else X { X while( --word_count >= 0 ) X *((int *) to)++ = *((int *) from)++; X X while( --byte_count >= 0 ) X *to++ = *from++; X } X X return( temp_to ); X } / echo x - memory.h gres '^X' '' > memory.h << '/' X/* memory.h */ X Xchar *memcpy(); Xchar *memccpy(); Xchar *memchr(); Xchar *memset(); Xint memcmp(); / echo x - memset.3 gres '^X' '' > memset.3 << '/' XSUBROUTINES X memset(3) - memory initialize X XINVOCATION X #include X X char *memset( vector, chr, count ) X char *vector; X int chr; X int count; X XEXPLANATION X The first characters of are set to . X XRESULTS X Returns . X XREFERENCES X bzero(3) / echo x - memset.c gres '^X' '' > memset.c << '/' X/* memset(3) X * X * Author: Terrence W. Holm Sep. 1988 X */ X X Xchar *memset( vector, chr, count ) X char *vector; X int chr; X int count; X X { X register char *memory = vector; X X while( --count >= 0 ) X *memory++ = chr; X X return( vector ); X } / ---------------------------------------------------------- Edwin L. Froese uw-beaver!ubc-cs!mprg!handel!froese Terrence W. Holm uw-beaver!uvicctr!tholm