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.

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