cd and spath
This commit is contained in:
parent
d41781b2c7
commit
7efd27a635
@ -8,4 +8,9 @@ Author= Jakub Adamski
|
|||||||
Working commands
|
Working commands
|
||||||
help - manual
|
help - manual
|
||||||
exit - ends microshell
|
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.
@ -4,6 +4,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
|
||||||
const int max = 1000;
|
const int max = 1000;
|
||||||
|
|
||||||
@ -35,20 +38,72 @@ int cut(char **argv, char *command){
|
|||||||
return w+1;
|
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 main (){
|
||||||
int ile, i;
|
int ile, i, spath = 1;
|
||||||
while (1){
|
while (1){
|
||||||
char* argv[100];
|
char* argv[100];
|
||||||
char komenda[max], directory[max];
|
char komenda[max], directory[max];
|
||||||
if (getcwd(directory, sizeof(directory)) == NULL){
|
if (getcwd(directory, sizeof(directory)) == NULL){
|
||||||
perror("getcwd() error");
|
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);
|
fgets(komenda, max, stdin);
|
||||||
ile = cut(argv, komenda);
|
ile = cut(argv, komenda);
|
||||||
|
|
||||||
if (argv[0]==NULL)
|
if (argv[0]==NULL)
|
||||||
continue;
|
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")){
|
else if (!strcmp(argv[0], "help")){
|
||||||
if (fork() == 0){
|
if (fork() == 0){
|
||||||
execlp("cat", "cat", "help", NULL);
|
execlp("cat", "cat", "help", NULL);
|
||||||
|
Loading…
Reference in New Issue
Block a user