diff --git a/Microshell/Makefile b/Microshell/Makefile index 15f9ce4..6872834 100644 --- a/Microshell/Makefile +++ b/Microshell/Makefile @@ -1,4 +1,4 @@ microshell: microshell.c - gcc -Wall -ansi -o microshell microshell.c + gcc -Wall -ansi -o microshell microshell.c -lreadline -lncurses clean: rm microshell diff --git a/Microshell/help b/Microshell/help index 19e6780..1391faa 100644 --- a/Microshell/help +++ b/Microshell/help @@ -7,9 +7,9 @@ Index number=s444341 Limits Max length of line=1000 Max number of arguments=100 -Max length of argument=100 ------------- Working commands +up/down arrow - commands history help - manual exit - ends microshell cd - changing directory @@ -20,9 +20,11 @@ spath: 0 - turns off displaying current directory 1 - turns on displaying current directory PATH commands + if you want colors use + for example ls --color WARNING -dont use path shorcuts +dont use enviroment shorcuts example cd ~/ or echo $HOME ------------- diff --git a/Microshell/microshell b/Microshell/microshell index 37159ed..2aed7bf 100755 Binary files a/Microshell/microshell and b/Microshell/microshell differ diff --git a/Microshell/microshell.c b/Microshell/microshell.c index 6d0ef53..a8982d2 100644 --- a/Microshell/microshell.c +++ b/Microshell/microshell.c @@ -7,35 +7,21 @@ #include #include #include +#include +#include const int max = 1000; -int cut(char **argv, char *command){ - int i = 0, w = 0, x = 0; - char argument[100]; - while (1){ - if(command[i] == 10){ - argv[w] = NULL; - break; - } - else if (command[i] == 32){ - i+=1; - x=0; - continue; - } - else{ - while(command[i] != 32 && command[i]!=10){ - argument[x]=command[i]; - x+=1; - i+=1; - } - argument[x] = '\0'; - argv[w]=malloc(x+1 * sizeof(char)); - strcpy(argv[w], argument); - w+=1; - } +void cut(char **argv, char *command){ + int w = 0; + char sep[] = " "; + char *ptr = strtok(command, sep); + while (ptr != NULL){ + argv[w]=ptr; + w+=1; + ptr = strtok(NULL, sep); } - return w+1; + argv[w]=NULL; } void cd (char **argv){ @@ -78,21 +64,39 @@ void spathf (char **argv, int *spath){ } int main (){ - int ile, i, spath = 1; + int spath = 1; + using_history(); while (1){ char* argv[100]; - char komenda[max], directory[max]; + char *komenda, *prompt, directory[max]; if (getcwd(directory, sizeof(directory)) == NULL){ perror("getcwd() error"); return -1; } struct passwd *p = getpwuid(getuid()); - /*dodac obsluge bledow do get?*/ - if (spath) - printf("[%s]", directory); - printf("%s$ ", p->pw_name); /* mozna zrobic (*p).pw_name */ - fgets(komenda, max, stdin); - ile = cut(argv, komenda); + + if (spath){ + prompt = (char*)malloc(strlen(directory)+strlen(p->pw_name)+22 *sizeof(char)); + prompt[0]='\0'; + strcat(prompt, "\033[1;32m"); + strcat(prompt, "["); + strcat(prompt, directory); + strcat(prompt, "]\033[0m"); + strcat(prompt, p->pw_name); + } + else{ + prompt = (char*)malloc(strlen(p->pw_name)+3 *sizeof(char)); + strcat(prompt, p->pw_name); + } + strcat(prompt, "$ "); + + if (!(komenda = readline(prompt))) + break; + if (komenda[0] == 0) + continue; + + add_history(komenda); + cut(argv, komenda); if (argv[0]==NULL) continue; @@ -124,8 +128,8 @@ int main (){ else wait(NULL); - for(i=0; ipw_name +mozna zrobic (*p).pw_name + +/*fgets(komenda, max, stdin);*/