ddnet/src/engine/shared/fifo.cpp

77 lines
1.3 KiB
C++
Raw Normal View History

2016-05-02 21:36:21 +00:00
#include "fifo.h"
2016-05-03 17:17:44 +00:00
#include <base/system.h>
2016-05-02 21:36:21 +00:00
#if defined(CONF_FAMILY_UNIX)
#include <engine/shared/config.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
void CFifo::Init(IConsole *pConsole, char *pFifoFile, int Flag)
{
m_File = NULL;
m_pConsole = pConsole;
if(pFifoFile[0] == '\0')
return;
m_Flag = Flag;
mkfifo(pFifoFile, 0600);
struct stat attribute;
stat(pFifoFile, &attribute);
if(!S_ISFIFO(attribute.st_mode))
{
dbg_msg("fifo", "'%s' is not a fifo, removing", pFifoFile);
fs_remove(pFifoFile);
mkfifo(pFifoFile, 0600);
stat(pFifoFile, &attribute);
if(!S_ISFIFO(attribute.st_mode))
{
dbg_msg("fifo", "can't remove file '%s', quitting", pFifoFile);
exit(2);
}
}
int fileFD = open(pFifoFile, O_RDONLY|O_NONBLOCK);
if(fileFD >= 0)
m_File = fdopen(fileFD, "r");
if(m_File == NULL)
dbg_msg("fifo", "can't open file '%s'", pFifoFile);
}
void CFifo::Shutdown()
{
2016-05-03 17:17:44 +00:00
if(m_File)
fclose(m_File);
2016-05-02 21:36:21 +00:00
}
void CFifo::Update()
{
if(m_File == NULL)
return;
2016-05-05 18:29:31 +00:00
rewind(m_File);
2016-05-02 21:36:21 +00:00
char aBuf[8192];
while(true)
{
char *pResult = fgets(aBuf, sizeof(aBuf), m_File);
if(pResult == NULL)
break;
int Last = str_length(pResult) - 1;
if(Last >= 0 && pResult[Last] == '\n')
pResult[Last] = '\0';
2016-05-02 21:37:40 +00:00
m_pConsole->ExecuteLineFlag(pResult, m_Flag, -1);
2016-05-02 21:36:21 +00:00
}
}
#endif