Overview

The FreeWorld OS C Library is a complete implementation of the C standard library, equivalent to glibc. It provides all standard C library functions required for C program execution, including I/O, memory management, string operations, math functions, and more.

The library is implemented in x64 assembly for maximum performance and direct kernel integration. All functions follow the System V AMD64 ABI calling convention.

Components

stdio - Standard I/O

Complete file I/O and formatted I/O support:

  • File Operations: fopen, fclose, fread, fwrite, fseek, ftell
  • Formatted I/O: printf, fprintf, sprintf, scanf, fscanf
  • Character I/O: fgetc, fputc, getchar, putchar
  • String I/O: fgets, fputs, gets, puts
  • Stream Management: Buffering, flushing, error handling
  • Standard Streams: stdin, stdout, stderr

Files: libc/stdio/stdio_core.asm, libc/stdio/stdio_helpers.asm

stdlib - Standard Library

Memory allocation, conversions, and utility functions:

  • Memory Management: malloc, free, realloc, calloc
  • String Conversions: atoi, atol, atoll, strtol, strtoul
  • Integer Conversions: itoa, ltoa, ultoa
  • Random Numbers: rand, srand, random, srandom
  • Environment: getenv, setenv, unsetenv
  • Program Control: exit, abort, atexit
  • Search & Sort: bsearch, qsort

Files: libc/stdlib/stdlib_core.asm

string - String Functions

Complete string manipulation and memory operations:

  • String Length: strlen, strnlen
  • String Copy: strcpy, strncpy, strdup, strndup
  • String Concatenation: strcat, strncat
  • String Comparison: strcmp, strncmp, strcasecmp, strncasecmp
  • String Search: strchr, strrchr, strstr, strpbrk, strspn, strcspn
  • String Tokens: strtok, strtok_r
  • Memory Operations: memcpy, memmove, memset, memcmp, memchr

Files: libc/string/string_core.asm

math - Math Functions

Complete mathematical function library:

  • Trigonometric: sin, cos, tan, asin, acos, atan, atan2
  • Hyperbolic: sinh, cosh, tanh, asinh, acosh, atanh
  • Exponential/Logarithmic: exp, log, log10, log2, pow, sqrt, cbrt
  • Rounding: floor, ceil, trunc, round, nearbyint
  • Remainder: fmod, remainder, remquo
  • Absolute Value: fabs, fabsf, fabsl
  • Other: hypot, fma, copysign, nan, nextafter

Files: libc/math/math_core.asm

ctype - Character Classification

Character classification and conversion functions:

  • Classification: isalnum, isalpha, isdigit, isxdigit
  • Case: islower, isupper, isblank, isspace
  • Printability: isprint, isgraph, ispunct, iscntrl
  • Conversion: tolower, toupper

Files: libc/ctype/ctype_core.asm

time - Time Functions

Time and date manipulation:

  • Time Retrieval: time, gettimeofday, clock_gettime
  • Time Conversion: localtime, gmtime, mktime, timegm
  • Time Formatting: strftime, asctime, ctime
  • Time Arithmetic: difftime
  • Time Structures: tm structure, time_t type

Files: libc/time/time_core.asm

Implementation Details

Architecture

The C library is implemented entirely in x64 assembly (NASM) for:

  • Maximum performance and direct kernel integration
  • Full control over system calls and kernel interfaces
  • No external dependencies beyond the kernel
  • Optimized for FreeWorld OS architecture

Calling Convention

All functions follow the System V AMD64 ABI:

  • Integer arguments: RDI, RSI, RDX, RCX, R8, R9
  • Floating-point arguments: XMM0-XMM7
  • Return value: RAX (integer), XMM0 (floating-point)
  • Caller-saved registers: RAX, RCX, RDX, RSI, RDI, R8-R11, XMM0-XMM15
  • Stack alignment: 16-byte aligned

Memory Management

The library uses kernel memory allocation functions:

  • kmalloc - Allocate kernel memory
  • kfree - Free kernel memory
  • Heap management for user-space programs
  • Automatic alignment to 16-byte boundaries

System Calls

The library interfaces with kernel system calls:

  • syscall_open - Open files
  • syscall_read - Read from files
  • syscall_write - Write to files
  • syscall_close - Close files
  • syscall_lseek - Seek in files
  • syscall_time - Get system time
  • syscall_gettimeofday - Get time with microseconds

Build System

The C library is built using the Makefile in libc/Makefile:

make -C libc all

This produces lib/libc.a, a static library archive containing all C library functions.

To link against the library:

ld -o program program.o -Llibc/lib -lc

Usage Examples

File I/O

FILE *fp = fopen("file.txt", "r");
if (fp) {
    char buffer[256];
    fread(buffer, 1, 256, fp);
    fclose(fp);
}

String Operations

char *str = "Hello, World!";
size_t len = strlen(str);
char *copy = malloc(len + 1);
strcpy(copy, str);

Math Functions

double x = 3.14159;
double sin_x = sin(x);
double sqrt_x = sqrt(x);
double pow_x = pow(x, 2.0);

Time Functions

time_t now = time(NULL);
struct tm *tm_info = localtime(&now);
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", tm_info);

Status

Status: ✅ Complete

The C library is fully implemented and provides complete glibc-equivalent functionality for all standard C library components. All functions are optimized for x64 architecture and integrated with the FreeWorld OS kernel.