diff --git a/mark b/mark deleted file mode 100755 index 23823a8..0000000 --- a/mark +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/bash - -BOOKMARKS_FILE=~/.bookmarks - -# Catch any invalid input. -if [[ $# -gt 1 ]]; then - echo "Improper usage: mark { -ls | -rm [ keyword ] }" - echo " eg. mark" - echo " eg. mark -ls" - echo " eg. mark -rm portfolio" - echo " eg. mark -h" - echo " eg. mark --help" - - exit - -# Help page -elif [[ $1 == "-h" || $1 == "--help" ]]; then - echo "\nCreate and manage persistent bookmarks inside of terminal sessions.\n" - echo "Usage:" - echo " mark" - echo " mark -ls" - echo " eg. run main" - echo " mark -rm [ keyword ]" - echo " eg. mark -rm portfolio" - echo " mark -h" - echo " mark --help" - - exit - -# Add a bookmark to the bookmarks file. -elif [ $# == 0 ]; then - echo $(pwd) >> $BOOKMARKS_FILE - -# List bookmarks stored in the bookmarks file. -elif [ "$1" == "-ls" ]; then - echo "Bookmarks:" - cat $BOOKMARKS_FILE - -# Remove a bookmark stored in the bookmarks file. -elif [ "$1" == "-rm" ]; then - # The user is trying to remove a single bookmark - # Check if the bookmark exists - if [ $(grep -c "$2" $BOOKMARKS_FILE) -gt 0 ]; then - - # Remove the bookmark - sed -i .bak $2d $BOOKMARKS_FILE - else - echo "Bookmark not found" - fi - -# Invalid argument, display an error message -else - echo "Usage: mark [-ls] [-rm [number | number TO number]]" -fi diff --git a/src/mark.c b/src/mark.c new file mode 100644 index 0000000..0cfe48c --- /dev/null +++ b/src/mark.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include + +// Displays error message upon improper usage. +void error() { + printf("\nImproper usage: mark { -ls | -rm [ keyword(s) ] }\n" + " Usage:\n" + " mark\n" + " mark -ls\n" + " mark -rm [ keyword ]\n" + " mark -h\n" + " mark --help" + ); +} + +// Displays help message. +void help() { + printf("\nCreate and manage persistent bookmarks in the terminal.\n" + "Usage:\n" + " mark\n" + " mark -ls\n" + " mark -rm [ keyword ]\n" + " mark -h\n" + " mark --help" + ); +} + +// Lists all bookmarks in file. +void list(FILE * f) { + int c; + printf("Bookmarks:\n"); + while(1) { + c = fgetc(f); + if(feof(f)) break; + printf("%c", c); + } +} + +// Removes a bookmark from the file. +void removeMark(FILE * f, char *argv[]) { + printf("remove mark function not implemented. received input to remove: %s.", argv[2]); // TODO: Need to implement. +} + +int main(int argc, char *argv[]) { + char * bookmarksFile = strcat(getenv("HOME"), "/.bookmarks"); + FILE * f = fopen(bookmarksFile, "a+"); + + // Unable to access file. + if(f == NULL) { + printf("Error accessing file located at %s.", bookmarksFile); + return 1; + } + + // Invalid number of arguments. + else if(argc > 2 && strcmp(argv[1], "-rm") != 0) { + error(); + return 1; + } + + // Append the current directory to the bookmarks file. + else if(argc == 1) { + char cwd[PATH_MAX]; + fprintf(f, getcwd(cwd, sizeof(cwd))); + printf("Successfully bookmarked %s.", cwd); + } + + // Display help. + else if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) help(); + + // List bookmarks. + else if(strcmp(argv[1], "-ls") == 0) list(f); + + else if(strcmp(argv[1], "-rm") == 0) removeMark(f, argv); + + // Invalid argument. + else { + error(); + return 1; + } + + fclose(f); + return 0; +}