From 334a2e68518004dc15c3e8a7b6adb4bc199ba1da Mon Sep 17 00:00:00 2001 From: mananapr Date: Tue, 12 Feb 2019 17:39:10 +0530 Subject: [PATCH] Added support for cli arguments --- cf.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/cf.c b/cf.c index 169b89b..7eddbf3 100644 --- a/cf.c +++ b/cf.c @@ -147,7 +147,7 @@ int startx, starty, maxx, maxy; Initializes the program Sets the relevant file paths */ -void init() +void init(int argc, char* argv[]) { // Set Locale (For wide characters) setlocale(LC_ALL, ""); @@ -231,6 +231,40 @@ void init() printf("Couldn't open current directory"); exit(1); } + + // Path of directory was specified + if(argc == 2) + { + // Absolute path + if(argv[1][0] == '/') + { + free(dir); + allocSize = snprintf(NULL, 0, "%s", argv[1]); + dir = malloc(allocSize+1); + snprintf(dir,allocSize+1,"%s", argv[1]); + } + // Relative path + else + { + char *temp; + int temp_size; + temp_size = snprintf(NULL, 0, "%s", argv[1]); + allocSize = snprintf(NULL, 0, "%s", dir); + temp = malloc(temp_size + allocSize + 2); + snprintf(temp, temp_size + allocSize + 2, "%s/%s", dir, argv[1]); + free(dir); + dir = malloc(temp_size + allocSize + 2); + snprintf(dir, temp_size + allocSize + 2, "%s", temp); + free(temp); + } + } + + // Excess arguments given + else if(argc > 2) + { + printf("Incorrect Usage!\n"); + exit(1); + } } @@ -1446,7 +1480,7 @@ void goBack() int main(int argc, char* argv[]) { // Initialization - init(); + init(argc, argv); curses_init(); // For Storing user keypress @@ -1827,6 +1861,7 @@ int main(int argc, char* argv[]) // Allocate Memory to `buf` to store output of command buf = malloc(PATH_MAX); + memset(buf, '\0', PATH_MAX); fp = fopen(temp_dir, "r"); while(fgets(buf,PATH_MAX,fp) != NULL){} fclose(fp);