summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <[email protected]>2018-04-21 15:32:16 +0100
committerKatolaZ <[email protected]>2018-04-21 15:32:16 +0100
commit771b998ba5d1f451586aef0562d0012442d6bf23 (patch)
tree2f1c93699edd48a23d70d3655e35f2961e04c41d
parentb10f9522863c937c08d41e2d6da48174cdb3906a (diff)
Fixed w and f
-rw-r--r--TODO.org30
-rw-r--r--buff.c21
-rw-r--r--buff.h4
-rw-r--r--main.c70
4 files changed, 101 insertions, 24 deletions
diff --git a/TODO.org b/TODO.org
index b4c0c48..b0a6e36 100644
--- a/TODO.org
+++ b/TODO.org
@@ -1,22 +1,32 @@
#+TODO: TODO IN-PROGRESS | DONE
-* TODO Commands
+* IN-PROGRESS FIXME
+** DONE check file name input in 'f' and 'w'
+
+
+* IN-PROGRESS Commands [3/20]
** DONE c
-** TODO w
-** TODO W
+** DONE w
** TODO e
+** TODO W
** TODO E
-** TODO f
-** TODO h
-** TODO H
** TODO j
-** TODO Q
-** IN-PROGRESS q -- check for changes
** TODO r
-** TODO s
** TODO t
** TODO m
-** TODO !
+** TODO s
** TODO k
** TODO g,G,v,V
+** IN-PROGRESS q -- check for changes
+** TODO h
+** TODO H
+** TODO Q
+** TODO !
+** TODO r !
+** TODO w !
+** DONE f
+
+* TODO Addresses
+** TODO Offsets (+-)
+** TODO ;
diff --git a/buff.c b/buff.c
index 96cb12f..6462230 100644
--- a/buff.c
+++ b/buff.c
@@ -273,3 +273,24 @@ void change_lines(){
addr2 = addr1 =pos;
append_lines();
}
+
+
+
+int write_lines(){
+ FILE *fout;
+ line_t *l;
+ int tot=0;
+
+ if (! (fout = fopen(fname, "w+"))){
+ return -1;
+ }
+
+ l = b_start;
+ while(l){
+ tot += fprintf(fout, "%s", l->c);
+ l = l->next;
+ }
+ fclose(fout);
+ printf("%d\n", tot);
+ return 0;
+}
diff --git a/buff.h b/buff.h
index 740006d..f97ed78 100644
--- a/buff.h
+++ b/buff.h
@@ -23,6 +23,8 @@ extern line_t *cur;
extern unsigned int num;
extern unsigned int pos;
extern int addr1, addr2;
+extern char *fname;
+
int read_lines(FILE *);
void print_lines(char);
@@ -32,7 +34,7 @@ void print_lineno();
void append_lines();
void insert_lines();
void delete_lines();
-
+int write_lines();
#endif /* __BUFF_H__ */
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 <filein>\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();
}