rishtik/src/boitoutil/oui-dire.c

50 lines
793 B
C
Raw Normal View History

2021-02-21 23:00:47 +00:00
/** Code de la commande echo
* @file oui-dire.c
* @author rick <rick@gnous.eu>
* @date 2021
2021-02-20 17:34:43 +00:00
*/
#include "oui-dire.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2021-02-21 23:00:47 +00:00
/**
* echo(): Affiche sur la sortie standart un message
* @args: message à afficher sur la sortie standart
*/
2021-02-20 17:34:43 +00:00
void echo(char *args)
{
char tmp = *args;
int i = 0;
while (tmp != '\0')
{
2021-02-24 01:01:44 +00:00
if (tmp == '$')
{
i++;
i += get_var((args+i), i);
}
else
{
putchar(tmp);
i++;
}
tmp = *(args + i);
2021-02-20 17:34:43 +00:00
}
putchar('\n');
}
2021-02-24 01:01:44 +00:00
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;
}