diff options
| author | Jake Mannens <jake72360@gmail.com> | 2018-08-26 13:29:33 +1000 |
|---|---|---|
| committer | Jake Mannens <jake72360@gmail.com> | 2018-08-26 13:29:33 +1000 |
| commit | 307eaac66d378c1f6150519123ec1204277464d9 (patch) | |
| tree | 8c6dc79c5c16bf776bd57ca78a439701cc10412e /lib | |
| parent | dafb5464fe8b9a3f8ff41de233f9ff6cd21162ea (diff) | |
Updated the style of the code in lib/string.c to conform to the rest of
the project.
Added the system call 'sys_panic' as well as the corresponding library
function panic() whose prototype is defined in unistd.h. This is to
allow userspace programs to deliberately crash the kernel for the
purpose of debugging.
Added the element 'write' as a function pointer in the tty_struct
structure. The purpose of this function pointer is to provide a means
for the TTY subsystem to notify the device driver of any newly added
data to the write queue.
Added the tty_write() function to the TTY subsystem. This function will
copy data provided by the user into the specified TTY device's write
queue, blocking if the queue's buffer is full. tty_write() will then
call the driver's write() function to notify it of new data.
Added the rsint_tx() function to the serial driver to handle transmit
interrupts by re-supplying the UART's transmit FIFO.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/panic.c | 8 | ||||
| -rw-r--r-- | lib/stdio.c | 14 | ||||
| -rw-r--r-- | lib/string.c | 56 |
3 files changed, 49 insertions, 29 deletions
diff --git a/lib/panic.c b/lib/panic.c new file mode 100644 index 0000000..a3eb0af --- /dev/null +++ b/lib/panic.c @@ -0,0 +1,8 @@ +#include <unistd.h> + +void panic(void) { + __asm__ volatile ( + "int $0x80" + :: "a" (__SYS_panic) + ); +} diff --git a/lib/stdio.c b/lib/stdio.c index 7eac64c..b1c00ba 100644 --- a/lib/stdio.c +++ b/lib/stdio.c @@ -1,11 +1,23 @@ #include <stdarg.h> #include <stdio.h> +#include <string.h> #include <sys/types.h> #include <unistd.h> -_syscall1(int, puts, char*, s); _syscall1(int, ctty, int, ctty); _syscall2(ssize_t, read, void*, buf, size_t, len); +_syscall2(ssize_t, write, void*, buf, size_t, len); + +int puts(char *s) { + size_t len = strlen(s); + ssize_t ret; + + ret = write(s, len); + if(ret < 0) + return EOF; + + return 0; +} int printf(char *fmt, ...) { int ret; diff --git a/lib/string.c b/lib/string.c index 0e8fbeb..e8659ad 100644 --- a/lib/string.c +++ b/lib/string.c @@ -1,6 +1,6 @@ #include <string.h> -void* memchr(const void* s, int c, size_t n) { +void *memchr(const void *s, int c, size_t n) { unsigned char *p = (unsigned char*) s; while(n--) { if(*p != (unsigned char) c) { @@ -12,9 +12,9 @@ void* memchr(const void* s, int c, size_t n) { return 0; } -int memcmp(const void* s1, const void* s2, size_t n) { - const unsigned char* p1 = (const unsigned char*) s1; - const unsigned char* p2 = (const unsigned char*) s2; +int memcmp(const void *s1, const void *s2, size_t n) { + const unsigned char *p1 = (const unsigned char*) s1; + const unsigned char *p2 = (const unsigned char*) s2; while(n--) { if(*p1 != *p2) { return *p1 - *p2; @@ -25,33 +25,33 @@ int memcmp(const void* s1, const void* s2, size_t n) { return 0; } -void* memcpy(void* dest, const void* src, size_t n) { +void *memcpy(void *dest, const void *src, size_t n) { char *dp = (char*) dest; const char *sp = (const char*) src; while(n--) *dp++ = *sp++; return dest; } -void* memmove(void* dest, const void* src, size_t n) { +void *memmove(void *dest, const void *src, size_t n) { unsigned char tmp[n]; memcpy(tmp, src, n); memcpy(dest, tmp, n); return dest; } -void* memset(void* s, int c, size_t n) { - unsigned char* p = (unsigned char*) s; +void *memset(void *s, int c, size_t n) { + unsigned char *p = (unsigned char*) s; while(n--) *p++ = (unsigned char) c; return s; } -size_t strlen(const char* str) { - const char* s; +size_t strlen(const char *str) { + const char *s; for(s = str; *s; ++s) {} return (s - str); } -char* strcat(char* dest, const char* src) { +char *strcat(char *dest, const char *src) { size_t dest_len = strlen(dest); size_t i; @@ -62,7 +62,7 @@ char* strcat(char* dest, const char* src) { return dest; } -char* strncat(char* dest, const char* src, size_t n) { +char *strncat(char *dest, const char *src, size_t n) { size_t dest_len = strlen(dest); size_t i; @@ -73,27 +73,27 @@ char* strncat(char* dest, const char* src, size_t n) { return dest; } -char* strchr(const char* s, int c) { +char *strchr(const char *s, int c) { while(*s != (char) c) { if(!*s++) return 0; } return (char*) s; } -char* strrchr(char* s, int c) { - char* ret = 0; +char *strrchr(char *s, int c) { + char *ret = 0; do { if(*s == (char) c) ret = s; } while(*s++); return ret; } -int strcmp(const char* s1, const char* s2) { +int strcmp(const char *s1, const char *s2) { while(*s1 && (*s1 == *s2)) s1++, s2++; return *(const unsigned char*) s1 - *(const unsigned char*) s2; } -int strncmp(const char* s1, const char* s2, size_t n) { +int strncmp(const char *s1, const char *s2, size_t n) { while(n--) { if(*s1++ != *s2++) { return *(unsigned char*) (s1 - 1) - *(unsigned char*) (s2 - 1); @@ -102,14 +102,14 @@ int strncmp(const char* s1, const char* s2, size_t n) { return 0; } -char* strcpy(char* dest, const char* src) { - char* ret = dest; +char *strcpy(char *dest, const char *src) { + char *ret = dest; while(*dest++ = *src++); return ret; } -char* strncpy(char* dest, const char* src, size_t n) { - char* ret = dest; +char *strncpy(char *dest, const char *src, size_t n) { + char *ret = dest; do { if(!n--) return ret; } while (*dest++ = *src++); @@ -117,7 +117,7 @@ char* strncpy(char* dest, const char* src, size_t n) { return ret; } -size_t strcspn(const char* s1, const char* s2) { +size_t strcspn(const char *s1, const char *s2) { size_t ret = 0; while(*s1) { if(strchr(s2, *s1)) { @@ -129,20 +129,20 @@ size_t strcspn(const char* s1, const char* s2) { return ret; } -size_t strspn(const char* s1, const char* s2) { +size_t strspn(const char *s1, const char *s2) { size_t ret = 0; while(*s1 && strchr(s2, *s1++)) ret++; return ret; } -char* strpbrk(const char* s1, const char* s2) { +char *strpbrk(const char *s1, const char *s2) { while(*s1) { if(strchr(s2, *s1++)) return (char*) --s1; } return 0; } -char* strstr(const char* s1, const char* s2) { +char *strstr(const char *s1, const char *s2) { size_t n = strlen(s2); while(*s1) { if(!memcmp(s1++, s2, n)) { @@ -152,8 +152,8 @@ char* strstr(const char* s1, const char* s2) { return 0; } -char* strtok(char* str, const char* delim) { - static char* p = 0; +char *strtok(char *str, const char *delim) { + static char *p = 0; if(str) { p = str; } else if(!p) { @@ -166,7 +166,7 @@ char* strtok(char* str, const char* delim) { return str; } -size_t strxfrm(char* dest, const char* src, size_t n) { +size_t strxfrm(char *dest, const char *src, size_t n) { size_t n2 = strlen(src); if(n > n2) strcpy(dest, src); return n2; |
