Added 'cd' and 'dir' command

This commit is contained in:
netbenixcn 2020-06-07 11:16:25 +02:00
parent 5479b61dbf
commit c80f6617c3
7 changed files with 112 additions and 10 deletions

2
.gitignore vendored
View file

@ -9,3 +9,5 @@ logo.o
sql_test.o sql_test.o
gtk_test.o gtk_test.o
sys_info.o sys_info.o
show_dir.o
change_dir.o

20
commands/change_dir.c Normal file
View file

@ -0,0 +1,20 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include "change_dir.h"
#include "../utils/logger.h"
void changeCurrentWorkDir(char *arg[]){
DIR *d;
struct dirent *dir;
d = opendir(arg[1]);
if(d == NULL){
printf("Folder not found.\n");
return;
} else {
chdir(arg[1]);
}
closedir(d);
}

6
commands/change_dir.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef _CHANGE_DIR_H_
#define _CHANGE_DIR_H_
void changeCurrentWorkDir(char * arg[]);
#endif

49
commands/show_dir.c Normal file
View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <linux/limits.h>
#include <dirent.h>
#include <string.h>
#include "show_dir.h"
#include "../utils/logger.h"
// Color Codes:
// Reset: \033[0m
// Red: \033[0;31m
// Green: \033[0;32m
// Yellow: \033[0;33m
// Blue: \033[0;34m
// Magenta: \033[0;35m
// Cyan: \033[0;36m
// BoldRed: \033[1;31m
// BoldGreen: \033[1;32m
// BoldYellow: \033[1;33m
// BoldBlue: \033[1;34m
// BoldMagenta: \033[1;35m
// BoldCyan: \033[1;36m
void showDirectory(char *arg[]){
DIR *d;
struct dirent *dir;
char path[PATH_MAX];
d = opendir(arg[1]);
if(d == NULL){
printf("Folder not found.\n");
return;
}
realpath(arg[1], path);
printf("Path: %s\n", path);
if(d){
while((dir = readdir(d))){
if(dir->d_type == 8){ //IF IS FILE
printf("\033[1;34m%s\033[0m\n", dir->d_name);
} else if(dir->d_type == 4){ //IF IS DIR
printf("\033[1;33m%s\033[0m\n", dir->d_name);
} else { //IF IS !FILE AND !DIR
printf("%s\n", dir->d_name);
}
}
closedir(d);
}
}

6
commands/show_dir.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef _SHOW_DIR_H_
#define _SHOW_DIR_H_
void showDirectory(char *arg[]);
#endif

23
main.c
View file

@ -5,8 +5,12 @@
#include <gdk/gdk.h> #include <gdk/gdk.h>
#include <gdk/gdkconfig.h> #include <gdk/gdkconfig.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <unistd.h>
#include <linux/limits.h>
#include "commands/help.h" #include "commands/help.h"
#include "commands/show_dir.h"
#include "commands/change_dir.h"
#include "utils/gtk_test.h" #include "utils/gtk_test.h"
#include "utils/sql_test.h" #include "utils/sql_test.h"
#include "etc/logo.h" #include "etc/logo.h"
@ -56,26 +60,36 @@ int commandHandler(char* cmd){
p[strlen(p)-1] = 0; p[strlen(p)-1] = 0;
arg[i] = p; arg[i] = p;
if(!strcmp(arg[0], "list") && argc < 2){ if(!strcmp(arg[0], "list")){
printf("\033[1;34mlist\033[0m : this list\n"); printf("\033[1;34mlist\033[0m : this list\n");
printf("\033[1;34mversion\033[0m : shows the version\n"); printf("\033[1;34mversion\033[0m : shows the version\n");
printf("\033[1;34mexit\033[0m : exit the program\n"); printf("\033[1;34mexit\033[0m : exit the program\n");
return 0; return 0;
} else if(!strcmp(arg[0], "version") && argc < 2){ } else if(!strcmp(arg[0], "version")){
printf("netbenixCMD (Version: \033[1;34m%s\033[0m)\n", VERSION); printf("netbenixCMD (Version: \033[1;34m%s\033[0m)\n", VERSION);
printf("Author: \033[1;34m%s\033[0m\n", AUTHOR); printf("Author: \033[1;34m%s\033[0m\n", AUTHOR);
logger("Showing program version."); logger("Showing program version.");
return 0; return 0;
} else if(!strcmp(arg[0], "exit") && argc < 2){ } else if(!strcmp(arg[0], "exit")){
return 1; return 1;
} else if(!strcmp(arg[0], "dir")){
showDirectory(arg);
return 0;
} else if(!strcmp(arg[0], "cd")){
changeCurrentWorkDir(arg);
return 0;
} else { } else {
printf("Unknown command. Please use --help for more information.\n"); printf("Unknown command. Please use --help for more information.\n");
logger("User entered unknown command."); logger("User entered unknown command.");
return 0; return 0;
} }
} }
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
char buffer[1024]; char buffer[1024];
extern char loggerDirPath[PATH_MAX];
realpath("." , loggerDirPath);
logger("================================================"); logger("================================================");
snprintf(buffer, sizeof(buffer), "Starting netbenixCMD (Version: %s)", VERSION); snprintf(buffer, sizeof(buffer), "Starting netbenixCMD (Version: %s)", VERSION);
logger(buffer); logger(buffer);
@ -114,7 +128,8 @@ int main(int argc, char *argv[]){
char cmd[128]; char cmd[128];
logger("Starting Command Handler."); logger("Starting Command Handler.");
while (!exit){ while (!exit){
printf("\033[0;32mnCMD> \033[0m"); char cwp[255];
printf("\033[0;32m%s> \033[0m", getcwd(cwp, 255));
fgets(cmd, 128, stdin); fgets(cmd, 128, stdin);
exit = commandHandler(cmd); exit = commandHandler(cmd);
} }

View file

@ -2,7 +2,7 @@ CC= gcc
ARGS= -export-dynamic -ansi -std=gnu99 ARGS= -export-dynamic -ansi -std=gnu99
CFLAGS= $(shell pkg-config --cflags gtk+-3.0) -I/usr/include/mysql CFLAGS= $(shell pkg-config --cflags gtk+-3.0) -I/usr/include/mysql
LDLIBS= $(shell pkg-config --libs gtk+-3.0) -L/usr/lib/mysql -lmysqlclient LDLIBS= $(shell pkg-config --libs gtk+-3.0) -L/usr/lib/mysql -lmysqlclient
OBJ= main.o logo.o logger.o help.o sys_info.o gtk_test.o sql_test.o OBJ= main.o logo.o logger.o help.o sys_info.o gtk_test.o sql_test.o show_dir.o change_dir.o
netbenixCMD: $(OBJ) netbenixCMD: $(OBJ)
$(CC) $(CFLAGS) $(ARGS) -o netbenixCMD $(OBJ) $(LDLIBS) $(CC) $(CFLAGS) $(ARGS) -o netbenixCMD $(OBJ) $(LDLIBS)
@ -20,3 +20,7 @@ gtk_test.o: utils/gtk_test.c
$(CC) $(CFLAGS) -c utils/gtk_test.c $(CC) $(CFLAGS) -c utils/gtk_test.c
sql_test.o: utils/sql_test.c sql_test.o: utils/sql_test.c
$(CC) $(CFLAGS) -c utils/sql_test.c $(CC) $(CFLAGS) -c utils/sql_test.c
show_dir.o: commands/show_dir.c
$(CC) $(CFLAGS) -c commands/show_dir.c
change_dir.o: commands/change_dir.c
$(CC) $(CFLAGS) -c commands/change_dir.c