Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- void aff_tab(char **tab) {
- int i = 0;
- int j = 0;
- while (tab[i] != NULL) {
- printf("%s", tab[i]);
- printf("\n");
- i++;
- }
- }
- char **str_to_word_tab(char *str) {
- int i = 0;
- int j = 0;
- int k = 0;
- char **tab = malloc(sizeof(char *) * strlen(str) + 1);
- while (str[k] != '=' && str[k] != '\0')
- k++;
- k++;
- while (str[k] != '\0') {
- j = 0;
- tab[i] = malloc(sizeof(char) * strlen(str) + 1);
- while (str[k] != ':' && str[k] != '\0') {
- tab[i][j++] = str[k++];
- }
- tab[i][j] = '/';
- k++;
- i++;
- }
- return (tab);
- }
- char *get_path(char **env) {
- int i = 0;
- int v = 0;
- while (env[i]) {
- if (v == 0 && env[i][0] == 'P' && env[i][1] == 'A' && env[i][2] == 'T' && env[i][3] == 'H' && env[i][4] == '=') {
- v = 1;
- return (env[i]);
- }
- else
- i++;
- }
- return (NULL);
- }
- char **concat_line(char **tab, char *cmd, char **env) {
- int i = 0;
- int j = 0;
- int k = 0;
- int c = 0;
- char *src;
- while (tab[i] != NULL) {
- j = k = c = 0;
- src = malloc(sizeof(char) * (strlen(tab[i]) + strlen(cmd) + 1));
- while (tab[i][j] != '\0') {
- src[k++] = tab[i][j++];
- }
- while (cmd[c] != '\0') {
- src[k++] = cmd[c++];
- }
- tab[i] = src;
- if (execve(tab[i], NULL, env) == -1)
- printf("%s", tab[i]);
- free(src);
- i++;
- }
- // aff_tab(tab);
- return (tab);
- }
- int main(int ac, char **av, char **env) {
- char *array = NULL;
- size_t len = 0;
- char **tab;
- while (1) {
- printf("$>");
- getline(&array, &len, stdin);
- tab = str_to_word_tab(get_path(env));
- tab = concat_line(tab, array, env);
- //aff_tab(tab);
- //try_command(tab = concat_line(tab, array), array, env);
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement