2018-12-19 19:01:55 +01:00
|
|
|
#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
|
2018-12-19 19:28:05 +01:00
|
|
|
#define BRIGHT 1
|
|
|
|
#define RED 31
|
|
|
|
#define GREEN 32
|
|
|
|
#define YELLOW 33
|
|
|
|
#define BLUE 34
|
|
|
|
#define PINK 35
|
|
|
|
#define CYAN 36
|
|
|
|
#define WHITE 37
|
2018-12-19 19:01:55 +01:00
|
|
|
|
|
|
|
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;
|
2018-12-19 19:28:05 +01:00
|
|
|
|
|
|
|
void domyslny_kolor()
|
|
|
|
{
|
|
|
|
printf("%c[%dm",0x1B,0);
|
|
|
|
}
|
2018-12-19 19:01:55 +01:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
gethostname(hostName, 99);
|
|
|
|
getcwd(cwd, 999);
|
|
|
|
|
2018-12-19 19:28:05 +01:00
|
|
|
printf("%c[%dm%s",0x1B, YELLOW, pw->pw_name);
|
|
|
|
domyslny_kolor();
|
|
|
|
printf("@");
|
|
|
|
printf("%c[%dm%s ",0x1B, YELLOW, hostName);
|
|
|
|
printf("%c[%dm%s",0x1B, CYAN, cwd);
|
|
|
|
printf("%c[%dm $ ",0x1B, GREEN);
|
|
|
|
domyslny_kolor();
|
2018-12-19 19:01:55 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|