만들어야 하는 빌트인 함수
- echo, cd, pwd, export, unset, env, exit
echo 만들기
option : -n 처리하기
shell
echo {값} : {값} 출력
echo -n {값} : {값} 출력 후 맨 마지막에 % 추가.
echo : "\n" 출력
echo -n : 아무것도 출력하지 않는다.
c
static int is_option(char *str)
{
if (str[0] == '-' && str[1] == 'n')
return (1);
return (0);
}
static char *echo_split(char *str)
{
char *newstr;
int len;
int i;
int x;
len = ft_strlen(str) - 2;
newstr = malloc(sizeof(char *) * (len + 1));
if (!newstr)
exit (1);
i = 2;
x = 0;
while (str[i])
newstr[x++] = str[i++];
newstr[x] = '\0';
return (newstr);
}
int ft_echo(t_tree *tree)
{
char **newtxt;
if (is_option(tree->str))
{
newtxt = (str);
printf("%s%%\n", newtxt[1]);
free(newtxt);
}
else
printf("%s", tree->str);
}
cd 만들기
option이 없다. 절대경로/상대경로만 가능
shell
cd : $HOME으로
cd ~ : $HOME으로
cd .. : 상위 디렉토리로
cd . :
cd {file_name} : file_name 검사
file_name이 있으면 : ()
file_name이 없으면 : 에러 방출 (에러 코드 1)
cd {인자1} {인자2} : {인자1}만 사용
반환값
성공 : 0
실패 : 1
c
pwd 만들기
option이 없다.
shell
pwd
반환값 :
성공 시 0
실패 시 1
만드는 법 : getcwd()를 사용한다.
c
void ft_pwd()
{
char *buffer;
char *result;
result = getcwd(buffer, 0);
if (result != NULL)
printf("%s\n", result);
}
export 만들기
option이 없다.
shell
export : 전체 목록을 보여준다.
export {키} : 정상적으로 연결리스트에 담긴다.
export {키}= : 정상적으로 연결리스트에 담긴다.
export {키}={값} : 정상적으로 연결리스트에 담긴다.
shell
1. 환경변수 연결리스트를 끌어온다.
2. 새 키와 값을 넣어준다.
=이 있는데, 값이 없다면 ''로 넣어주고, equal=1
=이 없는데, 값도 없다면 ''로 넣어주고, equal=0
c
t_tree *make_dict(char *txt)
{
t_tree *new;
char **newtxt;
new = malloc(sizeof(t_tree));
if (!new)
exit (1);
if (env_first_equal(txt) == -1)
{
new->key = txt;
new->value = "";
new->equal = 0;
}
else
{
newtxt = env_split(txt);
new->key = newtxt[0];
if (newtxt[1] == NULL)
new->value = "";
else
new->value = newtxt[1];
new->equal = 1;
}
return (new);
}
void ft_export(char *txt, t_dict **env_dict)
{
t_tree *cur;
t_tree *newdict;
cur = (*env_dict);
while (cur)
cur = cur->next;
newdict = make_dict();
cur->next = newdict;
}
unset 만들기
option이 없다.
shell
c
env 만들기
option이 없다.
shell
env : 전체 목록 중 {키}와 {값}이 모두 담긴 노드만 보여준다.
export A =>
export A= => equal=1이므로 A=''로 보임
export A={값} => equal=1이므로 보임.
c
void ft_env(t_dict **env_dict)
{
t_dict *cur;
cur = (*env_dict);
while (cur)
{
if (cur->equal == 0)
printf("%s=''", cur->key);
else
{
if (cur->value)
printf("%s=%s", cur->key, cur->value);
else
printf("%s=''", cur->key);
}
cur = cur->next;
}
}
exit 만들기
option이 없다.
shell
c