summaryrefslogtreecommitdiff
path: root/src/user/knob/block.c
blob: 94bd073844e2de9815b0c5a3257273e9250a9821 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdbool.h>
#include <stdint.h>
#include <knob/heap.h>

//unsophisticated, should copy by dwords where available
void blockcpy(void *to, const void *from, uint32_t size) {
  for (uint32_t i = 0; i < size; ++i)
    *(uint8_t *)(to++) = *(const uint8_t *)(from++);
}

//unsophisticated, should check by dwords wheere available
__attribute__ ((__pure__))
bool blockequ(const void *a, const void *b, uint32_t size) {
  for (uint32_t i = 0; i < size; ++i)
    if (*(uint8_t *)(a++) != *(uint8_t *)(b++))
      return false;
  return true;
}

//returns length without null-terminator
uint32_t strcpy(char *to, const char *from) {
  uint32_t i = 0;
  do
    to[i] = from[i];
  while (from[i++]);
  return i - 1;
}

char *strdup(const char *from) {
  const char *end = from;
  while (*(end++))
    ;
  char *buf = get_block(end - from);
  blockcpy(buf, from, end - from);
  return buf;
}