Source code of Windows XP (NT5)
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.

302 lines
8.3 KiB

  1. #include <tchar.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. void ShowHelp(void);
  6. LPSTR StripWhitespace(LPSTR pszString);
  7. LPWSTR MakeWideStrFromAnsi(UINT uiCodePage, LPSTR psz);
  8. void CheckForFullAccess(void);
  9. //***************************************************************************
  10. //*
  11. //* purpose: main
  12. //*
  13. //***************************************************************************
  14. int __cdecl main(int argc,char *argv[])
  15. {
  16. int iRet = 0;
  17. int argno;
  18. char * pArg = NULL;
  19. char * pCmdStart = NULL;
  20. TCHAR szFilePath1[_MAX_PATH];
  21. TCHAR szFilePath2[_MAX_PATH];
  22. TCHAR szParamString_C[_MAX_PATH];
  23. int iDo_A = FALSE;
  24. int iDo_B = FALSE;
  25. int iDo_C = FALSE;
  26. int iDo_D = FALSE;
  27. int iGotParamE = FALSE;
  28. *szFilePath1 = '\0';
  29. *szFilePath2 = '\0';
  30. *szParamString_C = '\0';
  31. _tcscpy(szFilePath1,_T(""));
  32. _tcscpy(szFilePath2,_T(""));
  33. _tcscpy(szParamString_C,_T(""));
  34. for(argno=1; argno<argc; argno++)
  35. {
  36. if ( argv[argno][0] == '-' || argv[argno][0] == '/' )
  37. {
  38. switch (argv[argno][1])
  39. {
  40. case 'a':
  41. case 'A':
  42. iDo_A = TRUE;
  43. break;
  44. case 'b':
  45. case 'B':
  46. iDo_B = TRUE;
  47. break;
  48. case 'c':
  49. case 'C':
  50. iDo_C = TRUE;
  51. break;
  52. case 'd':
  53. case 'D':
  54. iDo_D = TRUE;
  55. break;
  56. case 'e':
  57. case 'E':
  58. // Get the string for this flag
  59. pArg = CharNextA(argv[argno]);
  60. pArg = CharNextA(pArg);
  61. if (*pArg == ':')
  62. {
  63. char szTempString[_MAX_PATH];
  64. pArg = CharNextA(pArg);
  65. // Check if it's quoted
  66. if (*pArg == '\"')
  67. {
  68. pArg = CharNextA(pArg);
  69. pCmdStart = pArg;
  70. while ((*pArg) && (*pArg != '\"')){pArg = CharNextA(pArg);}
  71. }
  72. else
  73. {
  74. pCmdStart = pArg;
  75. while (*pArg){pArg = CharNextA(pArg);}
  76. }
  77. *pArg = '\0';
  78. lstrcpyA(szTempString, StripWhitespace(pCmdStart));
  79. // Convert to unicode
  80. #if defined(UNICODE) || defined(_UNICODE)
  81. MultiByteToWideChar(CP_ACP, 0, szTempString, -1, (LPWSTR) szParamString_C, _MAX_PATH);
  82. #else
  83. _tcscpy(szParamString_C,szTempString);
  84. #endif
  85. iGotParamE = TRUE;
  86. }
  87. break;
  88. case '?':
  89. goto main_exit_with_help;
  90. break;
  91. }
  92. }
  93. else
  94. {
  95. if (_tcsicmp(szFilePath1, _T("")) == 0)
  96. {
  97. // if no arguments, then get the filename portion
  98. #if defined(UNICODE) || defined(_UNICODE)
  99. MultiByteToWideChar(CP_ACP, 0, argv[argno], -1, (LPTSTR) szFilePath1, _MAX_PATH);
  100. #else
  101. _tcscpy(szFilePath1,argv[argno]);
  102. #endif
  103. }
  104. else
  105. {
  106. if (_tcsicmp(szFilePath2, _T("")) == 0)
  107. {
  108. // if no arguments, then get the filename portion
  109. #if defined(UNICODE) || defined(_UNICODE)
  110. MultiByteToWideChar(CP_ACP, 0, argv[argno], -1, (LPTSTR) szFilePath2, _MAX_PATH);
  111. #else
  112. _tcscpy(szFilePath2,argv[argno]);
  113. #endif
  114. }
  115. }
  116. }
  117. }
  118. //
  119. // Check what were supposed to do
  120. //
  121. if (TRUE == iDo_A)
  122. {
  123. // Check if we have access
  124. CheckForFullAccess();
  125. goto main_exit_gracefully;
  126. }
  127. if (_tcsicmp(szFilePath1, _T("")) == 0)
  128. {
  129. goto main_exit_with_help;
  130. }
  131. goto main_exit_gracefully;
  132. main_exit_gracefully:
  133. exit(iRet);
  134. main_exit_with_help:
  135. ShowHelp();
  136. exit(iRet);
  137. }
  138. //***************************************************************************
  139. //*
  140. //* purpose: ?
  141. //*
  142. //***************************************************************************
  143. void ShowHelp(void)
  144. {
  145. TCHAR szModuleName[_MAX_PATH];
  146. TCHAR szFilename_only[_MAX_PATH];
  147. // get the modules full pathed filename
  148. if (0 != GetModuleFileName(NULL,(LPTSTR) szModuleName,_MAX_PATH))
  149. {
  150. // Trim off the filename only.
  151. _tsplitpath(szModuleName, NULL, NULL, szFilename_only, NULL);
  152. _tprintf(_T("Testing Utility\n\n"));
  153. _tprintf(_T("%s [-a] [-b] [-c] [-d] [-e:paramfore] [drive:][path]filename1 [drive:][path]filename2\n\n"),szFilename_only);
  154. }
  155. return;
  156. }
  157. //***************************************************************************
  158. //*
  159. //* purpose:
  160. //*
  161. //***************************************************************************
  162. LPSTR StripWhitespace( LPSTR pszString )
  163. {
  164. LPSTR pszTemp = NULL;
  165. if ( pszString == NULL )
  166. {
  167. return NULL;
  168. }
  169. while ( *pszString == ' ' || *pszString == '\t' )
  170. {
  171. pszString += 1;
  172. }
  173. // Catch case where string consists entirely of whitespace or empty string.
  174. if ( *pszString == '\0' )
  175. {
  176. return pszString;
  177. }
  178. pszTemp = pszString;
  179. pszString += lstrlenA(pszString) - 1;
  180. while ( *pszString == ' ' || *pszString == '\t' )
  181. {
  182. *pszString = '\0';
  183. pszString -= 1;
  184. }
  185. return pszTemp;
  186. }
  187. //***************************************************************************
  188. //*
  189. //* purpose: return back a Alocated wide string from a ansi string
  190. //* caller must free the returned back pointer with GlobalFree()
  191. //*
  192. //***************************************************************************
  193. LPWSTR MakeWideStrFromAnsi(UINT uiCodePage, LPSTR psz)
  194. {
  195. LPWSTR pwsz;
  196. int i;
  197. // make sure they gave us something
  198. if (!psz)
  199. {
  200. return NULL;
  201. }
  202. // compute the length
  203. i = MultiByteToWideChar(uiCodePage, 0, psz, -1, NULL, 0);
  204. if (i <= 0) return NULL;
  205. // allocate memory in that length
  206. pwsz = (LPWSTR) GlobalAlloc(GPTR,i * sizeof(WCHAR));
  207. if (!pwsz) return NULL;
  208. // clear out memory
  209. memset(pwsz, 0, wcslen(pwsz) * sizeof(WCHAR));
  210. // convert the ansi string into unicode
  211. i = MultiByteToWideChar(uiCodePage, 0, (LPSTR) psz, -1, pwsz, i);
  212. if (i <= 0)
  213. {
  214. GlobalFree(pwsz);
  215. pwsz = NULL;
  216. return NULL;
  217. }
  218. // make sure ends with null
  219. pwsz[i - 1] = 0;
  220. // return the pointer
  221. return pwsz;
  222. }
  223. void CheckForFullAccess(void)
  224. {
  225. HKEY hRootKeyType = HKEY_LOCAL_MACHINE;
  226. HKEY hKey1 = NULL;
  227. HKEY hKey2 = NULL;
  228. DWORD dwError;
  229. TCHAR szPath[200];
  230. _tcscpy(szPath, _T("System\\CurrentControlSet\\Services\\W3SVC"));
  231. dwError = RegOpenKeyEx(hRootKeyType, szPath, 0, KEY_ALL_ACCESS, &hKey1);
  232. _tprintf(_T("key=%s\r\n"),szPath);
  233. if (ERROR_ACCESS_DENIED == dwError){_tprintf(_T("Access Denied\r\n"));}
  234. else{_tprintf(_T("Access Granted!\r\n"));}
  235. _tprintf(_T("\r\n"),szPath);
  236. _tcscpy(szPath, _T("System\\CurrentControlSet\\Services\\W3SVC\\Parameters"));
  237. dwError = RegOpenKeyEx(hRootKeyType, szPath, 0, KEY_ALL_ACCESS, &hKey1);
  238. _tprintf(_T("key=%s\r\n"),szPath);
  239. if (ERROR_ACCESS_DENIED == dwError){_tprintf(_T("Access Denied\r\n"));}
  240. else{_tprintf(_T("Access Granted!\r\n"));}
  241. _tprintf(_T("\r\n"),szPath);
  242. _tcscpy(szPath, _T("System\\CurrentControlSet\\Services\\W3SVC\\Testing"));
  243. dwError = RegOpenKeyEx(hRootKeyType, szPath, 0, KEY_ALL_ACCESS, &hKey1);
  244. _tprintf(_T("key=%s\r\n"),szPath);
  245. if (ERROR_ACCESS_DENIED == dwError){_tprintf(_T("Access Denied\r\n"));}
  246. else{_tprintf(_T("Access Granted!\r\n"));}
  247. _tprintf(_T("\r\n"),szPath);
  248. _tcscpy(szPath, _T("System\\CurrentControlSet\\Services\\W3SVC\\Testing\\Testing2"));
  249. dwError = RegOpenKeyEx(hRootKeyType, szPath, 0, KEY_ALL_ACCESS, &hKey2);
  250. _tprintf(_T("key=%s\r\n"),szPath);
  251. if (ERROR_ACCESS_DENIED == dwError){_tprintf(_T("Access Denied\r\n"));}
  252. else{_tprintf(_T("Access Granted!\r\n"));}
  253. _tprintf(_T("\r\n"),szPath);
  254. RegCloseKey(hKey1);
  255. RegCloseKey(hKey2);
  256. return;
  257. }