diff --git a/lib/gis/getl.c b/lib/gis/getl.c index 8d0a3ddef71..7daae816a70 100644 --- a/lib/gis/getl.c +++ b/lib/gis/getl.c @@ -11,6 +11,7 @@ * \author Original author CERL */ +#include #include #include @@ -48,55 +49,38 @@ int G_getl(char *buf, int n, FILE *fd) * i.e. \\n (\\012), \\r (\\015), and * \\r\\n (\\015\\012) style newlines. * - * * Reads in at most n-1 characters from stream (the last spot * is reserved for the end-of-string NUL) and stores them into the * buffer pointed to by buf. Reading stops after an EOF or a - * newline. New line is not stored in the buffer. At least n - * must be allocated for the string buffer. + * newline. New line is not stored in the buffer. At least n + * bytes must be allocated for the string buffer. * - * \param buf: string buffer to receive read data, at least n must be - * allocated \param n: maximum number of bytes to read \param fd: file - * descriptor structure + * \param buf: string buffer to receive read data, at least n + * bytes must be allocated + * \param n: maximum number of bytes to read + * \param fd: file descriptor structure * * \return 1 on success * \return 0 EOF */ int G_getl2(char *buf, int n, FILE *fd) { - int i = 0; - int c; - int ret = 1; - - while (i < n - 1) { - c = fgetc(fd); - - if (c == EOF) { - if (i == 0) { /* Read correctly (return 1) last line in file without - '\n' */ - ret = 0; - } - break; - } - - if (c == '\n') - break; /* UNIX */ - - if (c == '\r') { /* DOS or MacOS9 */ - if ((c = fgetc(fd)) != EOF) { - if (c != - '\n') { /* MacOS9 - we have to return the char to stream */ - ungetc(c, fd); - } - } - break; - } + if (buf == NULL || fd == NULL || n <= 1) { + return 0; + } - buf[i] = c; + if (fgets(buf, n, fd) == NULL) { + return 0; /* EOF or error */ + } - i++; + /* Remove newline characters (\n, \r\n, or \r) */ + int len = strlen(buf); + if (len > 0 && buf[len - 1] == '\n') { + buf[--len] = '\0'; + } + if (len > 0 && buf[len - 1] == '\r') { + buf[--len] = '\0'; } - buf[i] = '\0'; - return ret; + return 1; }