198 lines
3.9 KiB
C
198 lines
3.9 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>
|
|
#include <readline/readline.h>
|
|
#include <readline/history.h>
|
|
|
|
const int max = 1000;
|
|
|
|
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);
|
|
}
|
|
argv[w]=NULL;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void help (char **argv){
|
|
if (fork() == 0){
|
|
execlp("less", "less", "help", NULL);
|
|
perror("execlp");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
else
|
|
wait(NULL);
|
|
}
|
|
|
|
void exe (char **argv){
|
|
if (fork() == 0){
|
|
execvp(argv[0], argv);
|
|
perror("execvp");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
else
|
|
wait(NULL);
|
|
}
|
|
|
|
void tree (char **argv, char * directory){
|
|
if (argv[1] != NULL && argv[2] != NULL){
|
|
printf("Too many arguments\nshowtree takes no arguments \nor exacly 1 argument for desired directory\n");
|
|
return;
|
|
}
|
|
else if (argv[1] != NULL && argv[2] == NULL){
|
|
printf("Dokladnie 1 arg\n");
|
|
return;
|
|
}
|
|
else{
|
|
if (fork() == 0){
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
char ndr[max];
|
|
strcpy(ndr, directory);
|
|
|
|
if ((d = opendir(ndr)) != NULL){ /*wchodzimy do katalogu*/
|
|
while ((ent = readdir(d)) != NULL){ /*czytamy zawartosc*/
|
|
char *name = ent->d_name;
|
|
if (!(!strcmp(name, ".") || !strcmp(name, ".."))){ /*jesli nazwa rozna od . lub .. */
|
|
if (fork()==0){
|
|
if (chdir(name) == 0){ /*udalo nam sie wejsc do katalogu*/
|
|
closedir(d);
|
|
printf("Katalog: %s\n", name);
|
|
getcwd(ndr, sizeof(ndr)); /*zmieniamy directory*/
|
|
d = opendir(ndr);
|
|
}
|
|
else{
|
|
printf("Plik: %s\n", name);
|
|
exit(0);
|
|
}
|
|
}
|
|
else
|
|
wait(NULL);
|
|
}
|
|
}
|
|
closedir(d);
|
|
}
|
|
exit(0);
|
|
}
|
|
else{
|
|
wait(NULL);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main (){
|
|
int spath = 1;
|
|
using_history();
|
|
struct passwd *p = getpwuid(getuid());
|
|
|
|
while (1){
|
|
char *argv[100];
|
|
char *komenda, *prompt, directory[max];
|
|
|
|
if (getcwd(directory, sizeof(directory)) == NULL){
|
|
perror("getcwd() error");
|
|
return -1;
|
|
}
|
|
|
|
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);
|
|
|
|
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")){
|
|
help(argv);
|
|
continue;
|
|
}
|
|
else if (!strcmp(argv[0], "showtree")){
|
|
tree(argv, directory);
|
|
continue;
|
|
}
|
|
else if (!strcmp(argv[0], "exit"))
|
|
break;
|
|
else
|
|
exe(argv);
|
|
|
|
free(komenda);
|
|
free(prompt);
|
|
}
|
|
return 0;
|
|
}
|