cd and spath

This commit is contained in:
Jakub Adamski 2019-01-11 15:48:26 +01:00
parent d41781b2c7
commit 7efd27a635
3 changed files with 63 additions and 3 deletions

View File

@ -8,4 +8,9 @@ Author= Jakub Adamski
Working commands
help - manual
exit - ends microshell
cd - changing directory
(nie dziala dla np ~/)
jak zrobie echo $HOME tez nie dziala
dodac informacje o limitach dlugosci albo zrobic
to dynamicznie

Binary file not shown.

View File

@ -4,6 +4,9 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
const int max = 1000;
@ -35,20 +38,72 @@ int cut(char **argv, char *command){
return w+1;
}
void cd (char **argv){
if(argv[1] == NULL){
printf("Add arguments\n");
return;
}
else if(argv[2] != NULL){
printf("Too many arguments\n");
return;
}
else{
if (chdir(argv[1]) != 0)
perror("chdir error");
return;
}
}
void spathf (char **argv, int *spath){
int l;
if (argv[1] == NULL){
printf("Add arguments\n");
return;
}
else if (argv[2] != NULL){
printf("Too many arguments\n");
return;
}
else if (!strcmp(argv[1], "help")){
printf("Arguments:\n0 turns off path showing\n1 turns on path showing\n");
return;
}
l = argv[1][0] - '0';
if (argv[1][1] != 0 || l < 0 || l > 1){
printf("Wrong number\n");
return;
}
else
*spath = l;
}
int main (){
int ile, i;
int ile, i, spath = 1;
while (1){
char* argv[100];
char komenda[max], directory[max];
if (getcwd(directory, sizeof(directory)) == NULL){
perror("getcwd() error");
return 1;
return -1;
}
printf("[%s]$ ", directory);
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 (argv[0]==NULL)
continue;
else if (!strcmp(argv[0], "spath")){
spathf(argv, &spath);
continue;
}
else if (!strcmp(argv[0], "cd")){
cd(argv);
continue;
}
else if (!strcmp(argv[0], "help")){
if (fork() == 0){
execlp("cat", "cat", "help", NULL);