meta: move photon/libc to root

This commit is contained in:
2026-02-08 20:45:25 +00:00
parent f00e74260d
commit 345a37962e
140 changed files with 0 additions and 0 deletions

12
libc/string/fread.c Normal file
View File

@@ -0,0 +1,12 @@
#include <stdio.h>
#include <__fio.h>
size_t fread(void *ptr, size_t size, size_t count, FILE *fp)
{
if (!size || !count) {
return 0;
}
unsigned int written = __fio_read(fp, ptr, size * count);
return written / size;
}

0
libc/string/fwrite.c Normal file
View File

14
libc/string/memcmp.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stddef.h>
int memcmp(const void *a, const void *b, size_t n)
{
const unsigned char *s1 = a, *s2 = b;
for (size_t i = 0; i < n; i++) {
if (s1[i] != s2[i]) {
return ((s1[i] < s2[i]) ? -1 : 1);
}
}
return 0;
}

43
libc/string/memcpy.c Normal file
View File

@@ -0,0 +1,43 @@
#include <stddef.h>
#define ALIGNED(p) (!((long)p & (sizeof(long) - 1)))
#define QUADBLOCKSIZE (sizeof(long) << 2)
#define BLOCKSIZE (sizeof(long))
void *memcpy(void *dest, const void *src, size_t sz)
{
/* Only try word-writing if the pointers are aligned and
* the buffer is big enough */
if (sz >= BLOCKSIZE && ALIGNED(dest) && ALIGNED(src)) {
long *dest_word = dest;
const long *src_word = src;
/* First try copying 4x blocks per iteration */
while (sz >= QUADBLOCKSIZE) {
*dest_word++ = *src_word++;
*dest_word++ = *src_word++;
*dest_word++ = *src_word++;
*dest_word++ = *src_word++;
sz -= QUADBLOCKSIZE;
}
/* Then try copying 1x words per iteration */
while (sz >= BLOCKSIZE) {
*dest_word++ = *src_word++;
sz -= BLOCKSIZE;
}
/* Then go back to the byte-level copying */
dest = dest_word;
src = src_word;
}
char *dest0 = dest;
const char *src0 = src;
while (sz-- > 0) {
*dest0++ = *src0++;
}
return dest;
}

27
libc/string/memmove.c Normal file
View File

@@ -0,0 +1,27 @@
#include <string.h>
#define ALIGNED(p) (!((long)p & (sizeof(long) - 1)))
#define QUADBLOCKSIZE (sizeof(long) << 2)
#define BLOCKSIZE (sizeof(long))
static void *memcpy_r(void *dest, const void *src, size_t sz)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (size_t i = 0; i < sz; i++) {
size_t b = sz - i - 1;
d[b] = s[b];
}
return dest;
}
void *memmove(void *dest, const void *src, size_t n)
{
if (dest < src) {
return memcpy(dest, src, n);
} else {
return memcpy_r(dest, src, n);
}
}

28
libc/string/memset.c Normal file
View File

@@ -0,0 +1,28 @@
#include <stddef.h>
void *memset(void *ptr, int value, size_t sz)
{
value = (unsigned char)value;
unsigned int realval =
value |
(value << 8) |
(value << 16) |
(value << 24);
unsigned char *buf = ptr;
size_t i = 0;
for (i = 0; i < sz; i += sizeof(unsigned int)) {
unsigned int *t = (unsigned int *)&buf[i];
*t = realval;
}
if (sz % sizeof(unsigned int)) {
i -= sizeof(unsigned int);
while (i < sz) {
buf[i++] = value;
}
}
return ptr;
}

8
libc/string/strcat.c Normal file
View File

@@ -0,0 +1,8 @@
#include <string.h>
char *strcat(char *dest, const char *src)
{
size_t len = strlen(dest);
strcpy(dest + len, src);
return dest;
}

14
libc/string/strchr.c Normal file
View File

@@ -0,0 +1,14 @@
#include <string.h>
char *strchr(const char *str, int ch)
{
char c = ch;
for (size_t i = 0; str[i]; i++) {
if (str[i] == c) {
return (char *)&str[i];
}
}
return NULL;
}

25
libc/string/strcmp.c Normal file
View File

@@ -0,0 +1,25 @@
#include <stddef.h>
int strcmp(const char *s1, const char *s2)
{
int i;
for (i = 0; s1[i] == s2[i]; i++) {
if (s1[i] == '\0') {
return 0;
}
}
return s1[i] - s2[i];
}
int strncmp(const char *s1, const char *s2, size_t n)
{
for (; n > 0; s1++, s2++, --n) {
if (*s1 != *s2) {
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : 1);
} else if (*s1 == '\0') {
return 0;
}
}
return 0;
}

26
libc/string/strcpy.c Normal file
View File

@@ -0,0 +1,26 @@
char *strcpy(char *output, const char *input)
{
unsigned int i;
for (i = 0; input[i] != 0; i++) {
output[i] = input[i];
}
output[i] = '\0';
return output;
}
char *strncpy(char *output, const char *input, unsigned int count)
{
unsigned int size = count;
unsigned int i;
for (i = 0; i < size; i++) {
output[i] = input[i];
if (input[i] == 0) {
break;
}
}
output[i] = '\0';
return output;
}

15
libc/string/strcspn.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stddef.h>
size_t strcspn(const char *str1, const char *str2)
{
size_t i;
for (i = 0; str1[i]; i++) {
for (size_t ii = 0; str2[ii]; ii++) {
if (str1[i] == str2[ii]) {
return i;
}
}
}
return i;
}

11
libc/string/strdup.c Normal file
View File

@@ -0,0 +1,11 @@
#include <string.h>
#include <stdlib.h>
char *strdup(const char *str)
{
size_t len = strlen(str);
char *out = malloc(len + 1);
memcpy(out, str, len);
out[len] = 0;
return out;
}

152
libc/string/strerror.c Normal file
View File

@@ -0,0 +1,152 @@
#include <string.h>
#include <stdio.h>
#include <errno.h>
static const char *sys_errlist[] = {
[EPERM] = "Operation not permitted",
[ENOENT] = "No such file or directory",
[ESRCH] = "No such process",
[EINTR] = "Interrupted system call",
[EIO] = "Input/output error",
[ENXIO] = "No such device or address",
[E2BIG] = "Argument list too long",
[ENOEXEC] = "Exec format error",
[EBADF] = "Bad file descriptor",
[ECHILD] = "No child processes",
[EAGAIN] = "Resource temporarily unavailable",
[ENOMEM] = "Cannot allocate memory",
[EACCES] = "Permission denied",
[EFAULT] = "Bad address",
[ENOTBLK] = "Block device required",
[EBUSY] = "Device or resource busy",
[EEXIST] = "File exists",
[EXDEV] = "Invalid cross-device link",
[ENODEV] = "No such device",
[ENOTDIR] = "Not a directory",
[EISDIR] = "Is a directory",
[EINVAL] = "Invalid argument",
[ENFILE] = "Too many open files in system",
[EMFILE] = "Too many open files",
[ENOTTY] = "Inappropriate ioctl for device",
[ETXTBSY] = "Text file busy",
[EFBIG] = "File too large",
[ENOSPC] = "No space left on device",
[ESPIPE] = "Illegal seek",
[EROFS] = "Read-only file system",
[EMLINK] = "Too many links",
[EPIPE] = "Broken pipe",
[EDOM] = "Numerical argument out of domain",
[ERANGE] = "Numerical result out of range",
[EDEADLK] = "Resource deadlock avoided",
[ENAMETOOLONG] = "File name too long",
[ENOLCK] = "No locks available",
[ENOSYS] = "Function not implemented",
[ENOTEMPTY] = "Directory not empty",
[ELOOP] = "Too many levels of symbolic links",
[ENOMSG] = "No message of desired type",
[EIDRM] = "Identifier removed",
[ECHRNG] = "Channel number out of range",
[EL2NSYNC] = "Level 2 not synchronized",
[EL3HLT] = "Level 3 halted",
[EL3RST] = "Level 3 reset",
[ELNRNG] = "Link number out of range",
[EUNATCH] = "Protocol driver not attached",
[ENOCSI] = "No CSI structure available",
[EL2HLT] = "Level 2 halted",
[EBADE] = "Invalid exchange",
[EBADR] = "Invalid request descriptor",
[EXFULL] = "Exchange full",
[ENOANO] = "No anode",
[EBADRQC] = "Invalid request code",
[EBADSLT] = "Invalid slot",
[EBFONT] = "Bad font file format",
[ENOSTR] = "Device not a stream",
[ENODATA] = "No data available",
[ETIME] = "Timer expired",
[ENOSR] = "Out of streams resources",
[ENONET] = "Machine is not on the network",
[ENOPKG] = "Package not installed",
[EREMOTE] = "Object is remote",
[ENOLINK] = "Link has been severed",
[EADV] = "Advertise error",
[ESRMNT] = "Srmount error",
[ECOMM] = "Communication error on send",
[EPROTO] = "Protocol error",
[EMULTIHOP] = "Multihop attempted",
[EDOTDOT] = "RFS specific error",
[EBADMSG] = "Bad message",
[EOVERFLOW] = "Value too large for defined data type",
[ENOTUNIQ] = "Name not unique on network",
[EBADFD] = "File descriptor in bad state",
[EREMCHG] = "Remote address changed",
[ELIBACC] = "Can not access a needed shared library",
[ELIBBAD] = "Accessing a corrupted shared library",
[ELIBSCN] = ".lib section in a.out corrupted",
[ELIBMAX] = "Attempting to link in too many shared libraries",
[ELIBEXEC] = "Cannot exec a shared library directly",
[EILSEQ] = "Invalid or incomplete multibyte or wide character",
[ERESTART] = "Interrupted system call should be restarted",
[ESTRPIPE] = "Streams pipe error",
[EUSERS] = "Too many users",
[ENOTSOCK] = "Socket operation on non-socket",
[EDESTADDRREQ] = "Destination address required",
[EMSGSIZE] = "Message too long",
[EPROTOTYPE] = "Protocol wrong type for socket",
[ENOPROTOOPT] = "Protocol not available",
[EPROTONOSUPPORT] = "Protocol not supported",
[ESOCKTNOSUPPORT] = "Socket type not supported",
[ENOTSUP] = "Operation not supported",
[EPFNOSUPPORT] = "Protocol family not supported",
[EAFNOSUPPORT] = "Address family not supported by protocol",
[EADDRINUSE] = "Address already in use",
[EADDRNOTAVAIL] = "Cannot assign requested address",
[ENETDOWN] = "Network is down",
[ENETUNREACH] = "Network is unreachable",
[ENETRESET] = "Network dropped connection on reset",
[ECONNABORTED] = "Software caused connection abort",
[ECONNRESET] = "Connection reset by peer",
[ENOBUFS] = "No buffer space available",
[EISCONN] = "Transport endpoint is already connected",
[ENOTCONN] = "Transport endpoint is not connected",
[ESHUTDOWN] = "Cannot send after transport endpoint shutdown",
[ETOOMANYREFS] = "Too many references: cannot splice",
[ETIMEDOUT] = "Connection timed out",
[ECONNREFUSED] = "Connection refused",
[EHOSTDOWN] = "Host is down",
[EHOSTUNREACH] = "No route to host",
[EALREADY] = "Operation already in progress",
[EINPROGRESS] = "Operation now in progress",
[ESTALE] = "Stale file handle",
[EUCLEAN] = "Structure needs cleaning",
[ENOTNAM] = "Not a XENIX named type file",
[ENAVAIL] = "No XENIX semaphores available",
[EISNAM] = "Is a named type file",
[EREMOTEIO] = "Remote I/O error",
[EDQUOT] = "Disk quota exceeded",
[ENOMEDIUM] = "No medium found",
[EMEDIUMTYPE] = "Wrong medium type",
[ECANCELED] = "Operation canceled",
[ENOKEY] = "Required key not available",
[EKEYEXPIRED] = "Key has expired",
[EKEYREVOKED] = "Key has been revoked",
[EKEYREJECTED] = "Key was rejected by service",
[EOWNERDEAD] = "Owner died",
[ENOTRECOVERABLE] = "State not recoverable",
[ERFKILL] = "Operation not possible due to RF-kill",
[EHWPOISON] = "Memory page has hardware error",
};
static int n_sys_errlist = sizeof sys_errlist / sizeof sys_errlist[0];
static char g_msg_buf[128];
char *strerror(int errnum)
{
if (errnum <= 0 || errnum >= n_sys_errlist) {
snprintf(g_msg_buf, sizeof g_msg_buf, "Unknown error: %d", errnum);
} else {
strncpy(g_msg_buf, sys_errlist[errnum], sizeof g_msg_buf - 1);
g_msg_buf[sizeof g_msg_buf - 1] = '\0';
}
return g_msg_buf;
}

11
libc/string/strlen.c Normal file
View File

@@ -0,0 +1,11 @@
#include <string.h>
size_t strlen(const char *str)
{
const char *start = str;
while (*str) {
str++;
}
return str - start;
}

15
libc/string/strrchr.c Normal file
View File

@@ -0,0 +1,15 @@
#include <string.h>
char *strrchr(const char *s, int c)
{
/* include null term */
long sz = strlen(s) + 1;
for (long i = sz; i >= 0; i--) {
if (s[i] == c) {
return (char *)(s + i);
}
}
return NULL;
}

62
libc/string/strtok.c Normal file
View File

@@ -0,0 +1,62 @@
#include <string.h>
static char *global_sp = NULL;
char *strtok(char *str, const char *delim)
{
return strtok_r(str, delim, &global_sp);
}
char *strtok_r(char *str, const char *delim, char **sp)
{
int i = 0;
int len = strlen(delim);
if (len == 0) {
return NULL;
}
if (!str && !sp) {
return NULL;
}
if (str) {
*sp = str;
}
char *p_start = *sp;
while (1) {
for (i = 0; i < len; i++) {
if (*p_start == delim[i]) {
p_start++;
break;
}
}
if (i == len) {
*sp = p_start;
break;
}
}
if (**sp == '\0') {
*sp = NULL;
return *sp;
}
while (**sp != '\0') {
for (i = 0; i < len; i++) {
if (**sp == delim[i]) {
**sp = '\0';
break;
}
}
(*sp)++;
if (i < len) {
break;
}
}
return p_start;
}