Thumbnail

rani/cscroll.git

Clone URL: https://git.buni.party/rani/cscroll.git

commit 12d9b708aaf23a4d3ac91745bcbea713decb5317 Author: Raniconduh <clagv.randomgames@gmail.com> Date: Sun Oct 10 16:40:41 2021 +0000 added -p option to print path diff --git a/include/io.h b/include/io.h index 0e6ea35..ca1838a 100644 --- a/include/io.h +++ b/include/io.h @@ -36 +37 @@  #endif    #include <stdbool.h> +#include <stdio.h>    #define NO_IDENT 0   @@ -353 +366 @@ void terminate_curses(void);  void curses_write_file(struct dir_entry_t *, bool);  char curses_getch(void);  char * prompt(char *, char **); + +extern bool print_path; +extern FILE * stdout_back; diff --git a/src/io.c b/src/io.c index b0fc05a..54e4400 100644 --- a/src/io.c +++ b/src/io.c @@ -111 +120 @@  #include <ncurses.h>  #include <stdbool.h>  #include <string.h> +#include <stdio.h>    #include "dir.h"  #include "io.h"   +bool print_path = false; +FILE * stdout_back = NULL; +  void curses_init(void) { + if (print_path) { + stdout_back = stdout; + stdout = fopen("/dev/tty", "w"); + } +   initscr();   curs_set(0);   noecho(); @@ -326 +4111 @@ void terminate_curses(void) {   curs_set(1);   echo();   endwin(); + + if (print_path) { + fclose(stdout); + stdout = stdout_back; + }  }     diff --git a/src/main.c b/src/main.c index 0d5dfbd..62f20bc 100644 --- a/src/main.c +++ b/src/main.c @@ -818 +826 @@  #include "dir.h"  #include "io.h"   -int main(int argc, char ** argv) { - curses_init(); - +int main(int argc, char ** argv) {   if (argc > 1) { - cwd = malloc(strlen(argv[1]) + 2); - strcpy(cwd, argv[1]); - } else { + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-p")) { + print_path = true; + } else { + cwd = malloc(strlen(argv[i]) + 2); + strcpy(cwd, argv[i]); + } + } + } + + if (!cwd) {   char * p = getenv("PWD");   cwd = malloc(strlen(p) + 2);   strcpy(cwd, p);   }   + curses_init(); +   list_dir(cwd);     size_t cursor = 1; @@ -1455 +1538 @@ done:   free_dir_entries();   free(dir_entries);   terminate_curses(); + + if (print_path) puts(cwd); +   return 0;  }