diff --git a/Makefile b/Makefile index 3b27b6b..2aeb7de 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ -SRCS = env.c +UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c +SRCS = env.c ${UTILS_SRC} OBJS = ${SRCS:.c=.o} @@ -16,7 +17,7 @@ LIBS = libftx/libftx.a all: ${NAME} ${NAME}: ${OBJS} - make -C libftx + make -C libftx all ${CC} ${OBJS} -o ${NAME} ${LIBS} clean: diff --git a/argprinter b/argprinter new file mode 100755 index 0000000..11c8a1d Binary files /dev/null and b/argprinter differ diff --git a/env.c b/env.c index cadd04e..b0c1342 100644 --- a/env.c +++ b/env.c @@ -1,6 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ +/* Updated: 2023/02/02 17:39:56 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "env.h" #include "libftx/libftx.h" +void print_export(t_list **head, int fd) +{ + t_list *current; + char *ctn; + + current = *head; + while (current != NULL) + { + ctn = current->content; + if (*(ft_strchr(ctn, '=') - 1) != '_') + { + write(fd, "declare -x ", 11); + ft_putstr_fd(ctn, fd); + write(fd, "\n", 1); + } + current = current->next; + } +} + +void print_env(t_list **head, int fd) +{ + t_list *current; + + current = *head; + while (current != NULL) + { + ft_putstr_fd(current->content, fd); + write(fd, "\n", 1); + current = current->next; + } +} + int strcmp_alphabet(char *s1, char *s2) { int i; @@ -28,28 +72,34 @@ void ft_double_swap(char **a, char **b) *b = c; } +void exchange(char **a, char **b, char **c) +{ + void *d; + + d = *a; + *a = *b; + *b = *c; + *c = d; +} + void add_sort(t_list **head, char *str) { t_list *current; char *last; current = *head; - while (current->next != NULL) - { - if (strcmp_alphabet(str, current->content) == 0) - break ; + while (current->next != NULL && strcmp_alphabet(str, current->content) != 0) current = current->next; - } - last = current->content; - current->content = str; + if (strcmp_alphabet(str, current->content) == 1) + last = str; + else + exchange(&last, (char **)(¤t->content), &str); while (current != NULL) { if (current->next == NULL) - { current->next = ft_calloc(1, sizeof(t_list)); - if (current->next == NULL) - return ; - } + if (current->next == NULL) + return ; current = current->next; if (current->content == NULL) { @@ -57,7 +107,7 @@ void add_sort(t_list **head, char *str) return ; } else - ft_double_swap((char**)(¤t->content), &last); + ft_double_swap((char **)(¤t->content), &last); } } @@ -66,34 +116,24 @@ t_list **init_env(char **env) t_list **head; int i; - head = ft_calloc(1, sizeof(t_list*)); + head = ft_calloc(1, sizeof(t_list *)); *head = ft_calloc(1, sizeof(t_list)); if (*head == NULL) return (NULL); i = -1; while (env[++i]) { - if (ft_strnstr(env[i], "XDG_SESSION_CLASS=user", 200)) + if (ft_strnstr(env[i], "XMODIFIERS=@im=ibus", 200)) write(1, "", 0); add_sort(head, env[i]); } - //current->next = NULL; - return(head); + return (head); } -int main(int argc, char *argv[], char **env) +/*int main(int argc, char *argv[], char **env) { - t_list **new_env; - t_list *current; - (void)argc; (void)argv; - new_env = init_env(env); - current = *new_env; - while (current != NULL) - { - ft_printf("%s\n", current->content); - current = current->next; - } + print_export(init_env(env)); return (0); -} +}*/ diff --git a/env.o b/env.o deleted file mode 100644 index 21bf7fe..0000000 Binary files a/env.o and /dev/null differ diff --git a/libftx/libftx.a b/libftx/libftx.a index 18620f9..6919e75 100644 Binary files a/libftx/libftx.a and b/libftx/libftx.a differ diff --git a/libftx/libftx.h b/libftx/libftx.h index e9d6a7a..3f74279 100644 --- a/libftx/libftx.h +++ b/libftx/libftx.h @@ -6,7 +6,7 @@ /* By: cchauvet +# include +# include -typedef struct s_list -{ - void *content; - void *next; - int tag; -} t_list +int ft_file_is_readable(char *path); +int ft_file_is_writeable(char *path); typedef struct cmd { diff --git a/utils/.ft_is_a_quote.c.swp b/utils/.ft_is_a_quote.c.swp new file mode 100644 index 0000000..222da86 Binary files /dev/null and b/utils/.ft_is_a_quote.c.swp differ diff --git a/utils/ft_getstr.c b/utils/ft_getstr.c new file mode 100644 index 0000000..c55fed4 --- /dev/null +++ b/utils/ft_getstr.c @@ -0,0 +1,27 @@ +#include "utils.h" + +char *ft_getstr(char *str, size_t n) +{ + size_t start; + size_t stop; + char c; + int quote; + + start = n; + stop = n; + quote = ft_is_in_quote(str, n); + if (quote == 0) + c = ' '; + else + { + if (quote == 1) + c = '\''; + else + c = '"'; + } + while (str[start - 1] != c && start > 0) + start--; + while (str[stop] != c && str[stop] != '\0') + stop++; + return (ft_strndup(str + start, stop - start)); +} diff --git a/utils/ft_getstr.o b/utils/ft_getstr.o new file mode 100644 index 0000000..ca97c05 Binary files /dev/null and b/utils/ft_getstr.o differ diff --git a/utils/ft_is_in_quote.c b/utils/ft_is_in_quote.c new file mode 100644 index 0000000..3b697d1 --- /dev/null +++ b/utils/ft_is_in_quote.c @@ -0,0 +1,29 @@ +#include "utils.h" + +int ft_is_in_quote(char *str, size_t n) +{ + size_t double_quoted; + size_t simple_quoted; + size_t i; + + double_quoted = 0; + simple_quoted = 0; + i = 0; + while (str[i] != '\0' && i < n) + { + if (str[i] == '"') + { + if (simple_quoted == 0) + double_quoted = !double_quoted; + + } + if (str[i] == '\'') + { + if (double_quoted == 0) + simple_quoted = !simple_quoted; + + } + i++; + } + return (simple_quoted == 1 + (double_quoted == 1) * 2); +} diff --git a/utils/ft_is_in_quote.o b/utils/ft_is_in_quote.o new file mode 100644 index 0000000..3c3b6ac Binary files /dev/null and b/utils/ft_is_in_quote.o differ diff --git a/utils/ft_strnchr.c b/utils/ft_strnchr.c new file mode 100644 index 0000000..6654db9 --- /dev/null +++ b/utils/ft_strnchr.c @@ -0,0 +1,15 @@ +#include "utils.h" + +ssize_t ft_strnchr(char *str, char c) +{ + size_t i; + + i = 0; + while (str[i] != '\0') + { + if (str[i] == c) + return (i); + i++; + } + return (-1); +} diff --git a/utils/ft_strnchr.o b/utils/ft_strnchr.o new file mode 100644 index 0000000..8b0ac81 Binary files /dev/null and b/utils/ft_strnchr.o differ diff --git a/utils/ft_strncpy.c b/utils/ft_strncpy.c new file mode 100644 index 0000000..16de576 --- /dev/null +++ b/utils/ft_strncpy.c @@ -0,0 +1,14 @@ +#include "utils.h" + +size_t ft_strncpy(char *dst, char *src, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + dst[i] = src[i]; + i++; + } + return (i); +} diff --git a/utils/ft_strncpy.o b/utils/ft_strncpy.o new file mode 100644 index 0000000..c5a7006 Binary files /dev/null and b/utils/ft_strncpy.o differ diff --git a/utils/ft_strreplace.c b/utils/ft_strreplace.c new file mode 100644 index 0000000..8a2a2ba --- /dev/null +++ b/utils/ft_strreplace.c @@ -0,0 +1,17 @@ +#include "utils.h" + +char *ft_strreplace(char *str, char *fill, size_t start, size_t stop) +{ + char *out; + size_t sum; + + out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char))); + if (out == NULL) + return (NULL); + ft_strncpy(out, str, start); + ft_strncpy(out + start, fill, ft_strlen(fill)); + sum = start + ft_strlen(fill); + ft_strncpy(out + sum, str + stop, ft_strlen(str) - stop); + out[sum + ft_strlen(str) - stop] = '\0'; + return (out); +} diff --git a/utils/ft_strreplace.o b/utils/ft_strreplace.o new file mode 100644 index 0000000..1422e58 Binary files /dev/null and b/utils/ft_strreplace.o differ diff --git a/utils/utils.h b/utils/utils.h new file mode 100644 index 0000000..0f24105 --- /dev/null +++ b/utils/utils.h @@ -0,0 +1,12 @@ +#ifndef FT_UTILS +# define FT_UTILS +# include +# include "../libftx/libftx.h" + +size_t ft_strncpy(char *dst, char *src, size_t n); +int ft_is_in_quote(char *str, size_t n); +char *ft_strreplace(char *str, char *fill, size_t start, size_t stop); +ssize_t ft_strnchr(char *str, char c); +char *ft_getstr(char *str, size_t n); + +#endif