更新libc

This commit is contained in:
XIAOYI12 2024-09-01 16:43:58 +08:00
parent fb7818c44f
commit 1327ae7724
20 changed files with 246 additions and 118 deletions

View File

@ -1,6 +1,11 @@
#ifndef CRASHPOWEROS_CTYPE_H #ifndef CRASHPOWEROS_CTYPE_H
#define CRASHPOWEROS_CTYPE_H #define CRASHPOWEROS_CTYPE_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
int ispunct(char ch); int ispunct(char ch);
char toupper(char ch); char toupper(char ch);
char tolower(char ch); char tolower(char ch);

View File

@ -4,6 +4,16 @@
#define MIN(i, j) (((i) < (j)) ? (i) : (j)) #define MIN(i, j) (((i) < (j)) ? (i) : (j))
#define MAX(i, j) (((i) > (j)) ? (i) : (j)) #define MAX(i, j) (((i) > (j)) ? (i) : (j))
#define F32_EPSILON 1e-5f
#define F64_EPSILON 1e-10
#define PI 3.14159265358979323846264338327950288
#define E 2.718281828459045235360287
#define SQRT2 1.41421356237309504880168872420969807
#define PHI 1.61803398874989484820458683436563811772030917980576
#include <stdint.h> #include <stdint.h>
void srandlevel(unsigned short randlevel_); void srandlevel(unsigned short randlevel_);
@ -15,5 +25,13 @@ double pow(double a,long long b);
unsigned long long ull_pow(unsigned long long a,unsigned long long b); unsigned long long ull_pow(unsigned long long a,unsigned long long b);
double sqrt(double x); double sqrt(double x);
float q_sqrt(float number); float q_sqrt(float number);
double mod(double x, double y);
double sin(double x);
double cos(double x);
double tan(double x);
double asin(double x);
double acos(double x);
double atan(double x);
double atan2(double y, double x);
#endif #endif

View File

@ -10,9 +10,7 @@
#define BIN 0x0 #define BIN 0x0
#define PLUS 0x10 #define PLUS 0x10
#include <stdint.h> #include "ctype.h"
#include <stddef.h>
#include <stdarg.h>
typedef struct FILE { typedef struct FILE {
unsigned int mode; unsigned int mode;

View File

@ -1,7 +1,7 @@
#ifndef CRASHPOWEROS_STDLIB_H #ifndef CRASHPOWEROS_STDLIB_H
#define CRASHPOWEROS_STDLIB_H #define CRASHPOWEROS_STDLIB_H
#include <stddef.h> #include "ctype.h"
long long atoi(const char* s); long long atoi(const char* s);
void *malloc(size_t size); void *malloc(size_t size);

View File

@ -1,8 +1,7 @@
#ifndef CRASHPOWEROS_STRING_H #ifndef CRASHPOWEROS_STRING_H
#define CRASHPOWEROS_STRING_H #define CRASHPOWEROS_STRING_H
#include <stddef.h> #include "ctype.h"
#include <stdint.h>
const char* memchr(const char* buf,char c,unsigned long long count); const char* memchr(const char* buf,char c,unsigned long long count);
void *memmove(void *dest, const void *src, size_t num); void *memmove(void *dest, const void *src, size_t num);

View File

@ -13,8 +13,7 @@
#define SYSCALL_VFS_READFILE 10 #define SYSCALL_VFS_READFILE 10
#define SYSCALL_VFS_WRITEFILE 11 #define SYSCALL_VFS_WRITEFILE 11
#include <stdint.h> #include "ctype.h"
#include <stddef.h>
void syscall_print(char* c); void syscall_print(char* c);
void syscall_putchar(char c); void syscall_putchar(char c);

View File

@ -1,4 +1,4 @@
OBJS_PACK = out/syscall.obj out/print.obj out/string.obj out/libc.obj OBJS_PACK = out/syscall.obj out/print.obj out/string.obj out/libc.obj out/math.obj
default : $(OBJS_PACK) default : $(OBJS_PACK)

View File

@ -1,4 +1,5 @@
#include "../include/math.h" #include "../include/math.h"
#include "../include/ctype.h"
static unsigned long long rand_seed = 1 ; static unsigned long long rand_seed = 1 ;
static unsigned short max_bit = 32 ; static unsigned short max_bit = 32 ;
@ -75,3 +76,80 @@ float q_sqrt(float number){
y = y * (f - (x * y * y)) ; y = y * (f - (x * y * y)) ;
return number * y ; return number * y ;
} }
double mod(double x, double y){
return x - (int32_t)(x / y) * y;
}
double sin(double x){
x = mod(x, 2 * PI);
double sum = x;
double term = x;
int n = 1;
bool sign = true;
while (term > F64_EPSILON || term < -F64_EPSILON) {
n += 2;
term *= x * x / (n * (n - 1));
sum += sign ? -term : term;
sign = !sign;
}
return sum;
}
double cos(double x){
x = mod(x, 2 * PI);
double sum = 1;
double term = 1;
int n = 0;
bool sign = true;
while (term > F64_EPSILON || term < -F64_EPSILON) {
n += 2;
term *= x * x / (n * (n - 1));
sum += sign ? -term : term;
sign = !sign;
}
return sum;
}
double tan(double x) {
return sin(x) / cos(x);
}
double asin(double x){
double sum = x;
double term = x;
int n = 1;
while (term > F64_EPSILON || term < -F64_EPSILON) {
term *= (x * x * (2 * n - 1) * (2 * n - 1)) / (2 * n * (2 * n + 1));
sum += term;
n++;
}
return sum;
}
double acos(double x) {
return PI / 2 - asin(x);
}
double atan(double x){
double sum = x;
double term = x;
int n = 1;
bool sign = true;
while (term > F64_EPSILON || term < -F64_EPSILON) {
term *= x * x * (2 * n - 1) / (2 * n + 1);
sum += sign ? -term : term;
sign = !sign;
n++;
}
return sum;
}
double atan2(double y, double x){
if (x > 0) return atan(y / x);
if (x < 0 && y >= 0) return atan(y / x) + PI;
if (x < 0 && y < 0) return atan(y / x) - PI;
if (x == 0 && y > 0) return PI / 2;
if (x == 0 && y < 0) return -PI / 2;
return 0;
}

View File

@ -211,4 +211,4 @@ char *strdup(const char *str) {
while ((*ret++ = *strat++) != '\0') {} while ((*ret++ = *strat++) != '\0') {}
return ret - (len + 1); return ret - (len + 1);
} }

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include "../include/syscall.h" #include "../include/syscall.h"
#include "../include/string.h" #include "../include/string.h"
#include "../include/math.h"
static int gets(char *buf, int buf_size) { static int gets(char *buf, int buf_size) {
int index = 0; int index = 0;
@ -49,6 +50,10 @@ int main(){
char *argv[50]; char *argv[50];
int argc = -1; int argc = -1;
char *buffer[255]; char *buffer[255];
double x = sin(12);
printf("%08f",x);
while (1){ while (1){
syscall_get_cd(buffer); syscall_get_cd(buffer);
printf("%s$ ",buffer); printf("%s$ ",buffer);

View File

@ -32,7 +32,7 @@ char read_serial() {
int is_transmit_empty() { return io_in8(SERIAL_PORT + 5) & 0x20; } int is_transmit_empty() { return io_in8(SERIAL_PORT + 5) & 0x20; }
void write_serial(char a) { void write_serial(char a) {
return; //return;
while (is_transmit_empty() == 0); while (is_transmit_empty() == 0);
io_out8(SERIAL_PORT, a); io_out8(SERIAL_PORT, a);
} }

View File

@ -62,11 +62,6 @@ void vbe_scroll() {
void vbe_draw_char(char c, int32_t x, int32_t y) { void vbe_draw_char(char c, int32_t x, int32_t y) {
if (c == ' ') { if (c == ' ') {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 9; j++) {
//screen[(y + i) * width + x + j] = back_color;
}
}
return; return;
} }
@ -79,7 +74,7 @@ void vbe_draw_char(char c, int32_t x, int32_t y) {
for (int j = 0; j < 9; j++) { for (int j = 0; j < 9; j++) {
if (font[i] & (0x80 >> j)) { if (font[i] & (0x80 >> j)) {
screen[(y + i) * width + x + j] = color; screen[(y + i) * width + x + j] = color;
} //else screen[(y + i) * width + x + j] = back_color; }
} }
} }
} }
@ -187,6 +182,8 @@ void initVBE(multiboot_t *info) {
c_width = width / 9; c_width = width / 9;
c_height = height / 16; c_height = height / 16;
logkf("SCREEN BASS: %08x\n",screen);
vbe_clear(); vbe_clear();
Bmp *bmp = (Bmp*) &logo_bmp; Bmp *bmp = (Bmp*) &logo_bmp;

View File

@ -288,13 +288,7 @@ l9660_status l9660_read(l9660_file *f, void *buf, size_t size, size_t *read) {
return L9660_OK; return L9660_OK;
} }
char *strdup(const char *s) {
size_t l = strlen(s);
char *d = kmalloc(l + 1);
if (!d)
return NULL;
return memcpy(d, s, l + 1);
}
bool read_sector(l9660_fs *fs, void *buf, uint32_t sector) { bool read_sector(l9660_fs *fs, void *buf, uint32_t sector) {
return CDROM_Read(sector, 1, buf, fs->disk_number); return CDROM_Read(sector, 1, buf, fs->disk_number);
} }

View File

@ -32,6 +32,10 @@ typedef int bool;
#define false 0 #define false 0
#define EOF -1 #define EOF -1
char* replaceAll(char* src, char* find, char* replaceWith);
char *strstr(char *str1, char *str2);
char *strncpy(char *dest, const char *src, unsigned long long count);
char *strdup(const char *str);
unsigned int rand(void); unsigned int rand(void);
void srand(unsigned long seed); void srand(unsigned long seed);
void insert_char(char* str, int pos, char ch); void insert_char(char* str, int pos, char ch);
@ -59,6 +63,8 @@ int vsprintf(char *buf, const char *fmt, va_list args);
int sprintf(char *buf, const char *fmt, ...); int sprintf(char *buf, const char *fmt, ...);
long int strtol(const char *str,char **endptr,int base); long int strtol(const char *str,char **endptr,int base);
int getFindStrCount(char* src, char* find);
void reset_kernel(); void reset_kernel();
void shutdown_kernel(); void shutdown_kernel();
uint32_t memory_all(); uint32_t memory_all();

View File

@ -1,4 +0,0 @@
#ifndef CRASHPOWEROS_CPP_H
#define CRASHPOWEROS_CPP_H
#endif

View File

@ -1,29 +0,0 @@
#ifndef CRASHPOWEROS_PROC_H
#define CRASHPOWEROS_PROC_H
#include "common.h"
struct stackframe {
uint32_t gs;
uint32_t fs;
uint32_t es;
uint32_t ds;
uint32_t edi;
uint32_t esi;
uint32_t ebp;
uint32_t kernel_esp;
uint32_t ebx;
uint32_t edx;
uint32_t ecx;
uint32_t eax;
uint32_t retaddr;
uint32_t eip;
uint32_t cs;
uint32_t eflags;
uint32_t esp;
uint32_t ss;
};
void proc_install();
#endif

View File

@ -76,7 +76,6 @@ void kernel_main(multiboot_t *multiboot) {
initVBE(multiboot); initVBE(multiboot);
char* cmdline = multiboot->cmdline; char* cmdline = multiboot->cmdline;
if(cmdline != NULL){ if(cmdline != NULL){
printf("Multiboot command line: %s\n",cmdline); printf("Multiboot command line: %s\n",cmdline);

View File

@ -1,5 +0,0 @@
#include "../include/proc.h"
void proc_install(){
}

View File

@ -1,55 +0,0 @@
#include "../include/cpp.h"
#include <stdint.h>
#include <stddef.h>
extern "C" uint32_t kmalloc(size_t);
extern "C" void kfree(void*);
extern "C" void __cxa_pure_virtual()
{
// Do nothing or print an error message.
}
void *operator
new(size_t
size)
{
return
(void*) kmalloc(size);
}
void *operator
new[](
size_t size
)
{
return (void*)kmalloc(size);
}
void operator
delete(void *p, unsigned int size) {
kfree(p);
}
void operator
delete[](
void *p,
unsigned int size
)
{
kfree(p);
}
void operator
delete(void *p) {
kfree(p);
}
void operator
delete[](
void *p
)
{
kfree(p);
}

View File

@ -9,6 +9,42 @@ uint32_t ALIGN_F(const uint32_t addr, const uint32_t _align) {
return (uint32_t)((addr + _align - 1) & (~(_align - 1))); return (uint32_t)((addr + _align - 1) & (~(_align - 1)));
} }
char *strstr(char *str1, char *str2) {
if (str1 == 0 || str2 == 0)return 0;
const char *temp = 0;
const char *res = 0;
while (*str1 != '\0') {
temp = str1;
res = str2;
while (*temp == *res)++temp, ++res;
if (*res == '\0')return str1;
++str1;
}
return 0;
}
char *strncpy(char *dest, const char *src, unsigned long long count) {
if (dest == 0 || src == 0)return 0;
char *ret = dest;
while (count)*dest = *src, ++dest, ++src, --count;
return ret;
}
char *strdup(const char *str) {
if (str == NULL)
return NULL;
char *strat = (char *) str;
int len = 0;
while (*str++ != '\0')
len++;
char *ret = (char *) kmalloc(len + 1);
while ((*ret++ = *strat++) != '\0') {}
return ret - (len + 1);
}
void insert_char(char *str, int pos, char ch) { void insert_char(char *str, int pos, char ch) {
int i; int i;
for (i = strlen(str); i >= pos; i--) { for (i = strlen(str); i >= pos; i--) {
@ -503,3 +539,90 @@ void trim(char *s) {
while (*p && isspace(*p)) ++p, --len; while (*p && isspace(*p)) ++p, --len;
memmove(s, p, len + 1); memmove(s, p, len + 1);
} }
char* replaceAll(char* src, char* find, char* replaceWith){
//如果find或者replace为null则返回和src一样的字符串。
if(find == NULL || replaceWith == NULL){
return strdup(src);
}
//指向替换后的字符串的head。
char* afterReplaceHead = NULL;
//总是指向新字符串的结尾位置。
char* afterReplaceIndex = NULL;
//find字符串在src字符串中出现的次数
int count = 0;
int i,j,k;
int srcLen = strlen(src);
int findLen = strlen(find);
int replaceWithLen = strlen(replaceWith);
//指向src字符串的某个位置从该位置开始复制子字符串到afterReplaceIndex初始从src的head开始复制。
char* srcIndex = src;
//src字符串的某个下标从该下标开始复制字符串到afterReplaceIndex初始为src的第一个字符。
int cpStrStart = 0;
//获取find字符串在src字符串中出现的次数
count = getFindStrCount(src, find);
//如果没有出现则返回和src一样的字符串。
if(count == 0){
return strdup(src);
}
//为新字符串申请内存
afterReplaceHead = afterReplaceIndex = (char*)kmalloc(srcLen + 1 + (replaceWithLen - findLen) * count);
//初始化新字符串内存
memset(afterReplaceHead, '\0',sizeof(afterReplaceHead));
for(i = 0,j = 0,k = 0;i!=srcLen;i++){
//如果find字符串的字符和src中字符串的字符是否相同。
if(src[i] == find[j]){
//如果刚开始比较则将i的值先赋给k保存。
if(j == 0){
k = i;
}
//如果find字符串包含在src字符串中
if(j == (findLen-1)){
j = 0;
//拷贝src中find字符串之前的字符串到新字符串中
strncpy(afterReplaceIndex, srcIndex, i - findLen - cpStrStart + 1);
//修改afterReplaceIndex
afterReplaceIndex = afterReplaceIndex + i - findLen - cpStrStart + 1;
//修改srcIndex
srcIndex = srcIndex + i - findLen - cpStrStart + 1;
//cpStrStart
cpStrStart = i + 1;
//拷贝replaceWith字符串到新字符串中
strncpy(afterReplaceIndex, replaceWith, replaceWithLen);
//修改afterReplaceIndex
afterReplaceIndex = afterReplaceIndex + replaceWithLen;
//修改srcIndex
srcIndex = srcIndex + findLen;
}else{
j++;
}
}else{
//如果find和src比较过程中出现不相等的情况则将保存的k值还给i
if(j != 0){
i = k;
}
j = 0;
}
}
//最后将src中最后一个与find匹配的字符串后面的字符串复制到新字符串中。
strncpy(afterReplaceIndex, srcIndex, i - cpStrStart);
return afterReplaceHead;
}
int getFindStrCount(char* src, char* find){
int count = 0;
char* position =src;
int findLen = strlen(find);
while((position = strstr(position, find)) != NULL){
count++;
position = position + findLen;
}
return count;
}