132 lines
2.4 KiB
C
132 lines
2.4 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <pwd.h>
|
|
|
|
const int max = 1000;
|
|
|
|
int 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;
|
|
}
|
|
}
|
|
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, spath = 1;
|
|
while (1){
|
|
char* argv[100];
|
|
char komenda[max], directory[max];
|
|
if (getcwd(directory, sizeof(directory)) == NULL){
|
|
perror("getcwd() error");
|
|
return -1;
|
|
}
|
|
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("less", "less", "help", NULL);
|
|
perror("execlp");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
else
|
|
wait(NULL);
|
|
}
|
|
else if (!strcmp(argv[0], "exit"))
|
|
break;
|
|
else
|
|
if (fork() == 0){
|
|
execvp(argv[0], argv);
|
|
perror("execvp");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
else
|
|
wait(NULL);
|
|
|
|
for(i=0; i<ile; i++)
|
|
free(argv[i]);
|
|
}
|
|
return 0;
|
|
}
|