From 52f76688223af3a925380715fd43c86dfb6de275 Mon Sep 17 00:00:00 2001 From: Krystian Rzepa Date: Wed, 19 Dec 2018 18:01:55 +0000 Subject: [PATCH] Zaktualizuj 'microshell/microshell.c' --- microshell/microshell.c | 87 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/microshell/microshell.c b/microshell/microshell.c index e69de29..54715a5 100644 --- a/microshell/microshell.c +++ b/microshell/microshell.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include + +#define space " \n" +#define max_c 500 + +int help() { + printf ("microshell v1.0. © 2018 Krystian Rzepa\n"); + printf("Obslugiwane polecenia:\n\ + help - pomoc\n\ + exit - wyjscie\n\ + cd - zmiana aktualnego katalogu\n\ + pwd - wyswietla aktualny katalog\n\ + obsluga programow z parametrami\n"); +} + + +const char *getUserName() +{ + uid_t uid = geteuid(); + struct passwd *pw = getpwuid(uid); + + return pw->pw_name; +} + + +int main() +{ + char cwd[1000], hostName[100], *pwd[1000]; + register struct passwd *pw; + register uid_t uid; + uid = geteuid (); + pw = getpwuid (uid); + + char line[max_c], lend[max_c], * commands[max_c], ** command; + + while (1) { + gethostname(hostName, 99); + getcwd(cwd, 999); + + printf("%s@%s %s $ ", pw->pw_name, hostName, cwd); + + fflush(stdout); + if (fgets(line, max_c, stdin )) { + + command = commands; + *command++ = strtok(line,space); + while ((*command++ = strtok(NULL,space))); + + + if (strcmp(commands[0], "help")==0) help(); + + else if (strcmp(commands[0], "exit")==0) { + if (commands[1]==0) exit(0); + else exit(atoi(commands[1])); + } + + else if (strcmp(commands[0], "cd")==0) { + if (commands[1]==0) printf("Wymagany argument\n"); + int ret; + ret = chdir(commands[1]); + strcpy(lend, "pwd"); + } + + else if (strcmp(commands[0], "pwd")==0) printf ("%s\n", cwd); + + else { + pid_t x; + x = fork(); + + if (x==0) { + if (commands[1] == 0) execlp(commands[0], commands[0], (char *)NULL); + else execlp(commands[0], commands[0], commands[1], (char *)NULL); + printf("Nie ma takiego polecenia. Wpisz help aby wyswietlic dostepne polecenia.\n"); + } + else waitpid(x, NULL, 0); + + } + } + } + + return 0; +} \ No newline at end of file