Pliki_SOP/Microshell/microshell.c
Jakub Adamski d742f3f1f3 fork
2019-01-10 14:08:07 +01:00

62 lines
1.1 KiB
C

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
const int max = 1000;
void cut(char **argv, char *command){
int i = 0, w = 0, x = 0;
char argument[100];
while (1){
if(command[i] == 10){
argv[w] = NULL;
break;
}
else if (command[i] == 32){
i+=1;
x=0;
continue;
}
else{
while(command[i] != 32 && command[i]!=10){
argument[x]=command[i];
x+=1;
i+=1;
}
argument[x] = '\0';
argv[w]=malloc(x+1 * sizeof(char));
strcpy(argv[w], argument);
w+=1;
}
}
}
int main (){
char* argv[100];
while (1){
char komenda[max], directory[max];
if (getcwd(directory, sizeof(directory)) == NULL){
perror("getcwd() error");
return 1;
}
printf("[%s]$ ", directory);
fgets(komenda, max, stdin);
cut(argv, komenda);
if (argv[0]==NULL)
continue;
else if (!strcmp(argv[0], "exit"))
break;
else
if (fork() == 0){
ptintf("/n");
execvp(argv[0], argv);
}
else{
wait(NULL);
printf("koniec komendy");
}
}
return 0;
}