parent
293f556a7e
commit
1ec003f2c4
@ -1 +0,0 @@
|
||||
../21-shell/cpu
|
@ -1 +0,0 @@
|
||||
../21-shell/drivers
|
@ -1 +0,0 @@
|
||||
../../21-shell/libc/function.h
|
@ -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
|
@ -1 +0,0 @@
|
||||
../../21-shell/libc/string.c
|
@ -0,0 +1,77 @@
|
||||
#include "string.h"
|
||||
#include "../cpu/type.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);
|
||||
}
|
||||
|
||||
void hex_to_ascii(int n, char str[]) {
|
||||
append(str, '0');
|
||||
append(str, 'x');
|
||||
char zeros = 0;
|
||||
|
||||
s32 tmp;
|
||||
int i;
|
||||
for (i = 28; i > 0; i -= 4) {
|
||||
tmp = (n >> i) & 0xF;
|
||||
if (tmp == 0 && zeros == 0) continue;
|
||||
zeros = 1;
|
||||
if (tmp > 0xA) append(str, tmp - 0xA + 'a');
|
||||
else append(str, tmp + '0');
|
||||
}
|
||||
|
||||
tmp = n & 0xF;
|
||||
if (tmp >= 0xA) append(str, tmp - 0xA + 'a');
|
||||
else append(str, tmp + '0');
|
||||
}
|
||||
|
||||
/* 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];
|
||||
}
|
@ -1 +0,0 @@
|
||||
../../21-shell/libc/string.h
|
@ -0,0 +1,12 @@
|
||||
#ifndef STRINGS_H
|
||||
#define STRINGS_H
|
||||
|
||||
void int_to_ascii(int n, char str[]);
|
||||
void hex_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
|
Loading…
Reference in new issue