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 <stdbool.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2019-05-31 07:23:23 +03:00
|
|
|
|
2022-01-29 01:51:34 +03:00
|
|
|
bool isprint(int c) {
|
|
|
|
return c >= ' ' && c <= '~';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isspace(int c) {
|
|
|
|
return (c >= '\t' && c <= 0xD) || c == ' ';
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:22:50 +03:00
|
|
|
bool isalpha(int c) {
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isdigit(int c) {
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-05 09:57:54 +03:00
|
|
|
int abs(int i) {
|
|
|
|
return i < 0 ? -i : i;
|
|
|
|
}
|
|
|
|
|
2019-05-31 07:23:23 +03:00
|
|
|
char *strcpy(char *dest, const char *src) {
|
2020-04-24 15:48:14 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:48:14 +03:00
|
|
|
char *strncpy(char *dest, const char *src, size_t n) {
|
|
|
|
size_t i;
|
2019-05-31 07:23:23 +03:00
|
|
|
|
2020-04-24 15:48:14 +03:00
|
|
|
for (i = 0; i < n && src[i]; i++)
|
2019-05-31 07:23:23 +03:00
|
|
|
dest[i] = src[i];
|
2020-04-24 15:48:14 +03:00
|
|
|
for ( ; i < n; i++)
|
|
|
|
dest[i] = 0;
|
2019-05-31 07:23:23 +03:00
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:48:14 +03:00
|
|
|
int strcmp(const char *s1, const char *s2) {
|
|
|
|
for (size_t i = 0; ; i++) {
|
|
|
|
char c1 = s1[i], c2 = s2[i];
|
|
|
|
if (c1 != c2)
|
2020-08-27 02:31:32 +03:00
|
|
|
return c1 < c2 ? -1 : 1;
|
2020-04-24 15:48:14 +03:00
|
|
|
if (!c1)
|
|
|
|
return 0;
|
2019-05-31 07:23:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 09:03:49 +03:00
|
|
|
int strcasecmp(const char *s1, const char *s2) {
|
|
|
|
for (size_t i = 0; ; i++) {
|
|
|
|
char c1 = s1[i], c2 = s2[i];
|
|
|
|
if (tolower(c1) != tolower(c2))
|
|
|
|
return c1 < c2 ? -1 : 1;
|
|
|
|
if (!c1)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:48:14 +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)
|
2020-08-27 02:31:32 +03:00
|
|
|
return c1 < c2 ? -1 : 1;
|
2020-04-24 15:48:14 +03:00
|
|
|
if (!c1)
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-31 07:23:23 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:22:50 +03:00
|
|
|
int strncasecmp(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 (tolower(c1) != tolower(c2))
|
|
|
|
return c1 < c2 ? -1 : 1;
|
|
|
|
if (!c1)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-31 07:23:23 +03:00
|
|
|
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) {
|
2020-11-05 14:50:42 +03:00
|
|
|
uint8_t array[4];
|
2020-11-05 03:37:45 +03:00
|
|
|
const char *current = src;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-11-05 14:50:42 +03:00
|
|
|
const char *newcur;
|
|
|
|
uint64_t value = strtoui(current, &newcur, 10);
|
|
|
|
if (current == newcur)
|
|
|
|
return -1;
|
|
|
|
current = newcur;
|
|
|
|
if (*current == 0 && i < 3)
|
|
|
|
return -1;
|
|
|
|
if (value > 255)
|
|
|
|
return -1;
|
|
|
|
current++;
|
|
|
|
array[i] = value;
|
2020-11-05 03:37:45 +03:00
|
|
|
}
|
|
|
|
memcpy(dst, array, 4);
|
|
|
|
return 0;
|
|
|
|
}
|