training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

mystrmem.c (2211B)


      1 /* Exercise 8.32 */
      2 
      3 #include <stdio.h>
      4 
      5 void *memcpy(void *s1, const void *s2, size_t n);
      6 void *memmove(void *s1, const void *s2, size_t n);
      7 int memcmp(const void *s1, const void *s2, size_t n);
      8 void *memchr(const void *s, int c, size_t n);
      9 void *memset(void *s, int c, size_t n);
     10 
     11 int main(void)
     12 {
     13    char strsrc[] = "this is a string!";
     14    char strdst[40] = { 0 }, *strp;
     15    int n;
     16 
     17    strp = memcpy(strdst, strsrc, sizeof(strdst));
     18    printf("memcpy(): %s\n", strp);
     19 
     20    strp = memmove(strdst, "\"ab\"", 4);
     21    printf("memmove(): %s\n", strp);
     22 
     23    n = memcmp(strdst, strsrc, 5);
     24    printf("memcmp(): %d\n", n);
     25 
     26    strp = memchr(strsrc, 'a', sizeof(strsrc));
     27    printf("memchr(): %s\n", strp);
     28 
     29    strp = memset(strdst, 'x', 4);
     30    printf("memset(): %s\n", strdst);
     31 
     32    return 0;
     33 } /* E0F main */
     34 
     35 /* copy n bytes from s2 to s1 */
     36 void *memcpy(void *s1, const void *s2, size_t n)
     37 {
     38    int i;
     39    char *dst = s1;
     40    const char *src = s2;
     41 
     42    for(i = 0; i < (int)n && (*dst++ = *src++) != '\0'; i++)
     43       ; /* Empty body */
     44 
     45    return s1;
     46 } /* eof memcpy() */
     47 
     48 /* copy n bytes from s2 to s1. works even
     49  * if s2 and s1 represents the same array */
     50 void *memmove(void *s1, const void *s2, size_t n)
     51 {
     52    int i;
     53    char *dst = s1, *tmp;
     54    const char *src = s2;
     55 
     56    for(i = 0; i < (int)n; i++)
     57       *(tmp + i) = *(src + i);
     58 
     59    for(i = 0; i < (int)n; i++)
     60       *dst++ = *tmp++;
     61 
     62    return s1;
     63 } /* eof memmove() */
     64 
     65 /* compare n bytes of s1 and s2 */
     66 int memcmp(const void *s1, const void *s2, size_t n)
     67 {
     68    int i;
     69    const char *s = s1, *d = s2;
     70 
     71    long n1 = 0, n2 = 0;
     72 
     73    for(i = 0; i < (int)n && s[i] != '\0' && d[i] != '\0'; i++) {
     74       n1 += s[i];
     75       n2 += d[i];
     76    }
     77 
     78    return n1 - n2;
     79 } /* eof memcmp() */
     80 
     81 /* locate the character c to the string s */
     82 void *memchr(const void *s, int c, size_t n)
     83 {
     84    int i;
     85    char *p = (char *)s;
     86 
     87    for(i = 0; i < (int)n && p[i] != '\0'; i++) {
     88       if( p[i] == (unsigned char)c ) {
     89 	 return &p[i];
     90       }
     91    }
     92 
     93    return NULL;
     94 } /* eof memchr() */
     95 
     96 /* writes n bytes of c to s */
     97 void *memset(void *s, int c, size_t n)
     98 {
     99    int i;
    100    char *p = s;
    101 
    102    for(i = 0; i < (int)n; i++) {
    103       p[i] = (unsigned char)c;
    104    }
    105 
    106    return s;
    107 } /* eof memset() */
    108