Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

memmove.c (783B)


      1 #include "../stdint.h"
      2 #include "string.h"
      3 
      4 // memmove is a 'overlap-safe' operation of memcpy (memcopy). Implementation of the latter can be found at ./memcpy.c
      5 void *memmove (void *dest, const void *src, size_t n) {
      6     unsigned char *srrc = (unsigned char *)src;
      7     unsigned char *dst = (unsigned char *)dest;
      8 
      9     if (dst > srrc) { // if destination beginning overlaps with srrc[end] or < srrc[end]
     10         // forward logic
     11 
     12         for (size_t i = 0; i < n; i++) {
     13             dst[i] = srrc[i];
     14         }
     15 
     16     } else if (dst < srrc) { // if source beginning overlaps with dest[end] or < dest[end]
     17         // backward logic
     18 
     19         for (size_t i = n; i > 0; i--) {
     20             dst[i - 1] = srrc[i - 1];
     21         }
     22 
     23     }
     24 
     25     return dest; // now it points wherever dst starts
     26 }