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.

492 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,
  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,
  122. IniFileW
  123. );
  124. Keyword = Keywords;
  125. while (*Keyword) {
  126. GetPrivateProfileStringW( Section, Keyword, NULL,
  127. KeyValue, 2048,
  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. int __cdecl
  236. main( argc, argv )
  237. int argc;
  238. char *argv[];
  239. {
  240. int i, n;
  241. LPSTR s, IniFile, SectionName, KeywordName, KeywordValue;
  242. WCHAR IniFileW[_MAX_PATH];
  243. WCHAR SectionNameW[_MAX_PATH];
  244. BOOL rc;
  245. ConvertAppToOem( argc, argv );
  246. if (argc < 1) {
  247. Usage();
  248. }
  249. IniFile = "win.ini";
  250. SectionName = NULL;
  251. KeywordName = NULL;
  252. KeywordValue = NULL;
  253. argc -= 1;
  254. argv += 1;
  255. while (argc--) {
  256. s = *argv++;
  257. if (*s == '-' || *s == '/') {
  258. while (*++s) {
  259. switch ( tolower( *s ) ) {
  260. case 'r': fRefresh = TRUE;
  261. break;
  262. case 's': fSummary = TRUE;
  263. break;
  264. case 'u': fUnicode = TRUE;
  265. break;
  266. case 'f': if (argc) {
  267. argc -= 1;
  268. IniFile = *argv++;
  269. break;
  270. }
  271. default: Usage();
  272. }
  273. }
  274. } else
  275. if (SectionName == NULL) {
  276. if (argc && !strcmp( *argv, ".")) {
  277. SectionName = s;
  278. argc -= 1;
  279. argv += 1;
  280. if (argc) {
  281. if (!strcmp( *argv, "=" )) {
  282. argc -= 1;
  283. argv += 1;
  284. KeywordName = NULL;
  285. if (argc) {
  286. KeywordValue = calloc( 1, 4096 );
  287. s = KeywordValue;
  288. while (argc) {
  289. strcpy( s, *argv++ );
  290. s += strlen( s ) + 1;
  291. argc -= 1;
  292. }
  293. } else {
  294. KeywordValue = (LPSTR)-1;
  295. }
  296. } else {
  297. argc -= 1;
  298. KeywordName = *argv++;
  299. }
  300. } else {
  301. KeywordName = NULL;
  302. }
  303. } else
  304. if (KeywordName = strchr( s, '.' )) {
  305. *KeywordName++ = '\0';
  306. SectionName = s;
  307. } else {
  308. SectionName = s;
  309. }
  310. } else
  311. if (!strcmp( s, "=" )) {
  312. if (argc) {
  313. argc -= 1;
  314. KeywordValue = *argv++;
  315. } else {
  316. KeywordValue = (LPSTR)-1;
  317. }
  318. } else {
  319. Usage();
  320. }
  321. }
  322. if (fRefresh) {
  323. printf( "Refreshing .INI file mapping information for %s\n", IniFile );
  324. WritePrivateProfileString( NULL, NULL, NULL, IniFile );
  325. exit( 0 );
  326. }
  327. printf( "%s contents of %s\n", KeywordValue ? "Modifying" : "Displaying", IniFile );
  328. MultiByteToWideChar(GetACP(), 0, IniFile, strlen(IniFile)+1, IniFileW, sizeof(IniFileW)/sizeof(WCHAR));
  329. if (SectionName)
  330. MultiByteToWideChar(GetACP(), 0, SectionName, strlen(SectionName)+1, SectionNameW, sizeof(SectionNameW)/sizeof(WCHAR));
  331. if (SectionName == NULL) {
  332. if (fUnicode)
  333. DumpIniFileW( IniFileW );
  334. else
  335. DumpIniFileA( IniFile );
  336. } else
  337. if (KeywordName == NULL) {
  338. if (fUnicode)
  339. DumpIniFileSectionW( IniFileW, SectionNameW );
  340. else
  341. DumpIniFileSectionA( IniFile, SectionName );
  342. if (KeywordValue != NULL) {
  343. printf( "Above application variables are being deleted" );
  344. if (KeywordValue != (LPSTR)-1) {
  345. printf( " and rewritten" );
  346. } else {
  347. KeywordValue = NULL;
  348. }
  349. if (fUnicode) {
  350. WCHAR KeywordNameW[4096];
  351. WCHAR KeywordValueW[4096];
  352. MultiByteToWideChar(GetACP(), 0, KeywordName, strlen(KeywordName)+1, KeywordNameW, sizeof(KeywordNameW)/sizeof(WCHAR));
  353. if (KeywordValue)
  354. MultiByteToWideChar(GetACP(), 0, KeywordValue, strlen(KeywordValue)+1, KeywordValueW, sizeof(KeywordValueW)/sizeof(WCHAR));
  355. rc = WritePrivateProfileStringW( SectionNameW,
  356. KeywordNameW,
  357. KeywordValue ? KeywordValueW : NULL,
  358. IniFileW
  359. );
  360. } else {
  361. rc = WritePrivateProfileStringA( SectionName,
  362. KeywordName,
  363. KeywordValue,
  364. IniFile
  365. );
  366. }
  367. if (!rc) {
  368. printf( " *** failed, ErrorCode -== %u\n", GetLastError() );
  369. } else {
  370. printf( " [ok]\n", GetLastError() );
  371. }
  372. }
  373. } else {
  374. printf( "[%s]\n %s == ", SectionName, KeywordName );
  375. if (fUnicode) {
  376. WCHAR KeywordNameW[4096];
  377. MultiByteToWideChar(GetACP(), 0, KeywordName, strlen(KeywordName)+1, KeywordNameW, sizeof(KeywordNameW)/sizeof(WCHAR));
  378. n = GetPrivateProfileStringW( SectionNameW,
  379. KeywordNameW,
  380. L"*** Section or keyword not found ***",
  381. (LPWSTR)KeyValueBuffer,
  382. sizeof( KeyValueBuffer ),
  383. IniFileW
  384. );
  385. } else {
  386. n = GetPrivateProfileStringA( SectionName,
  387. KeywordName,
  388. "*** Section or keyword not found ***",
  389. KeyValueBuffer,
  390. sizeof( KeyValueBuffer ),
  391. IniFile
  392. );
  393. }
  394. if (KeywordValue == NULL && n == 0 && GetLastError() != NO_ERROR) {
  395. printf( " (ErrorCode == %u)\n", GetLastError() );
  396. } else {
  397. if (fUnicode)
  398. wprintf( L"%s", KeyValueBuffer );
  399. else
  400. printf( "%s", KeyValueBuffer );
  401. if (KeywordValue == NULL) {
  402. printf( "\n" );
  403. } else {
  404. if (KeywordValue == (LPSTR)-1) {
  405. printf( " (deleted)" );
  406. KeywordValue = NULL;
  407. } else {
  408. printf( " (set to %s)", KeywordValue );
  409. }
  410. if (fUnicode) {
  411. WCHAR KeywordNameW[4096];
  412. WCHAR KeywordValueW[4096];
  413. MultiByteToWideChar(GetACP(), 0, KeywordName, strlen(KeywordName)+1, KeywordNameW, sizeof(KeywordNameW)/sizeof(WCHAR));
  414. if (KeywordValue)
  415. MultiByteToWideChar(GetACP(), 0, KeywordValue, strlen(KeywordValue)+1, KeywordValueW, sizeof(KeywordValueW)/sizeof(WCHAR));
  416. rc = WritePrivateProfileStringW( SectionNameW,
  417. KeywordNameW,
  418. KeywordValue ? KeywordValueW : NULL,
  419. IniFileW
  420. );
  421. } else {
  422. rc = WritePrivateProfileStringA( SectionName,
  423. KeywordName,
  424. KeywordValue,
  425. IniFile
  426. );
  427. }
  428. if (!rc) {
  429. printf( " *** failed, ErrorCode -== %u", GetLastError() );
  430. }
  431. printf( "\n" );
  432. }
  433. }
  434. }
  435. return ( 0 );
  436. }