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.

486 lines
14 KiB

  1. /*
  2. * Utility program to dump the contents of a Windows .ini file.
  3. * one form to another. Usage:
  4. *
  5. * ini [-f FileSpec] [SectionName | SectionName.KeywordName [= Value]]
  6. *
  7. *
  8. */
  9. #include "ini.h"
  10. BOOL fRefresh;
  11. BOOL fSummary;
  12. BOOL fUnicode;
  13. void
  14. DumpIniFileA(
  15. char *IniFile
  16. )
  17. {
  18. char *Sections, *Section;
  19. char *Keywords, *Keyword;
  20. char *KeyValue;
  21. Sections = LocalAlloc( 0, 8192 );
  22. if (Sections) {
  23. memset( Sections, 0xFF, 8192 );
  24. } else {
  25. return;
  26. }
  27. Keywords = LocalAlloc( 0, 8192 );
  28. if (Keywords) {
  29. memset( Keywords, 0xFF, 8192 );
  30. } else {
  31. LocalFree(Sections);
  32. return;
  33. }
  34. KeyValue = LocalAlloc( 0, 2048 );
  35. if (KeyValue) {
  36. memset( KeyValue, 0xFF, 2048 );
  37. } else {
  38. LocalFree(Keywords);
  39. LocalFree(Sections);
  40. return;
  41. }
  42. *Sections = '\0';
  43. if (!GetPrivateProfileStringA( NULL, NULL, NULL,
  44. Sections, 8192,
  45. IniFile
  46. )
  47. ) {
  48. printf( "*** Unable to read - rc == %d\n", GetLastError() );
  49. }
  50. Section = Sections;
  51. while (*Section) {
  52. printf( "[%s]\n", Section );
  53. if (!fSummary) {
  54. *Keywords = '\0';
  55. GetPrivateProfileStringA( Section, NULL, NULL,
  56. Keywords, 4096,
  57. IniFile
  58. );
  59. Keyword = Keywords;
  60. while (*Keyword) {
  61. GetPrivateProfileStringA( Section, Keyword, NULL,
  62. KeyValue, 2048,
  63. IniFile
  64. );
  65. printf( " %s=%s\n", Keyword, KeyValue );
  66. while (*Keyword++) {
  67. }
  68. }
  69. }
  70. while (*Section++) {
  71. }
  72. }
  73. LocalFree( Sections );
  74. LocalFree( Keywords );
  75. LocalFree( KeyValue );
  76. return;
  77. }
  78. void
  79. DumpIniFileW(
  80. WCHAR *IniFileW
  81. )
  82. {
  83. WCHAR *Sections, *Section;
  84. WCHAR *Keywords, *Keyword;
  85. WCHAR *KeyValue;
  86. Sections = LocalAlloc( 0, 8192 );
  87. if (Sections) {
  88. memset( Sections, 0xFF, 8192 );
  89. } else {
  90. return;
  91. }
  92. Keywords = LocalAlloc( 0, 8192 );
  93. if (Keywords) {
  94. memset( Keywords, 0xFF, 8192 );
  95. } else {
  96. LocalFree(Sections);
  97. return;
  98. }
  99. KeyValue = LocalAlloc( 0, 2048 );
  100. if (KeyValue) {
  101. memset( KeyValue, 0xFF, 2048 );
  102. } else {
  103. LocalFree(Keywords);
  104. LocalFree(Sections);
  105. return;
  106. }
  107. *Sections = '\0';
  108. if (!GetPrivateProfileStringW( NULL, NULL, NULL,
  109. Sections, 8192 / sizeof(WCHAR),
  110. IniFileW
  111. )
  112. ) {
  113. wprintf( L"*** Unable to read - rc == %d\n", GetLastError() );
  114. }
  115. Section = Sections;
  116. while (*Section) {
  117. wprintf( L"[%s]\n", Section );
  118. if (!fSummary) {
  119. *Keywords = '\0';
  120. GetPrivateProfileStringW( Section, NULL, NULL,
  121. Keywords, 4096 / sizeof(WCHAR),
  122. IniFileW
  123. );
  124. Keyword = Keywords;
  125. while (*Keyword) {
  126. GetPrivateProfileStringW( Section, Keyword, NULL,
  127. KeyValue, 2048 / sizeof(WCHAR),
  128. IniFileW
  129. );
  130. wprintf( L" %s=%s\n", Keyword, KeyValue );
  131. while (*Keyword++) {
  132. }
  133. }
  134. }
  135. while (*Section++) {
  136. }
  137. }
  138. LocalFree( Sections );
  139. LocalFree( Keywords );
  140. LocalFree( KeyValue );
  141. return;
  142. }
  143. void
  144. DumpIniFileSectionA(
  145. char *IniFile,
  146. char *SectionName
  147. )
  148. {
  149. DWORD cb;
  150. char *SectionValue;
  151. char *s;
  152. cb = 4096;
  153. while (TRUE) {
  154. SectionValue = LocalAlloc( 0, cb );
  155. if (!SectionValue) {
  156. return;
  157. }
  158. *SectionValue = '\0';
  159. if (GetPrivateProfileSection( SectionName,
  160. SectionValue,
  161. cb,
  162. IniFile
  163. ) == cb-2
  164. ) {
  165. LocalFree( SectionValue );
  166. cb *= 2;
  167. } else {
  168. break;
  169. }
  170. }
  171. printf( "[%s]\n", SectionName );
  172. s = SectionValue;
  173. while (*s) {
  174. printf( " %s\n", s );
  175. while (*s++) {
  176. }
  177. }
  178. LocalFree( SectionValue );
  179. return;
  180. }
  181. void
  182. DumpIniFileSectionW(
  183. WCHAR *IniFile,
  184. WCHAR *SectionName
  185. )
  186. {
  187. DWORD cb;
  188. WCHAR *SectionValue;
  189. WCHAR *s;
  190. cb = 4096;
  191. while (TRUE) {
  192. SectionValue = LocalAlloc( 0, cb );
  193. if (!SectionValue) {
  194. return;
  195. }
  196. *SectionValue = L'\0';
  197. if (GetPrivateProfileSectionW( SectionName,
  198. SectionValue,
  199. cb,
  200. IniFile
  201. ) == cb-2
  202. ) {
  203. LocalFree( SectionValue );
  204. cb *= 2;
  205. } else {
  206. break;
  207. }
  208. }
  209. wprintf( L"[%s]\n", SectionName );
  210. s = SectionValue;
  211. while (*s) {
  212. wprintf( L" %s\n", s );
  213. while (*s++) {
  214. }
  215. }
  216. LocalFree( SectionValue );
  217. return;
  218. }
  219. void
  220. Usage( void )
  221. {
  222. fputs( "usage: INI | [-f FileSpec] [-r | [SectionName | SectionName.KeywordName [ = Value]]]\n"
  223. "Where...\n"
  224. " -f Specifies the name of the .ini file. WIN.INI is the default.\n"
  225. " -s Print only the sections in the .ini file\n"
  226. " -u Use the Unicode version of GetPrivateProfileString\n"
  227. "\n"
  228. " -r Refresh the .INI file migration information for the specified file.\n"
  229. "\n"
  230. " blanks around = sign are required when setting the value.\n",
  231. stderr);
  232. exit( 1 );
  233. }
  234. char KeyValueBuffer[ 4096 ];
  235. WCHAR IniFileW[_MAX_PATH];
  236. WCHAR SectionNameW[_MAX_PATH];
  237. WCHAR KeywordNameW[4096];
  238. WCHAR KeywordValueW[4096];
  239. int __cdecl main( int argc, char *argv[] )
  240. {
  241. int i, n;
  242. LPSTR s, IniFile, SectionName, KeywordName, KeywordValue;
  243. BOOL rc;
  244. ConvertAppToOem( argc, argv );
  245. if (argc < 1) {
  246. Usage();
  247. }
  248. IniFile = "win.ini";
  249. SectionName = NULL;
  250. KeywordName = NULL;
  251. KeywordValue = NULL;
  252. argc -= 1;
  253. argv += 1;
  254. while (argc--) {
  255. s = *argv++;
  256. if (*s == '-' || *s == '/') {
  257. while (*++s) {
  258. switch ( tolower( *s ) ) {
  259. case 'r': fRefresh = TRUE;
  260. break;
  261. case 's': fSummary = TRUE;
  262. break;
  263. case 'u': fUnicode = TRUE;
  264. break;
  265. case 'f': if (argc) {
  266. argc -= 1;
  267. IniFile = *argv++;
  268. break;
  269. }
  270. default: Usage();
  271. }
  272. }
  273. } else
  274. if (SectionName == NULL) {
  275. if (argc && !strcmp( *argv, ".")) {
  276. SectionName = s;
  277. argc -= 1;
  278. argv += 1;
  279. if (argc) {
  280. if (!strcmp( *argv, "=" )) {
  281. argc -= 1;
  282. argv += 1;
  283. KeywordName = NULL;
  284. if (argc) {
  285. KeywordValue = calloc( 1, 4096 );
  286. s = KeywordValue;
  287. while (argc) {
  288. strcpy( s, *argv++ );
  289. s += strlen( s ) + 1;
  290. argc -= 1;
  291. }
  292. } else {
  293. KeywordValue = (LPSTR)-1;
  294. }
  295. } else {
  296. argc -= 1;
  297. KeywordName = *argv++;
  298. }
  299. } else {
  300. KeywordName = NULL;
  301. }
  302. } else
  303. if (KeywordName = strchr( s, '.' )) {
  304. *KeywordName++ = '\0';
  305. SectionName = s;
  306. } else {
  307. SectionName = s;
  308. }
  309. } else
  310. if (!strcmp( s, "=" )) {
  311. if (argc) {
  312. argc -= 1;
  313. KeywordValue = *argv++;
  314. } else {
  315. KeywordValue = (LPSTR)-1;
  316. }
  317. } else {
  318. Usage();
  319. }
  320. }
  321. if (fRefresh) {
  322. printf( "Refreshing .INI file mapping information for %s\n", IniFile );
  323. WritePrivateProfileString( NULL, NULL, NULL, IniFile );
  324. exit( 0 );
  325. }
  326. printf( "%s contents of %s\n", KeywordValue ? "Modifying" : "Displaying", IniFile );
  327. MultiByteToWideChar(GetACP(), 0, IniFile, strlen(IniFile)+1, IniFileW, sizeof(IniFileW)/sizeof(WCHAR));
  328. if (SectionName)
  329. MultiByteToWideChar(GetACP(), 0, SectionName, strlen(SectionName)+1, SectionNameW, sizeof(SectionNameW)/sizeof(WCHAR));
  330. if (SectionName == NULL) {
  331. if (fUnicode)
  332. DumpIniFileW( IniFileW );
  333. else
  334. DumpIniFileA( IniFile );
  335. } else
  336. if (KeywordName == NULL) {
  337. if (fUnicode)
  338. DumpIniFileSectionW( IniFileW, SectionNameW );
  339. else
  340. DumpIniFileSectionA( IniFile, SectionName );
  341. if (KeywordValue != NULL) {
  342. printf( "Above application variables are being deleted" );
  343. if (KeywordValue != (LPSTR)-1) {
  344. printf( " and rewritten" );
  345. } else {
  346. KeywordValue = NULL;
  347. }
  348. if (fUnicode) {
  349. MultiByteToWideChar(GetACP(), 0, KeywordName, strlen(KeywordName)+1, KeywordNameW, sizeof(KeywordNameW)/sizeof(WCHAR));
  350. if (KeywordValue)
  351. MultiByteToWideChar(GetACP(), 0, KeywordValue, strlen(KeywordValue)+1, KeywordValueW, sizeof(KeywordValueW)/sizeof(WCHAR));
  352. rc = WritePrivateProfileStringW( SectionNameW,
  353. KeywordNameW,
  354. KeywordValue ? KeywordValueW : NULL,
  355. IniFileW
  356. );
  357. } else {
  358. rc = WritePrivateProfileStringA( SectionName,
  359. KeywordName,
  360. KeywordValue,
  361. IniFile
  362. );
  363. }
  364. if (!rc) {
  365. printf( " *** failed, ErrorCode -== %u\n", GetLastError() );
  366. } else {
  367. puts ( " [ok]");
  368. }
  369. }
  370. } else {
  371. printf( "[%s]\n %s == ", SectionName, KeywordName );
  372. if (fUnicode) {
  373. MultiByteToWideChar(GetACP(), 0, KeywordName, strlen(KeywordName)+1, KeywordNameW, sizeof(KeywordNameW)/sizeof(WCHAR));
  374. n = GetPrivateProfileStringW( SectionNameW,
  375. KeywordNameW,
  376. L"*** Section or keyword not found ***",
  377. (LPWSTR)KeyValueBuffer,
  378. sizeof( KeyValueBuffer ) / sizeof(WCHAR),
  379. IniFileW
  380. );
  381. } else {
  382. n = GetPrivateProfileStringA( SectionName,
  383. KeywordName,
  384. "*** Section or keyword not found ***",
  385. KeyValueBuffer,
  386. sizeof( KeyValueBuffer ),
  387. IniFile
  388. );
  389. }
  390. if (KeywordValue == NULL && n == 0 && GetLastError() != NO_ERROR) {
  391. printf( " (ErrorCode == %u)\n", GetLastError() );
  392. } else {
  393. if (fUnicode)
  394. wprintf( L"%s", (WCHAR *)KeyValueBuffer );
  395. else
  396. printf( "%s", KeyValueBuffer );
  397. if (KeywordValue == NULL) {
  398. printf( "\n" );
  399. } else {
  400. if (KeywordValue == (LPSTR)-1) {
  401. printf( " (deleted)" );
  402. KeywordValue = NULL;
  403. } else {
  404. printf( " (set to %s)", KeywordValue );
  405. }
  406. if (fUnicode) {
  407. MultiByteToWideChar(GetACP(), 0, KeywordName, strlen(KeywordName)+1, KeywordNameW, sizeof(KeywordNameW)/sizeof(WCHAR));
  408. if (KeywordValue)
  409. MultiByteToWideChar(GetACP(), 0, KeywordValue, strlen(KeywordValue)+1, KeywordValueW, sizeof(KeywordValueW)/sizeof(WCHAR));
  410. rc = WritePrivateProfileStringW( SectionNameW,
  411. KeywordNameW,
  412. KeywordValue ? KeywordValueW : NULL,
  413. IniFileW
  414. );
  415. } else {
  416. rc = WritePrivateProfileStringA( SectionName,
  417. KeywordName,
  418. KeywordValue,
  419. IniFile
  420. );
  421. }
  422. if (!rc) {
  423. printf( " *** failed, ErrorCode -== %u", GetLastError() );
  424. }
  425. printf( "\n" );
  426. }
  427. }
  428. }
  429. return ( 0 );
  430. }