I'm making a line editor(for a file) & up tp append function.(type in "a")
the append function is to append something after the current line.
im now getting seg fault but cant really find out what cause the problem
really frustrated
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define NUMBERLINES 1000  // max number of lines that can be read
#define LINELENGTH  1000  // max length of a line
#define FNAMELENGTH 100   // max length of a file name
#define CMDLENGTH   102   // max length of a command (e.g.'f' + ' ' + filename)


typedef struct Node {
     char   line[LINELENGTH];
     struct Node *next;
} NodeT;


NodeT* ScanInput(FILE *, char*, NodeT*);
void PrintCommand(void);
void PrintSpace(void);
NodeT *PrintAll(NodeT *);
NodeT *ReadFile(char *);
void Unknown(void);
void PrintCur(NodeT *);
NodeT *makeNode(char *);
NodeT *GotoB(NodeT* );
NodeT *ReadFirst(FILE *);
NodeT *DecP(NodeT *, char *);
NodeT *IncP(NodeT *, char *);  
NodeT *Append(char *,NodeT *);



int main(int argc, char **argv) {
 
   FILE *fp;
   fp = NULL;
   char filename[FNAMELENGTH];
   NodeT *cur = NULL;  

   if(argc > 2){
      printf("Usage: ./led [filename]\n");
      exit(1);
   }

   if(argv != NULL){
      strcpy(filename,argv[1]);
      if((fp = fopen(filename,"r")) != NULL){
      printf("Existing file\n");      
      cur = ReadFirst(fp);
      fclose(fp);
      }
      
      else{
         fp = fopen(filename,"w");
         printf("Creating file %s\n", filename);
      }
   }
   while(1) cur = ScanInput(fp, filename, cur);

   return 0;

}


NodeT* ScanInput(FILE *s, char *argv, NodeT *cur){
   char input[CMDLENGTH];   
   char filename[FNAMELENGTH];
   NodeT *head = NULL;
   NodeT *tmp = NULL;
   char curline[LINELENGTH];
   FILE *fp;
   fp = NULL;
   
   
   strcpy(filename,argv);
   

   printf("? ");   
      
   fgets(input,CMDLENGTH,stdin);   
   if(strcmp(input,"h\n") == 0){
      PrintCommand();
   }
   if(strcmp(input,"q\n") == 0){
      exit(1);
   }
   if(strcmp(input,"p\n") == 0){
      
      head = ReadFile(filename);
      PrintAll(head);
      cur = GotoB(head);
      
   }
   if(strcmp(input,"x\n") == 0){
      printf("Forced exit, changes not saved\n");
      exit(1);
   }
   if(strcmp(input,".\n") == 0){
      PrintCur(cur);
   }
   if(strcmp(input,"-\n") == 0){
      cur = DecP(cur,filename);
      PrintCur(cur);
   }
   if((strcmp(input,"+\n") == 0) || (strcmp(input,"\n") == 0)){
      cur = IncP(cur,filename);
      PrintCur(cur);
   }      

   
   if(strcmp(input,"a\n") == 0){
      head = Append(filename,cur);
           
   }


/*
   if(strcmp(input,"f") == 0){
      if(scanf("%s",filename) != 0) {
         Unknown();
      }
      if((fp1 = fopen(filename,"r")) != NULL){
         fclose(fp1);
         printf("File %s already exist, saving will overwrite", filename);
      }
      else {
         printf("Creating file :%s\n", filename);
         fp1 = fopen(filename,"w");
         
      }
   }*/


   return cur;

}


NodeT *ReadFirst(FILE *fp){
   NodeT *tmp;

   char line[LINELENGTH];
   
   if(fgets(line, LINELENGTH , fp) != NULL){
      tmp = makeNode(line);
   }
   return tmp;
}



NodeT *GotoB(NodeT* head){
   NodeT *tmp;
   tmp = head;
   if(tmp == NULL){
      return NULL;
   }
   while(tmp->next != NULL){
      tmp = tmp -> next;
   }
   
   return tmp;
}

void PrintCur(NodeT *cur){
   //if(cur == NULL) return ;
   
   printf("%s",cur->line);
}




void PrintCommand(){
   
   printf("Commands are (in upper or lower case):\n");
   PrintSpace();
   printf("q:      quit\n");
   PrintSpace();
   printf("s:      save\n");
   PrintSpace();
   printf("x:      force exit\n");
   PrintSpace();
   printf("f <filename>: the file is called <filename>\n");
   PrintSpace();
   printf("h:      print this help message\n");
   PrintSpace();
   printf("d:      delete current line\n");
   PrintSpace();
   printf("a:      append after current line, terminated by '.'\n");
   PrintSpace();
   printf("i:      insert before current line, terminated by '.'\n");
   PrintSpace();
   printf("p:      print all lines\n");
   PrintSpace();
   printf(".:      print current line\n");
   PrintSpace();
   printf("+:      increment line and print\n");   
   PrintSpace();
   printf("<return>:     same as '+'\n");
   PrintSpace();
   printf("-:      decrement line and print\n");
   PrintSpace();
   printf("number: make 'number' the current line\n");

}

void PrintSpace(){
   printf("        ");
}
void Unknown(){
   printf("Unknown command: ignoring\n");
}
        
NodeT *PrintAll (NodeT *s) {
   NodeT *cur;
   int line = 1;
   cur = s;

   if(strcmp(cur->line,"\n") == 0) {
      printf("Empty File\n");
      return NULL;
   }

   while(cur != NULL) {
    
      printf("LINE %d %s", line, cur->line);
      cur = cur->next;
      line++;
   }
   return cur;

}

NodeT *ReadFile(char *filename){
   int i = 0;   
   char line [LINELENGTH ];    
   char *get;
   FILE *fp;
   fp = NULL;
   
   NodeT *head;
   NodeT *cur;
   NodeT *new;
   
   if((fp = fopen(filename,"r")) == NULL){
      printf("Empty File\n");   
      printf("%s\n",filename);
      return NULL;
   }
   
   if((get = fgets(line, LINELENGTH , fp)) != NULL) {
      head = makeNode(line);
      cur = head;
      
   }

    

   while (fgets(line, LINELENGTH , fp) != NULL) {
      new = makeNode(line);
      cur->next = new;
      cur = new;
      i++;
   }
 

   fclose(fp);
   return head;
}


NodeT *Append(char *filename, NodeT *cur){
   FILE *fp;
   NodeT *head;
   NodeT *tmp;
   NodeT *inputhead;
   NodeT *inputcur;
   NodeT *inputnew;
   NodeT *tmp1;
   
   char line [LINELENGTH];
   char c;

   int i = 1,linenum = 1;

   head = ReadFile(filename);
   tmp = head;
   fgets(line,LINELENGTH, stdin);
   while(1){
      fgets(line,LINELENGTH, stdin);
      sscanf(line,"%c",&c);
      if(c == '.') break;
      if(i == 1){
         inputhead = makeNode(line);
         inputcur = inputhead;
         
      }
      else{
         inputnew = makeNode(line);
         inputcur->next = inputnew;
      }
      i++;
   }

   while(strcmp(tmp->line,cur->line) != 0 ){
      tmp = tmp->next;
   }

   cur = tmp->next;

   tmp->next = inputhead;
   inputcur->next = cur;

   fp = fopen(filename,"w+");

   tmp1 = head;
   while(tmp1 != NULL){
      fprintf(fp,"%s",tmp1->line);
      tmp1 = tmp1->next;
      linenum++;
   }
   
   return head;

}    



NodeT *makeNode(char *line) {
   NodeT *new;
   new = malloc(sizeof(NodeT));
   if (new == NULL) {
     
     exit(1); 
   }
   strcpy(new->line,line);
   new->next = NULL;
   return new;
}

NodeT *DecP(NodeT *cur, char *filename){
   NodeT *tmp;
   
      

   if((tmp = ReadFile(filename)) == NULL){
      printf("Empty File\n");
      return NULL;
   }

   while(strcmp(tmp->next->line,cur->line) != 0){
      tmp = tmp->next;
   }

   return tmp;
   
}

NodeT *IncP(NodeT *cur, char *filename){
   NodeT *tmp;
      

   if((tmp = ReadFile(filename)) == NULL){
      printf("Empty File\n");
      return NULL;
   }

   while(strcmp(tmp->line,cur->line) != 0){
      tmp = tmp->next;
   }

   if(tmp->next == NULL) return tmp;
   else return tmp->next;
}