LibC Reference¶
TilekarOS includes a minimal libc implementation for kernel and future user-space use.
1. stdio.h (Standard Input/Output)¶
Header File: stdio.h
Code Preview: stdio.h
printf¶
Outputs a formatted string to the default terminal. Supports %s, %c, %d, %i, %u, %x, %X, and %p.
vsnprintf¶
Writes a formatted string to a buffer with a size limit, preventing buffer overflows.
puts¶
Writes a string and a newline (\n) to the default terminal.
2. string.h (String Manipulation)¶
Header File: string.h
Code Preview: string.h
#ifndef _STRING_H
#define _STRING_H 1
#include <sys/cdefs.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
int memcmp(const void*, const void*, size_t);
void* memcpy(void* __restrict, const void* __restrict, size_t);
void* memmove(void*, const void*, size_t);
void* memset(void*, int, size_t);
size_t strlen(const char*);
char* strcpy(char* __restrict, const char* __restrict);
char* strncpy(char* __restrict, const char* __restrict, size_t);
int strcmp(const char*, const char*);
char* strchr(const char*, int);
char* strrchr(const char*, int);
#ifdef __cplusplus
}
#endif
#endif
memcpy¶
Copies n bytes from src to dest. The memory areas must not overlap.
memmove¶
Copies n bytes from src to dest. Memory areas may overlap.
memset¶
Fills the first n bytes of the memory area pointed to by s with the constant byte c.
memcmp¶
Compares the first n bytes of memory areas s1 and s2.
strlen¶
Calculates the length of the string s, excluding the terminating null byte (\0).
strcmp¶
Compares two strings. Returns an integer less than, equal to, or greater than zero if s1 is found to be less than, match, or be greater than s2.
strncmp¶
Compares up to n characters of two strings.
strcpy¶
Copies a string from src to dest.
strncpy¶
Copies up to n characters from src to dest.
strchr¶
Finds the first occurrence of character c in string s.
strrchr¶
Finds the last occurrence of character c in string s.
3. stdlib.h (Standard Library)¶
Header File: stdlib.h
Code Preview: stdlib.h
abort¶
Causes abnormal program termination. In the kernel, this typically triggers a panic.
4. Test/Example: Using LibC in Kernel¶
#include <stdio.h>
#include <string.h>
void libc_demo() {
char buf[32];
strcpy(buf, "TilekarOS");
printf("Welcome to %s!\n", buf);
if (strcmp(buf, "TilekarOS") == 0) {
puts("String match successful.");
}
}