title: recoding getline function on C
body: <p>I need a function who reproduce a basic of the getline function for another program, this function should be able to read a FP properly and without a huge buffer (256 should be enough here)
  I have 2 problem on the code below
  - the function start to return wrong info when the buffer is exeeded, for this file for example <a href="http://pastebin.com/BXj3SH92" rel="nofollow">http://pastebin.com/BXj3SH92</a> ( ./a.out &lt; file )
  - when i call my function without giving it a file it should work just like a cat but it stop after i press the first enter key ( ./a.out )</p>
  
  <p>here is the code: </p>
  
  <pre><code>#define MOAR_BUF 256
  
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  char            *my_getline(const int fd)
  {
    static char   save[MOAR_BUF];
    static int    i = MOAR_BUF;
    int           g;
    int           f;
    char          *save2;
  
    save2 = NULL;
    g = 0;
    f = 0;
    if (g == 0 &amp;&amp; i &gt;= (MOAR_BUF -1))
      {
        i = 0;
        g = read(fd, save, MOAR_BUF + 1);
        save[g] = '\0';
      }
    if (i &lt;= MOAR_BUF &amp;&amp; save[i] != '\0')
      {
        save2 = malloc((sizeof(*save2) * MOAR_BUF));
        while(save[i] != '\n' &amp;&amp; save[i] != '\0')
          save2[f++] = save[i++];
        save2[f] = '\0';
        if (save[i] != '\0')
          i = i + 1;
      }
    return(save2);
  }
  
  int             main()
  {
    int           i;
    char          **converted_map;
    char          *map;
  
    i = 0;
    converted_map = malloc(sizeof(*converted_map) * 30);
    while(map = my_getline(0))
      {
        converted_map[i] = strdup(map);
        printf("%s\n", converted_map[i]);
        i++;
      }
  }
  </code></pre>