static char rcsid[] = "@(#)$Id: mail_gets.c,v 1.13 2006/04/16 21:01:35 hurtta Exp $";
/******************************************************************************
* The Elm (ME+) Mail System - $Revision: 1.13 $ $State: Exp $
*
* Modified by: Kari Hurtta <hurtta+elm@posti.FMI.FI>
* (was hurtta+elm@ozone.FMI.FI)
******************************************************************************
* The Elm Mail System
*
* Copyright (c) 1992 USENET Community Trust
*****************************************************************************/
/** get a line from the mail file, but be tolerant of nulls
The length of the line is returned
**/
#include "headers.h"
int mail_gets(buffer, size, mailfile)
char *buffer;
int size;
FILE *mailfile;
{
int line_bytes = 0, ch;
char *c = buffer;
size--; /* allow room for zero terminator on end, just in case */
/* getc should return EOF on feof or ferror so we
do not test them on loop condition:
!feof(mailfile) && !ferror(mailfile)
*/
while (line_bytes < size) {
ch = getc(mailfile); /* Macro, faster than fgetc() ! */
if (ch == EOF) {
if (line_bytes > 0 && *(c-1) != '\n') {
++line_bytes;
*c++ = '\n';
}
break;
}
*c++ = ch;
++line_bytes;
if (ch == '\n')
break;
}
*c = 0; /* mail_gets is also used on places where zero
* terminated strings are assumed
*/
return line_bytes;
}
/* malloc_gets added by Kari Hurtta
Do not return \n
return -1 if limit exceeded, buffer is still alloced
return -2 if error or if feof() is true before reading anything
*/
int malloc_gets(buffer, limit, mailfile)
char **buffer;
int limit; /* -1 if no limit */
FILE *mailfile;
{
char * buf = *buffer;
int line_bytes = 0, ch;
int alloced = 0;
if (feof(mailfile))
return -2; /* Is called after EOF */
while (!feof(mailfile) && !ferror(mailfile) &&
(line_bytes < limit || limit < 0)) {
int ch = getc(mailfile); /* Macro, faster than fgetc() ! */
if (EOF == ch ||
'\n' == ch)
break;
if (line_bytes+1 > alloced) {
int n = alloced + 100;
if (n > limit && limit > line_bytes)
n = limit;
buf = safe_realloc(buf,n);
alloced = n;
}
buf[line_bytes++] = ch;
}
if (line_bytes > 0 ||
!feof(mailfile) && !ferror(mailfile)) {
buf = safe_realloc(buf,line_bytes+1);
buf[line_bytes] = '\0';
}
*buffer = buf;
if (line_bytes >= limit)
return -1;
if (ferror(mailfile))
return -2;
return line_bytes;
}
/*
* Local Variables:
* mode:c
* c-basic-offset:4
* buffer-file-coding-system: iso-8859-1
* End:
*/
syntax highlighted by Code2HTML, v. 0.9.1