Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/CRISTOP-bot/cris-os-v2/llms.txt

Use this file to discover all available pages before exploring further.

kstring.h provides the kernel’s freestanding string and memory utilities. Because CrisOS v2 is compiled with -nostdlib, there is no <string.h> or <stdio.h>. All string and memory operations kernel-wide use these functions instead, ensuring no dependency on any C runtime. Include kstring.h in any kernel translation unit that needs string manipulation, comparison, or integer-to-string conversion.

String Length

kstrlen

size_t kstrlen(const char *s);
Returns the number of bytes in s before the terminating NUL character. Equivalent to libc strlen.
s
const char *
required
NUL-terminated string to measure.
Returns: The length of s in bytes, not including the NUL terminator.

String Comparison

kstrcmp

int kstrcmp(const char *a, const char *b);
Compares two NUL-terminated strings lexicographically using unsigned byte values. Equivalent to libc strcmp.
a
const char *
required
First NUL-terminated string.
b
const char *
required
Second NUL-terminated string.
Returns: A negative integer if a < b, 0 if a == b, or a positive integer if a > b.

kstrncmp

int kstrncmp(const char *a, const char *b, size_t n);
Compares at most n bytes of two strings. Stops at the first differing byte or at a NUL terminator in either string. Equivalent to libc strncmp.
a
const char *
required
First string.
b
const char *
required
Second string.
n
size_t
required
Maximum number of bytes to compare.
Returns: A negative integer if a < b, 0 if equal within n bytes, or a positive integer if a > b.

String Copy and Concatenation

kstrcpy

char *kstrcpy(char *dest, const char *src, size_t maxlen);
Copies src into dest, writing at most maxlen - 1 characters and always NUL-terminating the result. Similar to BSD strlcpy rather than strcpy — the destination is always NUL-terminated regardless of whether src was truncated.
dest
char *
required
Destination buffer.
src
const char *
required
NUL-terminated source string.
maxlen
size_t
required
Total size of the destination buffer in bytes, including space for the NUL terminator. If maxlen is 0, nothing is written.
Returns: dest.
Unlike strcpy, kstrcpy will never write beyond maxlen bytes into dest. The result is always NUL-terminated when maxlen > 0.

kstrcat

char *kstrcat(char *dest, const char *src, size_t maxlen);
Appends src to the end of dest. maxlen is the total size of the destination buffer (not the number of bytes to append). The result is always NUL-terminated when maxlen > 0 and dest already contains a NUL within the buffer.
dest
char *
required
Destination buffer containing a NUL-terminated string to append to.
src
const char *
required
NUL-terminated string to append.
maxlen
size_t
required
Total capacity of the destination buffer in bytes. Appending stops before the buffer would be exceeded.
Returns: dest.
maxlen is the full buffer size, not the remaining free space. This differs from libc strncat, where the third argument is the maximum number of bytes to append.

kstrstr

const char *kstrstr(const char *haystack, const char *needle);
Finds the first occurrence of the substring needle within haystack. Equivalent to libc strstr.
haystack
const char *
required
NUL-terminated string to search within.
needle
const char *
required
NUL-terminated substring to find. If empty, haystack is returned immediately.
Returns: A pointer to the first occurrence of needle in haystack; NULL if not found.

kstrchr

const char *kstrchr(const char *s, char c);
Finds the first occurrence of character c in s. Equivalent to libc strchr. Does not search past the NUL terminator.
s
const char *
required
NUL-terminated string to search.
c
char
required
Character to search for.
Returns: A pointer to the first occurrence of c in s; NULL if not found.

Memory Operations

kmemset

void *kmemset(void *s, int c, size_t n);
Fills the first n bytes of s with the byte value (unsigned char)c. Equivalent to libc memset.
s
void *
required
Pointer to the memory region to fill.
c
int
required
Byte value to fill with (only the low 8 bits are used).
n
size_t
required
Number of bytes to fill.
Returns: s.

kmemcpy

void *kmemcpy(void *dest, const void *src, size_t n);
Copies n bytes from src to dest. The regions must not overlap. Equivalent to libc memcpy.
dest
void *
required
Destination memory region.
src
const void *
required
Source memory region. Must not overlap with dest.
n
size_t
required
Number of bytes to copy.
Returns: dest.

kmemcmp

int kmemcmp(const void *a, const void *b, size_t n);
Compares the first n bytes of two memory regions using unsigned byte values. Equivalent to libc memcmp.
a
const void *
required
First memory region.
b
const void *
required
Second memory region.
n
size_t
required
Number of bytes to compare.
Returns: A negative integer if a < b, 0 if both regions are identical, or a positive integer if a > b.

Integer Conversion

kitoa

void kitoa(long value, char *buf, size_t maxlen);
Converts the signed long integer value to its decimal string representation and writes it into buf. Negative values are prefixed with '-'. The result is always NUL-terminated when maxlen > 0. There is no direct libc equivalent; the closest standard function is snprintf(buf, maxlen, "%ld", value).
value
long
required
The signed integer to convert.
buf
char *
required
Output buffer to write the decimal string into.
maxlen
size_t
required
Total capacity of buf in bytes. The output is silently truncated if maxlen is too small. If maxlen is 0, nothing is written.
Returns: Nothing (void).

Inline Helpers

These functions are defined as static inline in kstring.h and generate no separate object code.

kskip_spaces

static inline const char *kskip_spaces(const char *s);
Advances s past any leading whitespace characters: space (' '), horizontal tab ('\t'), carriage return ('\r'), and newline ('\n').
s
const char *
required
Pointer into a string, positioned at or before the first non-whitespace character.
Returns: A pointer to the first character in s that is not a whitespace character; or a pointer to the NUL terminator if s is entirely whitespace.

kstreq

static inline int kstreq(const char *a, const char *b);
Tests two NUL-terminated strings for equality. This is a convenience wrapper; unlike kstrcmp, which returns 0 for equal strings, kstreq returns 1 for equal strings and 0 for unequal — matching a boolean convention and avoiding the inverted-zero check.
a
const char *
required
First NUL-terminated string.
b
const char *
required
Second NUL-terminated string.
Returns: 1 if a and b are identical; 0 otherwise.
kstreq returns 1 for equality, which is the opposite sign convention from kstrcmp. Use kstreq when you want a boolean result and kstrcmp when you need ordering information.

Usage Examples

#include "kstring.h"
#include "console.h"

/* Convert an integer result to a string and print it */
char buf[32];
kitoa(sum, buf, sizeof(buf));
console_print(buf);

/* Boolean string comparison in a command dispatcher */
if (kstreq(cmd, "help")) {
    /* handle help command */
}

/* Strip leading whitespace from shell input */
const char *p = kskip_spaces(input);

/* Safe bounded copy into a fixed-size kernel buffer */
char name[VFS_NAME_SIZE];
kstrcpy(name, user_input, sizeof(name));

/* Append a suffix with overflow protection */
char path[128];
kstrcpy(path, "/home/", sizeof(path));
kstrcat(path, username, sizeof(path));

Build docs developers (and LLMs) love