home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / SETENV.ZIP / SETENV.C next >
C/C++ Source or Header  |  1991-09-02  |  17KB  |  725 lines

  1. /* Includes */
  2. #include <dos.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <direct.h>
  8. #include <conio.h>
  9.  
  10. /* Defines */
  11. #define VERSION         1
  12. #define RELEASE         0
  13. #define MODIFICATION    0
  14.  
  15. #define PRESERVE        0
  16. #define UPPER           1
  17. #define LOWER           2
  18.  
  19. #define TRUE            1
  20. #define FALSE           0
  21.  
  22. #define ESC             27
  23.  
  24. /* Function Prototypes */
  25.     void GetString( char * pszPrompt, int iLength, char * pszDefault, int iEcho );
  26.     void InputString( char * pszBuffer, int iLength, int iEcho );
  27.     void ParseFilespec( char * pszFilespec );
  28.     char * SubString( char * pszString, int iStart, int iLength );
  29.     void GetOSVersion( void );
  30.     void GetNextDrive( void );
  31.     void EditPath( char * pszDirectories );
  32.     int AddPath( char * pszDirectoryPtr );
  33.     int DelPath( char * pszDirectoryPtr );
  34.     void GetTimeAndDate( int iFormat );
  35.     void OpenFile( char * pszFileSpec );
  36.     void WriteLine( char * pszLine );
  37.     void CloseFile( void );
  38.     void DeleteFile( void );
  39.     void Help( void );
  40.     void Quit( int iRC );
  41.  
  42. /* Globals */
  43.     int iCase;
  44.     char szPath[ _MAX_PATH + 6 ];
  45.     FILE * pOutputFile;
  46.     char * pszOutputFileSpec;
  47.  
  48. void main( int argc, char *argv[] )
  49.     {
  50.          int iStart;
  51.          int iLength;
  52.          char szBuffer[ 512 ];
  53.  
  54.          if ( argc > 1 )
  55.              {
  56.                  if ( *argv[ 1 ] == '?' )
  57.                     {
  58.                         Help();
  59.                     }
  60.              }
  61.  
  62.          /*
  63.             Check for Switches--they must be the last parameters.
  64.          */
  65.          iCase = PRESERVE;
  66.          for ( iStart = 0; iStart < 2; iStart++ )
  67.             {
  68.                 if ( ( *argv[ argc - 1 ] == '/' ) &&
  69.                      ( 1 < strlen( argv[ argc - 1 ] ) ) )
  70.                     {
  71.                         /*
  72.                             "Remove" parameter from arg list.
  73.                         */
  74.                         argc--;
  75.                         switch ( toupper( *( argv[argc] + 1 ) ) )
  76.                             {
  77.                                 case 'U':
  78.                                     {
  79.                                          iCase = UPPER;
  80.                                          break;
  81.                                     }
  82.  
  83.                                 case 'L':
  84.                                     {
  85.                                          iCase = LOWER;
  86.                                          break;
  87.                                     }
  88.  
  89.                                 case 'P':
  90.                                     {
  91.                                         iCase = PRESERVE;
  92.                                         break;
  93.                                     }
  94.  
  95.                                 default:
  96.                                     {
  97.                                         printf( "SETENV: Invalid parameter '%s'\n", argv[argc] );
  98.                                         Quit( 2 );
  99.                                         break;
  100.                                     }
  101.                             }
  102.                     }
  103.             }
  104.          /*
  105.             Call the appropriate function.
  106.          */
  107.          switch( toupper( *argv[ 1 ] ) )
  108.             {
  109.                  case 'E':
  110.                     {
  111.                          if ( argc < 2 )
  112.                             {
  113.                                 Help();
  114.                             }
  115.                          EditPath( argv[ 2 ] );
  116.                          break;
  117.                     }
  118.  
  119.                  case 'F':
  120.                     {
  121.                          if ( argc < 3 )
  122.                             {
  123.                                 Help();
  124.                             }
  125.                          ParseFilespec( argv[ 2 ] );
  126.                          break;
  127.                     }
  128.  
  129.                  case 'G':
  130.                     {
  131.                          GetNextDrive();
  132.                          break;
  133.                     }
  134.  
  135.                  case 'P':
  136.                     {
  137.                          if ( argc < 4 ) Help();
  138.                          iLength = atoi( argv[3] );
  139.                          if ( argc > 4 )
  140.                             {
  141.                                 GetString( argv[2], iLength, argv[ 4 ], 1 );
  142.                             }
  143.                          else
  144.                             {
  145.                                 GetString( argv[2], iLength, "", 1 );
  146.                             }
  147.                          break;
  148.                     }
  149.  
  150.                  case 'Q':
  151.                     {
  152.                          if ( argc < 4 ) Help();
  153.                          iLength = atoi( argv[3] );
  154.                          if ( argc > 4 )
  155.                             {
  156.                                 GetString( argv[2], iLength, argv[ 4 ], 0 );
  157.                             }
  158.                          else
  159.                             {
  160.                                 GetString( argv[2], iLength, "", 0 );
  161.                             }
  162.                          break;
  163.                     }
  164.  
  165.                  case 'S':
  166.                     {
  167.                          if ( argc < 4 )
  168.                             {
  169.                                 Help();
  170.                             }
  171.                          iStart = atoi( argv[ 3 ] );
  172.                          if ( iStart == 0 )
  173.                             {
  174.                                  puts( "SETENV: Invalid substring starting position." );
  175.                                  Quit( 1 );
  176.                             }
  177.                          iLength = atoi( argv[ 4 ] );
  178.                          if ( iLength == 0 )
  179.                             {
  180.                                  puts( "SETENV: Invalid substring length." );
  181.                                  Quit( 1 );
  182.                             }
  183.                          SubString( argv[ 2 ], iStart, iLength );
  184.                          break;
  185.                     }
  186.  
  187.                  case 'T':
  188.                     {
  189.                          if ( argc < 3 )
  190.                             {
  191.                                 Help();
  192.                             }
  193.                          iStart = atoi( argv[ 2 ] );
  194.                          GetTimeAndDate( iStart );
  195.                          break;
  196.                     }
  197.  
  198.                  case 'V':
  199.                     {
  200.                          GetOSVersion();
  201.                          break;
  202.                     }
  203.  
  204.                  default:
  205.                     {
  206.                          Help();
  207.                          break;
  208.                     }
  209.             }
  210.  
  211.         Quit( 0 );
  212.     }
  213.  
  214.  
  215. void GetString( char * pszPrompt, int iLength, char * pszDefault, int iEcho )
  216.     {
  217.         char * pszBuffer;
  218.         char * pszString;
  219.         int iRC;
  220.  
  221.         /* Allocate a buffer of length bytes and add bytes for "STRING=", and */
  222.         /* the null terminator. */
  223.         pszString = (char *)malloc( iLength + 7 + 1 );
  224.         if ( pszString == NULL )
  225.            {
  226.                puts( "SETENV: Couldn't allocate Buffer." );
  227.                WriteLine( "Set STRING=" );
  228.                Quit( 3 );
  229.            }
  230.  
  231.         pszBuffer = (char *)malloc( iLength );
  232.         if ( pszBuffer == NULL )
  233.             {
  234.                 free( pszString );
  235.                 puts( "SETENV: Couldn't allocate Buffer." );
  236.                 WriteLine( "Set STRING=" );
  237.                 Quit( 3 );
  238.             }
  239.  
  240.         cprintf( "%s", pszPrompt );
  241.         InputString( pszBuffer, iLength, iEcho );
  242.         if ( strlen ( pszBuffer ) == 0 )
  243.             {
  244.                 sprintf( pszString, "Set STRING=%s", pszDefault );
  245.             }
  246.         else
  247.             {
  248.                 sprintf( pszString, "Set STRING=%s",  pszBuffer );
  249.             }
  250.  
  251.         WriteLine( pszString );
  252.         free( pszBuffer );
  253.         free( pszString );
  254.         Quit( 0 );
  255.     }
  256.  
  257. void InputString( char * pszBuffer, int iLength, int iEcho )
  258.     {
  259.         int iI = 0;
  260.         char cChar;
  261.  
  262.         while( 1 )
  263.             {
  264.                 cChar = getch();
  265.  
  266.                 switch( cChar )
  267.                     {
  268.                         case '\r':
  269.                             {
  270.                                 *( pszBuffer + iI ) = '\0';
  271.                                 putchar( '\n' );
  272.                                 return;
  273.                             }
  274.  
  275.                         case '\b':
  276.                             {
  277.                                 if ( iI > 0 )
  278.                                     {
  279.                                         iI--;
  280.                                         printf( "\b \b" );
  281.                                     }
  282.  
  283.                                 break;
  284.                             }
  285.  
  286.                         case ESC:
  287.                             {
  288.                                 for ( ; iI > 0; iI-- )
  289.                                     {
  290.                                         printf( "\b \b" );
  291.                                     }
  292.                                 break;
  293.                             }
  294.  
  295.                         default:
  296.                             {
  297.                                 if ( iI < iLength )
  298.                                     {
  299.                                         *( pszBuffer + iI ) = cChar;
  300.                                         iI++;
  301.                                         if ( iEcho )
  302.                                             {
  303.                                                 putchar( cChar );
  304.                                             }
  305.                                         else
  306.                                             {
  307.                                                 putchar( '*' );
  308.                                             }
  309.                                     }
  310.                                 else
  311.                                     {
  312.                                         printf( "\a" );
  313.                                     }
  314.  
  315.                                 break;
  316.                             }
  317.                     }
  318.  
  319.             }
  320.     }
  321.  
  322. void ParseFilespec( char * pszFilespec )
  323.     {
  324.         char szDrive[ _MAX_DRIVE ];
  325.         char szDir[ _MAX_DIR ];
  326.         char szFname[ _MAX_FNAME ];
  327.         char szExt[ _MAX_FNAME ];
  328.         char szBuffer[ _MAX_PATH + 16 ];
  329.         int i;
  330.  
  331.         _splitpath( pszFilespec, szDrive, szDir, szFname, szExt );
  332.  
  333.         /*
  334.             Strip the ':' off of the drive
  335.         */
  336.         if ( ':' == szDrive[ strlen( szDrive ) - 1 ] )
  337.             {
  338.                 szDrive[ strlen( szDrive ) - 1 ] = 0;
  339.             }
  340.  
  341.         /* Strip the ending '\' off of the directory */
  342.         if ( '\\' == szDir[ strlen( szDir ) - 1 ] )
  343.             {
  344.               szDir[ strlen( szDir ) - 1 ] = 0;
  345.             }
  346.  
  347.         /* Strip the '.' off of the file extension */
  348.         if ( '.' == szExt[ 0 ] )
  349.             {
  350.                 memmove( &szExt[ 1 ], szExt, ( strlen( szExt ) - 1 ) );
  351.             }
  352.  
  353.         strcpy( szBuffer, "Set DRIVE=" );
  354.         strcat( szBuffer, szDrive );
  355.         WriteLine( szBuffer );
  356.  
  357.         strcpy( szBuffer, "Set DIR=" );
  358.         strcat( szBuffer, szDir );
  359.         WriteLine( szBuffer );
  360.  
  361.         strcpy( szBuffer, "Set FNAME=" );
  362.         strcat( szBuffer, szFname );
  363.         WriteLine( szBuffer );
  364.  
  365.         strcpy( szBuffer, "Set EXT=" );
  366.         strcat( szBuffer, szExt );
  367.         WriteLine( szBuffer );
  368.     }
  369.  
  370. char * SubString( char * pszString, int iStart, int iLength )
  371.     {
  372.         char szBuffer[ 256 ];
  373.  
  374.         if ( iStart > strlen( pszString ) )
  375.             {
  376.                 puts( "SETENV: Starting position is greater than string's length." );
  377.                 Quit( 1 );
  378.             }
  379.  
  380.         strcpy( szBuffer, "Set STRING=" );
  381.         strncat( szBuffer, ( pszString + ( iStart - 1 ) ), iLength );
  382.         szBuffer[ iLength + 7] = '\0';
  383.         WriteLine( szBuffer );
  384.         Quit( 0 );
  385.     }
  386.  
  387.  
  388. void GetOSVersion( void )
  389.     {
  390.         char szBuffer[ 512 ];
  391.         char szValue[ 16 ];
  392.  
  393.  
  394.         strcpy( szBuffer, "Set OSVer=" );
  395.         itoa( _osmajor, szValue, 10 );
  396.         strcat( szBuffer, szValue );
  397.         WriteLine( szBuffer );
  398.  
  399.         strcpy( szBuffer, "Set OSRel=" );
  400.         itoa( ( _osminor / 10 ), szValue, 10 );
  401.         strcat( szBuffer, szValue );
  402.         WriteLine( szBuffer );
  403.  
  404.         strcpy( szBuffer, "Set OSMod=" );
  405.         itoa( ( _osminor % 10 ), szValue, 10 );
  406.         strcat( szBuffer, szValue );
  407.         WriteLine( szBuffer );
  408.         Quit( 0 );
  409.     }
  410.  
  411.  
  412. void EditPath( char * pszDirectories )
  413.     {
  414.         char szBuffer[ _MAX_PATH + 2 ];
  415.         char * pszDirEnd;
  416.         char * pszPath;
  417.         int iDirLength;
  418.  
  419.          /*
  420.             Initialize the global path variable.
  421.          */
  422.          pszPath = getenv( "PATH" );
  423.          if ( pszPath != NULL )
  424.             {
  425.                 strcpy( szPath, pszPath );
  426.             }
  427.          else
  428.             {
  429.                 strcpy( szPath, "" );
  430.             }
  431.  
  432.         pszDirEnd = pszDirectories + strlen( pszDirectories );
  433.         do
  434.             {
  435.                 iDirLength = strcspn( pszDirectories, ";" );
  436.                 strncpy( szBuffer, pszDirectories, iDirLength );
  437.                 szBuffer[ iDirLength ] = 0;
  438.                 switch( szBuffer[ 0 ] )
  439.                     {
  440.                         case '+':
  441.                             {
  442.                                 AddPath( &szBuffer[ 1 ] );
  443.                                 break;
  444.                             }
  445.  
  446.                         case '-':
  447.                             {
  448.                                 DelPath( &szBuffer[ 1 ] );
  449.                                 break;
  450.                             }
  451.  
  452.                         default:
  453.                             {
  454.                                 printf( "SETENV: No '+' or '-' specified with %s.\n", szBuffer );
  455.                                 break;
  456.                             }
  457.                     }
  458.                 pszDirectories = pszDirectories + iDirLength + 1;
  459.             } while( pszDirectories < pszDirEnd );
  460.  
  461.         if ( _osmode == DOS_MODE )
  462.             {
  463.             if ( 123 < strlen( szPath ) )
  464.                 {
  465.                 printf( "SETENV: New PATH would exceed 128 characters!" );
  466.                 Quit( 1 );
  467.                 }
  468.             }
  469.         else
  470.             {
  471.                 strcpy( szBuffer, "PATH=" );
  472.                 strcat( szBuffer, szPath );
  473.                 WriteLine( szBuffer );
  474.                 Quit( 0 );
  475.             }
  476.     }
  477.  
  478. int AddPath( char * pszDirectory )
  479.     {
  480.         char szNewPath[ _MAX_PATH + 1 ];
  481.         char * pszPath;
  482.  
  483.          /*
  484.             If there's not a ';' at the end of the PATH, add one, we'll
  485.             need it to use strtok during the search.
  486.          */
  487.          if ( 0 < ( strlen( szPath ) ) )
  488.             {
  489.                 if ( ';' != szPath[ strlen( szPath ) - 1 ] )
  490.                     {
  491.                         strcat( szPath, ";" );
  492.                     }
  493.             }
  494.  
  495.          /*
  496.             Set up the new path by putting the new directory first in line.
  497.          */
  498.          strcpy( szNewPath, pszDirectory );
  499.  
  500.          /*
  501.             If there's a semi-colon at the end of the new directory, we have
  502.             to remove it for the compare...
  503.          */
  504.          if ( ';' == *( pszDirectory + strlen( pszDirectory ) - 1 ) )
  505.              {
  506.                 *( pszDirectory + strlen( pszDirectory ) - 1 ) = '\0';
  507.              }
  508.          /*
  509.             ...otherwise, add a semi-colon to the end of the new path.
  510.          */
  511.          else
  512.              {
  513.                 strcat( szNewPath, ";" );
  514.              }
  515.  
  516.          pszPath = strtok( szPath, ";" );
  517.          while( pszPath != NULL )
  518.              {
  519.                  /*
  520.                     If the directory isn't already in the path, add it.
  521.                  */
  522.                  if ( 0 != strcmpi( pszPath, pszDirectory ) )
  523.                      {
  524.                          strcat( szNewPath, pszPath );
  525.                          strcat( szNewPath, ";" );
  526.                      }
  527.                  pszPath = strtok( NULL, ";" );
  528.              }
  529.  
  530.          strcpy( szPath, szNewPath );
  531.          return( 0 );
  532.     }
  533.  
  534. int DelPath( char * pszDirectory )
  535.     {
  536.         char szNewPath[ _MAX_PATH + 1 ] = "";
  537.         char * pszPath;
  538.  
  539.         if ( 0 == strlen( szPath ) )
  540.             {
  541.                 return( 0 );
  542.             }
  543.  
  544.         /*
  545.            If there's not a ';' at the end of the PATH, add one, we'll
  546.            need it to use strtok.
  547.         */
  548.         if ( ';' != szPath[ strlen( szPath ) - 1 ] )
  549.             {
  550.                strcat( szPath, ";" );
  551.             }
  552.  
  553.         /*
  554.            Remove the ';' at the end of the specified directory, strtok returns
  555.            tokens without the delimeters.
  556.         */
  557.         if ( ';' == *( pszDirectory + strlen( pszDirectory ) - 1 ) )
  558.             {
  559.                *( pszDirectory + strlen( pszDirectory ) - 1 ) = '\0';
  560.             }
  561.  
  562.         pszPath = strtok( szPath, ";" );
  563.         while( pszPath != NULL )
  564.             {
  565.                 /*
  566.                    If the directory doesn't match, add it.
  567.                 */
  568.                 if ( 0 != strcmpi( pszPath, pszDirectory ) )
  569.                    {
  570.                         strcat( szNewPath, pszPath );
  571.                         strcat( szNewPath, ";" );
  572.                    }
  573.                 pszPath = strtok( NULL, ";" );
  574.             }
  575.  
  576.         strcpy( szPath, szNewPath );
  577.         return( 0 );
  578.     }
  579.  
  580.  
  581. void GetTimeAndDate( int iFormat )
  582.     {
  583.         char szTime[] = "TIME=hh:mm:ss ";
  584.         char szDate[] = "DATE=mm/dd/yy";
  585.  
  586.         _strtime( &szTime[5] );
  587.         if( iFormat == 12 )
  588.            {
  589.                 iFormat = atoi( &szTime[5] );
  590.  
  591.                 if ( iFormat > 12 )
  592.                    {
  593.                         iFormat -= 12;
  594.                         szTime[ 13 ] = 'p';
  595.                    }
  596.                 else
  597.                    {
  598.                         szTime[ 13 ] = 'a';
  599.                    }
  600.                 sprintf( &szTime[5], "%02i", iFormat );
  601.                 szTime[7] = ':';
  602.            }
  603.         else
  604.             {
  605.                 szTime[13] = 0x0;
  606.             }
  607.  
  608.         _strdate( &szDate[5] );
  609.         WriteLine( szTime );
  610.         WriteLine( szDate );
  611.         Quit( 0 );
  612.     }
  613.  
  614.  
  615. void GetNextDrive( void )
  616.     {
  617.         int iNextDrive,
  618.             iCurrentDrive;
  619.  
  620.         char szNextDrive[] = "Set NEXTDRIVE=A";
  621.  
  622.         iCurrentDrive =_getdrive();
  623.  
  624.         for ( iNextDrive = 1; iNextDrive <= 26; iNextDrive++ )
  625.             {
  626.                 if (  0 != _chdrive( iNextDrive ) )
  627.                     {
  628.                         iNextDrive = 'A' + ( iNextDrive - 1 );
  629.                         _chdrive( iCurrentDrive );
  630.                         sprintf( szNextDrive, "Set NEXTDRIVE=%c", iNextDrive );
  631.                         WriteLine( szNextDrive );
  632.                         Quit( 0 );
  633.                     }
  634.             }
  635.         puts( "SETENV: All drives are in use." );
  636.         _chdrive( iCurrentDrive );
  637.         Quit( 1 );
  638.     }
  639.  
  640. void WriteLine( char * szLine )
  641.     {
  642.         char * pszEqual;
  643.  
  644.         if ( iCase != PRESERVE )
  645.             {
  646.                 pszEqual = strchr( szLine, '=' );
  647.                 pszEqual++;
  648.                 switch ( iCase )
  649.                     {
  650.                         case UPPER:
  651.                             {
  652.                                 strupr( pszEqual );
  653.                                 break;
  654.                             }
  655.  
  656.                         case LOWER:
  657.                             {
  658.                                 strlwr( pszEqual );
  659.                                 break;
  660.                             }
  661.  
  662.                         default:
  663.                             {
  664.                                break;
  665.                             }
  666.                     }
  667.             }
  668.         printf( "%s\n", szLine );
  669.     }
  670.  
  671. void Quit( int iRC )
  672.     {
  673.         exit( iRC );
  674.     }
  675.  
  676. void Help( void )
  677.     {
  678.         printf( "SETENV %d.%d%d by MJ Winkler, %s, %s.\n",
  679.                   VERSION, RELEASE, MODIFICATION, __DATE__, __TIME__ );
  680.         puts( "SETENV will write a series of SET statements to standard output" );
  681.         puts( "that will create or modify environment variables." );
  682.         puts( "Syntax is: SETENV command [argument(s)] [/U | /L | /P] [/M]" );
  683.         puts( "    Where: filename = the name of the file to create." );
  684.         puts( "           command = the SETENV command (see list below).");
  685.         puts( "           argument(s) = the arguments, if any, to the command." );
  686.         puts( "" );
  687.         puts( "           /U = Make returned environment strings uppercase." );
  688.         puts( "           /L = Make returned environment strings lowercase." );
  689.         puts( "           /P = Preserve case (default).\n" );
  690.         puts( "ErrorLevel will be set >= 1 if there is an error.\n" );
  691.         puts( "Cmd Arguments               Description and variable(s) created." );
  692.         puts( "--- ----------------------- --------------------------------------------------" );
  693.         puts( " ?                          Displays this help information." );
  694.         puts( " E  [+ | -]directory;       Edit the PATH variable. Use '+' to insert a" );
  695.         puts( "                            directory, '-' to remove one. Separate directories" );
  696.         puts( "                            with a ';'. You may also list multiple directories" );
  697.         puts( "                            at one time (i.e +C:\\123;-C:\\WP51;)." );
  698.         puts( " F  filespec                Splits a filespec into its component parts." );
  699.         puts( "                              DRIVE=drive letter (no ':')" );
  700.         puts( "                              DIR=directory(s) (no ending '\\')" );
  701.         puts( "                              FNAME=file name" );
  702.         puts( "                              EXT=file extension (no '.')" );
  703.         puts( " G                          Gets the next available drive letter." );
  704.         puts( "                              NEXTDRIVE=drive leter (no ':')" );
  705.         puts( " P  prompt length [default] Prompts for input of specified length and" );
  706.         puts( "                            returns the default if nothing is entered." );
  707.         puts( "                              STRING=string entered or default" );
  708.         puts( " Q  prompt length [default] Just like P but echos *'s instead" );
  709.         puts( "                            the actual characters--great for" );
  710.         puts( "                            passwords." );
  711.         puts( "                              STRING=string entered or default" );
  712.         puts( " S  string start length     Extracts a substring." );
  713.         puts( "                              STRING=requested portion of string" );
  714.         puts( " T  12 or 24                Time (12 or 24 hour format) and Date. If 12 hour" );
  715.         puts( "                            format is selected, 'a' or 'p' will be appended" );
  716.         puts( "                            to TIME." );
  717.         puts( "                              TIME=hh:mm:ss" );
  718.         puts( "                              DATE=mm/dd/yy" );
  719.         puts( " V                          OS version." );
  720.         puts( "                              OSVER=version number" );
  721.         puts( "                              OSMOD=modification number" );
  722.         puts( "                              OSREL=release number" );
  723.         Quit( 1 );
  724.     }
  725.