test: tenv.c Source File

Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

# tenv.c

Go to the documentation of this file.

    00001 /*++
    00002
    00003 Copyright (c) 1989  Microsoft Corporation
    00004
    00005 Module Name:
    00006
    00007     tenv.c
    00008
    00009 Abstract:
    00010
    00011     Test program for the NT OS Runtime Library (RTL) Environment API Calls
    00012
    00013 Author:
    00014
    00015     Steve Wood (stevewo) 30-Jan-1991
    00016
    00017 Revision History:
    00018
    00019 --*/
    00020
    00021 #include <nt.h>
    00022 #include <ntrtl.h>
    00023 #include <nturtl.h>
    00024 #include <stdio.h>
    00025
    00026 VOID
    00027 DumpEnvironment( PVOID env )
    00028 {
    00029     PWCHAR s = env;
    00030
    00031     while (*s) {
    00032         printf( "%79.79ws\n", s );
    00033         while (*s++) {
    00034             }
    00035         }
    00036 }
    00037
    00038 VOID
    00039 SetEnvironment(
    00040     PVOID *env,
    00041     PCHAR Name,
    00042     PCHAR Value
    00043     );
    00044
    00045 VOID
    00046 SetEnvironment(
    00047     PVOID *env,
    00048     PCHAR Name,
    00049     PCHAR Value
    00050     )
    00051 {
    00052     NTSTATUS Status;
    00053     STRING NameString, ValueString;
    00054     UNICODE_STRING uNameString, uValueString;
    00055
    00056     RtlInitString( &NameString, Name );
    00057     Status = RtlAnsiStringToUnicodeString(&uNameString, &NameString, TRUE);
    00058     if (!NT_SUCCESS( Status )) {
    00059         printf( " - failed converting to Unicode, Status == %X\n", Status );
    00060         DumpEnvironment(*env);
    00061         printf( "\n" );
    00062         return;
    00063     }
    00064     if (Value != NULL) {
    00065         RtlInitString( &ValueString, Value );
    00066         Status = RtlAnsiStringToUnicodeString(&uValueString, &ValueString, TRUE);
    00067         printf( "TENV: set variable (%X) %Z=%Z\n", *env, &NameString, &ValueString );
    00068         Status = RtlSetEnvironmentVariable( env, &uNameString, &uValueString );
    00069         printf( "TENV: (%X)", *env);
    00070         RtlFreeUnicodeString(&uNameString);
    00071         RtlFreeUnicodeString(&uValueString);
    00072         }
    00073     else {
    00074         printf( "TENV: delete variable (%X) %Z\n", *env, &NameString );
    00075         Status = RtlSetEnvironmentVariable( env, &uNameString, NULL );
    00076         printf( "TENV: (%X)", *env, &NameString, &ValueString );
    00077         RtlFreeUnicodeString(&uNameString);
    00078         }
    00079
    00080     if (NT_SUCCESS( Status )) {
    00081         printf( "\n" );
    00082         }
    00083     else {
    00084         printf( " - failed, Status == %X\n", Status );
    00085         }
    00086     DumpEnvironment(*env);
    00087     printf( "\n" );
    00088 }
    00089
    00090
    00091 int
    00092 _cdecl
    00093 main(
    00094     int argc,
    00095     char **argv,
    00096     char **envp
    00097     )
    00098 {
    00099     int i;
    00100     PVOID env;
    00101     PVOID nenv;
    00102     NTSTATUS Status;
    00103     char bigbuf[4100];
    00104
    00105     for (i=0; i<argc; i++) {
    00106         printf( "argv[ %d ] = %s\n", i, argv[ i ] );
    00107         }
    00108
    00109     i = 0;
    00110     while (envp[ i ]) {
    00111         printf( "envp[ %d ] = %s\n", i, envp[ i ] );
    00112         i++;
    00113         }
    00114
    00115     for (i=0 ; i<4099 ; i++)
    00116         bigbuf[i] = (i%26) + (((i&1) == 0) ? 'a' : 'A');
    00117     bigbuf[4099] = '\0';
    00118
    00119     env = NtCurrentPeb()->ProcessParameters->Environment;
    00120     Status = RtlCreateEnvironment(TRUE, &nenv); // clone current
    00121     if (!NT_SUCCESS( Status )) {
    00122         printf( "Unable to create clone environment - %X\n", Status );
    00123         return 1;
    00124     }
    00125
    00126     // First, check with process environment
    00127     DumpEnvironment( &env);
    00128     SetEnvironment( &env, "aaaa", "12345" );
    00129     SetEnvironment( &env, "aaaa", "1234567890" );
    00130     SetEnvironment( &env, "aaaa", "1" );
    00131     SetEnvironment( &env, "aaaa", "" );
    00132     SetEnvironment( &env, "aaaa", NULL );
    00133     SetEnvironment( &env, "AAAA", "12345" );
    00134     SetEnvironment( &env, "AAAA", "1234567890" );
    00135     SetEnvironment( &env, "AAAA", "1" );
    00136     SetEnvironment( &env, "AAAA", "" );
    00137     SetEnvironment( &env, "AAAA", NULL );
    00138     SetEnvironment( &env, "MMMM", "12345" );
    00139     SetEnvironment( &env, "MMMM", "1234567890" );
    00140     SetEnvironment( &env, "MMMM", "1" );
    00141     SetEnvironment( &env, "MMMM", "" );
    00142     SetEnvironment( &env, "MMMM", NULL );
    00143     SetEnvironment( &env, "ZZZZ", "12345" );
    00144     SetEnvironment( &env, "ZZZZ", "1234567890" );
    00145     SetEnvironment( &env, "ZZZZ", "1" );
    00146     SetEnvironment( &env, "ZZZZ", "" );
    00147     SetEnvironment( &env, "ZZZZ", NULL );
    00148     SetEnvironment( &env, "BIGBUF", bigbuf );
    00149     SetEnvironment( &env, "BIGBUF", NULL );
    00150
    00151     // Second, check with non-process environment
    00152     DumpEnvironment(nenv);
    00153     SetEnvironment( &nenv, "aaaa", "12345" );
    00154     SetEnvironment( &nenv, "aaaa", "1234567890" );
    00155     SetEnvironment( &nenv, "aaaa", "1" );
    00156     SetEnvironment( &nenv, "aaaa", "" );
    00157     SetEnvironment( &nenv, "aaaa", NULL );
    00158     SetEnvironment( &nenv, "AAAA", "12345" );
    00159     SetEnvironment( &nenv, "AAAA", "1234567890" );
    00160     SetEnvironment( &nenv, "AAAA", "1" );
    00161     SetEnvironment( &nenv, "AAAA", "" );
    00162     SetEnvironment( &nenv, "AAAA", NULL );
    00163     SetEnvironment( &nenv, "MMMM", "12345" );
    00164     SetEnvironment( &nenv, "MMMM", "1234567890" );
    00165     SetEnvironment( &nenv, "MMMM", "1" );
    00166     SetEnvironment( &nenv, "MMMM", "" );
    00167     SetEnvironment( &nenv, "MMMM", NULL );
    00168     SetEnvironment( &nenv, "ZZZZ", "12345" );
    00169     SetEnvironment( &nenv, "ZZZZ", "1234567890" );
    00170     SetEnvironment( &nenv, "ZZZZ", "1" );
    00171     SetEnvironment( &nenv, "ZZZZ", "" );
    00172     SetEnvironment( &nenv, "ZZZZ", NULL );
    00173     SetEnvironment( &nenv, "BIGBUF", bigbuf );
    00174     SetEnvironment( &nenv, "BIGBUF", NULL );
    00175     return( 0 );
    00176 }