dodanie poczatkow tree

This commit is contained in:
Jakub Adamski 2019-01-13 12:07:28 +01:00
parent 672691d576
commit 3db7748748
3 changed files with 81 additions and 18 deletions

Binary file not shown.

View File

@ -63,17 +63,84 @@ void spathf (char **argv, int *spath){
*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], lastdir[max];
strcpy(ndr, directory);
while(1){
strcpy(lastdir, ndr);
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 (chdir(name) == 0){ /*udalo nam sie wejsc do katalogu*/
closedir(d);
printf("Katalog: %s\n", name);
getcwd(ndr, sizeof(ndr)); /*zmieniamy directory*/
break; /*break czytanie poprzedniego katalogu*/
}
else
printf("Plik: %s\n", name);
}
}
}
if (!strcmp(lastdir, ndr))
break; /*jezeli nie zmienilismy kat to wychodzimy zeby nie czytac tego samego jeszcze raz*/
}
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;
}
struct passwd *p = getpwuid(getuid());
if (spath){
prompt = (char*)malloc(strlen(directory)+strlen(p->pw_name)+22 *sizeof(char));
@ -109,24 +176,17 @@ int main (){
continue;
}
else if (!strcmp(argv[0], "help")){
if (fork() == 0){
execlp("less", "less", "help", NULL);
perror("execlp");
exit(EXIT_FAILURE);
help(argv);
continue;
}
else
wait(NULL);
else if (!strcmp(argv[0], "showtree")){
tree(argv, directory);
continue;
}
else if (!strcmp(argv[0], "exit"))
break;
else
if (fork() == 0){
execvp(argv[0], argv);
perror("execvp");
exit(EXIT_FAILURE);
}
else
wait(NULL);
exe(argv);
free(komenda);
free(prompt);

View File

@ -15,7 +15,10 @@ nie byo ncurses
instaluje apt install
i dorzucam do gcc -ncurses
!!!
dodam moja funkcje showtree ktora bedzie wyswietlac w formie drzewa strukture katalogow
oraz funkcje wyswietlajaca procesy proc
!!!
z komentarzy
p->pw_name