From 7edc3ede1e7c17dd26040520c65765ef4ed8e782 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Fri, 31 Jan 2025 19:45:59 +0100 Subject: working on some basic commands like listing the current directory and get the pid of a process, following the APUE --- main.c | 63 ++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 23 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index ce2d007..b0ebe5c 100644 --- a/main.c +++ b/main.c @@ -1,34 +1,51 @@ #include #include + + #include #include -#include + #include -int main() { - const bool RUNNING = true; - char *COMMAND_HISTORY; - COMMAND_HISTORY = (char *)malloc(sizeof(char)); +// import files for reading the current directory +#include "apue.c" +#include + +#define true 1 +#define false 0 + +void err_quit(char * str) { + printf("%s", str); +} +void err_sys(char * str) { + printf("%s", str); +} + +void PROCESS_PID(char *string) { + printf("PID %ld\n", (long)getpid()); +} + +void LIST_CURRENT_DIRECTORY(int argc, char *argv[]) +{ + DIR *dp; + struct dirent *dirp; + if (argc != 2) + err_quit("usage: ls directory_name"); + if ((dp = opendir(argv[1])) == NULL) + err_sys( argv[1]); + while ((dirp = readdir(dp)) != NULL) + printf("%s\n", dirp->d_name); + closedir(dp); +} + + +int main(int argc, char *argv[]) { + + printf("succefully started the program"); // RUNNING THE PROGRAM ON AN ENDLESS LOOP // BREAKS WHEN USER ENTERS EXIT - while(RUNNING) { - char *TEMP = ""; - - scanf("%s", &TEMP); - for (int i = 0; (TEMP + i) != "/0"; i++) - { - tolower(TEMP[0]); - } - - if (TEMP == "exit") { - printf("exited successfully"); - } - else { - strcpy(&TEMP, (COMMAND_HISTORY)); - printf("%s",COMMAND_HISTORY); - } - - } + // ReSharper disable once CppDFAEndlessLoop + return 0; } -- cgit v1.2.3-70-g09d2