Skip to content

Commit cc0fb5a

Browse files
committed
Bugfix in kvlog & strowhatever implementations
1 parent 297505b commit cc0fb5a

File tree

8 files changed

+275
-56
lines changed

8 files changed

+275
-56
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fi
66
. "$ROOT/ghost.sh"
77

88
# Prioritized applications that need to be built first
9-
APPLICATION_PRIORITY=("libproperties" "libdevice" "libps2" "libps2driver" "libinput" "libwindow" "libfont" "libterminal" "libvideo" "libpci" "libahci")
9+
APPLICATION_PRIORITY=("libjson" "libproperties" "libdevice" "libps2" "libps2driver" "libinput" "libwindow" "libfont" "libterminal" "libvideo" "libpci" "libahci")
1010

1111
# Flags
1212
FIRST_RUN=0

libc/src/stdio/kvlog.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,38 @@ void kvlog(const char* message, va_list l)
3232
va_list lc;
3333
va_copy(lc, l);
3434

35-
// First try
3635
uint32_t messageLen = strlen(message);
37-
uint32_t bufLen = messageLen * 4;
36+
uint32_t bufLen = messageLen * 2;
3837

39-
char* buf = malloc(bufLen);
40-
if(!buf)
41-
{
42-
g_log("failed to allocate buffer for kernel logging");
43-
return;
44-
}
45-
46-
int printed = vsnprintf(buf, bufLen, message, l);
47-
int success = printed == messageLen - 1;
48-
if(success)
49-
g_log(buf);
38+
char* buf = NULL;
39+
int printed = -1;
5040

51-
free(buf);
52-
53-
// Buffer too small? Second try
54-
if(!success)
41+
for(;;)
5542
{
56-
bufLen = messageLen * 8;
57-
buf = (char*) malloc(bufLen);
43+
free(buf);
44+
buf = malloc(bufLen);
45+
5846
if(!buf)
5947
{
60-
g_log("failed to allocate buffer for kernel logging on retry");
48+
g_log("failed to allocate buffer for kernel logging");
49+
va_end(lc);
6150
return;
6251
}
6352

64-
vsnprintf(buf, bufLen, message, lc);
65-
g_log(buf);
53+
va_list ltmp;
54+
va_copy(ltmp, lc);
55+
printed = vsnprintf(buf, bufLen, message, ltmp);
56+
va_end(ltmp);
6657

67-
free(buf);
58+
if(printed >= 0 && (uint32_t) printed < bufLen)
59+
{
60+
g_log(buf);
61+
break;
62+
}
63+
64+
bufLen *= 2;
6865
}
6966

67+
free(buf);
7068
va_end(lc);
7169
}

libc/src/stdlib/bsearch.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,24 @@
2626
*
2727
*/
2828
void* bsearch(const void* value, const void* array, size_t num_elements,
29-
size_t size, int (*comparator)(const void*, const void*)) {
30-
klog("warning: bsearch is not implemented");
31-
return 0;
32-
}
29+
size_t size, int (*comparator)(const void*, const void*))
30+
{
31+
size_t low = 0;
32+
size_t high = num_elements;
33+
34+
while(low < high)
35+
{
36+
size_t mid = low + (high - low) / 2;
37+
const void* elem = (const char*) array + mid * size;
3338

39+
int cmp = comparator(value, elem);
40+
if(cmp < 0)
41+
high = mid;
42+
else if(cmp > 0)
43+
low = mid + 1;
44+
else
45+
return (void*) elem;
46+
}
47+
48+
return NULL;
49+
}

libc/src/stdlib/strtod.c

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,64 @@
1818
* *
1919
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020

21-
#include "stdlib.h"
22-
#include "errno.h"
23-
#include <ghost/system.h>
21+
#include <ctype.h>
22+
#include <math.h>
2423

2524
/**
2625
*
2726
*/
2827
double strtod(const char* str, char** endptr)
2928
{
30-
__G_NOT_IMPLEMENTED("strtod");
29+
const char* p = str;
30+
while(isspace(*p))
31+
p++;
32+
33+
int sign = 1;
34+
if(*p == '+' || *p == '-')
35+
{
36+
if(*p == '-')
37+
sign = -1;
38+
p++;
39+
}
40+
41+
double value = 0.0;
42+
while(isdigit(*p))
43+
{
44+
value = value * 10.0 + (*p - '0');
45+
p++;
46+
}
47+
48+
if(*p == '.')
49+
{
50+
p++;
51+
double frac = 1.0;
52+
while(isdigit(*p))
53+
{
54+
frac *= 0.1;
55+
value += (*p - '0') * frac;
56+
p++;
57+
}
58+
}
59+
60+
int exp_sign = 1;
61+
int exp_val = 0;
62+
if(*p == 'e' || *p == 'E')
63+
{
64+
p++;
65+
if(*p == '+' || *p == '-')
66+
{
67+
if(*p == '-')
68+
exp_sign = -1;
69+
p++;
70+
}
71+
while(isdigit(*p))
72+
{
73+
exp_val = exp_val * 10 + (*p - '0');
74+
p++;
75+
}
76+
}
77+
78+
if(endptr)
79+
*endptr = (char*) p;
80+
return sign * value * pow(10.0, exp_sign * exp_val);
3181
}

libc/src/stdlib/strtof.c

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,64 @@
1818
* *
1919
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020

21-
#include "stdlib.h"
22-
#include "errno.h"
23-
#include <ghost/system.h>
21+
#include <ctype.h>
22+
#include <math.h>
2423

2524
/**
2625
*
2726
*/
2827
float strtof(const char* str, char** endptr)
2928
{
30-
__G_NOT_IMPLEMENTED("strtof");
29+
const char* p = str;
30+
while(isspace(*p))
31+
p++;
32+
33+
int sign = 1;
34+
if(*p == '+' || *p == '-')
35+
{
36+
if(*p == '-')
37+
sign = -1;
38+
p++;
39+
}
40+
41+
double val = 0.0;
42+
while(isdigit(*p))
43+
{
44+
val = val * 10.0 + (*p - '0');
45+
p++;
46+
}
47+
48+
if(*p == '.')
49+
{
50+
p++;
51+
double frac = 1.0;
52+
while(isdigit(*p))
53+
{
54+
frac *= 0.1;
55+
val += (*p - '0') * frac;
56+
p++;
57+
}
58+
}
59+
60+
int exp_sign = 1;
61+
int exp_val = 0;
62+
if(*p == 'e' || *p == 'E')
63+
{
64+
p++;
65+
if(*p == '+' || *p == '-')
66+
{
67+
if(*p == '-')
68+
exp_sign = -1;
69+
p++;
70+
}
71+
while(isdigit(*p))
72+
{
73+
exp_val = exp_val * 10 + (*p - '0');
74+
p++;
75+
}
76+
}
77+
78+
if(endptr)
79+
*endptr = (char*) p;
80+
return (float) (sign * val * pow(10.0, exp_sign * exp_val));
3181
}

libc/src/stdlib/strtol.c

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,71 @@
1818
* *
1919
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020

21-
#include "stdlib.h"
22-
#include "errno.h"
23-
#include "stdio.h"
21+
#include <ctype.h>
22+
#include <limits.h>
2423

2524
/**
2625
*
2726
*/
28-
long strtol(const char* str, char** endptr, int base) {
27+
long strtol(const char* str, char** endptr, int base)
28+
{
29+
const char* p = str;
30+
while(isspace(*p))
31+
p++;
2932

30-
klog("warning: strtol is not implemented");
31-
return 0;
33+
int sign = 1;
34+
if(*p == '+' || *p == '-')
35+
{
36+
if(*p == '-')
37+
sign = -1;
38+
p++;
39+
}
40+
41+
if(base == 0)
42+
{
43+
if(*p == '0')
44+
{
45+
if(p[1] == 'x' || p[1] == 'X')
46+
{
47+
base = 16;
48+
p += 2;
49+
}
50+
else
51+
{
52+
base = 8;
53+
p++;
54+
}
55+
}
56+
else
57+
base = 10;
58+
}
59+
else if(base == 16 && *p == '0' && (p[1] == 'x' || p[1] == 'X'))
60+
p += 2;
61+
62+
long result = 0;
63+
while(*p)
64+
{
65+
int digit;
66+
if(isdigit(*p))
67+
digit = *p - '0';
68+
else if(isalpha(*p))
69+
digit = (tolower(*p) - 'a') + 10;
70+
else
71+
break;
72+
if(digit >= base)
73+
break;
74+
75+
if(result > (LONG_MAX - digit) / base)
76+
{
77+
result = LONG_MAX;
78+
break;
79+
}
80+
81+
result = result * base + digit;
82+
p++;
83+
}
84+
85+
if(endptr)
86+
*endptr = (char*) p;
87+
return sign * result;
3288
}

libc/src/stdlib/strtoul.c

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,68 @@
1818
* *
1919
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020

21-
#include "stdlib.h"
22-
#include "errno.h"
23-
#include <ghost/system.h>
21+
#include <ctype.h>
22+
#include <limits.h>
2423

25-
/**
26-
*
27-
*/
2824
unsigned long strtoul(const char* str, char** endptr, int base)
2925
{
30-
__G_NOT_IMPLEMENTED("strtoul");
26+
const char* p = str;
27+
while(isspace(*p))
28+
p++;
29+
30+
int neg = 0;
31+
if(*p == '+' || *p == '-')
32+
{
33+
if(*p == '-')
34+
neg = 1;
35+
p++;
36+
}
37+
38+
if(base == 0)
39+
{
40+
if(*p == '0')
41+
{
42+
if(p[1] == 'x' || p[1] == 'X')
43+
{
44+
base = 16;
45+
p += 2;
46+
}
47+
else
48+
{
49+
base = 8;
50+
p++;
51+
}
52+
}
53+
else
54+
base = 10;
55+
}
56+
else if(base == 16 && *p == '0' && (p[1] == 'x' || p[1] == 'X'))
57+
p += 2;
58+
59+
unsigned long result = 0;
60+
while(*p)
61+
{
62+
int digit;
63+
if(isdigit(*p))
64+
digit = *p - '0';
65+
else if(isalpha(*p))
66+
digit = (tolower(*p) - 'a') + 10;
67+
else
68+
break;
69+
if(digit >= base)
70+
break;
71+
72+
if(result > (ULONG_MAX - digit) / base)
73+
{
74+
result = ULONG_MAX;
75+
break;
76+
}
77+
78+
result = result * base + digit;
79+
p++;
80+
}
81+
82+
if(endptr)
83+
*endptr = (char*) p;
84+
return neg ? (unsigned long) (-(long) result) : result;
3185
}

0 commit comments

Comments
 (0)