rulimine/stage2/lib/libc.c

94 lines
1.7 KiB
C
Raw Normal View History

2019-05-31 07:23:23 +03:00
#include <stddef.h>
#include <stdint.h>
#include <lib/libc.h>
2020-11-05 03:37:45 +03:00
#include <limits.h>
#include <stdbool.h>
#include <lib/blib.h>
2019-05-31 07:23:23 +03:00
2020-06-11 13:13:27 +03:00
int toupper(int c) {
if (c >= 'a' && c <= 'z') {
return c - 0x20;
}
return c;
}
int tolower(int c) {
if (c >= 'A' && c <= 'Z') {
return c + 0x20;
}
return c;
}
2019-05-31 07:23:23 +03:00
char *strcpy(char *dest, const char *src) {
size_t i;
2019-05-31 07:23:23 +03:00
for (i = 0; src[i]; i++)
dest[i] = src[i];
dest[i] = 0;
return dest;
}
char *strncpy(char *dest, const char *src, size_t n) {
size_t i;
2019-05-31 07:23:23 +03:00
for (i = 0; i < n && src[i]; i++)
2019-05-31 07:23:23 +03:00
dest[i] = src[i];
for ( ; i < n; i++)
dest[i] = 0;
2019-05-31 07:23:23 +03:00
return dest;
}
int strcmp(const char *s1, const char *s2) {
for (size_t i = 0; ; i++) {
char c1 = s1[i], c2 = s2[i];
if (c1 != c2)
return c1 < c2 ? -1 : 1;
if (!c1)
return 0;
2019-05-31 07:23:23 +03:00
}
}
int strncmp(const char *s1, const char *s2, size_t n) {
for (size_t i = 0; i < n; i++) {
char c1 = s1[i], c2 = s2[i];
if (c1 != c2)
return c1 < c2 ? -1 : 1;
if (!c1)
return 0;
}
2019-05-31 07:23:23 +03:00
return 0;
}
size_t strlen(const char *str) {
size_t len;
for (len = 0; str[len]; len++);
return len;
}
2020-11-05 03:37:45 +03:00
int inet_pton(const char *src, void *dst) {
uint8_t array[4] = {};
const char *current = src;
for (int i = 0; i < 4; i++) {
long int value = strtoui(current, 0, 10);
if (value > 255)
return -1;
for (int j = 0; j < 3; j++) {
if (*current != '\0' && *current != '.')
current++;
else
break;
}
current++;
array[i] = value;
}
memcpy(dst, array, 4);
return 0;
}