Back to index

lightning-sunbird  0.9+nobinonly
env.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File:        env.c
00040 ** Description: Testing environment variable operations
00041 **
00042 */
00043 #include "prenv.h"
00044 #include "plgetopt.h"
00045 
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049 
00050 PRIntn  debug = 0;
00051 PRIntn  verbose = 0;
00052 PRBool  failedAlready = PR_FALSE;
00053 
00054 #define  ENVNAME    "NSPR_ENVIRONMENT_TEST_VARIABLE"
00055 #define  ENVVALUE   "The expected result"
00056 #define  ENVBUFSIZE 256
00057 
00058 char    *envBuf; /* buffer pointer. We leak memory here on purpose! */
00059 
00060 static char * NewBuffer( size_t size )
00061 {
00062     char *buf = malloc( size );
00063     if ( NULL == buf ) {
00064         printf("env: NewBuffer() failed\n");
00065         exit(1);
00066     }
00067     return(buf);
00068 } /* end NewBuffer() */
00069 
00070 PRIntn main(PRIntn argc, char *argv[])
00071 {
00072     char    *value;
00073     PRStatus    rc;
00074 
00075     {   /* Get command line options */
00076         PLOptStatus os;
00077         PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
00078 
00079            while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00080         {
00081                   if (PL_OPT_BAD == os) continue;
00082             switch (opt->option)
00083             {
00084             case 'd':  /* debug */
00085                 debug = 1;
00086                 break;
00087             case 'v':  /* verbose */
00088                 verbose = 1;
00089                 break;
00090              default:
00091                 break;
00092             }
00093         }
00094            PL_DestroyOptState(opt);
00095     } /* end block "Get command line options" */
00096 
00097 #if 0 
00098     {
00099         /*
00100         ** This uses Windows native environment manipulation
00101         ** as an experiment. Note the separation of namespace!
00102         */
00103         BOOL rv;
00104         DWORD   size;
00105         rv = SetEnvironmentVariable( ENVNAME, ENVVALUE );
00106         if ( rv == 0 )  {
00107             if (debug) printf("env: Shit! SetEnvironmentVariable() failed\n");
00108             failedAlready = PR_TRUE;
00109         }
00110         if (verbose) printf("env: SetEnvironmentVariable() worked\n");
00111 
00112         size = GetEnvironmentVariable( ENVNAME, envBuf, ENVBUFSIZE );    
00113         if ( size == 0 )  {
00114             if (debug) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf );
00115             failedAlready = PR_TRUE;
00116         }
00117         if (verbose) printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf);
00118 
00119         value = PR_GetEnv( ENVNAME );
00120         if ( (NULL == value ) || (strcmp( value, ENVVALUE)))  {
00121             if (debug) printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value);
00122             failedAlready = PR_TRUE;
00123         }
00124         if (verbose) printf("env: PR_GetEnv() worked. Found: %s\n", value);
00125     }
00126 #endif
00127 
00128     /* set an environment variable, read it back */
00129     envBuf = NewBuffer( ENVBUFSIZE );
00130     sprintf( envBuf, ENVNAME "=" ENVVALUE );
00131     rc = PR_SetEnv( envBuf );
00132     if ( PR_FAILURE == rc )  {
00133         if (debug) printf( "env: PR_SetEnv() failed setting\n");
00134         failedAlready = PR_TRUE;
00135     } else {
00136         if (verbose) printf("env: PR_SetEnv() worked.\n");
00137     }
00138 
00139     value = PR_GetEnv( ENVNAME );
00140     if ( (NULL == value ) || (strcmp( value, ENVVALUE)))  {
00141         if (debug) printf( "env: PR_GetEnv() Failed after setting\n" );
00142         failedAlready = PR_TRUE;
00143     } else {
00144         if (verbose) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value );
00145     }
00146 
00147 /* ---------------------------------------------------------------------- */
00148     /* un-set the variable, using RAW name... should not work */
00149     envBuf = NewBuffer( ENVBUFSIZE );
00150     sprintf( envBuf, ENVNAME );
00151     rc = PR_SetEnv( envBuf );
00152     if ( PR_FAILURE == rc )  {
00153         if (verbose) printf( "env: PR_SetEnv() not un-set using RAW name. Good!\n");
00154     } else {
00155         if (debug) printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" );
00156         failedAlready = PR_TRUE;
00157     }
00158 
00159     value = PR_GetEnv( ENVNAME );
00160     if ( NULL == value ) {
00161         if (debug) printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" );
00162         failedAlready = PR_TRUE;
00163     } else {
00164         if (verbose) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value );
00165     }
00166     
00167 /* ---------------------------------------------------------------------- */
00168     /* set it again ... */
00169     envBuf = NewBuffer( ENVBUFSIZE );
00170     sprintf( envBuf, ENVNAME "=" ENVVALUE );
00171     rc = PR_SetEnv( envBuf );
00172     if ( PR_FAILURE == rc )  {
00173         if (debug) printf( "env: PR_SetEnv() failed setting the second time.\n");
00174         failedAlready = PR_TRUE;
00175     } else {
00176         if (verbose) printf("env: PR_SetEnv() worked.\n");
00177     }
00178 
00179     /* un-set the variable using the form name= */
00180     envBuf = NewBuffer( ENVBUFSIZE );
00181     sprintf( envBuf, ENVNAME "=" );
00182     rc = PR_SetEnv( envBuf );
00183     if ( PR_FAILURE == rc )  {
00184         if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
00185         failedAlready = PR_TRUE;
00186     } else {
00187         if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
00188     }
00189 
00190     value = PR_GetEnv( ENVNAME );
00191     if (( NULL == value ) || ( 0x00 == *value )) {
00192         if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
00193     } else {
00194         if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
00195         failedAlready = PR_TRUE;
00196     }
00197 /* ---------------------------------------------------------------------- */
00198     /* un-set the variable using the form name= */
00199     envBuf = NewBuffer( ENVBUFSIZE );
00200     sprintf( envBuf, ENVNAME "999=" );
00201     rc = PR_SetEnv( envBuf );
00202     if ( PR_FAILURE == rc )  {
00203         if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
00204         failedAlready = PR_TRUE;
00205     } else {
00206         if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
00207     }
00208 
00209     value = PR_GetEnv( ENVNAME "999" );
00210     if (( NULL == value ) || ( 0x00 == *value )) {
00211         if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
00212     } else {
00213         if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
00214         failedAlready = PR_TRUE;
00215     }
00216 
00217 /* ---------------------------------------------------------------------- */
00218     if (debug || verbose) printf("\n%s\n", (failedAlready)? "FAILED" : "PASSED" );
00219     return( (failedAlready)? 1 : 0 );
00220 }  /* main() */
00221 
00222 /* env.c */