update libs

This commit is contained in:
Freya Murphy 2024-01-27 03:01:34 -05:00
parent 434c159462
commit edc40ed8b1
Signed by: freya
GPG key ID: 744AB800E383AE52
3 changed files with 270 additions and 12 deletions

View file

@ -1,3 +1,4 @@
#pragma once
enum acpi_status { enum acpi_status {
ACPI_SUCCESS = 0, ACPI_SUCCESS = 0,

View file

@ -1,12 +1,60 @@
#pragma once
/**
* The strcmp() function compares the two strings s1 and s2. The locale is not taken into account
* (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters.
*/
int strncmp(const char *s1, const char *s2, unsigned long n);
/** /**
* The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory * The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory
* areas s1 and s2. * areas s1 and s2.
*/ */
int memcmp(const void *s1, const void *s2, unsigned long n); extern int memcmp(const void *restrict s1, const void *restrict s2, unsigned long n);
/**
* The memcpy() function copies n bytes from memory area src to memory area dest.
* The memory areas must not overlap.
*/
extern void *memcpy(void *restrict dest, const void *restrict src, unsigned long n);
/**
* The memmove() function copies n bytes from memory area src to memory area dest. The memory areas
* may overlap: copying takes place as though the bytes in src are first copied into a temporary array
* that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.
*/
extern void *memmove(void *dest, const void *src, unsigned long n);
/**
* The memset() function fills the first n bytes of the memory area pointed to by s with the constant
* byte c.
*/
extern void *memset(void *restrict dest, int c, unsigned long n);
/**
* The strcmp() function compares the two strings s1 and s2. The locale is not taken into account
* (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters.
*/
extern int strncmp(const char *restrict s1, const char *restrict s2, unsigned long n);
/**
* @returns 1 if c is a space
*/
int isspace(int c);
/**
* @returns 1 if c is a digit (0 - 9)
*/
int isdigit(int c);
char itoc(int i);
int ctoi(char c);
int atoi(const char* s);
long int atol(const char* s);
long long int atoll(const char* s);
char *itoa(int n, char *buffer, int radix);
char *ltoa(long int n, char *buffer, int radix);
char *utoa(unsigned int n, char *buffer, int radix);
char *ultoa(unsigned long int n, char *buffer, int radix);
char *ftoa(float f, char *buffer);
int strtoi(const char *str, char **endptr, int base);
long int strtol(const char *str, char **endptr, int base);
long long int strtoll(const char *str, char **endptr, int base);

219
src/lib.c
View file

@ -1,14 +1,223 @@
#include <lib.h> #include <lib.h>
int strncmp(const char *lhs, const char *rhs, unsigned long n) { #include <stddef.h>
int memcmp(const void *restrict vl, const void *restrict vr, unsigned long n) {
const unsigned char *l = vl, *r = vr;
for (; n && *l == *r; n--, l++, r++);
return n ? *l-*r : 0;
}
void *memcpy(void *restrict dest, const void *restrict src, unsigned long n) {
char *d = dest;
const char *s = src;
for (; n; n--) *d++ = *s++;
return dest;
}
void *memmove(void *dest, const void *src, unsigned long n) {
char *d = dest;
const char *s = src;
if (d==s) return d;
if (d<s) {
for (; n; n--) *d++ = *s++;
} else {
while (n) n--, d[n] = s[n];
}
return dest;
}
void *memset(void *restrict dest, int c, unsigned long n) {
unsigned char *d = dest;
for (; n; n--) *d++ = c;
return dest;
}
int strncmp(const char *restrict lhs, const char *restrict rhs, unsigned long n) {
const unsigned char *l=(void *)lhs, *r=(void *)rhs; const unsigned char *l=(void *)lhs, *r=(void *)rhs;
if (!n--) return 0; if (!n--) return 0;
for (; *l && *r && n && *l == *r ; l++, r++, n--); for (; *l && *r && n && *l == *r ; l++, r++, n--);
return *l - *r; return *l - *r;
} }
int memcmp(const void *vl, const void *vr, unsigned long n) { int isspace(int c) {
const unsigned char *l = vl, *r = vr; switch (c) {
for (; n && *l == *r; n--, l++, r++); case ' ':
return n ? *l-*r : 0; case '\t':
case '\v':
case '\f':
case '\r':
case '\n':
return 1;
default:
return 0;
} }
}
int isdigit(int c) {
return c - '0' > -1 && c - '0' < 10;
}
#define ATOX(name, type) \
type name(const char* s) { \
for(; isspace(*s); s++); \
int neg = 0; \
switch (*s) { \
case '-': \
neg = 1; \
/* FALLTHRU */ \
case '+': \
s++; \
break; \
} \
type num = 0; \
for (; *s == '0'; s++); \
for (; isdigit(*s); s++) { \
num *= 10; \
num += *s - '0'; \
} \
return num * (neg ? -1 : 1); \
}
ATOX(atoi, int)
ATOX(atol, long int)
ATOX(atoll, long long int)
char itoc(int i) {
if(i < 10) {
return '0' + i;
} else {
return 'a' + (i - 10);
}
}
int ctoi(char c) {
if(c < 'A') {
return c - '0';
} else if(c < 'a') {
return c - 'A' + 10;
} else {
return c - 'a' + 10;
}
}
#define UXTOA(type, name) \
char *name(unsigned type n, char *buffer, int radix) { \
if (n == 0) { \
buffer[0] = '0'; \
buffer[1] = '\0'; \
return buffer; \
} \
char *start = buffer; \
for (; n; n /= radix) { \
*buffer++ = itoc(n % radix); \
} \
*buffer-- = '\0'; \
while(buffer > start) { \
char tmp = *start; \
*start++ = *buffer; \
*buffer-- = tmp; \
} \
return buffer; \
}
UXTOA(int, utoa)
UXTOA(long int, ultoa)
#define XTOA(type, name) \
char *name(type n, char* buffer, int radix) { \
if (n == 0) { \
buffer[0] = '0'; \
buffer[1] = '\0'; \
return buffer; \
} \
if (n < 0) { \
*buffer++ = '-'; \
n = -n; \
} \
char *start = buffer; \
for (; n; n /= radix) { \
*buffer++ = itoc(n % radix); \
} \
*buffer-- = '\0'; \
while(buffer > start) { \
char tmp = *start; \
*start++ = *buffer; \
*buffer-- = tmp; \
} \
return buffer; \
}
XTOA(int, itoa)
XTOA(long int, ltoa)
char *ftoa(float f, char *buffer) {
char *b = buffer;
if (f < 0 || f == -0)
*buffer++ = '-';
int n, i=0, k=0;
n = (int) f;
while (n > 0) {
f /= 10;
n = (int)f;
i++;
}
*(buffer+i) = '.';
f *= 10;
n = (int) f;
f -= n;
while ((n > 0.1) || (i > k)) {
if (k == i)
k++;
*(buffer+k) = '0' + n;
f *= 10;
n = (int) f;
f -= n;
k++;
}
*(buffer+k) = '\0';
return b;
}
#define STRTOX(name, type) \
type name(const char *s, char **endptr, int radix) { \
*endptr = NULL; \
for(; isspace(*s); s++); \
int neg = 0; \
switch (*s) { \
case '-': \
neg = 1; \
/* FALLTHRU */ \
case '+': \
s++; \
break; \
} \
if (!radix) { \
if (*s == '0') { \
s++; \
if (*s == 'x' || *s == 'X') { \
s++; \
radix = 16; \
} else { \
radix = 8; \
} \
} else { \
radix = 10; \
} \
} \
for (; *s == '0'; s++, *endptr = (void*) s); \
type num = 0; \
for (; isdigit(*s); s++, *endptr = (void*) s) { \
num *= radix; \
num += *s - '0'; \
} \
return num * (neg ? -1 : 1); \
}
STRTOX(strtoi, int)
STRTOX(strtol, long int)
STRTOX(strtoll, long long int)