From 771b998ba5d1f451586aef0562d0012442d6bf23 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Sat, 21 Apr 2018 15:32:16 +0100 Subject: Fixed w and f --- main.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 13 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e0b60ff..57c3b0e 100644 --- a/main.c +++ b/main.c @@ -13,7 +13,7 @@ unsigned int num = 0; unsigned int pos = 0; /* addr1 is the last address, while addr2 is the previous one */ int addr1, addr2; - +char *fname; const char* skip_blank(const char **c){ @@ -99,6 +99,25 @@ int get_addr(const char **cmd){ } +int get_fname(char **cmd){ + + static char b[4096]; + int s, n; + + n = sscanf(*cmd, "%s", b); + if (n <1 ) + return 0; + fprintf(stderr, " >>> read fname: %s\n", b); + s = strlen(b); + if (s){ + fname = realloc(fname, (s+1) * sizeof(char)); + memcpy(fname, b, s+1); + return 1; + } + return -1; +} + + void main_loop(){ char *cmd; @@ -142,6 +161,19 @@ void main_loop(){ addr2 = addr1; delete_lines(); break; + case 'f': + if (ret > 0) + E; + else{ + cmd += 1; + if (get_fname(&cmd)>=0) + printf("%s\n", fname); + else{ + perror(fname); + E; + } + } + break; case 'i': if (addr1 > -1) move_to_line(addr1, 0); @@ -173,6 +205,18 @@ void main_loop(){ case 'q': exit(0); break; + case 'w': + cmd +=1; + get_fname(&cmd); + if (!fname) + E; + else{ + if (write_lines()){ + perror(fname); + E; + } + } + break; case '=': if (addr1 == -1) addr1 = num; @@ -190,19 +234,19 @@ void main_loop(){ int main(int argc, char *argv[]){ FILE *fin; - - if (argc < 2){ - printf("Usage: %s \n", argv[0]); - exit(1); - } - - if ((fin = fopen(argv[1], "r+"))){ - read_lines(fin); - } - else{ - printf("Error opening file %s\n", argv[1]); + + fname = NULL; + if (argc > 1){ + fname = malloc((strlen(argv[1])+1) * sizeof(char)); + memcpy(fname, argv[1], strlen(argv[1]) + 1); + if (fin = fopen(fname, "r+")){ + read_lines(fin); + fclose(fin); + } + else{ + perror(fname); + } } - main_loop(); } -- cgit v1.2.3