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.

176 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. tenv.c
  5. Abstract:
  6. Test program for the NT OS Runtime Library (RTL) Environment API Calls
  7. Author:
  8. Steve Wood (stevewo) 30-Jan-1991
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <stdio.h>
  15. VOID
  16. DumpEnvironment( PVOID env )
  17. {
  18. PWCHAR s = env;
  19. while (*s) {
  20. printf( "%79.79ws\n", s );
  21. while (*s++) {
  22. }
  23. }
  24. }
  25. VOID
  26. SetEnvironment(
  27. PVOID *env,
  28. PCHAR Name,
  29. PCHAR Value
  30. );
  31. VOID
  32. SetEnvironment(
  33. PVOID *env,
  34. PCHAR Name,
  35. PCHAR Value
  36. )
  37. {
  38. NTSTATUS Status;
  39. STRING NameString, ValueString;
  40. UNICODE_STRING uNameString, uValueString;
  41. RtlInitString( &NameString, Name );
  42. Status = RtlAnsiStringToUnicodeString(&uNameString, &NameString, TRUE);
  43. if (!NT_SUCCESS( Status )) {
  44. printf( " - failed converting to Unicode, Status == %X\n", Status );
  45. DumpEnvironment(*env);
  46. printf( "\n" );
  47. return;
  48. }
  49. if (Value != NULL) {
  50. RtlInitString( &ValueString, Value );
  51. Status = RtlAnsiStringToUnicodeString(&uValueString, &ValueString, TRUE);
  52. printf( "TENV: set variable (%X) %Z=%Z\n", *env, &NameString, &ValueString );
  53. Status = RtlSetEnvironmentVariable( env, &uNameString, &uValueString );
  54. printf( "TENV: (%X)", *env);
  55. RtlFreeUnicodeString(&uNameString);
  56. RtlFreeUnicodeString(&uValueString);
  57. }
  58. else {
  59. printf( "TENV: delete variable (%X) %Z\n", *env, &NameString );
  60. Status = RtlSetEnvironmentVariable( env, &uNameString, NULL );
  61. printf( "TENV: (%X)", *env, &NameString, &ValueString );
  62. RtlFreeUnicodeString(&uNameString);
  63. }
  64. if (NT_SUCCESS( Status )) {
  65. printf( "\n" );
  66. }
  67. else {
  68. printf( " - failed, Status == %X\n", Status );
  69. }
  70. DumpEnvironment(*env);
  71. printf( "\n" );
  72. }
  73. int
  74. _cdecl
  75. main(
  76. int argc,
  77. char **argv,
  78. char **envp
  79. )
  80. {
  81. int i;
  82. PVOID env;
  83. PVOID nenv;
  84. NTSTATUS Status;
  85. char bigbuf[4100];
  86. for (i=0; i<argc; i++) {
  87. printf( "argv[ %d ] = %s\n", i, argv[ i ] );
  88. }
  89. i = 0;
  90. while (envp[ i ]) {
  91. printf( "envp[ %d ] = %s\n", i, envp[ i ] );
  92. i++;
  93. }
  94. for (i=0 ; i<4099 ; i++)
  95. bigbuf[i] = (i%26) + (((i&1) == 0) ? 'a' : 'A');
  96. bigbuf[4099] = '\0';
  97. env = NtCurrentPeb()->ProcessParameters->Environment;
  98. Status = RtlCreateEnvironment(TRUE, &nenv); // clone current
  99. if (!NT_SUCCESS( Status )) {
  100. printf( "Unable to create clone environment - %X\n", Status );
  101. return 1;
  102. }
  103. // First, check with process environment
  104. DumpEnvironment( &env);
  105. SetEnvironment( &env, "aaaa", "12345" );
  106. SetEnvironment( &env, "aaaa", "1234567890" );
  107. SetEnvironment( &env, "aaaa", "1" );
  108. SetEnvironment( &env, "aaaa", "" );
  109. SetEnvironment( &env, "aaaa", NULL );
  110. SetEnvironment( &env, "AAAA", "12345" );
  111. SetEnvironment( &env, "AAAA", "1234567890" );
  112. SetEnvironment( &env, "AAAA", "1" );
  113. SetEnvironment( &env, "AAAA", "" );
  114. SetEnvironment( &env, "AAAA", NULL );
  115. SetEnvironment( &env, "MMMM", "12345" );
  116. SetEnvironment( &env, "MMMM", "1234567890" );
  117. SetEnvironment( &env, "MMMM", "1" );
  118. SetEnvironment( &env, "MMMM", "" );
  119. SetEnvironment( &env, "MMMM", NULL );
  120. SetEnvironment( &env, "ZZZZ", "12345" );
  121. SetEnvironment( &env, "ZZZZ", "1234567890" );
  122. SetEnvironment( &env, "ZZZZ", "1" );
  123. SetEnvironment( &env, "ZZZZ", "" );
  124. SetEnvironment( &env, "ZZZZ", NULL );
  125. SetEnvironment( &env, "BIGBUF", bigbuf );
  126. SetEnvironment( &env, "BIGBUF", NULL );
  127. // Second, check with non-process environment
  128. DumpEnvironment(nenv);
  129. SetEnvironment( &nenv, "aaaa", "12345" );
  130. SetEnvironment( &nenv, "aaaa", "1234567890" );
  131. SetEnvironment( &nenv, "aaaa", "1" );
  132. SetEnvironment( &nenv, "aaaa", "" );
  133. SetEnvironment( &nenv, "aaaa", NULL );
  134. SetEnvironment( &nenv, "AAAA", "12345" );
  135. SetEnvironment( &nenv, "AAAA", "1234567890" );
  136. SetEnvironment( &nenv, "AAAA", "1" );
  137. SetEnvironment( &nenv, "AAAA", "" );
  138. SetEnvironment( &nenv, "AAAA", NULL );
  139. SetEnvironment( &nenv, "MMMM", "12345" );
  140. SetEnvironment( &nenv, "MMMM", "1234567890" );
  141. SetEnvironment( &nenv, "MMMM", "1" );
  142. SetEnvironment( &nenv, "MMMM", "" );
  143. SetEnvironment( &nenv, "MMMM", NULL );
  144. SetEnvironment( &nenv, "ZZZZ", "12345" );
  145. SetEnvironment( &nenv, "ZZZZ", "1234567890" );
  146. SetEnvironment( &nenv, "ZZZZ", "1" );
  147. SetEnvironment( &nenv, "ZZZZ", "" );
  148. SetEnvironment( &nenv, "ZZZZ", NULL );
  149. SetEnvironment( &nenv, "BIGBUF", bigbuf );
  150. SetEnvironment( &nenv, "BIGBUF", NULL );
  151. return( 0 );
  152. }