Skip to content

Commit

Permalink
feat: Add -d option for remove mark functionality and fix truncation …
Browse files Browse the repository at this point in the history
…warning
  • Loading branch information
yellow-footed-honeyguide committed Sep 16, 2024
1 parent 217a654 commit 4328154
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cmake_minimum_required(VERSION 3.10)
project(nooks VERSION 1.3.5)
project(nooks VERSION 1.4.1)

# Set C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED True)

# Add executable
add_compile_options(-Wall -Wextra -Wpedantic)

add_executable(nooks nooks.c handle_arguments.c actions.c)

# Set the default build output directory
Expand Down
32 changes: 28 additions & 4 deletions actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#define FACAD_CMD "facad" // Define command name for custom 'facad' utility
#define LS_CMD "ls" // Define command name for 'ls' utility
// Define ls command arguments
#define LS_ARGS "-A", "-F", "--group-directories-first", "--sort=extension", "--color=always"

int quiet_mode = 0; // Global flag for quiet mode, initialized to false
Expand Down Expand Up @@ -41,7 +40,11 @@ void save_current_directory(const char *spot) {
CONFIG_FILE); // Construct config file path

char temp_path[MAX_PATH]; // Buffer for temporary file path
snprintf(temp_path, sizeof(temp_path), "%s.tmp", config_path); // Construct temporary file path
// Construct temporary file path
if (strncat(strcpy(temp_path, config_path), ".tmp",
sizeof(temp_path) - strlen(config_path) - 1) != temp_path) {
fprintf(stderr, "Path truncated\n");
}

FILE *file = fopen(config_path, "r"); // Open config file for reading
FILE *temp_file = fopen(temp_path, "w"); // Open temporary file for writing
Expand Down Expand Up @@ -263,9 +266,30 @@ void print_help() {
/**
* @brief Prints the version information of the program
*
* This function outputs the version number of the application,
* which is defined in the NOOKS_VERSION macro.
* Example: nooks -v, nooks --version
* Value of NOOKS_VERSION macro defined in CMakeLists.txt
*/
void print_version() {
printf("%s\n", NOOKS_VERSION); // Print version information
}

/**
* @brief Remove selected mark.
* config_name is hardcoded
* Example: nooks -d work
*
*/
void remove_mark(const char *mark) {
const char *home_dir = getenv("HOME");
char config_name[256];
snprintf(config_name, sizeof(config_name), "%s/.nooks", home_dir);
FILE *f = fopen(config_name, "r"), *t = fopen("temp", "w");
char s[100];
size_t len = strlen(mark);
while (fgets(s, 100, f))
if (strncmp(s, mark, len)) fputs(s, t);
fclose(f);
fclose(t);
remove(config_name);
rename("temp", config_name);
}
1 change: 1 addition & 0 deletions actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define CONFIG_FILE "/.nooks"

void save_current_directory(const char *spot);
void remove_mark(const char *mark);
void go_to_directory(const char *spot);
void show_all_spots();
void print_usage();
Expand Down
30 changes: 21 additions & 9 deletions handle_arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ void handle_arguments(int argc, char *argv[]) { // Function to process command-
char *directory_name = NULL; // Pointer to store the directory name
int save_mark_option_used = 0; // Flag for save-mark option usage
int all_option_used = 0; // Flag for all option usage
char *mark_name = NULL;
int delete_option_used = 0; // Flag for delete mark option

// Define long options for getopt_long
static struct option long_options[] = {
{"save-mark", required_argument, 0, 's'}, // Option to save current directory
{"delete-mark", required_argument, 0, 'd'}, // Option to save current directory
{"all", no_argument, 0, 'a'}, // Option to show all saved spots
{"help", no_argument, 0, 'h'}, // Option to display help
{"version", no_argument, 0, 'v'}, // Option to display version
Expand All @@ -27,11 +30,15 @@ void handle_arguments(int argc, char *argv[]) { // Function to process command-
int long_index = 0; // Index of the current option in long_options array

// Parse command-line options using getopt_long
while ((opt = getopt_long(argc, argv, "s:ahvq", long_options, &long_index)) != -1) {
while ((opt = getopt_long(argc, argv, "s:d:ahvq", long_options, &long_index)) != -1) {
switch (opt) { // Switch on the current option
case 's': // Save current directory with provided name
directory_name = optarg; // Store the provided directory name
save_mark_option_used = 1; // Set the flag for save-mark option
break;
case 'd':
mark_name = optarg;
delete_option_used = 1; // Set the flag for delete-mark option
break;
case 'a': // Show all saved spots
all_option_used = 1; // Set the flag for all option
Expand All @@ -57,7 +64,7 @@ void handle_arguments(int argc, char *argv[]) { // Function to process command-
} else { // For other unknown options
fprintf(stderr, "Unknown option '-%c'.\n", optopt); // Print error message
fprintf(stderr,
"Usage: %s [-s|--save-mark mark_name] [-a|--all] [-h|--help] "
"Usage: %s [-s|--save-mark mark_name] [-d|--delete-mark mark_name] [-a|--all] [-h|--help] "
"[directory_name]\n",
argv[0]); // Print usage information
exit(EXIT_FAILURE); // Exit the program with failure status
Expand All @@ -68,20 +75,25 @@ void handle_arguments(int argc, char *argv[]) { // Function to process command-
}
}

// Handle the -a or --all option
// Handle the --all or -a option
if (all_option_used) {
show_all_spots(); // Call function to display all saved spots
return; // Exit the function
show_all_spots();
return;
}

// Handle the --save or -s option
// Handle the --save-mark or -s option
if (save_mark_option_used) {
save_current_directory(
directory_name); // Call function to save current directory with provided name
save_current_directory(directory_name);
}

// Handle the --delete-mark or -d option
if (delete_option_used) {
puts("hello");
remove_mark(mark_name);
}

// Handle non-option arguments (directory name without flag)
if (optind < argc && !save_mark_option_used) {
go_to_directory(argv[optind]); // Call function to navigate to the provided directory
go_to_directory(argv[optind]);
}
}

0 comments on commit 4328154

Please sign in to comment.