Zaktualizuj 'microshell/microshell.c'

This commit is contained in:
Krystian Rzepa 2018-12-19 18:01:55 +00:00
parent fd1d2a348b
commit 52f7668822

View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <string.h>
#include <wait.h>
#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;
}