title: using readline() to edit commands, in c
body: <p>i have to implement a few CLI features and now I am trying to use readline() so that the user can edit or go through their commands. Its working so far in that it allows the user to enter their commands and scroll through the history. Its when the user tries to edit the command. The cursor somehow manages to pass the command and go into the prompt eg
  "desktop r1234|5$: ls"  where "desktop r12345(sp)$: " is the prompt showing working directory &amp; root dir; and "|" is the cursor. The cursor should stop in-between '$' and 'ls' ie "desktop r12345$:| ls"  .The showWrkngDir() method just displays a prompt just like a normal terminal.</p>
  
  <pre><code>int main (int argc, char * argv[])
  {
  
      showWrkngDir();
      static char *line_read = (char *)NULL;
      using_history();
      rl_readline_name = basename(argv[0]);
      if (line_read)
      {
              free (line_read);
              line_read = (char *)NULL;
      }
  
      while(strcmp((line_read = readline ("")) , "EXIT") != 0)
      {
              if (line_read &amp;&amp; *line_read)
                      add_history (line_read);
  
              tokenize(line_read);
              showWrkngDir();
      }
  
      return 0;
  }
  
  
  void showWrkngDir()
  {
      char curDir[MAX_COMMAND_SZ];
      char *env;
  
      getcwd(curDir, sizeof(curDir));
      env = (char *)getenv("USER");
  
      printf("%s ",basename(curDir));
  
      printf("%s(sp)$ ", env);
  }
  </code></pre>