2001-08-17 05:46:24 +00:00
|
|
|
|
/*****************************************************************************
|
|
|
|
|
*
|
2002-04-28 14:27:23 +00:00
|
|
|
|
* $Id$
|
2001-08-17 05:46:24 +00:00
|
|
|
|
* Purpose ...............: Global message base functions
|
|
|
|
|
*
|
|
|
|
|
*****************************************************************************
|
2005-08-19 20:48:54 +00:00
|
|
|
|
* Copyright (C) 1997-2005
|
2001-08-17 05:46:24 +00:00
|
|
|
|
*
|
|
|
|
|
* Michiel Broek FIDO: 2:280/2802
|
|
|
|
|
* Beekmansbos 10
|
|
|
|
|
* 1971 BV IJmuiden
|
|
|
|
|
* the Netherlands
|
|
|
|
|
*
|
|
|
|
|
* This file is part of MBSE BBS.
|
|
|
|
|
*
|
|
|
|
|
* This BBS is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
|
* later version.
|
|
|
|
|
*
|
|
|
|
|
* MBSE BBS is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with MBSE BBS; see the file COPYING. If not, write to the Free
|
|
|
|
|
* Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2002-06-30 12:48:44 +00:00
|
|
|
|
#include "../config.h"
|
2004-02-21 14:24:03 +00:00
|
|
|
|
#include "mbselib.h"
|
2001-08-17 05:46:24 +00:00
|
|
|
|
#include "msgtext.h"
|
|
|
|
|
#include "msg.h"
|
|
|
|
|
#include "jammsg.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *strlwr (char *s)
|
|
|
|
|
{
|
|
|
|
|
char *p = s;
|
|
|
|
|
|
|
|
|
|
while (*p != '\0') {
|
|
|
|
|
*p = (char)tolower (*p);
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *strupr (char *s)
|
|
|
|
|
{
|
|
|
|
|
char *p = s;
|
|
|
|
|
|
|
|
|
|
while (*p != '\0') {
|
|
|
|
|
*p = (char)toupper (*p);
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int filelength(int fd)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int retval = -1L;
|
|
|
|
|
struct stat buf;
|
2001-08-17 05:46:24 +00:00
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
if (fd != -1) {
|
|
|
|
|
fstat(fd, &buf);
|
|
|
|
|
retval = buf.st_size;
|
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
return (retval);
|
2001-08-17 05:46:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int tell(int fd)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int retval = -1L;
|
2001-08-17 05:46:24 +00:00
|
|
|
|
|
|
|
|
|
if (fd != -1)
|
|
|
|
|
retval = lseek(fd, 0L, SEEK_CUR);
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Add a message
|
|
|
|
|
*/
|
|
|
|
|
int Msg_AddMsg()
|
|
|
|
|
{
|
|
|
|
|
if (!MsgBase.Locked)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return JAM_AddMsg();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Close current message base
|
|
|
|
|
*/
|
|
|
|
|
void Msg_Close(void)
|
|
|
|
|
{
|
|
|
|
|
if (MsgBase.Locked)
|
|
|
|
|
Msg_UnLock();
|
|
|
|
|
|
|
|
|
|
JAM_Close();
|
|
|
|
|
MsgText_Clear();
|
|
|
|
|
MsgBase.Open = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Delete message number
|
|
|
|
|
*/
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int Msg_Delete(unsigned int ulMsg)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
if (!MsgBase.Locked)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return JAM_Delete(ulMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-04-28 14:27:23 +00:00
|
|
|
|
/*
|
|
|
|
|
* Delete message base
|
|
|
|
|
*/
|
|
|
|
|
void Msg_DeleteMsgBase(char *Base)
|
|
|
|
|
{
|
|
|
|
|
JAM_DeleteJAM(Base);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-17 05:46:24 +00:00
|
|
|
|
int Msg_GetLastRead(lastread *LR)
|
|
|
|
|
{
|
|
|
|
|
return JAM_GetLastRead(LR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get highest message number
|
|
|
|
|
*/
|
2005-10-11 20:49:41 +00:00
|
|
|
|
unsigned int Msg_Highest(void)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return MsgBase.Highest = JAM_Highest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int Msg_Lock(unsigned int ulTimeout)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return MsgBase.Locked = JAM_Lock(ulTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get lowest message number
|
|
|
|
|
*/
|
2005-10-11 20:49:41 +00:00
|
|
|
|
unsigned int Msg_Lowest(void)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return MsgBase.Lowest = JAM_Lowest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Msg_New(void)
|
|
|
|
|
{
|
|
|
|
|
JAM_New();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Msg_NewLastRead(lastread LR)
|
|
|
|
|
{
|
|
|
|
|
return JAM_NewLastRead(LR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int Msg_Next(unsigned int * ulMsg)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return JAM_Next(ulMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return number of messages
|
|
|
|
|
*/
|
2005-10-11 20:49:41 +00:00
|
|
|
|
unsigned int Msg_Number(void)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return MsgBase.Total = JAM_Number();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Open specified message base
|
|
|
|
|
*/
|
|
|
|
|
int Msg_Open(char *Base)
|
|
|
|
|
{
|
|
|
|
|
int RetVal = FALSE;
|
|
|
|
|
|
|
|
|
|
if (MsgBase.Open) {
|
|
|
|
|
if (strcmp(MsgBase.Path, Base) != 0)
|
|
|
|
|
Msg_Close();
|
|
|
|
|
else
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RetVal = JAM_Open(Base);
|
|
|
|
|
|
|
|
|
|
MsgBase.Open = RetVal;
|
|
|
|
|
|
|
|
|
|
strcpy(MsgBase.Path, Base);
|
|
|
|
|
return RetVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Pack deleted messages from the message base.
|
|
|
|
|
*/
|
|
|
|
|
void Msg_Pack(void)
|
|
|
|
|
{
|
|
|
|
|
if (!MsgBase.Locked)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
JAM_Pack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int Msg_Previous (unsigned int * ulMsg)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return JAM_Previous(ulMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int Msg_ReadHeader (unsigned int ulMsg)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return JAM_ReadHeader(ulMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Read message
|
|
|
|
|
*/
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int Msg_Read(unsigned int ulMsg, int nWidth)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return JAM_Read(ulMsg, nWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Msg_SetLastRead(lastread LR)
|
|
|
|
|
{
|
|
|
|
|
if (!MsgBase.Locked)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return JAM_SetLastRead(LR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Unlock the message base
|
|
|
|
|
*/
|
|
|
|
|
void Msg_UnLock(void)
|
|
|
|
|
{
|
|
|
|
|
JAM_UnLock();
|
|
|
|
|
MsgBase.Locked = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Write message header
|
|
|
|
|
*/
|
2005-10-11 20:49:41 +00:00
|
|
|
|
int Msg_WriteHeader (unsigned int ulMsg)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
if (!MsgBase.Locked)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return JAM_WriteHeader(ulMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Write messagetext from file, strip linefeeds.
|
|
|
|
|
*/
|
|
|
|
|
void Msg_Write(FILE *fp)
|
|
|
|
|
{
|
|
|
|
|
char *Buf;
|
|
|
|
|
|
2003-09-02 20:25:42 +00:00
|
|
|
|
Buf = calloc(MAX_LINE_LENGTH +1, sizeof(char));
|
2004-04-13 19:29:54 +00:00
|
|
|
|
while ((Fgets(Buf, MAX_LINE_LENGTH, fp)) != NULL)
|
2001-08-17 05:46:24 +00:00
|
|
|
|
MsgText_Add2(Buf);
|
|
|
|
|
|
|
|
|
|
free(Buf);
|
|
|
|
|
}
|
2004-04-13 19:29:54 +00:00
|
|
|
|
|
2001-08-17 05:46:24 +00:00
|
|
|
|
|
2003-09-14 12:24:18 +00:00
|
|
|
|
typedef struct {
|
2005-10-11 20:49:41 +00:00
|
|
|
|
unsigned int Subject;
|
|
|
|
|
unsigned int Number;
|
2003-09-14 12:24:18 +00:00
|
|
|
|
} MSGLINK;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-10-27 11:08:09 +00:00
|
|
|
|
/*
|
|
|
|
|
* Changes ansi background and foreground color
|
|
|
|
|
*/
|
|
|
|
|
void msg_colour(int, int);
|
|
|
|
|
void msg_colour(int fg, int bg)
|
|
|
|
|
{
|
|
|
|
|
int att=0, fore=37, back=40;
|
|
|
|
|
|
|
|
|
|
if (fg<0 || fg>31 || bg<0 || bg>7) {
|
|
|
|
|
fprintf(stdout, "ANSI: Illegal colour specified: %i, %i\n", fg, bg);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-08-20 15:39:04 +00:00
|
|
|
|
fprintf(stdout, "[");
|
2004-10-27 11:08:09 +00:00
|
|
|
|
|
|
|
|
|
if ( fg > WHITE) {
|
|
|
|
|
fprintf(stdout, "5;");
|
|
|
|
|
fg-= 16;
|
|
|
|
|
}
|
|
|
|
|
if (fg > LIGHTGRAY) {
|
|
|
|
|
att=1;
|
|
|
|
|
fg=fg-8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fg == BLACK) fore=30;
|
|
|
|
|
else if (fg == BLUE) fore=34;
|
|
|
|
|
else if (fg == GREEN) fore=32;
|
|
|
|
|
else if (fg == CYAN) fore=36;
|
|
|
|
|
else if (fg == RED) fore=31;
|
|
|
|
|
else if (fg == MAGENTA) fore=35;
|
|
|
|
|
else if (fg == BROWN) fore=33;
|
|
|
|
|
else fore=37;
|
|
|
|
|
|
|
|
|
|
if (bg == BLUE) back=44;
|
|
|
|
|
else if (bg == GREEN) back=42;
|
|
|
|
|
else if (bg == CYAN) back=46;
|
|
|
|
|
else if (bg == RED) back=41;
|
|
|
|
|
else if (bg == MAGENTA) back=45;
|
|
|
|
|
else if (bg == BROWN) back=43;
|
|
|
|
|
else if (bg == LIGHTGRAY) back=47;
|
|
|
|
|
else back=40;
|
|
|
|
|
|
|
|
|
|
fprintf(stdout, "%d;%d;%dm", att, fore, back);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-09-14 12:24:18 +00:00
|
|
|
|
/*
|
|
|
|
|
* Link messages in one area.
|
|
|
|
|
* Returns -1 if error, else the number of linked messages.
|
|
|
|
|
*/
|
|
|
|
|
int Msg_Link(char *Path, int do_quiet, int slow_util)
|
|
|
|
|
{
|
|
|
|
|
int i, m, msg_link = 0;
|
2005-10-11 20:49:41 +00:00
|
|
|
|
unsigned int Number, Prev, Next, Crc, Total;
|
2003-09-14 12:24:18 +00:00
|
|
|
|
char Temp[128], *p;
|
|
|
|
|
MSGLINK *Link;
|
|
|
|
|
|
|
|
|
|
if (! Msg_Open(Path)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
2005-08-19 20:48:54 +00:00
|
|
|
|
msg_colour(LIGHTRED, BLACK);
|
2003-09-14 12:24:18 +00:00
|
|
|
|
printf(" (linking)");
|
2005-08-19 20:48:54 +00:00
|
|
|
|
msg_colour(LIGHTMAGENTA, BLACK);
|
2003-09-14 12:24:18 +00:00
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((Total = Msg_Number()) != 0L) {
|
|
|
|
|
if (Msg_Lock(30L)) {
|
2003-12-08 18:01:25 +00:00
|
|
|
|
if ((Link = (MSGLINK *)malloc((Total + 1) * sizeof(MSGLINK))) != NULL) {
|
|
|
|
|
memset(Link, 0, (Total + 1) * sizeof(MSGLINK));
|
2003-09-14 12:24:18 +00:00
|
|
|
|
Number = Msg_Lowest();
|
|
|
|
|
i = 0;
|
|
|
|
|
do {
|
|
|
|
|
Msg_ReadHeader(Number);
|
|
|
|
|
strcpy(Temp, Msg.Subject);
|
|
|
|
|
p = strupr(Temp);
|
|
|
|
|
if (!strncmp(p, "RE:", 3)) {
|
|
|
|
|
p += 3;
|
|
|
|
|
if (*p == ' ')
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
Link[i].Subject = StringCRC32(p);
|
|
|
|
|
Link[i].Number = Number;
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
if (((i % 10) == 0) && (!do_quiet)) {
|
2005-10-11 20:49:41 +00:00
|
|
|
|
printf("%6d / %6u\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i, Total);
|
2003-09-14 12:24:18 +00:00
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
} while(Msg_Next(&Number) == TRUE);
|
|
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
2005-10-11 20:49:41 +00:00
|
|
|
|
printf("%6d / %6u\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i, Total);
|
2003-09-14 12:24:18 +00:00
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
Number = Msg_Lowest();
|
|
|
|
|
i = 0;
|
|
|
|
|
do {
|
|
|
|
|
Msg_ReadHeader(Number);
|
|
|
|
|
Prev = Next = 0;
|
|
|
|
|
Crc = Link[i].Subject;
|
|
|
|
|
|
|
|
|
|
for (m = 0; m < Total; m++) {
|
|
|
|
|
if (m == i)
|
|
|
|
|
continue;
|
|
|
|
|
if (Link[m].Subject == Crc) {
|
|
|
|
|
if (m < i)
|
|
|
|
|
Prev = Link[m].Number;
|
|
|
|
|
else if (m > i) {
|
|
|
|
|
Next = Link[m].Number;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (((i % 10) == 0) && (!do_quiet)) {
|
2005-10-11 20:49:41 +00:00
|
|
|
|
printf("%6d / %6u\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i, Total);
|
2003-09-14 12:24:18 +00:00
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Msg.Original != Prev || Msg.Reply != Next) {
|
|
|
|
|
Msg.Original = Prev;
|
|
|
|
|
Msg.Reply = Next;
|
|
|
|
|
Msg_WriteHeader(Number);
|
|
|
|
|
msg_link++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
} while(Msg_Next(&Number) == TRUE);
|
|
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
2005-10-11 20:49:41 +00:00
|
|
|
|
printf("%6d / %6u\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i, Total);
|
2003-09-14 12:24:18 +00:00
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(Link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
|
|
|
|
printf(" \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
Msg_UnLock();
|
|
|
|
|
} else {
|
|
|
|
|
Syslog('+', "Can't lock %s", Path);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Msg_Close();
|
|
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
|
|
|
|
printf("\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
return msg_link;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-27 11:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
2004-04-13 19:29:54 +00:00
|
|
|
|
/*
|
|
|
|
|
* Fgets() is like fgets() but never returns the line terminator
|
|
|
|
|
* at end of line and handles that line terminators:
|
|
|
|
|
*
|
|
|
|
|
* DOS/WINDOWS -> CR/LF
|
|
|
|
|
* UNIX -> LF only
|
|
|
|
|
* MAC -> CR only
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
char *Fgets(char *l, int size, FILE *f) {
|
|
|
|
|
char *cp = l;
|
|
|
|
|
int cr, eol = FALSE;
|
2003-09-14 12:24:18 +00:00
|
|
|
|
|
2004-04-13 19:29:54 +00:00
|
|
|
|
if (feof(f)) return NULL;
|
|
|
|
|
|
|
|
|
|
cr = FALSE;
|
|
|
|
|
while (size>1 && !feof(f)) {
|
|
|
|
|
int c = fgetc(f);
|
|
|
|
|
if (c == EOF) {
|
|
|
|
|
if (ferror(f)) return NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (cr && c != '\n') {
|
|
|
|
|
/* CR end-of-line (MAC) */
|
|
|
|
|
ungetc(c,f);
|
|
|
|
|
eol = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
} else
|
|
|
|
|
cr = (c=='\r');
|
|
|
|
|
if ( cr )
|
|
|
|
|
continue;
|
|
|
|
|
--size;
|
|
|
|
|
if (c=='\n') { eol = TRUE; break; }
|
|
|
|
|
*(cp++) = c;
|
|
|
|
|
}
|
|
|
|
|
*cp = '\0';
|
|
|
|
|
|
|
|
|
|
cr = FALSE;
|
|
|
|
|
while (!eol && !feof(f)) {
|
|
|
|
|
int c = fgetc(f);
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
break;
|
|
|
|
|
if (cr && c != '\n') {
|
|
|
|
|
/* CR end-of-line (MAC) */
|
|
|
|
|
ungetc(c,f);
|
|
|
|
|
break;
|
|
|
|
|
} else
|
|
|
|
|
cr = (c=='\r');
|
|
|
|
|
if ( cr )
|
|
|
|
|
continue;
|
|
|
|
|
if (c=='\n') break;
|
|
|
|
|
}
|
|
|
|
|
return l;
|
|
|
|
|
}
|