Skip to content

Commit 7a0e096

Browse files
authored
Use string.h functions where applicable + some cleanup (#26)
* Use string.h functions in cpio.c * Use string.h functions in vfs.c * Use isprint in classic.c * Use string.h in console.c + some minor readability improvements * Use kmemcpy in memmgt.c * Deprecate walk_pagetable
1 parent 21ec4b9 commit 7a0e096

5 files changed

Lines changed: 35 additions & 88 deletions

File tree

kernel/src/kernel/console.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <kclib/ctype.h>
12
#include <kclib/string.h>
23
#include <kernel/console.h>
34
#include <kernel/graphics.h>
@@ -78,8 +79,7 @@ void init_console (size_t x_screen, size_t y_screen, size_t x_pad, size_t y_pad,
7879

7980
fs = font_size;
8081

81-
for (size_t i = 0; i < xc * yc; i++)
82-
buffer[i] = 32;
82+
kmemset (buffer, 32, xc * yc);
8383
}
8484

8585
/*!
@@ -93,11 +93,11 @@ void set_color (uint32_t c) { color = c; }
9393
Update the display.
9494
*/
9595
void update () {
96-
for (size_t i = 0; i < yc; i++) {
97-
for (size_t j = 0; j < xc; j++) {
98-
renderGlyph (glyph (buffer[i * xc + j]), 8, 5, xs + (fs * j * (5 + xsp)),
99-
ys + (fs * i * (8 + ysp)), fs, colorbuffer[i * xc + j]);
100-
}
96+
for (size_t pos = 0; pos < xc * yc; pos++) {
97+
size_t i = pos / xc;
98+
size_t j = pos % xc;
99+
renderGlyph (glyph (buffer[pos]), 8, 5, xs + (fs * j * (5 + xsp)),
100+
ys + (fs * i * (8 + ysp)), fs, colorbuffer[pos]);
101101
}
102102
}
103103

@@ -120,9 +120,8 @@ Print a single character to the screen.
120120
void putchar (unsigned char rc) {
121121
switch (rc) {
122122
case '\n':
123-
idx = xc * ((idx / xc) + 1);
124-
while (idx >= CONSOLE_HEIGHT * CONSOLE_WIDTH)
125-
idx -= CONSOLE_HEIGHT * CONSOLE_WIDTH;
123+
idx = ((idx / xc) + 1) * xc;
124+
if (idx >= xc * yc) idx -= xc * yc;
126125
return;
127126
}
128127
registerChar (rc, idx);

kernel/src/kernel/fs/cpio.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <kclib/ctype.h>
12
#include <kclib/stdio.h>
23
#include <kclib/string.h>
34
#include <kernel/error.h>
@@ -13,16 +14,13 @@
1314
static uint64_t hex_to_u64 (const char hex[8]) [[unsequenced]] {
1415
uint64_t val = 0;
1516
for (int i = 0; i < 8; i++) {
16-
char c = hex[i];
17-
uint8_t digit;
18-
if (c >= '0' && c <= '9')
17+
unsigned char c = (unsigned char)hex[i];
18+
uint8_t digit;
19+
if (!isxdigit (c)) break;
20+
if (isdigit (c))
1921
digit = c - '0';
20-
else if (c >= 'A' && c <= 'F')
21-
digit = c - 'A' + 10;
22-
else if (c >= 'a' && c <= 'f')
23-
digit = c - 'a' + 10;
2422
else
25-
break;
23+
digit = tolower (c) - 'a' + 10;
2624
val = (val << 4) | digit;
2725
}
2826
return val;
@@ -75,13 +73,7 @@ static int mkdir_if_required (const char* dir, inode* root) {
7573
if (error == -EPNOEXIST) {
7674
child_name = nullptr;
7775

78-
char* last_slash = nullptr;
79-
for (int i = len - 1; i >= 0; i--) {
80-
if (path[i] == '/') {
81-
last_slash = &path[i];
82-
break;
83-
}
84-
}
76+
char* last_slash = kstrrchr (path, '/');
8577

8678
if (last_slash && last_slash != path) {
8779
*last_slash = '\0';
@@ -134,17 +126,14 @@ static int parse_entry_to_inode (cpio_newc_header_t* header, const char* out_pat
134126
}
135127
}
136128
if (filetype == C_ISREG) {
137-
char* trailing_slashes = &filename[namesize - 1];
138-
while ((uintptr_t)trailing_slashes > (uintptr_t)filename && *trailing_slashes == '/')
139-
trailing_slashes--;
140-
trailing_slashes[1] = 0;
141-
142-
char* last_slash = trailing_slashes;
143-
while ((uintptr_t)last_slash > (uintptr_t)filename && *last_slash != '/')
144-
last_slash--;
129+
size_t flen = kstrlen (filename);
130+
while (flen > 1 && filename[flen - 1] == '/')
131+
flen--;
132+
filename[flen] = '\0';
145133

146-
if (last_slash != filename) { // i.e. there is a prefix
147-
*last_slash = 0;
134+
char* last_slash = kstrrchr (filename, '/');
135+
if (last_slash && last_slash != filename) {
136+
*last_slash = '\0';
148137
mkdir_if_required (filename, root_dir);
149138
*last_slash = '/';
150139
}

kernel/src/kernel/fs/vfs.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <kclib/ctype.h>
12
#include <kclib/string.h>
23
#include <kernel/error.h>
34
#include <kernel/fs/vfs.h>
@@ -9,7 +10,8 @@ inode* vfs_absolute_root = nullptr;
910

1011
static bool filename_has_invalid_chars (char* filename) {
1112
while (*filename != 0) {
12-
if (*filename == '/' || *filename < (char)32) return true;
13+
unsigned char c = (unsigned char)*filename;
14+
if (c == '/' || !isprint (c)) return true;
1315
filename++;
1416
}
1517
return false;
@@ -28,12 +30,10 @@ int vfs_resolve_parent (const char* path_arg, inode* root, inode** r_parent, cha
2830
char* path = kstrdup (path_arg);
2931
if (!path) return -ENOMEM;
3032

31-
char* last_slash = nullptr;
32-
for (int i = kstrlen (path) - 1; i >= 0; i--) {
33-
if (path[i] == '/') {
34-
last_slash = &path[i];
35-
break;
36-
}
33+
char* last_slash = kstrrchr (path, '/');
34+
if (!last_slash) {
35+
kfree (path);
36+
return -EINVARG;
3737
}
3838

3939
*r_name = kstrdup (last_slash + 1);
@@ -184,9 +184,8 @@ int do_lookup (char* filename, inode** result, inode* root) {
184184
}
185185

186186
// get the target_name
187-
char* target_name = (char*)kmalloc ((size_t)(next_slash - filename));
188-
kmemcpy ((void*)target_name, (void*)(filename + 1), (size_t)(next_slash - filename) - 1);
189-
target_name[(size_t)(next_slash - filename) - 1] = 0;
187+
size_t comp_len = (size_t)(next_slash - filename) - 1;
188+
char* target_name = kstrndup (filename + 1, comp_len);
190189

191190
if (kstrcmp (target_name, ".") == 0) {
192191
kfree (target_name);

kernel/src/kernel/hardfonts/classic.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <kclib/ctype.h>
12
#include <kernel/hardfonts/classic.h>
23
#include <stddef.h>
34
#include <stdint.h>
@@ -782,6 +783,6 @@ Return the glyph for a certain character from the classic font
782783
@return pointer to 5x8 glyph
783784
*/
784785
unsigned char* glyph (char index) {
785-
if (index < 32) return &__classic_font__[0][0][0];
786+
if (!isprint (index)) return &__classic_font__[0][0][0];
786787
return &__classic_font__[index - 32][0][0];
787788
}

kernel/src/kernel/memmgt.c

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -644,46 +644,6 @@ void init_memmgt (uint64_t p_hhdm_offset, struct limine_memmap_response* memmap_
644644
*/
645645
uintptr_t get_kernel_cr3 (void) { return kernel_cr3; }
646646

647-
/*!
648-
* Walks the page table hierarchy and prints present entries and their address ranges.
649-
* @deprecated
650-
*/
651-
void walk_pagetable () {
652-
pml4t_entry_t* pml4t_entry = &pml4_base_ptr[1];
653-
if (!pml4t_entry->present) return;
654-
655-
pdpt_entry_t* pdpt_base_ptr =
656-
(pdpt_entry_t*)get_vaddr_from_frame (pml4t_entry->pdpt_base_address);
657-
pdpt_entry_t* pdpt_entry = &pdpt_base_ptr[0];
658-
if (!pdpt_entry->present) return;
659-
660-
pd_entry_t* pd_base_ptr = (pd_entry_t*)get_vaddr_from_frame (pdpt_entry->pd_base_address);
661-
for (int k = 0; k < 512; k++) {
662-
pd_entry_t* pd_entry = &pd_base_ptr[k];
663-
if (!pd_entry->present) continue;
664-
kprintf ("PD %d: PT Base Address: 0x%lx\n", k, pd_entry->pt_base_address << 12);
665-
pt_entry_t* pt_base_ptr = (pt_entry_t*)get_vaddr_from_frame (pd_entry->pt_base_address);
666-
667-
bool is_present_pt[512] = {false};
668-
for (int k = 0; k < 512; k++)
669-
is_present_pt[k] = (&pt_base_ptr[k])->present;
670-
671-
// print ranges of present pd's
672-
int range_start = -1;
673-
for (int k = 0; k <= 512; k++) {
674-
if (k < 512 && is_present_pt[k]) {
675-
if (range_start == -1) range_start = k;
676-
} else if (range_start != -1) {
677-
if (range_start == k - 1)
678-
kprintf (" Present PT: %d\n", range_start);
679-
else
680-
kprintf (" Present PTs: %d-%d\n", range_start, k - 1);
681-
range_start = -1;
682-
}
683-
}
684-
}
685-
}
686-
687647
/*!
688648
* Finds the physical address mapped to a given virtual address.
689649
* @param vaddr The virtual address to look up.
@@ -753,8 +713,7 @@ int clone_user_memory (uint64_t cr3_src, uint64_t* cr3_dest) {
753713

754714
// map the higher half to be exactly the same as kernel's
755715
pml4t_entry_t* krnl_pml4_table = (pml4t_entry_t*)((uint64_t)get_kernel_cr3 () + hhdm_offset);
756-
for (int i = 256; i < 512; i++)
757-
dest_pml4_table[i] = krnl_pml4_table[i];
716+
kmemcpy (&dest_pml4_table[256], &krnl_pml4_table[256], 256 * sizeof (pml4t_entry_t));
758717

759718
// deep clone the lower half (user space)
760719
for (uint16_t pml4_index = 0; pml4_index < 256; pml4_index++) {

0 commit comments

Comments
 (0)