2019-01-10 12:56:56 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2019-01-10 14:17:29 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2019-01-11 15:48:26 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
2019-01-12 15:58:36 +01:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
2019-01-10 12:56:56 +01:00
|
|
|
|
|
|
|
const int max = 1000;
|
2019-01-10 19:52:29 +01:00
|
|
|
|
2019-01-12 15:58:36 +01:00
|
|
|
void cut(char **argv, char *command){
|
|
|
|
int w = 0;
|
|
|
|
char sep[] = " ";
|
|
|
|
char *ptr = strtok(command, sep);
|
|
|
|
while (ptr != NULL){
|
|
|
|
argv[w]=ptr;
|
|
|
|
w+=1;
|
|
|
|
ptr = strtok(NULL, sep);
|
2019-01-10 13:13:37 +01:00
|
|
|
}
|
2019-01-12 15:58:36 +01:00
|
|
|
argv[w]=NULL;
|
2019-01-10 12:56:56 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:48:26 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-10 12:56:56 +01:00
|
|
|
int main (){
|
2019-01-12 15:58:36 +01:00
|
|
|
int spath = 1;
|
|
|
|
using_history();
|
2019-01-10 13:13:37 +01:00
|
|
|
while (1){
|
2019-01-10 14:53:53 +01:00
|
|
|
char* argv[100];
|
2019-01-12 15:58:36 +01:00
|
|
|
char *komenda, *prompt, directory[max];
|
2019-01-10 13:13:37 +01:00
|
|
|
if (getcwd(directory, sizeof(directory)) == NULL){
|
|
|
|
perror("getcwd() error");
|
2019-01-11 15:48:26 +01:00
|
|
|
return -1;
|
2019-01-10 13:13:37 +01:00
|
|
|
}
|
2019-01-11 15:48:26 +01:00
|
|
|
struct passwd *p = getpwuid(getuid());
|
2019-01-12 15:58:36 +01:00
|
|
|
|
|
|
|
if (spath){
|
|
|
|
prompt = (char*)malloc(strlen(directory)+strlen(p->pw_name)+22 *sizeof(char));
|
|
|
|
prompt[0]='\0';
|
|
|
|
strcat(prompt, "\033[1;32m");
|
|
|
|
strcat(prompt, "[");
|
|
|
|
strcat(prompt, directory);
|
|
|
|
strcat(prompt, "]\033[0m");
|
|
|
|
strcat(prompt, p->pw_name);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
prompt = (char*)malloc(strlen(p->pw_name)+3 *sizeof(char));
|
|
|
|
strcat(prompt, p->pw_name);
|
|
|
|
}
|
|
|
|
strcat(prompt, "$ ");
|
|
|
|
|
|
|
|
if (!(komenda = readline(prompt)))
|
|
|
|
break;
|
|
|
|
if (komenda[0] == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
add_history(komenda);
|
|
|
|
cut(argv, komenda);
|
2019-01-11 15:48:26 +01:00
|
|
|
|
2019-01-10 13:59:45 +01:00
|
|
|
if (argv[0]==NULL)
|
|
|
|
continue;
|
2019-01-11 15:48:26 +01:00
|
|
|
else if (!strcmp(argv[0], "spath")){
|
|
|
|
spathf(argv, &spath);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[0], "cd")){
|
|
|
|
cd(argv);
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-10 23:53:16 +01:00
|
|
|
else if (!strcmp(argv[0], "help")){
|
|
|
|
if (fork() == 0){
|
2019-01-11 17:17:03 +01:00
|
|
|
execlp("less", "less", "help", NULL);
|
2019-01-10 23:53:16 +01:00
|
|
|
perror("execlp");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wait(NULL);
|
|
|
|
}
|
2019-01-10 15:05:24 +01:00
|
|
|
else if (!strcmp(argv[0], "exit"))
|
2019-01-10 14:57:55 +01:00
|
|
|
break;
|
2019-01-10 13:17:30 +01:00
|
|
|
else
|
2019-01-10 15:08:03 +01:00
|
|
|
if (fork() == 0){
|
2019-01-10 14:04:29 +01:00
|
|
|
execvp(argv[0], argv);
|
2019-01-10 15:08:03 +01:00
|
|
|
perror("execvp");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-01-10 14:15:12 +01:00
|
|
|
else
|
|
|
|
wait(NULL);
|
2019-01-10 14:36:35 +01:00
|
|
|
|
2019-01-12 15:58:36 +01:00
|
|
|
free(komenda);
|
|
|
|
free(prompt);
|
2019-01-10 13:13:37 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2019-01-10 12:56:56 +01:00
|
|
|
}
|