Skip to content

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
#ifndef _STDIO_H
#define _STDIO_H 1

#include <sys/cdefs.h>

#define EOF (-1)

#ifdef __cplusplus
extern "C" {
#endif

int printf(const char* __restrict, ...);
int sprintf(char* __restrict, const char* __restrict, ...);
int putchar(int);
int puts(const char*);

#ifdef __cplusplus
}
#endif

#endif

printf

int printf(const char* format, ...);
Outputs a formatted string to the default terminal. Supports %s, %c, %d, %i, %u, %x, %X, and %p.

vsnprintf

int vsnprintf(char* str, size_t size, const char* format, va_list ap);
Writes a formatted string to a buffer with a size limit, preventing buffer overflows.

puts

int puts(const char* s);
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

void* memcpy(void* dest, const void* src, size_t n);
Copies n bytes from src to dest. The memory areas must not overlap.

memmove

void* memmove(void* dest, const void* src, size_t n);
Copies n bytes from src to dest. Memory areas may overlap.

memset

void* memset(void* s, int c, size_t n);
Fills the first n bytes of the memory area pointed to by s with the constant byte c.

memcmp

int memcmp(const void* s1, const void* s2, size_t n);
Compares the first n bytes of memory areas s1 and s2.

strlen

size_t strlen(const char* s);
Calculates the length of the string s, excluding the terminating null byte (\0).

strcmp

int strcmp(const char* s1, const char* s2);
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

int strncmp(const char* s1, const char* s2, size_t n);
Compares up to n characters of two strings.

strcpy

char* strcpy(char* dest, const char* src);
Copies a string from src to dest.

strncpy

char* strncpy(char* dest, const char* src, size_t n);
Copies up to n characters from src to dest.

strchr

char* strchr(const char* s, int c);
Finds the first occurrence of character c in string s.

strrchr

char* strrchr(const char* s, int c);
Finds the last occurrence of character c in string s.


3. stdlib.h (Standard Library)

Header File: stdlib.h

Code Preview: stdlib.h
#ifndef _STDLIB_H
#define _STDLIB_H 1

#include <sys/cdefs.h>

#ifdef __cplusplus
extern "C" {
#endif

__attribute__((__noreturn__))
void abort(void);

#ifdef __cplusplus
}
#endif

#endif

abort

void abort(void);
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.");
    }
}

References