Finished Part I, moved part 2 relavent files to own folder.
This commit is contained in:
8
Part2/A4/08_shell/libc/function.h
Executable file
8
Part2/A4/08_shell/libc/function.h
Executable file
@@ -0,0 +1,8 @@
|
||||
#ifndef FUNCTION_H
|
||||
#define FUNCTION_H
|
||||
|
||||
/* Sometimes we want to keep parameters to a function for later use
|
||||
* and this is a solution to avoid the 'unused parameter' compiler warning */
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
#endif
|
13
Part2/A4/08_shell/libc/mem.c
Executable file
13
Part2/A4/08_shell/libc/mem.c
Executable file
@@ -0,0 +1,13 @@
|
||||
#include "mem.h"
|
||||
|
||||
void memory_copy(u8 *source, u8 *dest, int nbytes) {
|
||||
int i;
|
||||
for (i = 0; i < nbytes; i++) {
|
||||
*(dest + i) = *(source + i);
|
||||
}
|
||||
}
|
||||
|
||||
void memory_set(u8 *dest, u8 val, u32 len) {
|
||||
u8 *temp = (u8 *)dest;
|
||||
for ( ; len != 0; len--) *temp++ = val;
|
||||
}
|
9
Part2/A4/08_shell/libc/mem.h
Executable file
9
Part2/A4/08_shell/libc/mem.h
Executable file
@@ -0,0 +1,9 @@
|
||||
#ifndef MEM_H
|
||||
#define MEM_H
|
||||
|
||||
#include "../cpu/types.h"
|
||||
|
||||
void memory_copy(u8 *source, u8 *dest, int nbytes);
|
||||
void memory_set(u8 *dest, u8 val, u32 len);
|
||||
|
||||
#endif
|
56
Part2/A4/08_shell/libc/string.c
Executable file
56
Part2/A4/08_shell/libc/string.c
Executable file
@@ -0,0 +1,56 @@
|
||||
#include "string.h"
|
||||
|
||||
/**
|
||||
* K&R implementation
|
||||
*/
|
||||
void int_to_ascii(int n, char str[]) {
|
||||
int i, sign;
|
||||
if ((sign = n) < 0) n = -n;
|
||||
i = 0;
|
||||
do {
|
||||
str[i++] = n % 10 + '0';
|
||||
} while ((n /= 10) > 0);
|
||||
|
||||
if (sign < 0) str[i++] = '-';
|
||||
str[i] = '\0';
|
||||
|
||||
reverse(str);
|
||||
}
|
||||
|
||||
/* K&R */
|
||||
void reverse(char s[]) {
|
||||
int c, i, j;
|
||||
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
|
||||
c = s[i];
|
||||
s[i] = s[j];
|
||||
s[j] = c;
|
||||
}
|
||||
}
|
||||
|
||||
/* K&R */
|
||||
int strlen(char s[]) {
|
||||
int i = 0;
|
||||
while (s[i] != '\0') ++i;
|
||||
return i;
|
||||
}
|
||||
|
||||
void append(char s[], char n) {
|
||||
int len = strlen(s);
|
||||
s[len] = n;
|
||||
s[len+1] = '\0';
|
||||
}
|
||||
|
||||
void backspace(char s[]) {
|
||||
int len = strlen(s);
|
||||
s[len-1] = '\0';
|
||||
}
|
||||
|
||||
/* K&R
|
||||
* Returns <0 if s1<s2, 0 if s1==s2, >0 if s1>s2 */
|
||||
int strcmp(char s1[], char s2[]) {
|
||||
int i;
|
||||
for (i = 0; s1[i] == s2[i]; i++) {
|
||||
if (s1[i] == '\0') return 0;
|
||||
}
|
||||
return s1[i] - s2[i];
|
||||
}
|
11
Part2/A4/08_shell/libc/string.h
Executable file
11
Part2/A4/08_shell/libc/string.h
Executable file
@@ -0,0 +1,11 @@
|
||||
#ifndef STRINGS_H
|
||||
#define STRINGS_H
|
||||
|
||||
void int_to_ascii(int n, char str[]);
|
||||
void reverse(char s[]);
|
||||
int strlen(char s[]);
|
||||
void backspace(char s[]);
|
||||
void append(char s[], char n);
|
||||
int strcmp(char s1[], char s2[]);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user