File: getline.c

package info (click to toggle)
icmake 6.30-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,360 kB
  • ctags: 1,415
  • sloc: ansic: 7,727; makefile: 1,465; sh: 244; asm: 126; cpp: 39
file content (35 lines) | stat: -rw-r--r-- 1,124 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "builtin.ih"

#define     bufsize     (200)
#define     buflast     (bufsize - 1)
#define     lastch      (bufsize - 2)

char *getLine(FILE *file)
{
    char *dest = xstrdup("");
    char buffer[bufsize];

    while (1)
    {
        buffer[lastch] = 0;                 /* overwritten by \n or the */
                                            /* last char read of a line */
                                            /* otherwise, the end of the*/
                                            /* line is earlier          */

        if (!fgets(buffer, buflast, file))  /* reading fails          */
            break;

        dest = xstrcat(dest, buffer);       /* append the buffer        */
                                            /* EOLN encountered         */
        if (buffer[lastch] == '\n' || !buffer[lastch])
            break;                          /* end of line at then end  */
                                            /* or earlier               */
    }

    if (*dest)                              /* line read                */
        return dest;

    free(dest);
    return NULL;
}