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.

377 lines
9.8 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. /***************************************************************************
  3. *
  4. * ACINIUPD.C
  5. *
  6. * Utility to update INI files
  7. *
  8. *
  9. ****************************************************************************/
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <windows.h>
  14. #include <winsta.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <locale.h>
  18. #include <utilsub.h>
  19. #include <string.h>
  20. #include <winnlsp.h>
  21. #include "aciniupd.h"
  22. #include "tsappcmp.h"
  23. #define WININI L"win.ini"
  24. /*
  25. * Global Data
  26. */
  27. WCHAR file_name[MAX_IDS_LEN+1]; // ini file name
  28. WCHAR section_name[MAX_IDS_LEN+1]; // section name
  29. WCHAR key_name[MAX_IDS_LEN+1]; // key name
  30. WCHAR new_string[MAX_IDS_LEN+1]; // new string
  31. USHORT help_flag = FALSE; // User wants help
  32. USHORT fEditValue = FALSE; // Update the value associated with the key
  33. USHORT fEditKey = FALSE; // Update the key name
  34. USHORT fUserIni = FALSE; // Make change to the user's windows directory
  35. USHORT fVerbose = FALSE; // Verbose mode for debugging
  36. TOKMAP ptm[] = {
  37. {L"/?", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &help_flag},
  38. {L"/e", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fEditValue},
  39. {L"/k", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fEditKey},
  40. {L"/u", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fUserIni},
  41. {L"/v", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fVerbose},
  42. {L" ", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_IDS_LEN, file_name},
  43. {L" ", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_IDS_LEN, section_name},
  44. {L" ", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_IDS_LEN, key_name},
  45. {L" ", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_IDS_LEN, new_string},
  46. {0, 0, 0, 0, 0}
  47. };
  48. /*
  49. * Local functions
  50. */
  51. void Usage(BOOLEAN bError);
  52. int UpdateValue(PWCHAR fileName, PWCHAR sectionName, PWCHAR keyName, PWCHAR newString);
  53. int UpdateKey(PWCHAR fileName, PWCHAR sectionName, PWCHAR keyName, PWCHAR newString);
  54. /******************************************************************************
  55. *
  56. * main
  57. *
  58. ******************************************************************************/
  59. int __cdecl
  60. main( INT argc, CHAR **argv )
  61. {
  62. WCHAR **argvW;
  63. WCHAR wcSrcPort[MAX_PATH], wcDestPort[MAX_PATH];
  64. ULONG ulSrcPort, ulDestPort, rc;
  65. BOOLEAN State, Changed = FALSE;
  66. int result = SUCCESS;
  67. BOOL InstallState;
  68. setlocale(LC_ALL, ".OCP");
  69. SetThreadUILanguage(0);
  70. /*
  71. * Massage the command line.
  72. */
  73. argvW = MassageCommandLine((DWORD)argc);
  74. if (argvW == NULL) {
  75. ErrorPrintf(IDS_ERROR_MALLOC);
  76. return(FAILURE);
  77. }
  78. /*
  79. * parse the cmd line without parsing the program name (argc-1, argv+1)
  80. */
  81. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  82. /*
  83. * Check for error from ParseCommandLine
  84. */
  85. if ( help_flag || (rc && !(rc & PARSE_FLAG_NO_PARMS)) ) {
  86. if ( !help_flag ) {
  87. Usage(TRUE);
  88. return(FAILURE);
  89. } else {
  90. Usage(FALSE);
  91. return(SUCCESS);
  92. }
  93. }
  94. if (wcscmp( file_name, L"" ) == 0 ||
  95. wcscmp( section_name, L"" ) == 0 ||
  96. wcscmp( key_name, L"" ) == 0) {
  97. Usage( TRUE );
  98. return (FAILURE);
  99. }
  100. rc = 1;
  101. #if 0
  102. State = CtxGetIniMapping();
  103. /*
  104. * Change the INI mapping mode if necessary
  105. */
  106. if (!State && fUserIni) {
  107. rc = CtxSetIniMapping( TRUE );
  108. Changed = TRUE;
  109. }
  110. else if (State && !fUserIni) {
  111. rc = CtxSetIniMapping( FALSE );
  112. Changed = TRUE;
  113. }
  114. #else
  115. InstallState = TermsrvAppInstallMode();
  116. if( InstallState && fUserIni ) {
  117. rc = SetTermsrvAppInstallMode( FALSE );
  118. Changed = TRUE;
  119. } else if( !InstallState && !fUserIni ) {
  120. rc = SetTermsrvAppInstallMode( TRUE );
  121. Changed = TRUE;
  122. }
  123. #endif // 0
  124. /*
  125. * Exit if failed to change user mode
  126. */
  127. if (!rc) {
  128. if (fVerbose) ErrorPrintf(IDS_ERROR_CHANGE_MODE, GetLastError());
  129. return (FAILURE);
  130. }
  131. if (fEditValue) {
  132. result = UpdateValue(file_name, section_name, key_name, new_string);
  133. }
  134. else if (fEditKey) {
  135. result = UpdateKey(file_name, section_name, key_name, new_string);
  136. }
  137. else {
  138. Usage(FALSE);
  139. result = FAILURE;
  140. }
  141. /*
  142. * Change back to the original mode if necessary. Assume it always successes.
  143. */
  144. if (Changed) {
  145. // rc = CtxSetIniMapping( State );
  146. rc = SetTermsrvAppInstallMode( InstallState );
  147. }
  148. return (result);
  149. } /* main */
  150. /*******************************************************************************
  151. *
  152. * Usage
  153. *
  154. * Output the usage message for this utility.
  155. *
  156. * ENTRY:
  157. * bError (input)
  158. * TRUE if the 'invalid parameter(s)' message should preceed the usage
  159. * message and the output go to stderr; FALSE for no such error
  160. * string and output goes to stdout.
  161. *
  162. * EXIT:
  163. *
  164. *
  165. ******************************************************************************/
  166. void
  167. Usage( BOOLEAN bError )
  168. {
  169. if ( bError ) {
  170. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  171. }
  172. ErrorPrintf(IDS_HELP_USAGE1);
  173. ErrorPrintf(IDS_HELP_USAGE2);
  174. ErrorPrintf(IDS_HELP_USAGE3);
  175. ErrorPrintf(IDS_HELP_USAGE4);
  176. ErrorPrintf(IDS_HELP_USAGE6);
  177. } /* Usage() */
  178. /******************************************************************************
  179. *
  180. * UpdateValue
  181. *
  182. * Update the associated value for the key
  183. *
  184. * ENTRY:
  185. * PWCHAR fileName
  186. * Ini file name
  187. * PWCHAR sectionName
  188. * Section name
  189. * PWCHAR keyName
  190. * Key name
  191. * pwchar newString
  192. * New value
  193. *
  194. * EXIT:
  195. * FAILURE / SUCCESS
  196. *
  197. *******************************************************************************/
  198. int UpdateValue( PWCHAR fileName,
  199. PWCHAR sectionName,
  200. PWCHAR keyName,
  201. PWCHAR newString )
  202. {
  203. BOOLEAN isWinIni;
  204. WCHAR value[5];
  205. UINT result = 0;
  206. isWinIni = wcscmp( fileName, WININI ) == 0 ? TRUE : FALSE;
  207. /*
  208. * If change is made to win.ini, call WriteProfileString API
  209. */
  210. if (isWinIni) {
  211. result = WriteProfileString( sectionName,
  212. keyName,
  213. newString );
  214. }
  215. /*
  216. * Otherwise, call WritePrivateProfileString API
  217. */
  218. else {
  219. result = WritePrivateProfileString( sectionName,
  220. keyName,
  221. newString,
  222. fileName );
  223. }
  224. if (result == 0) {
  225. if (fVerbose)
  226. {
  227. StringDwordErrorPrintf(IDS_ERROR_UPDATE_VALUE, keyName, GetLastError());
  228. }
  229. return (FAILURE);
  230. }
  231. return (SUCCESS);
  232. } /* UpdateValue */
  233. /******************************************************************************
  234. *
  235. * UpdateKey
  236. *
  237. * Update the key name
  238. *
  239. * ENTRY:
  240. * PWCHAR fileName
  241. * Ini file name
  242. * PWCHAR sectionName
  243. * Section name
  244. * PWCHAR keyName
  245. * Key name
  246. * PWCHAR newString
  247. * New key name
  248. *
  249. * EXIT:
  250. * FAILURE / SUCCESS
  251. *
  252. *******************************************************************************/
  253. int UpdateKey( PWCHAR fileName,
  254. PWCHAR sectionName,
  255. PWCHAR keyName,
  256. PWCHAR newString )
  257. {
  258. BOOLEAN isWinIni;
  259. PWCHAR value;
  260. UINT result;
  261. value = (WCHAR *)malloc( sizeof(WCHAR) * (MAX_IDS_LEN + 1) );
  262. if (value == NULL) {
  263. if (fVerbose) ErrorPrintf(IDS_ERROR_MALLOC);
  264. return (FAILURE);
  265. }
  266. __try
  267. {
  268. isWinIni = wcscmp( fileName, WININI ) == 0 ? TRUE : FALSE;
  269. /*
  270. * Get the value string
  271. */
  272. if (isWinIni) {
  273. result = GetProfileString( sectionName,
  274. keyName,
  275. L"",
  276. value,
  277. MAX_IDS_LEN+1 );
  278. }
  279. else {
  280. result = GetPrivateProfileString( sectionName,
  281. keyName,
  282. L"",
  283. value,
  284. MAX_IDS_LEN+1,
  285. fileName );
  286. }
  287. if (result == 0) {
  288. if (fVerbose)
  289. {
  290. StringErrorPrintf(IDS_ERROR_GET_VALUE, keyName);
  291. }
  292. return (FAILURE);
  293. }
  294. /*
  295. * Delete the old key
  296. */
  297. if (isWinIni) {
  298. result = WriteProfileString( sectionName, keyName, NULL );
  299. }
  300. else {
  301. result = WritePrivateProfileString( sectionName, keyName, NULL, fileName );
  302. }
  303. if (result == 0) {
  304. if (fVerbose)
  305. {
  306. StringDwordErrorPrintf(IDS_ERROR_DEL_KEY, keyName, GetLastError());
  307. }
  308. return (FAILURE);
  309. }
  310. /*
  311. * Add the new key
  312. */
  313. if (isWinIni) {
  314. result = WriteProfileString( sectionName, newString, value );
  315. }
  316. else {
  317. result = WritePrivateProfileString( sectionName, newString, value, fileName );
  318. }
  319. if (result == 0) {
  320. if (fVerbose)
  321. {
  322. StringDwordErrorPrintf(IDS_ERROR_UPDATE_KEY, keyName, GetLastError());
  323. }
  324. return (FAILURE);
  325. }
  326. return (SUCCESS);
  327. }
  328. __finally
  329. {
  330. free( value );
  331. }
  332. } /* UpdateKey */