ReactOS  0.4.12-dev-57-g7050ac4
qotd.c File Reference
#include "tcpsvcs.h"
#include <stdlib.h>
Include dependency graph for qotd.c:

Go to the source code of this file.

Functions

static BOOL SendQuote (SOCKET sock, char *Quote)
 
static BOOL RetrieveQuote (SOCKET sock)
 
DWORD WINAPI QotdHandler (VOID *sock_)
 

Variables

static WCHAR szFilePath [] = L"\\drivers\\etc\\quotes"
 

Function Documentation

◆ QotdHandler()

DWORD WINAPI QotdHandler ( VOID sock_)

Definition at line 135 of file qotd.c.

136 {
137  SOCKET sock = (SOCKET)sock_;
138  DWORD retVal = 0;
139 
140  if (!RetrieveQuote(sock))
141  {
142  LogEvent(L"QOTD: Error retrieving quote", 0, 0, LOG_FILE);
143  retVal = 1;
144  }
145 
146  LogEvent(L"QOTD: Shutting connection down", 0, 0, LOG_FILE);
147  if (ShutdownConnection(sock, FALSE))
148  {
149  LogEvent(L"QOTD: Connection is down", 0, 0, LOG_FILE);
150  }
151  else
152  {
153  LogEvent(L"QOTD: Connection shutdown failed", 0, 0, LOG_FILE);
154  LogEvent(L"QOTD: Terminating thread", 0, 0, LOG_FILE);
155  retVal = 1;
156  }
157 
158  LogEvent(L"QOTD: Terminating thread", 0, 0, LOG_FILE);
159  ExitThread(retVal);
160 }
static BOOL RetrieveQuote(SOCKET sock)
Definition: qotd.c:27
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
Definition: tcpcore.h:1455
UINT_PTR SOCKET
Definition: winsock.h:47

◆ RetrieveQuote()

static BOOL RetrieveQuote ( SOCKET  sock)
static

Definition at line 27 of file qotd.c.

Referenced by QotdHandler().

28 {
29  HANDLE hFile;
30  WCHAR szFullPath[MAX_PATH + 20];
31  DWORD dwBytesRead;
32  LPSTR lpQuotes;
33  LPSTR lpStr;
34  INT quoteNum;
35  INT NumQuotes = 0;
36  INT i;
37 
38  if(!GetSystemDirectoryW(szFullPath, MAX_PATH))
39  {
40  LogEvent(L"QOTD: Getting system path failed", GetLastError(), 0, LOG_FILE);
41  return FALSE;
42  }
43  wcscat(szFullPath, szFilePath);
44 
45 
46  LogEvent(L"QOTD: Opening quotes file", 0, 0, LOG_FILE);
47  hFile = CreateFileW(szFullPath,
49  0,
50  NULL,
53  NULL);
54  if (hFile == INVALID_HANDLE_VALUE)
55  {
56  LogEvent(L"QOTD: Error opening quotes file", GetLastError(), 0, LOG_FILE);
57  }
58  else
59  {
60  DWORD dwSize = GetFileSize(hFile, NULL);
61  lpQuotes = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dwSize + 1);
62  if (!lpQuotes)
63  {
64  CloseHandle(hFile);
65  return FALSE;
66  }
67 
68  ReadFile(hFile,
69  lpQuotes,
70  dwSize,
71  &dwBytesRead,
72  NULL);
73  CloseHandle(hFile);
74 
75  lpQuotes[dwSize] = 0;
76 
77  if (dwBytesRead != dwSize)
78  {
79  HeapFree(GetProcessHeap(), 0, lpQuotes);
80  return FALSE;
81  }
82 
83  lpStr = lpQuotes;
84  while (*lpStr)
85  {
86  if (*lpStr == '%')
87  NumQuotes++;
88  lpStr++;
89  }
90 
91  /* pick a random quote */
92  srand((unsigned int) GetTickCount());
93  quoteNum = rand() % NumQuotes;
94 
95  /* retrieve the full quote */
96  lpStr = lpQuotes;
97  for (i = 1; i <= quoteNum; i++)
98  {
99  /* move past preceding quote */
100  lpStr++;
101 
102  if (i == quoteNum)
103  {
104  LPSTR lpStart = lpStr;
105 
106  while (*lpStr != '%' && *lpStr != '\0')
107  lpStr++;
108 
109  *lpStr = 0;
110 
111  /* send the quote */
112  if (!SendQuote(sock, lpStart))
113  LogEvent(L"QOTD: Error sending data", 0, 0, LOG_FILE);
114  break;
115  }
116  else
117  {
118  while (*lpStr != '%' && *lpStr != '\0')
119  lpStr++;
120 
121  /* move past % and RN */
122  lpStr += 2;
123  }
124  }
125 
126  HeapFree(GetProcessHeap(), 0, lpQuotes);
127  return TRUE;
128  }
129 
130  return FALSE;
131 }
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
static WCHAR szFilePath[]
Definition: qotd.c:14
__wchar_t WCHAR
Definition: xmlstorage.h:180
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
void __cdecl srand(_In_ unsigned int _Seed)
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
char * LPSTR
Definition: xmlstorage.h:182
int32_t INT
Definition: typedefs.h:56
GLenum GLclampf GLint i
Definition: glfuncs.h:14
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:426
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1087
static BOOL SendQuote(SOCKET sock, char *Quote)
Definition: qotd.c:17
#define GENERIC_READ
Definition: compat.h:124
_In_ HANDLE hFile
Definition: mswsock.h:90
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2312
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
#define CreateFileW
Definition: compat.h:400
Definition: tcpcore.h:1455
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

◆ SendQuote()

static BOOL SendQuote ( SOCKET  sock,
char Quote 
)
static

Definition at line 17 of file qotd.c.

Referenced by RetrieveQuote().

18 {
19  INT strSize = strlen(Quote);
20  if (send(sock, Quote, strSize, 0) == SOCKET_ERROR)
21  return FALSE;
22 
23  return TRUE;
24 }
#define SOCKET_ERROR
Definition: winsock.h:333
#define TRUE
Definition: types.h:120
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int32_t INT
Definition: typedefs.h:56
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
Definition: tcpcore.h:1455

Variable Documentation

◆ szFilePath

WCHAR szFilePath[] = L"\\drivers\\etc\\quotes"
static