aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/libc/conv.c10
-rw-r--r--libs/libc/inc/conv.h4
-rw-r--r--libs/libc/inc/def.h28
-rw-r--r--libs/libc/inc/list.h2
-rw-r--r--libs/libc/inc/math.h2
-rw-r--r--libs/libc/inc/mem.h2
-rw-r--r--libs/libc/inc/str.h4
-rw-r--r--libs/libc/list.c2
-rw-r--r--libs/libc/math.c11
-rw-r--r--libs/libc/mem.c20
-rw-r--r--libs/libc/print.c2
-rw-r--r--libs/libc/str.c44
12 files changed, 103 insertions, 28 deletions
diff --git a/libs/libc/conv.c b/libs/libc/conv.c
index 64401ed..72a919b 100644
--- a/libs/libc/conv.c
+++ b/libs/libc/conv.c
@@ -6,7 +6,7 @@
#include <mem.h>
#include <str.h>
-int itoa(int value, char *buffer, int base)
+int itoa(s32 value, char *buffer, u32 base)
{
char tmp[16];
char *tp = tmp;
@@ -37,6 +37,7 @@ int itoa(int value, char *buffer, int base)
while (tp > tmp)
*buffer++ = *--tp;
+ *buffer = '\0';
return len;
}
@@ -59,8 +60,7 @@ static int normalize(f64 *val)
return exp;
}
-#define FLOAT_WIDTH 5
-char *ftoa(f64 value, char *buffer)
+char *ftoa(f64 value, char *buffer, u32 width)
{
int exp = 0;
u32 loc = 0;
@@ -91,13 +91,13 @@ char *ftoa(f64 value, char *buffer)
*buffer++ = '.';
- while (exp < 0 && loc < FLOAT_WIDTH) {
+ while (exp < 0 && loc < width) {
*buffer++ = '0';
--exp;
++loc;
}
- while (loc < FLOAT_WIDTH) {
+ while (loc < width) {
int digit = value * 10.0;
*buffer++ = digit + '0';
value = value * 10.0 - digit;
diff --git a/libs/libc/inc/conv.h b/libs/libc/inc/conv.h
index 8b88526..58937c2 100644
--- a/libs/libc/inc/conv.h
+++ b/libs/libc/inc/conv.h
@@ -5,7 +5,7 @@
#include <def.h>
-int itoa(int value, char *buffer, int base);
-char *ftoa(f64 value, char *buffer);
+int itoa(s32 value, char *buffer, u32 base);
+char *ftoa(f64 value, char *buffer, u32 width);
#endif
diff --git a/libs/libc/inc/def.h b/libs/libc/inc/def.h
index 3b173dd..84656b4 100644
--- a/libs/libc/inc/def.h
+++ b/libs/libc/inc/def.h
@@ -33,6 +33,7 @@ typedef long double f80;
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define ABS(a) ((u32)(((s32)(a) < 0) ? -(a) : (a)))
+#define FABS(a) ((f64)(((f64)(a) < 0.0) ? -(a) : (a)))
#define COUNT(a) (sizeof(a) / sizeof 0 [a])
@@ -42,8 +43,8 @@ typedef long double f80;
#define NUMERIC(a) ((a) >= '0' && ((a) <= '9'))
#define ALPHANUMERIC(a) (ALPHA(a) || NUMERIC(a))
-#define __STRINGIFY(a) #a
-#define STRINGIFY(a) __STRINGIFY(a)
+#define STRINGIFY_PARAM(a) #a
+#define STRINGIFY(a) STRINGIFY_PARAM(a)
#define ALIGN_UP(addr, align) (((addr) + (align)-1) & ~((align)-1))
#define ALIGN_DOWN(addr, align) ((addr) & ~((align)-1))
@@ -86,18 +87,17 @@ typedef long double f80;
#define EOF (-1)
#define NULL ((void *)0)
-#define U8_MAX 255
-#define S8_MAX 127
-#define S8_MIN -128
-#define U16_MAX 65535
-#define S16_MAX 32767
-#define S16_MIN -32768
-#define U32_MAX 4294967295
-#define S32_MAX 2147483647
-#define S32_MIN -2147483648
-
-#define LONG_MAX S32_MAX
-#define LONG_MIN S32_MIN
+#define U8_MAX ((u8)255)
+#define S8_MAX ((s8)127)
+#define S8_MIN ((s8)-128)
+#define U16_MAX ((u16)65535)
+#define S16_MAX ((s16)32767)
+#define S16_MIN ((s16)-32768)
+#define U32_MAX ((u32)4294967295)
+#define S32_MAX ((s32)2147483647)
+#define S32_MIN ((s32)-2147483648)
+#define F64_MAX ((f64)0x7fefffffffffffff)
+#define F64_MIN ((f64)0x10000000000000)
#define MILLION 1000000
#define BILLION 1000000000
diff --git a/libs/libc/inc/list.h b/libs/libc/inc/list.h
index fea98dc..4e5fe65 100644
--- a/libs/libc/inc/list.h
+++ b/libs/libc/inc/list.h
@@ -18,8 +18,6 @@ struct node {
struct list *list_new(void);
void list_destroy(struct list *list) NONNULL;
-/* struct node *list_new_node(); */ // TODO: Make node-specific things static/private?
-/* void list_add_node(struct list *list, struct node *node); */
struct node *list_add(struct list *list, void *data) NONNULL;
struct list *list_remove(struct list *list, struct node *node) NONNULL;
struct node *list_last(struct list *list) NONNULL;
diff --git a/libs/libc/inc/math.h b/libs/libc/inc/math.h
index 8affb72..4a38c5f 100644
--- a/libs/libc/inc/math.h
+++ b/libs/libc/inc/math.h
@@ -17,7 +17,9 @@
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.78539816339744830962
+f32 powf(f32 base, f32 exp);
f64 pow(f64 base, f64 exp);
+f32 sqrtf(f64 num);
f64 sqrt(f64 num);
f32 sinf(f32 angle);
diff --git a/libs/libc/inc/mem.h b/libs/libc/inc/mem.h
index 64f74f5..ef53cad 100644
--- a/libs/libc/inc/mem.h
+++ b/libs/libc/inc/mem.h
@@ -68,6 +68,7 @@ void free_debug(void *ptr, const char *file, int line, const char *func, const c
void *memcpy(void *dest, const void *src, u32 n) NONNULL;
void *memset(void *dest, u32 val, u32 n) NONNULL;
+const void *memcchr(const void *src, char c, u32 n) NONNULL;
void *memchr(void *src, char c, u32 n) NONNULL;
s32 memcmp(const void *s1, const void *s2, u32 n) NONNULL;
u8 mememp(const u8 *buf, u32 n) NONNULL;
@@ -75,6 +76,7 @@ u8 mememp(const u8 *buf, u32 n) NONNULL;
#ifdef KERNEL
void *memcpy_user(void *dest, const void *src, u32 n) NONNULL;
void *memset_user(void *dest, u32 val, u32 n) NONNULL;
+const void *memcchr_user(const void *src, char c, u32 n) NONNULL;
void *memchr_user(void *src, char c, u32 n) NONNULL;
s32 memcmp_user(const void *s1, const void *s2, u32 n) NONNULL;
u8 mememp_user(const u8 *buf, u32 n) NONNULL;
diff --git a/libs/libc/inc/str.h b/libs/libc/inc/str.h
index f024aaa..6b5d16f 100644
--- a/libs/libc/inc/str.h
+++ b/libs/libc/inc/str.h
@@ -8,6 +8,8 @@
PURE u32 strlen(const char *s) NONNULL;
PURE u32 strnlen(const char *s, u32 max) NONNULL;
u32 strlcpy(char *dst, const char *src, u32 size) NONNULL;
+PURE const char *strcchr(const char *s, char c) NONNULL;
+PURE const char *strrcchr(const char *s, char c) NONNULL;
PURE char *strchr(char *s, char c) NONNULL;
PURE char *strrchr(char *s, char c) NONNULL;
u32 strlcat(char *dst, const char *src, u32 size) NONNULL;
@@ -21,6 +23,8 @@ ATTR((malloc)) char *strdup(const char *s) NONNULL;
PURE u32 strlen_user(const char *s) NONNULL;
PURE u32 strnlen_user(const char *s, u32 max) NONNULL;
u32 strlcpy_user(char *dst, const char *src, u32 size) NONNULL;
+PURE const char *strcchr_user(const char *s, char c) NONNULL;
+PURE const char *strrcchr_user(const char *s, char c) NONNULL;
PURE char *strchr_user(char *s, char c) NONNULL;
PURE char *strrchr_user(char *s, char c) NONNULL;
u32 strlcat_user(char *dst, const char *src, u32 size) NONNULL;
diff --git a/libs/libc/list.c b/libs/libc/list.c
index 3f37478..a477285 100644
--- a/libs/libc/list.c
+++ b/libs/libc/list.c
@@ -61,7 +61,7 @@ NONNULL static struct node *list_add_node(struct list *list, struct node *node)
struct node *list_last(struct list *list)
{
- if (list->head)
+ if (!list->head)
return NULL;
struct node *iterator = list->head;
diff --git a/libs/libc/math.c b/libs/libc/math.c
index b84958b..23b1ca4 100644
--- a/libs/libc/math.c
+++ b/libs/libc/math.c
@@ -2,6 +2,11 @@
#include <math.h>
+f32 powf(f32 base, f32 exp)
+{
+ return (f32)pow(base, exp);
+}
+
f64 pow(f64 base, f64 exp)
{
f64 out;
@@ -26,6 +31,12 @@ f64 pow(f64 base, f64 exp)
}
// TODO: More efficient sqrt?
+
+f32 sqrtf(f64 num)
+{
+ return powf(num, .5);
+}
+
f64 sqrt(f64 num)
{
return pow(num, .5);
diff --git a/libs/libc/mem.c b/libs/libc/mem.c
index 080c88d..27f0416 100644
--- a/libs/libc/mem.c
+++ b/libs/libc/mem.c
@@ -48,6 +48,18 @@ void *memset(void *dest, u32 val, u32 n)
return dest;
}
+const void *memcchr(const void *src, char c, u32 n)
+{
+ const u8 *s = (const u8 *)src;
+
+ while (n-- > 0) {
+ if (*s == c)
+ return s;
+ s++;
+ }
+ return NULL;
+}
+
void *memchr(void *src, char c, u32 n)
{
u8 *s = (u8 *)src;
@@ -98,6 +110,14 @@ void *memset_user(void *dest, u32 val, u32 n)
return ret;
}
+const void *memcchr_user(const void *src, char c, u32 n)
+{
+ stac();
+ const void *ret = memcchr(src, c, n);
+ clac();
+ return ret;
+}
+
void *memchr_user(void *src, char c, u32 n)
{
stac();
diff --git a/libs/libc/print.c b/libs/libc/print.c
index da8ab25..264f893 100644
--- a/libs/libc/print.c
+++ b/libs/libc/print.c
@@ -54,7 +54,7 @@ int vsnprintf(char *str, u32 size, const char *format, va_list ap)
break;
case 'f':
temp_double = va_arg(ap, double);
- ftoa(temp_double, buffer);
+ ftoa(temp_double, buffer, 5);
length += strlcpy(&str[length], buffer, size - length);
break;
default:
diff --git a/libs/libc/str.c b/libs/libc/str.c
index 9e1bb2f..ce0fef6 100644
--- a/libs/libc/str.c
+++ b/libs/libc/str.c
@@ -34,7 +34,7 @@ u32 strlcpy(char *dst, const char *src, u32 size)
break;
if (!left) {
- if (!size)
+ if (size)
*dst = 0;
while (*src++)
;
@@ -75,6 +75,29 @@ s32 strncmp(const char *s1, const char *s2, u32 n)
return d;
}
+const char *strcchr(const char *s, char c)
+{
+ while (*s != c) {
+ if (!*s)
+ return NULL;
+ s++;
+ }
+
+ return s;
+}
+
+const char *strrcchr(const char *s, char c)
+{
+ const char *ret = 0;
+
+ do {
+ if (*s == c)
+ ret = s;
+ } while (*s++);
+
+ return ret;
+}
+
char *strchr(char *s, char c)
{
while (*s != c) {
@@ -120,8 +143,7 @@ u32 strlcat(char *dst, const char *src, u32 size)
}
src++;
}
-
- src = 0;
+ *dst = 0;
return len + (src - orig_src);
}
@@ -273,6 +295,22 @@ s32 strncmp_user(const char *s1, const char *s2, u32 n)
return ret;
}
+const char *strcchr_user(const char *s, char c)
+{
+ stac();
+ const char *ret = strcchr(s, c);
+ clac();
+ return ret;
+}
+
+const char *strrcchr_user(const char *s, char c)
+{
+ stac();
+ const char *ret = strrcchr(s, c);
+ clac();
+ return ret;
+}
+
char *strchr_user(char *s, char c)
{
stac();