Normed most of the stdme

This commit is contained in:
Maieul BOYER 2024-07-10 18:06:10 +02:00
parent 882d5cb5bb
commit ac5458d42c
No known key found for this signature in database
31 changed files with 149 additions and 617 deletions

View file

@ -6,12 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/05/16 17:26:27 by maiboyer ### ########.fr */
/* Updated: 2024/07/10 17:54:01 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#define _GNU_SOURCE
#include "./gnu_source.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/types.h"
@ -31,24 +30,24 @@
# define BACKTRACE_DEEP 256
# endif
static size_t convert_to_vma(t_usize addr)
static size_t convert_to_vma(t_usize addr)
{
Dl_info info;
struct link_map *link_map;
Dl_info info;
struct link_map *link_map;
dladdr1((void *)addr, &info, (void **)&link_map, RTLD_DL_LINKMAP);
return (addr - link_map->l_addr);
}
static void print_trace_inner(void **trace, t_str *messages, t_usize i)
static void print_trace_inner(void **trace, t_str *messages, t_usize i)
{
char syscom[1024];
t_i32 p;
char syscom[1024];
t_i32 p;
p = 0;
fprintf(stderr, "[bt] #%-4zu\t", i);
while (messages[i][p] != '(' && messages[i][p] != ' ' &&
messages[i][p] != 0)
messages[i][p] != 0)
++p;
fflush(stderr);
snprintf(
@ -60,12 +59,12 @@ static void print_trace_inner(void **trace, t_str *messages, t_usize i)
fprintf(stderr, "%s\n", messages[i]);
}
void print_trace(void)
void print_trace(void)
{
void *trace[BACKTRACE_DEEP];
t_str *messages;
t_i32 size;
t_i32 i;
void *trace[BACKTRACE_DEEP];
t_str *messages;
t_i32 size;
t_i32 i;
size = backtrace(trace, BACKTRACE_DEEP);
messages = backtrace_symbols(trace, size);
@ -79,20 +78,18 @@ void print_trace(void)
}
#else
void print_trace(void)
void print_trace(void)
{
}
#endif
void me_abort(t_str msg)
void me_abort(t_str msg)
{
if (msg == NULL)
msg = "No message (msg was NULL)";
me_putendl_fd("Memory information:", 2);
me_putstr_fd("Abort: ", 2);
me_putendl_fd(msg, 2);
print_trace();
// me_exit(1);
abort();
me_exit(134);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
/* Updated: 2024/07/07 19:11:39 by maiboyer ### ########.fr */
/* Updated: 2024/07/10 17:54:22 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@
// __attribute__((dtor)) to be run at exit for example:
// - close_all_slots
// - uninit global allocator
void me_exit(t_i32 exit_code)
void me_exit(t_i32 exit_code)
{
(get_stdin(), get_stdout(), get_stderr());
exit(exit_code);

18
stdme/src/os/gnu_source.h Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* gnu_source.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 17:53:46 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:54:49 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GNU_SOURCE_H
# define GNU_SOURCE_H
# define _GNU_SOURCE
#endif /* GNU_SOURCE_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/05/19 14:57:20 by maiboyer ### ########.fr */
/* Updated: 2024/07/10 18:04:36 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,11 +26,11 @@
bool find_path(const t_str *s);
bool find_null(const t_str *s);
bool str_start_with(t_const_str s, t_const_str prefix);
t_error handle_redirections(t_spawn_info *info, t_process *process);
t_error handle_redirections(t_spawn_info *info, t_process *process);
t_error spawn_process_exec(t_spawn_info info, t_process *process)
t_error spawn_process_exec(t_spawn_info info, t_process *process)
{
bool res;
bool res;
if (info.forked_free)
info.forked_free(info.forked_free_args);
@ -52,11 +52,11 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
return (NO_ERROR);
}
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
t_string *s)
{
t_str *splitted_path;
t_usize sp_index;
t_str *splitted_path;
t_usize sp_index;
splitted_path = str_split(path + 5, ':');
if (splitted_path == NULL)
@ -70,7 +70,7 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
string_push(s, info->binary_path);
sp_index++;
if (access(s->buf, X_OK | R_OK) == 0)
break;
break ;
}
sp_index = 0;
while (splitted_path[sp_index])
@ -79,16 +79,16 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
return (NO_ERROR);
}
t_error find_binary(t_spawn_info *info, t_process *process)
t_error find_binary(t_spawn_info *info, t_process *process)
{
t_usize p_idx;
t_string s;
t_usize p_idx;
t_string s;
(void)(process);
if (info->binary_path == NULL)
return (ERROR);
s = string_new(256);
if (str_start_with(info->binary_path, "/") ||
if (str_start_with(info->binary_path, "/") || \
str_find_chr(info->binary_path, '/') != NULL)
string_push(&s, info->binary_path);
else
@ -107,7 +107,8 @@ t_error find_binary(t_spawn_info *info, t_process *process)
return (string_free(s), ERROR);
}
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
static void cleanup(t_spawn_info info, t_process *process, \
bool cleanup_process)
{
if (cleanup_process && process->stdin.tag != INVALID)
close(process->stdin.vals.ro.fd);
@ -123,7 +124,7 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
mem_free(info.binary_path);
}
t_error spawn_process(t_spawn_info info, t_process *process)
t_error spawn_process(t_spawn_info info, t_process *process)
{
if (handle_redirections(&info, process))
return (cleanup(info, process, true), ERROR);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
/* Updated: 2024/05/19 14:51:07 by maiboyer ### ########.fr */
/* Updated: 2024/07/10 18:05:03 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,12 +14,12 @@
#include <stdbool.h>
#include <stdio.h>
bool find_null(const t_str *s)
bool find_null(const t_str *s)
{
return (s == NULL);
}
bool str_start_with(t_const_str s, t_const_str prefix)
bool str_start_with(t_const_str s, t_const_str prefix)
{
while (*prefix && *s)
{
@ -29,13 +29,13 @@ bool str_start_with(t_const_str s, t_const_str prefix)
return (*prefix == '\0');
}
bool find_path(const t_str *s)
bool find_path(const t_str *s)
{
t_str ss;
t_str ss;
if (*s == NULL)
return (false);
ss = *s;
return (ss[0] == 'P' && ss[1] == 'A' && ss[2] == 'T' && ss[3] == 'H' &&
return (ss[0] == 'P' && ss[1] == 'A' && ss[2] == 'T' && ss[3] == 'H' && \
ss[4] == '=');
}