Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

939 lines
27 KiB

  1. #include "convlog.h"
  2. #include <logconst.h>
  3. #define MAX_MONTH_SIZE 16
  4. char szJan[MAX_MONTH_SIZE];
  5. char szFeb[MAX_MONTH_SIZE];
  6. char szMar[MAX_MONTH_SIZE];
  7. char szApr[MAX_MONTH_SIZE];
  8. char szMay[MAX_MONTH_SIZE];
  9. char szJun[MAX_MONTH_SIZE];
  10. char szJul[MAX_MONTH_SIZE];
  11. char szAug[MAX_MONTH_SIZE];
  12. char szSep[MAX_MONTH_SIZE];
  13. char szOct[MAX_MONTH_SIZE];
  14. char szNov[MAX_MONTH_SIZE];
  15. char szDec[MAX_MONTH_SIZE];
  16. //
  17. // extended logging
  18. //
  19. DWORD dwHostNamePos = 0;
  20. DWORD dwUserNamePos = 0;
  21. DWORD dwDatePos = 0;
  22. DWORD dwTimePos = 0;
  23. DWORD dwMethodPos = 0;
  24. DWORD dwURIStemPos = 0;
  25. DWORD dwURIQueryPos = 0;
  26. DWORD dwHTTPStatusPos = 0;
  27. DWORD dwBytesSentPos = 0;
  28. DWORD dwBytesRecvPos = 0;
  29. DWORD dwServicePos = 0;
  30. DWORD dwVersionPos = 0;
  31. CHAR szGlobalDate[32] = {0};
  32. CHAR szGlobalTime[32] = {0};
  33. BOOL ExtendedFieldsDefined = FALSE;
  34. BOOL
  35. InitDateStrings(
  36. VOID
  37. )
  38. {
  39. HINSTANCE hInst = GetModuleHandle(NULL);
  40. if ( hInst == NULL ) {
  41. return(FALSE);
  42. }
  43. LoadString(hInst, IDS_JAN, szJan, sizeof(szJan));
  44. LoadString(hInst, IDS_FEB, szFeb, sizeof(szFeb));
  45. LoadString(hInst, IDS_MAR, szMar, sizeof(szMar));
  46. LoadString(hInst, IDS_APR, szApr, sizeof(szApr));
  47. LoadString(hInst, IDS_MAY, szMay, sizeof(szMay));
  48. LoadString(hInst, IDS_JUN, szJun, sizeof(szJun));
  49. LoadString(hInst, IDS_JUL, szJul, sizeof(szJul));
  50. LoadString(hInst, IDS_AUG, szAug, sizeof(szAug));
  51. LoadString(hInst, IDS_SEP, szSep, sizeof(szSep));
  52. LoadString(hInst, IDS_OCT, szOct, sizeof(szOct));
  53. LoadString(hInst, IDS_NOV, szNov, sizeof(szNov));
  54. LoadString(hInst, IDS_DEC, szDec, sizeof(szDec));
  55. return(TRUE);
  56. }
  57. PCHAR
  58. FindChar(
  59. IN PCHAR cp,
  60. IN CHAR cTarget
  61. )
  62. /*++
  63. This procedure increments a character pointer until it finds a comma or the
  64. NULL character. if it finds a comma, it replaces it with a NULL and increments
  65. the pointer. if it finds a NULL, it merely returns without changing the character.
  66. --*/
  67. {
  68. while ((*cp != cTarget) && (*cp != '\0'))
  69. cp++;
  70. if (*cp == cTarget)
  71. {
  72. *cp = '\0';
  73. cp++;
  74. cp = SkipWhite(cp);
  75. }
  76. return (cp);
  77. }
  78. PCHAR
  79. FindMSINETLogDelimChar( IN PCHAR cp )
  80. /*++
  81. This procedure increments a character pointer until it finds a comma+space or the
  82. NULL character. if it finds a comma+space, it replaces the comma with a NULL and increments
  83. the pointer past the space. if it finds a NULL, it merely returns without changing
  84. the character.
  85. --*/
  86. {
  87. while ( !(*cp == ',' && ISWHITE ( *(cp+1) )) && (*cp != '\0') && (*cp != '\r') && (*cp != '\n'))
  88. {
  89. cp++;
  90. }
  91. if (*cp == ',')
  92. {
  93. *cp = '\0';
  94. cp++;
  95. cp = SkipWhite(cp);
  96. }
  97. else
  98. if ((*cp=='\r') || (*cp=='\n'))\
  99. {
  100. *cp = '\0';
  101. }
  102. return (cp);
  103. }
  104. char * SkipWhite (char *cp)
  105. {
  106. while (ISWHITE (*cp))
  107. {
  108. cp++;
  109. }
  110. return (cp);
  111. }
  112. #if 0
  113. PCHAR
  114. ConvertDate(
  115. IN LPTSTR pszDate
  116. )
  117. /*++
  118. Convert the date from "15/May/1995" to "5/15/95" format
  119. --*/
  120. {
  121. static char pszRetDate[100];
  122. char *cpCurrent = pszDate;
  123. int nMonth=1;
  124. int nDay=1;
  125. int nYear=90;
  126. nDay = atoi( cpCurrent );
  127. cpCurrent=FindChar(cpCurrent,'/');
  128. if ( strncmp(cpCurrent,szJan,3) == 0 )
  129. {
  130. nMonth = 1;
  131. } else if ( strncmp(cpCurrent,szFeb,3) == 0 )
  132. {
  133. nMonth = 2;
  134. } else if ( strncmp(cpCurrent,szMar,3) == 0 )
  135. {
  136. nMonth = 3;
  137. } else if ( strncmp(cpCurrent,szApr,3) == 0 )
  138. {
  139. nMonth = 4;
  140. } else if ( strncmp(cpCurrent,szMay,3) == 0 )
  141. {
  142. nMonth = 5;
  143. } else if ( strncmp(cpCurrent,szJun,3) == 0 )
  144. {
  145. nMonth = 6;
  146. } else if ( strncmp(cpCurrent,szJul,3) == 0 )
  147. {
  148. nMonth = 7;
  149. } else if ( strncmp(cpCurrent,szAug,3) == 0 )
  150. {
  151. nMonth = 8;
  152. } else if ( strncmp(cpCurrent,szSep,3) == 0 )
  153. {
  154. nMonth = 9;
  155. } else if ( strncmp(cpCurrent,szOct,3) == 0 )
  156. {
  157. nMonth = 10;
  158. } else if ( strncmp(cpCurrent,szNov,3) == 0 )
  159. {
  160. nMonth = 11;
  161. } else if ( strncmp(cpCurrent,szDec,3) == 0 )
  162. {
  163. nMonth = 12;
  164. }
  165. cpCurrent=FindChar(cpCurrent,'/');
  166. nYear = atoi( cpCurrent )%100;
  167. sprintf(pszRetDate,"%d/%d/%d",nMonth,nDay,nYear);
  168. return pszRetDate;
  169. }
  170. #endif
  171. /* #pragma INTRINSA suppress=all */
  172. DWORD
  173. GetLogLine (
  174. IN FILE *fpInFile,
  175. IN PCHAR szBuf,
  176. IN DWORD cbBuf,
  177. IN LPINLOGLINE lpLogLine
  178. )
  179. {
  180. BOOL bRetCode = GETLOG_ERROR;
  181. CHAR *cpCurrent;
  182. CHAR buf[8*1024];
  183. static char szNULL[]="";
  184. static char szEmpty[]="-";
  185. static char szUnknownIP[] = "<UnknownIP>";
  186. static char szW3Svc[] = "W3Svc";
  187. static char szDefaultHTTPVersion[]="HTTP/1.0";
  188. lpLogLine->szClientIP = szNULL;
  189. lpLogLine->szUserName = szNULL;
  190. lpLogLine->szDate = szNULL;
  191. lpLogLine->szTime = szNULL;
  192. lpLogLine->szService = szNULL;
  193. lpLogLine->szServerName = szNULL;
  194. lpLogLine->szServerIP = szNULL;
  195. lpLogLine->szProcTime = szNULL;
  196. lpLogLine->szBytesRec = szNULL;
  197. lpLogLine->szBytesSent = szNULL;
  198. lpLogLine->szServiceStatus = szNULL;
  199. lpLogLine->szWin32Status = szNULL;
  200. lpLogLine->szOperation = szNULL;
  201. lpLogLine->szTargetURL = szNULL;
  202. lpLogLine->szUserAgent = szNULL;
  203. lpLogLine->szReferer = szNULL;
  204. lpLogLine->szParameters = szNULL;
  205. lpLogLine->szVersion = szDefaultHTTPVersion;
  206. if (NULL != fgets(szBuf, cbBuf, fpInFile)) {
  207. szBuf = SkipWhite(szBuf);
  208. if ((szBuf[0] != '\n') && ( szBuf[0] != '\0')) //is this an empty line?
  209. {
  210. bRetCode = GETLOG_SUCCESS;
  211. //
  212. // set current char pointer to start of string
  213. //
  214. cpCurrent = szBuf;
  215. if ( LogFileFormat == LOGFILE_NCSA ) {
  216. lpLogLine->szClientIP = szBuf;
  217. cpCurrent = FindChar(cpCurrent, ' ');
  218. lpLogLine->szClientIP = GetMachineName(lpLogLine->szClientIP);
  219. sprintf( buf,"%s %s",lpLogLine->szClientIP,cpCurrent);
  220. strcpy( szBuf, buf);
  221. //
  222. // After the strcpy the pointers cpCurrent and lpLogLine->szClientIP have
  223. // the potential to be miss alligned if the dns name is shorter or longer than the IP
  224. // address that it replaced. Simple fix reset the pointers to the beginning of the
  225. // string.
  226. //
  227. lpLogLine->szClientIP = szBuf;
  228. cpCurrent = szBuf;
  229. while ((*cpCurrent != '\0') && (*cpCurrent != '[') ) {
  230. cpCurrent++;
  231. }
  232. if ( *cpCurrent == '\0' ) {
  233. return(GETLOG_ERROR_PARSE_NCSA);
  234. }
  235. } else if (LogFileFormat == LOGFILE_MSINET ) {
  236. lpLogLine->szClientIP = szBuf;
  237. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  238. if (DoDNSConversion) {
  239. lpLogLine->szClientIP = GetMachineName(
  240. lpLogLine->szClientIP
  241. );
  242. if ( NoFormatConversion ) {
  243. sprintf( buf,"%s, %s",lpLogLine->szClientIP,cpCurrent);
  244. strcpy( szBuf, buf);
  245. return(GETLOG_SUCCESS);
  246. }
  247. }
  248. lpLogLine->szUserName = cpCurrent;
  249. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  250. lpLogLine->szDate = cpCurrent;
  251. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  252. lpLogLine->szTime = cpCurrent;
  253. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  254. lpLogLine->szService = cpCurrent;
  255. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  256. lpLogLine->szServerName = cpCurrent;
  257. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  258. lpLogLine->szServerIP = cpCurrent;
  259. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  260. lpLogLine->szProcTime = cpCurrent;
  261. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  262. lpLogLine->szBytesRec = cpCurrent;
  263. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  264. lpLogLine->szBytesSent = cpCurrent;
  265. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  266. lpLogLine->szServiceStatus = cpCurrent;
  267. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  268. lpLogLine->szWin32Status = cpCurrent;
  269. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  270. lpLogLine->szOperation = cpCurrent;
  271. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  272. lpLogLine->szTargetURL = cpCurrent;
  273. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  274. lpLogLine->szParameters = cpCurrent;
  275. cpCurrent = FindMSINETLogDelimChar (cpCurrent);
  276. if (lpLogLine->szClientIP[0] != '\0' &&
  277. lpLogLine->szUserName[0] != '\0' &&
  278. lpLogLine->szDate[0] != '\0' &&
  279. lpLogLine->szTime[0] != '\0' &&
  280. lpLogLine->szService[0] != '\0' &&
  281. lpLogLine->szServerName[0] != '\0' &&
  282. lpLogLine->szServerIP[0] != '\0' &&
  283. lpLogLine->szProcTime[0] != '\0' &&
  284. lpLogLine->szBytesRec[0] != '\0' &&
  285. lpLogLine->szBytesSent[0] != '\0' &&
  286. lpLogLine->szServiceStatus[0] != '\0' &&
  287. lpLogLine->szWin32Status[0] != '\0' &&
  288. lpLogLine->szOperation[0] != '\0' &&
  289. lpLogLine->szTargetURL[0] != '\0' &&
  290. lpLogLine->szParameters[0] != '\0'
  291. ) {
  292. bRetCode = GETLOG_SUCCESS;
  293. } else {
  294. return(GETLOG_ERROR_PARSE_MSINET);
  295. }
  296. } else if ( LogFileFormat == LOGFILE_CUSTOM ) {
  297. //
  298. // W3C Extended Logging
  299. //
  300. if ( szBuf[0] == '#' ) {
  301. PCHAR pszFields;
  302. cpCurrent = FindChar(cpCurrent, '#');
  303. bRetCode = GETLOG_SUCCESS;
  304. if ( strncmp(cpCurrent, "Fields:", 7) == 0 ) {
  305. DWORD pos;
  306. //
  307. // init positions
  308. //
  309. ExtendedFieldsDefined = TRUE;
  310. dwHostNamePos = 0;
  311. dwUserNamePos = 0;
  312. dwDatePos = 0;
  313. dwTimePos = 0;
  314. dwMethodPos = 0;
  315. dwURIStemPos = 0;
  316. dwURIQueryPos = 0;
  317. dwHTTPStatusPos = 0;
  318. dwBytesSentPos = 0;
  319. dwBytesRecvPos = 0;
  320. dwServicePos = 0;
  321. dwVersionPos = 0;
  322. cpCurrent = FindChar(cpCurrent, ':');
  323. (VOID)FindChar( cpCurrent, '\n' );
  324. for (pos = 1; *cpCurrent != '\0'; pos++) {
  325. PCHAR pszField = cpCurrent;
  326. cpCurrent=FindChar(cpCurrent,' ');
  327. if ( _stricmp( pszField, EXTLOG_CLIENT_IP_ID ) == 0 ) {
  328. dwHostNamePos = pos;
  329. } else if ( _stricmp( pszField, EXTLOG_USERNAME_ID ) == 0 ) {
  330. dwUserNamePos = pos;
  331. } else if ( _stricmp( pszField, EXTLOG_DATE_ID ) == 0 ) {
  332. dwDatePos = pos;
  333. } else if ( _stricmp( pszField, EXTLOG_TIME_ID ) == 0 ) {
  334. dwTimePos = pos;
  335. } else if ( _stricmp( pszField, EXTLOG_METHOD_ID ) == 0 ) {
  336. dwMethodPos = pos;
  337. } else if ( _stricmp( pszField, EXTLOG_URI_STEM_ID ) == 0 ) {
  338. dwURIStemPos = pos;
  339. } else if ( _stricmp( pszField, EXTLOG_URI_QUERY_ID ) == 0 ) {
  340. dwURIQueryPos = pos;
  341. } else if ( _stricmp( pszField, EXTLOG_HTTP_STATUS_ID ) == 0 ) {
  342. dwHTTPStatusPos = pos;
  343. } else if ( _stricmp( pszField, EXTLOG_BYTES_SENT_ID ) == 0 ) {
  344. dwBytesSentPos = pos;
  345. } else if ( _stricmp( pszField, EXTLOG_BYTES_RECV_ID ) == 0 ) {
  346. dwBytesRecvPos = pos;
  347. } else if ( _stricmp( pszField, EXTLOG_SITE_NAME_ID ) == 0 ) {
  348. dwServicePos = pos;
  349. } else if ( _stricmp( pszField, EXTLOG_PROTOCOL_VERSION_ID ) == 0 ) {
  350. dwVersionPos = pos;
  351. }
  352. }
  353. }
  354. if ( strncmp(cpCurrent, "Date:", 5) == 0 ) {
  355. //
  356. // Grab the global date
  357. //
  358. cpCurrent = FindChar(cpCurrent, ':');
  359. CopyMemory(szGlobalDate,cpCurrent, sizeof("2000-01-01") - 1);
  360. szGlobalDate[10] = '\0';
  361. //
  362. // And the global time
  363. //
  364. cpCurrent = FindChar(cpCurrent, ' ');
  365. CopyMemory(szGlobalTime,cpCurrent, sizeof("00:00:00") - 1);
  366. szGlobalTime[8] = '\0';
  367. }
  368. } else {
  369. DWORD pos;
  370. PCHAR pszValue;
  371. if ( !ExtendedFieldsDefined ) {
  372. return(GETLOG_ERROR_PARSE_EXTENDED);
  373. }
  374. //
  375. // Need at least 1 valid entry in the log line other than date & time
  376. if ( (dwHostNamePos == 0) &&
  377. (dwUserNamePos == 0) &&
  378. (dwMethodPos == 0) &&
  379. (dwURIStemPos == 0) &&
  380. (dwURIQueryPos == 0) &&
  381. (dwHTTPStatusPos == 0) &&
  382. (dwBytesSentPos == 0) &&
  383. (dwBytesRecvPos == 0) &&
  384. (dwServicePos == 0) &&
  385. (dwVersionPos == 0)
  386. )
  387. {
  388. return GETLOG_ERROR;
  389. }
  390. //
  391. // loop through entries
  392. //
  393. lpLogLine->szClientIP = szEmpty;
  394. lpLogLine->szUserName = szEmpty;
  395. lpLogLine->szDate = szEmpty;
  396. lpLogLine->szTime = szEmpty;
  397. lpLogLine->szOperation = szEmpty;
  398. lpLogLine->szTargetURL = szEmpty;
  399. lpLogLine->szParameters = szEmpty;
  400. lpLogLine->szServiceStatus = szEmpty;
  401. lpLogLine->szBytesSent = szEmpty;
  402. lpLogLine->szBytesRec = szEmpty;
  403. lpLogLine->szService = szW3Svc;
  404. lpLogLine->szVersion = szDefaultHTTPVersion;
  405. (VOID)FindChar( cpCurrent, '\n' );
  406. for (pos = 1;
  407. *cpCurrent != '\0';
  408. pos++) {
  409. pszValue = cpCurrent;
  410. cpCurrent = FindChar(cpCurrent,' ');
  411. if ( pos == dwHostNamePos ) {
  412. lpLogLine->szClientIP = pszValue;
  413. if (DoDNSConversion) {
  414. lpLogLine->szClientIP = GetMachineName(
  415. lpLogLine->szClientIP
  416. );
  417. }
  418. } else if (pos == dwUserNamePos) {
  419. lpLogLine->szUserName = pszValue;
  420. } else if (pos == dwDatePos) {
  421. lpLogLine->szDate = pszValue;
  422. } else if (pos == dwTimePos) {
  423. lpLogLine->szTime = pszValue;
  424. } else if (pos == dwMethodPos) {
  425. lpLogLine->szOperation = pszValue;
  426. } else if (pos == dwURIStemPos) {
  427. lpLogLine->szTargetURL = pszValue;
  428. } else if (pos == dwURIQueryPos) {
  429. lpLogLine->szParameters = pszValue;
  430. } else if (pos == dwHTTPStatusPos) {
  431. lpLogLine->szServiceStatus = pszValue;
  432. } else if (pos == dwBytesSentPos) {
  433. lpLogLine->szBytesSent = pszValue;
  434. } else if (pos == dwBytesRecvPos) {
  435. lpLogLine->szBytesRec = pszValue;
  436. } else if (pos == dwServicePos) {
  437. lpLogLine->szService = pszValue;
  438. } else if (pos == dwVersionPos) {
  439. lpLogLine->szVersion = pszValue;
  440. }
  441. }
  442. if ( lpLogLine->szDate == szEmpty ) {
  443. lpLogLine->szDate = szGlobalDate;
  444. }
  445. if ( lpLogLine->szTime == szEmpty ) {
  446. lpLogLine->szTime = szGlobalTime;
  447. }
  448. bRetCode = GETLOG_SUCCESS;
  449. }
  450. }
  451. } // end if first char = NewLine
  452. } // end if fgets != NULL
  453. return (bRetCode);
  454. }
  455. WORD
  456. DateStringToDOSDate(
  457. IN PCHAR szDate
  458. )
  459. {
  460. char *szDay;
  461. char *szMonth;
  462. char *szYear;
  463. char *cpCurrent;
  464. char szTmpStr[20];
  465. int iYear;
  466. if ( LogFileFormat == LOGFILE_CUSTOM ) {
  467. strcpy (szTmpStr, szDate);
  468. cpCurrent = szTmpStr;
  469. szYear = cpCurrent;
  470. cpCurrent = FindChar(cpCurrent,'-');
  471. szMonth = cpCurrent;
  472. cpCurrent = FindChar(cpCurrent,'-');
  473. szDay = cpCurrent;
  474. iYear=atoi(szYear);
  475. if ( iYear > 1980 ) {
  476. iYear -= 1980;
  477. }
  478. } else {
  479. strcpy (szTmpStr, szDate);
  480. cpCurrent = szTmpStr;
  481. if ( dwDateFormat == DateFormatJapan ) {
  482. // YY/MM/DD
  483. szYear = cpCurrent;
  484. cpCurrent = FindChar (cpCurrent, '/');
  485. szMonth = cpCurrent;
  486. cpCurrent = FindChar (cpCurrent, '/');
  487. szDay = cpCurrent;
  488. } else if (dwDateFormat == DateFormatGermany ) {
  489. // DD.MM.YY
  490. szDay = cpCurrent;
  491. cpCurrent = FindChar (cpCurrent, '.');
  492. szMonth = cpCurrent;
  493. cpCurrent = FindChar (cpCurrent, '.');
  494. szYear = cpCurrent;
  495. } else {
  496. // MM/DD/YY
  497. szMonth = cpCurrent;
  498. cpCurrent = FindChar (cpCurrent, '/');
  499. szDay = cpCurrent;
  500. cpCurrent = FindChar (cpCurrent, '/');
  501. szYear = cpCurrent;
  502. }
  503. iYear=atoi(szYear);
  504. if ( iYear < 80 ) {
  505. iYear += 2000;
  506. }
  507. if (iYear > 1980 ) {
  508. iYear = iYear - 1980;
  509. } else if (iYear >= 80 ) {
  510. iYear = iYear - 80;
  511. }
  512. }
  513. return ((iYear << 9) | (atoi(szMonth) << 5) | atoi(szDay));
  514. } // DateStringToDOSDate
  515. WORD
  516. TimeStringToDOSTime(
  517. IN PCHAR szTime,
  518. IN LPWORD lpwSec
  519. )
  520. {
  521. char *cpCurrent;
  522. char *szHour;
  523. char *szMinute;
  524. char *szSecond;
  525. char szTmpStr[20];
  526. strcpy (szTmpStr, szTime);
  527. cpCurrent = szTmpStr;
  528. szHour = cpCurrent;
  529. cpCurrent = FindChar (cpCurrent, ':');
  530. szMinute = cpCurrent;
  531. cpCurrent = FindChar (cpCurrent, ':');
  532. szSecond = cpCurrent;
  533. *lpwSec = (WORD)atoi(szSecond);
  534. return ( (atoi(szHour) << 11) | (atoi(szMinute) << 5) | (atoi(szSecond) / 2));
  535. }
  536. char *
  537. AscMonth (
  538. IN WORD wMonth,
  539. IN char *szMonth
  540. )
  541. {
  542. switch (wMonth)
  543. {
  544. case 1:
  545. strncpy (szMonth, szJan, 3);
  546. break;
  547. case 2:
  548. strncpy (szMonth, szFeb, 3);
  549. break;
  550. case 3:
  551. strncpy (szMonth, szMar, 3);
  552. break;
  553. case 4:
  554. strncpy (szMonth, szApr, 3);
  555. break;
  556. case 5:
  557. strncpy (szMonth, szMay, 3);
  558. break;
  559. case 6:
  560. strncpy (szMonth, szJun, 3);
  561. break;
  562. case 7:
  563. strncpy (szMonth, szJul, 3);
  564. break;
  565. case 8:
  566. strncpy (szMonth, szAug, 3);
  567. break;
  568. case 9:
  569. strncpy (szMonth, szSep, 3);
  570. break;
  571. case 10:
  572. strncpy (szMonth, szOct, 3);
  573. break;
  574. case 11:
  575. strncpy (szMonth, szNov, 3);
  576. break;
  577. case 12:
  578. strncpy (szMonth, szDec, 3);
  579. break;
  580. } //end switch
  581. szMonth[3] = '\0';
  582. return (szMonth);
  583. } //end AscMonth
  584. FILE *
  585. StartNewOutputLog (
  586. IN LPOUTFILESTATUS pOutFile,
  587. IN LPCSTR pszInFileName,
  588. IN PCHAR szDate
  589. )
  590. {
  591. BOOL bRet;
  592. DWORD dwErr;
  593. if (pOutFile->fpOutFile != NULL ) {
  594. fclose(pOutFile->fpOutFile);
  595. pOutFile->fpOutFile = NULL;
  596. bRet = MoveFileEx(
  597. pOutFile->szTmpFileName,
  598. pOutFile->szOutFileName,
  599. MOVEFILE_COPY_ALLOWED);
  600. if (!bRet) {
  601. dwErr = GetLastError();
  602. switch (dwErr)
  603. {
  604. case ERROR_FILE_EXISTS:
  605. case ERROR_ALREADY_EXISTS:
  606. CombineFiles(
  607. pOutFile->szTmpFileName,
  608. pOutFile->szOutFileName);
  609. break;
  610. case ERROR_PATH_NOT_FOUND:
  611. break;
  612. default:
  613. printfids(IDS_FILE_ERR, dwErr);
  614. exit (1);
  615. break;
  616. }
  617. }
  618. printfids(IDS_FILE_CLOSE, pOutFile->szOutFileName);
  619. }
  620. dwErr = GetTempPath(MAX_PATH, TempDir);
  621. if (0 != dwErr) {
  622. GetTempFileName(TempDir, "mhi", 0, pOutFile->szTmpFileName);
  623. } else {
  624. GetTempFileName(".", "mhi", 0, pOutFile->szTmpFileName);
  625. }
  626. pOutFile->fpOutFile = fopen(pOutFile->szTmpFileName, "w");
  627. sprintf(pOutFile->szOutFileName,
  628. "%s%s%s",
  629. OutputDir,
  630. pszInFileName,
  631. DoDNSConversion? ".ncsa.dns" : ".ncsa"
  632. );
  633. printfids (IDS_FILE_WRITE, pOutFile->szOutFileName);
  634. return (pOutFile->fpOutFile);
  635. } // StartNewOutputLog
  636. FILE *
  637. StartNewOutputDumpLog (
  638. IN LPOUTFILESTATUS pOutFile,
  639. IN LPCSTR pszInputFileName,
  640. IN LPCSTR pszExt
  641. )
  642. {
  643. BOOL bRet;
  644. DWORD dwErr;
  645. dwErr = GetTempPath(MAX_PATH, TempDir);
  646. if (0 != dwErr) {
  647. GetTempFileName(TempDir, "mhi", 0, pOutFile->szTmpFileName);
  648. } else {
  649. GetTempFileName(".", "mhi", 0, pOutFile->szTmpFileName);
  650. }
  651. pOutFile->fpOutFile = fopen(pOutFile->szTmpFileName, "w");
  652. sprintf(pOutFile->szOutFileName,"%s%s%s",
  653. OutputDir, pszInputFileName,
  654. pszExt
  655. );
  656. printfids (IDS_FILE_WRITE, pOutFile->szOutFileName);
  657. return (pOutFile->fpOutFile);
  658. } // StartNewOutputDumpLog
  659. /* #pragma INTRINSA suppress=all */
  660. VOID
  661. CombineFiles(
  662. IN LPTSTR lpszNew,
  663. IN LPTSTR lpszExisting
  664. )
  665. {
  666. FILE *fpExisting;
  667. FILE *fpNew;
  668. char szLine[4096];
  669. printfids(IDS_FILE_EXIST, lpszExisting);
  670. fpNew = fopen(lpszNew, "r");
  671. fpExisting = fopen(lpszExisting, "a");
  672. fgets(szLine, sizeof(szLine), fpNew);
  673. // last line contains only EOF, but does not overwrite szLine.
  674. // It should not be written.
  675. while (!feof(fpNew))
  676. {
  677. fputs(szLine, fpExisting);
  678. fgets(szLine, sizeof(szLine), fpNew);
  679. }
  680. if (fpNew) {
  681. fclose(fpNew);
  682. }
  683. if (fpExisting) {
  684. fclose(fpExisting);
  685. }
  686. DeleteFile(lpszNew);
  687. }
  688. void
  689. Usage(
  690. IN PCHAR szProg
  691. )
  692. {
  693. CHAR szTemp[MAX_PATH];
  694. GetTempPath(MAX_PATH, szTemp);
  695. printfids(IDS_HEADER1);
  696. printfids(IDS_HEADER2);
  697. printfids(IDS_HEADER3);
  698. printfids(IDS_HEADER4);
  699. printfids(IDS_USAGE1, szProg);
  700. printfids(IDS_USAGE2);
  701. printfids(IDS_USAGE3);
  702. printfids(IDS_USAGE4);
  703. printfids(IDS_USAGE5);
  704. printfids(IDS_USAGE7);
  705. printfids(IDS_USAGE8);
  706. printfids(IDS_USAGE9);
  707. printfids(IDS_USAGE10);
  708. printfids(IDS_USAGE11);
  709. printfids(IDS_USAGE12);
  710. printfids(IDS_USAGE13);
  711. printfids(IDS_USAGE14);
  712. printfids(IDS_USAGE15);
  713. printfids(IDS_USAGE16);
  714. printfids(IDS_SAMPLE0, szProg);
  715. printfids(IDS_SAMPLE1, szProg);
  716. printfids(IDS_SAMPLE2, szProg);
  717. printfids(IDS_SAMPLE3, szProg);
  718. return;
  719. }
  720. VOID
  721. printfids(
  722. IN DWORD ids,
  723. ...
  724. )
  725. {
  726. CHAR szBuff[2048];
  727. CHAR szString[2048];
  728. WCHAR szOutput[2048];
  729. va_list argList;
  730. //
  731. // Try and load the string
  732. //
  733. if ( !LoadString( GetModuleHandle( NULL ),
  734. ids,
  735. szString,
  736. sizeof( szString ) ))
  737. {
  738. printf( "Error loading string ID %d\n",
  739. ids );
  740. return;
  741. }
  742. va_start( argList, ids );
  743. vsprintf( szBuff, szString, argList );
  744. va_end( argList );
  745. MultiByteToWideChar( CP_ACP, 0, szBuff, -1, szOutput, sizeof(szOutput)/sizeof(WCHAR));
  746. WideCharToMultiByte( GetConsoleOutputCP(), 0, szOutput, wcslen(szOutput)+1,
  747. szBuff, sizeof(szBuff), NULL, NULL);
  748. printf(szBuff );
  749. }