-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
330 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* custom_str_utils2.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: minsuki2 <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/09/11 18:04:59 by minsuki2 #+# #+# */ | ||
/* Updated: 2022/09/11 19:02:59 by minsuki2 ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
char *ft_strcpy(const char *start, const char *end) | ||
{ | ||
char *ret; | ||
size_t len; | ||
int idx; | ||
|
||
len = end - start; | ||
ret = (char *)malloc(len + 1); | ||
if (ret == NULL) | ||
exit(GENERAL_EXIT_CODE); | ||
idx = 0; | ||
while (idx < (int)len) | ||
ret[idx++] = *start++; | ||
ret[idx] = '\0'; | ||
return (ret); | ||
} | ||
|
||
char *ft_chr_to_str(char c) | ||
{ | ||
char *str; | ||
|
||
str = (char *)my_malloc(2 * sizeof(char)); | ||
str[1] = '\0'; | ||
str[0] = c; | ||
return (str); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
#include "minishell.h" | ||
|
||
void print_dictionary_lst() | ||
{ | ||
int i; | ||
t_dic *cur; | ||
|
||
i = 0; | ||
while (i < DIC_MAX) | ||
{ | ||
printf("category : %s\n", g_dic[i].value); | ||
cur = g_dic[i].next; | ||
while (cur) | ||
{ | ||
printf("L %s=%s\n", cur->name, cur->value); | ||
cur = cur->next; | ||
} | ||
i++; | ||
} | ||
} | ||
|
||
void print_envp(char *envp[]) | ||
{ | ||
for (int i = 0; envp[i]; i++) | ||
printf("%s\n", envp[i]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* envp_utils.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: minsuki2 <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/09/09 15:25:42 by minsuki2 #+# #+# */ | ||
/* Updated: 2022/09/13 14:54:39 by minsuki2 ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
t_dic *make_envp_node(char *name, char *value, t_dic *next, t_dic *prev) | ||
{ | ||
t_dic *new; | ||
|
||
new = (t_dic *)my_calloc(1, sizeof(t_dic)); | ||
new->name = name; | ||
new->value = value; | ||
new->next = next; | ||
new->prev = prev; | ||
return (new); | ||
} | ||
|
||
void setting_dictionary(void) | ||
{ | ||
int i; | ||
char category; | ||
|
||
i = 0; | ||
category = 'A'; | ||
while (i < DIC_MAX) | ||
{ | ||
category += 4 * (category == 'Z' + 1) + (category == '_' + 1); | ||
g_dic[i].value = ft_chr_to_str(category++); | ||
g_dic[i].prev = &g_dic[i]; | ||
i++; | ||
} | ||
} | ||
|
||
void ft_lstadd_last(t_dic *head, t_dic *new) | ||
{ | ||
t_dic *const last = get_last_dic(head); | ||
|
||
head->prev = new; | ||
last->next = new; | ||
new->prev = last; | ||
} | ||
|
||
void ft_lstadd_next(t_dic *cur, t_dic *new) | ||
{ | ||
if (cur == get_last_dic(cur)) | ||
return ft_lstadd_last(get_first_dic(cur), new); | ||
new->next = cur->next; | ||
cur->next = new; | ||
new->prev = cur; | ||
} | ||
|
||
int comapre_order_dic(const t_dic *next, const t_dic *new) | ||
{ | ||
int i; | ||
const char *s1; | ||
const char *s2; | ||
|
||
i = 0; | ||
if (!next) | ||
return (GREATER_THAN); | ||
s1 = next->name; | ||
s2 = new->name; | ||
while (s1[i] && s2[i] && s1[i] == s2[i]) | ||
i++; | ||
return (s1[i] - s2[i]); | ||
} | ||
|
||
// int comapre_order_word(const char *s1, const char *s2) | ||
// { | ||
// int i; | ||
// | ||
// i = 0; | ||
// if (!s1) | ||
// return (GREATER_THAN); | ||
// while (s1[i] && s2[i] && s1[i] == s2[i]) | ||
// i++; | ||
// return (s1[i] - s2[i]); | ||
// } | ||
/* AA AB AC AA AAB => 음수 | ||
* AAA AAB => 음수 | ||
* AAB AB AAB => 양수 | ||
*/ | ||
|
||
void ft_lstadd_order(t_dic *head, t_dic *new) | ||
{ | ||
int old_val; | ||
int val; | ||
t_dic *cur; | ||
|
||
if (!head) | ||
return ; | ||
cur = head; | ||
old_val = LESS_THAN; | ||
while (cur) | ||
{ | ||
val = comapre_order_word(cur->next, new); | ||
if (old_val < 0 && val > 0) | ||
return ft_lstadd_next(cur, new); | ||
old_val = val; | ||
cur = cur->next; | ||
} | ||
} | ||
|
||
|
||
int chr_to_idx(char c) | ||
{ | ||
if ('A' <= c && c <= 'Z') | ||
return (c - 'A' + 0); | ||
else if ('a' <= c && c <= 'z') | ||
return (c - 'a' + 27); | ||
return (c - '_' + 26); | ||
} | ||
// 'A' 0 | ||
// 'Z' 25 | ||
// '_' 26 | ||
// 'a' 27 | ||
|
||
void char_dimen2_to_lst(char *envp[]) | ||
{ | ||
int idx; | ||
int j; | ||
char *pos; | ||
|
||
print_envp(envp); | ||
setting_dictionary(); | ||
j = 0; | ||
while (envp[j]) | ||
{ | ||
pos = ft_strchr_null(envp[j], '='); | ||
idx = chr_to_idx(*envp[j]); | ||
ft_lstadd_order(&g_dic[idx], make_envp_node(ft_strcpy(envp[j], pos), \ | ||
ft_strdup(pos + 1), NULL, NULL)); | ||
j++; | ||
} | ||
print_dictionary_lst(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
#include "minishell.h" | ||
|
||
int main(int argc, char *argv[], char *envp[]) | ||
{ | ||
int i; | ||
char *cur; | ||
int j; | ||
|
||
i = 0; | ||
j = 0; | ||
while (envp[i]) | ||
{ | ||
cur = envp[i]; | ||
while (*cur) | ||
{ | ||
ft_putchar_fd(*cur, STDOUT_FILENO); | ||
cur++; | ||
} | ||
ft_putchar_fd('\n', STDOUT_FILENO); | ||
i++; | ||
} | ||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.