11#pragma once
22
3+ /* Compact C standard library
4+ *
5+ * Provides essential C library functions for embedded systems including
6+ * string manipulation, memory management, character classification,
7+ * and basic I/O operations.
8+ */
9+
310#include <hal.h>
411
12+ /* Basic Type Definitions */
513#ifndef NULL
614#define NULL ((void *) 0) /* Standard NULL definition */
715#endif
@@ -10,25 +18,31 @@ typedef _Bool bool;
1018#define false ((bool) 0)
1119#define true ((bool) 1)
1220
13- /* Character classification macros */
14- #define isprint (c ) (' ' <= (c) && (c) <= '~')
15- #define isspace (c ) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
16- #define isdigit (c ) ('0' <= (c) && (c) <= '9')
17- #define islower (c ) ('a' <= (c) && (c) <= 'z')
18- #define isupper (c ) ('A' <= (c) && (c) <= 'Z')
19- #define isalpha (c ) (islower(c) || isupper(c))
20- #define isalnum (c ) (isalpha(c) || isdigit(c))
21+ /* Utility Macros */
2122#define min (a , b ) ((a) < (b) ? (a) : (b))
2223#define max (a , b ) ((a) > (b) ? (a) : (b))
2324
24- /* Endianness conversion macros (standard network to host byte order) */
25+ /* Character Classification Macros */
26+ #define isprint (c ) (' ' <= (c) && (c) <= '~') /* Printable character */
27+ #define isspace (c ) \
28+ ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r') /* Whitespace */
29+ #define isdigit (c ) ('0' <= (c) && (c) <= '9') /* Decimal digit */
30+ #define islower (c ) ('a' <= (c) && (c) <= 'z') /* Lowercase letter */
31+ #define isupper (c ) ('A' <= (c) && (c) <= 'Z') /* Uppercase letter */
32+ #define isalpha (c ) (islower(c) || isupper(c)) /* Alphabetic character */
33+ #define isalnum (c ) (isalpha(c) || isdigit(c)) /* Alphanumeric character */
34+
35+ /* Endianness Conversion Macros */
2536#if __BYTE_ORDER == __BIG_ENDIAN
37+ /* Big-endian: no conversion needed */
2638#define htons (n ) (n)
2739#define ntohs (n ) (n)
2840#define htonl (n ) (n)
2941#define ntohl (n ) (n)
3042
3143#else /* __LITTLE_ENDIAN */
44+ /* Little-endian: byte swapping required */
45+
3246/* 16-bit byte swap */
3347#define htons (n ) \
3448 (((((uint16_t) (n) & 0x00FF)) << 8) | (((uint16_t) (n) & 0xFF00) >> 8))
@@ -54,6 +68,7 @@ typedef _Bool bool;
5468 ? (x) \
5569 : (((uint64_t) htonl((uint32_t) ((x) & 0xFFFFFFFFULL))) << 32) | \
5670 htonl((uint32_t) (((x) >> 32) & 0xFFFFFFFFULL)))
71+
5772#define ntohll (x ) \
5873 ((1 == ntohl(1)) \
5974 ? (x) \
@@ -62,53 +77,76 @@ typedef _Bool bool;
6277
6378#endif /* __BYTE_ORDER */
6479
65- /* String manipulation functions */
80+ /* String Manipulation Functions */
81+
82+ /* String copying and concatenation */
6683char * strcpy (char * s1 , const char * s2 );
6784char * strncpy (char * s1 , const char * s2 , int32_t n );
6885char * strcat (char * s1 , const char * s2 );
6986char * strncat (char * s1 , const char * s2 , int32_t n );
87+
88+ /* String comparison */
7089int32_t strcmp (const char * s1 , const char * s2 );
7190int32_t strncmp (const char * s1 , const char * s2 , int32_t n );
91+
92+ /* String searching */
7293char * strstr (const char * s1 , const char * s2 );
73- size_t strlen (const char * s1 );
7494char * strchr (const char * s1 , int32_t c );
7595char * strpbrk (const char * s1 , const char * s2 );
96+
97+ /* String length */
98+ size_t strlen (const char * s1 );
99+
100+ /* String tokenization */
76101char * strsep (char * * pp , const char * delim );
77102char * strtok (char * s , const char * delim );
78103char * strtok_r (char * s , const char * delim , char * * holder );
79104
80- /* Memory manipulation functions */
105+ /* Memory Manipulation Functions */
106+
107+ /* Memory copying and moving */
81108void * memcpy (void * dst , const void * src , uint32_t n );
82109void * memmove (void * dst , const void * src , uint32_t n );
110+
111+ /* Memory comparison and initialization */
83112int32_t memcmp (const void * cs , const void * ct , uint32_t n );
84113void * memset (void * s , int32_t c , uint32_t n );
114+
115+ /* Mathematical Functions */
85116int32_t abs (int32_t n ); /* Absolute value */
86117
87- /* Character classification and conversion functions */
118+ /* String to Number Conversion */
88119int32_t strtol (const char * s , char * * end , int32_t base );
89120int32_t atoi (const char * s );
90121void itoa (int32_t i , char * s , int32_t base ); /* Integer to ASCII conversion */
91122
92- /* Random number generation */
123+ /* Random Number Generation */
93124
94125#ifndef RAND_MAX
95126#define RAND_MAX 32767U /* Default max value for random() */
96127#endif
97128
129+ /* Simple random number generator */
98130int32_t random (void );
99131void srand (uint32_t seed );
100132
101- /* POSIX-style opaque container for re-entrant random number generator state */
133+ /* POSIX-style re-entrant random number generator */
102134struct random_data {
103- uint32_t state ; /* state must never be zero */
135+ uint32_t state ; /* State must never be zero */
104136};
105137int random_r (struct random_data * buf , int32_t * result );
106138
107- /* Input/Output functions */
139+ /* Input/Output Functions */
140+
141+ /* Character and string output */
108142int32_t puts (const char * str );
143+
144+ /* Character and string input */
109145int getchar (void );
110146char * gets (char * s );
111147char * fgets (char * s , int n , void * f );
112148char * getline (char * s );
149+
150+ /* Formatted output */
113151int32_t printf (const char * fmt , ...);
114152int32_t sprintf (char * out , const char * fmt , ...);
0 commit comments