EE445M RTOS
Taken at the University of Texas Spring 2015
nexus.c
Go to the documentation of this file.
1 /* -*- mode: c; c-basic-offset: 4; -*- */
2 #include "nexus.h"
3 
4 void* memset(void* b, int c, int len) {
5 
6  int i;
7  unsigned char *p = b;
8  i = 0;
9  while(len > 0) {
10  *p = c;
11  ++p;
12  --len;
13  }
14  return b;
15 }
16 
17 uint32_t ustrlen(const char* s) {
18  uint32_t len = 0;
19  while(s[len]) { ++len; }
20  return(len);
21 }
22 
23 /* TODO: sanitize long to a type of specific length */
24 /* TODO: use a duff device (speed is the name of the game, remember?) */
25 void *memcpy(void *str1, const void *str2, long n) {
26 
27  long i = 0;
28  uint8_t *dest8 = (uint8_t*)str1;
29  uint8_t *source8 = (uint8_t*)str2;
30  for (i=0; i<n; ++i) {
31  dest8[i] = source8[i];
32  }
33 }
34 
35 int strcmp(const char* s1, const char* s2) {
36 
37  return(ustrncmp(s1, s2, (uint32_t)-1));
38 }
39 
40 int ustrncmp(const char *s1, const char *s2, uint32_t n) {
41 
42  /* Loop while there are more characters. */
43  while(n) {
44  /* If we reached a NULL in both strings, they must be equal so
45  * we end the comparison and return 0 */
46  if(!*s1 && !*s2) {
47  return(0);
48  }
49 
50  /* Compare the two characters and, if different, return the
51  * relevant return code. */
52  if(*s2 < *s1) {
53  return(1);
54  }
55  if(*s1 < *s2) {
56  return(-1);
57  }
58 
59  /* Move on to the next character. */
60  s1++;
61  s2++;
62  n--;
63  }
64  /* If we fall out, the strings must be equal for at least the
65  * first n characters so return 0 to indicate this. */
66  return 0;
67 }
68 
69 void ustrcpy(char* dest, const char* source) {
70  uint32_t i = 0;
71  while (1) {
72  dest[i] = source[i];
73  if (dest[i++] == '\0') { break; }
74  }
75 }
void ustrcpy(char *dest, const char *source)
Definition: nexus.c:69
int strcmp(const char *s1, const char *s2)
Definition: nexus.c:35
uint32_t ustrlen(const char *s)
Definition: nexus.c:17
int ustrncmp(const char *s1, const char *s2, uint32_t n)
Definition: nexus.c:40
void * memset(void *b, int c, int len)
Definition: nexus.c:4
void * memcpy(void *str1, const void *str2, long n)
Definition: nexus.c:25