home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / environ1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-24  |  1.1 KB  |  42 lines

  1. /* ENVIRON.C illustrates environment variable functions including:
  2.  *      getenv          putenv          _searchenv
  3.  */
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. main()
  10. {
  11.     char *pathvar, pathbuf[128], filebuf[128];
  12.  
  13.     /* Get the PATH environment variable and save a copy of it. */
  14.     pathvar = getenv( "PATH" );
  15.     strcpy( pathbuf, pathvar );
  16.     printf( "Old PATH: %s\n", pathvar ? pathvar : "variable not set");
  17.  
  18.     /* Add a new directory to the path. */
  19.     strcat( pathbuf, ";\\QC;" );
  20.     if( putenv( pathbuf ) == -1 )
  21.     {
  22.         printf( "Failed\n");
  23.         return 1;
  24.     }
  25.     else
  26.         printf( "New PATH: %s\n", pathbuf );
  27.  
  28.     /* Search for file in the new path. */
  29.     _searchenv( "QC.INI", "PATH", filebuf );
  30.     if( *filebuf )
  31.         printf( "QC.INI found at %s\n", filebuf );
  32.     else
  33.         printf( "QC.INI not found\n" );
  34.  
  35.     /* Restore original path. */
  36.     if( putenv( pathvar ) == -1 )
  37.         printf( "Failed\n");
  38.     else
  39.         printf( "Old PATH: %s\n", pathvar );
  40.     return 0;
  41. }
  42.