Ajout affichage des variables

This commit is contained in:
rick 2021-02-24 02:01:44 +01:00
parent feb019d000
commit af2081caaf
Signed by: Rick
GPG key ID: 2B593F087240EE99
2 changed files with 25 additions and 3 deletions

View file

@ -20,9 +20,30 @@ void echo(char *args)
int i = 0;
while (tmp != '\0')
{
putchar(tmp);
i++;
tmp = *(args + i);
if (tmp == '$')
{
i++;
i += get_var((args+i), i);
}
else
{
putchar(tmp);
i++;
}
tmp = *(args + i);
}
putchar('\n');
}
int get_var(char *str, int i)
{
char *tmp = calloc(strlen(str), sizeof(char));
strcpy(tmp, str);
char f = ' ';
char *token = strtok(tmp, &f);
printf("%s", getenv(token));
int ret = strlen(token);
free(tmp);
return ret;
}

View file

@ -9,5 +9,6 @@
void usage();
void echo(char *args);
int get_var(char *str, int i);
#endif