Browse Source

blic wip

master
Mathieu Serandour 5 months ago
parent
commit
f9e9ceb70e
  1. 4
      blib/Makefile
  2. 12
      blib/include/unistd.h
  3. 2
      blib/src/crt0.c
  4. 22
      blib/src/stdio.c
  5. 2
      blib/src/stdlib.c
  6. 1
      blib/src/string.c
  7. 36
      blib/src/unistd.c

4
blib/Makefile

@ -12,9 +12,9 @@ ASM:=nasm
CFLAGS := -O3 -mgeneral-regs-only -Wall -Wextra \
-ffreestanding -Iinclude/ -fno-pie -fno-stack-protector
-ffreestanding -Iinclude/ -fno-pie -fno-stack-protector -fno-pic
LDFLAGS := -shared -fno-pie
LDFLAGS := -shared -fno-pie -fno-pic
ARFLAGS:=rc

12
blib/include/unistd.h

@ -543,15 +543,15 @@ extern void *sbrk (uint64_t __delta);
/*
return 0 if successfull, -1 if error
*/
extern int forkexec(char* const cmdline[]);
extern int forkexec(char const * const cmdline[]);
/* Execute PATH with arguments ARGV and environment from `environ'. */
extern int execv (const char *__path, char *const __argv[]);
extern int execv (const char *__path, char const *const __argv[]);
/* Replace the current process, executing PATH with arguments ARGV and
environment ENVP. ARGV and ENVP are terminated by NULL pointers. */
extern int execve (const char *__path, char *const __argv[],
extern int execve (const char *__path, char const *const __argv[],
char *const __envp[]);
/* Execute PATH with all arguments after PATH until a NULL pointer,
@ -564,10 +564,10 @@ extern int execl (const char *__path, const char *__arg, ...);
/* Execute FILE, searching in the `PATH' environment variable if it contains
no slashes, with arguments ARGV and environment from `environ'. */
extern int execvp (const char *__file, char *const __argv[]);
extern int execvp (const char *__file, char const *const __argv[]);
extern int execvpe(const char *file, char *const argv[],
char *const envp[]);
extern int execvpe(const char *file, char const *const argv[],
char const *const envp[]);
/* Execute FILE, searching in the `PATH' environment variable if

2
blib/src/crt0.c

@ -47,7 +47,7 @@ _start(int argc, char* args, int envrionc, char* input_environ) {
char** p = environ;
while(p) {
for(unsigned i = 0; i < envrionc; i++) {
for(int i = 0; i < envrionc; i++) {
*p++ = strdup(input_environ);
input_environ += strlen(input_environ) + 1;

22
blib/src/stdio.c

@ -35,7 +35,7 @@ void stio_init(void) {
*stdin = (FILE){
.fd = STDIN_FILENO,
.flags = 0,
.mode = 0,
.mode = _IONBF,
.ungetc = -1,
.error = 0,
.eof = 0,
@ -50,7 +50,7 @@ void stio_init(void) {
*stdout = (FILE){
.fd = STDOUT_FILENO,
.flags = 0,
.mode = 0,
.mode = _IONBF,
.ungetc = -1,
.error = 0,
.eof = 0,
@ -65,7 +65,7 @@ void stio_init(void) {
*stderr = (FILE){
.fd = STDERR_FILENO,
.flags = 0,
.mode = 0,
.mode = _IONBF,
.ungetc = -1,
.error = 0,
.eof = 0,
@ -87,6 +87,8 @@ FILE *fopen (const char *restrict filename,
const char *restrict modes
) {
(void) modes;
int fd = open(filename, 0, 0);
if(fd < 0) {
return NULL;
@ -134,6 +136,8 @@ int fclose (FILE* stream) {
close(stream->fd);
free(stream->buffer);
free(stream);
return 0;
}
@ -141,6 +145,8 @@ int fclose (FILE* stream) {
const char* restrict modes,
FILE* restrict stream
) {
(void) modes;
fflush(stream);
close(stream->fd);
stream->fd = open(filename, 0, 0);
@ -192,10 +198,10 @@ int putchar(int c) {
}
int puts (const char *s) {
if(write(STDOUT_FILENO, s, strlen(s)) < 0) {
if(write(STDOUT_FILENO, s, strlen(s)) == 0) {
return EOF;
}
else if(write(STDOUT_FILENO, "\n", 1) < 0) {
else if(write(STDOUT_FILENO, "\n", 1) == 0) {
return EOF;
}
return 0;
@ -217,7 +223,7 @@ size_t fread (void *restrict ptr, size_t size,
}
if(rd != size * n)
if(rd != (int64_t)(size * n))
stream->eof = 1;
stream->pos += n;
@ -242,7 +248,7 @@ size_t fwrite (const void *restrict ptr, size_t size,
return 0;
}
if(n != size * n)
if(n != (int64_t)size * n)
stream->eof = 1;
stream->pos += n;
@ -294,7 +300,7 @@ int fputs(const char* restrict s, FILE* restrict stream) {
return EOF;
}
case _IONBF: {
int n = write(stream->fd, s, strlen(s));
size_t n = write(stream->fd, s, strlen(s));
if(n == strlen(s)) {
return 0;
}

2
blib/src/stdlib.c

@ -105,7 +105,7 @@ char* getenv(const char *name) {
int system(const char *command) {
return forkexec((char*[]){"/bin/sh", "-c", command, NULL});
return forkexec((char const*const[]){"/bin/sh", "-c", command, NULL});
}

1
blib/src/string.c

@ -339,6 +339,7 @@ char* strtok(char* restrict str, const char* restrict delim) {
char *strdup(const char *s) {
size_t len = strlen(s);
char* ret = malloc(len+1);
if(!ret)
return NULL;

36
blib/src/unistd.c

@ -16,7 +16,7 @@ static void* brk_addr = NULL;
int usleep (uint64_t __useconds) {
syscall(SC_SLEEP, &__useconds, sizeof(__useconds));
return syscall(SC_SLEEP, &__useconds, sizeof(__useconds));
}
@ -95,7 +95,7 @@ size_t pread (int fd, void* buf, size_t nbytes, off_t offset) {
off_t off = lseek(fd, 0, SEEK_CUR);
if(lseek(fd, offset, SEEK_SET) == -1) {
if(lseek(fd, offset, SEEK_SET) == (uint64_t)-1) {
return -1;
}
@ -128,7 +128,7 @@ size_t pwrite (int fd, const void* buf, size_t n, off_t offset);
* list buffer
* @return mallocated string list
*/
static char* create_string_list(char* const* arr, size_t* size) {
static char* create_string_list(char const* const* arr, size_t* size) {
// first compute the needed buffer size
// and the number of strings
@ -164,7 +164,14 @@ static char* create_string_list(char* const* arr, size_t* size) {
// base function for all exec* functions
static
int exec(const char* file, char* const argv[], char* const envp[], int new_process) {
int exec(const char* file,
char const* const argv[],
char const* const envp[],
int new_process
) {
// file is equal to argv[0]
(void) file;
size_t args_sz = 0, env_sz = 0;
char* args = create_string_list(argv, &args_sz);
@ -188,27 +195,30 @@ int exec(const char* file, char* const argv[], char* const envp[], int new_proce
}
int execvpe(const char* file, char* const argv[], char* const envp[]) {
int execvpe(const char* file,
char const* const argv[],
char const* const envp[]
) {
if(strcmp(file, argv[0]) != 0) {
return -1;
};
exec(file, argv, envp, 0);
return exec(file, argv, envp, 0);
}
int execv (const char* path, char* const argv[]) {
int execv (const char* path, char const* const argv[]) {
// @todo search in $PATH
return execvp(path, argv);
}
int execvp (const char *le, char *const argv[]) {
return exec(le, argv, __environ, 0);
int execvp (const char *le, char const* const argv[]) {
return exec(le, argv, (char const* const*)__environ, 0);
}
int forkexec(char* const cmdline[]) {
return exec(cmdline[0], cmdline, __environ, 1);
int forkexec(char const* const cmdline[]) {
return exec(cmdline[0], cmdline, (char const* const*)__environ, 1);
}
@ -233,7 +243,7 @@ char *getcwd (char *buf, size_t size) {
// reaquest cwd size first
size = syscall(SC_GETCWD, &args, sizeof(args));
if(size == -1) {
if(size == (uint64_t)-1) {
return NULL;
}
@ -262,7 +272,7 @@ int dup2 (int fd, int fd2) {
.fd = fd,
.fd2 = fd2,
};
return syscall(SC_DUP, &fd, sizeof(fd));
return syscall(SC_DUP, &args, sizeof(args));
}

Loading…
Cancel
Save