home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
SETENV.ZIP
/
SETENV.C
next >
Wrap
C/C++ Source or Header
|
1991-09-02
|
17KB
|
725 lines
/* Includes */
#include <dos.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <direct.h>
#include <conio.h>
/* Defines */
#define VERSION 1
#define RELEASE 0
#define MODIFICATION 0
#define PRESERVE 0
#define UPPER 1
#define LOWER 2
#define TRUE 1
#define FALSE 0
#define ESC 27
/* Function Prototypes */
void GetString( char * pszPrompt, int iLength, char * pszDefault, int iEcho );
void InputString( char * pszBuffer, int iLength, int iEcho );
void ParseFilespec( char * pszFilespec );
char * SubString( char * pszString, int iStart, int iLength );
void GetOSVersion( void );
void GetNextDrive( void );
void EditPath( char * pszDirectories );
int AddPath( char * pszDirectoryPtr );
int DelPath( char * pszDirectoryPtr );
void GetTimeAndDate( int iFormat );
void OpenFile( char * pszFileSpec );
void WriteLine( char * pszLine );
void CloseFile( void );
void DeleteFile( void );
void Help( void );
void Quit( int iRC );
/* Globals */
int iCase;
char szPath[ _MAX_PATH + 6 ];
FILE * pOutputFile;
char * pszOutputFileSpec;
void main( int argc, char *argv[] )
{
int iStart;
int iLength;
char szBuffer[ 512 ];
if ( argc > 1 )
{
if ( *argv[ 1 ] == '?' )
{
Help();
}
}
/*
Check for Switches--they must be the last parameters.
*/
iCase = PRESERVE;
for ( iStart = 0; iStart < 2; iStart++ )
{
if ( ( *argv[ argc - 1 ] == '/' ) &&
( 1 < strlen( argv[ argc - 1 ] ) ) )
{
/*
"Remove" parameter from arg list.
*/
argc--;
switch ( toupper( *( argv[argc] + 1 ) ) )
{
case 'U':
{
iCase = UPPER;
break;
}
case 'L':
{
iCase = LOWER;
break;
}
case 'P':
{
iCase = PRESERVE;
break;
}
default:
{
printf( "SETENV: Invalid parameter '%s'\n", argv[argc] );
Quit( 2 );
break;
}
}
}
}
/*
Call the appropriate function.
*/
switch( toupper( *argv[ 1 ] ) )
{
case 'E':
{
if ( argc < 2 )
{
Help();
}
EditPath( argv[ 2 ] );
break;
}
case 'F':
{
if ( argc < 3 )
{
Help();
}
ParseFilespec( argv[ 2 ] );
break;
}
case 'G':
{
GetNextDrive();
break;
}
case 'P':
{
if ( argc < 4 ) Help();
iLength = atoi( argv[3] );
if ( argc > 4 )
{
GetString( argv[2], iLength, argv[ 4 ], 1 );
}
else
{
GetString( argv[2], iLength, "", 1 );
}
break;
}
case 'Q':
{
if ( argc < 4 ) Help();
iLength = atoi( argv[3] );
if ( argc > 4 )
{
GetString( argv[2], iLength, argv[ 4 ], 0 );
}
else
{
GetString( argv[2], iLength, "", 0 );
}
break;
}
case 'S':
{
if ( argc < 4 )
{
Help();
}
iStart = atoi( argv[ 3 ] );
if ( iStart == 0 )
{
puts( "SETENV: Invalid substring starting position." );
Quit( 1 );
}
iLength = atoi( argv[ 4 ] );
if ( iLength == 0 )
{
puts( "SETENV: Invalid substring length." );
Quit( 1 );
}
SubString( argv[ 2 ], iStart, iLength );
break;
}
case 'T':
{
if ( argc < 3 )
{
Help();
}
iStart = atoi( argv[ 2 ] );
GetTimeAndDate( iStart );
break;
}
case 'V':
{
GetOSVersion();
break;
}
default:
{
Help();
break;
}
}
Quit( 0 );
}
void GetString( char * pszPrompt, int iLength, char * pszDefault, int iEcho )
{
char * pszBuffer;
char * pszString;
int iRC;
/* Allocate a buffer of length bytes and add bytes for "STRING=", and */
/* the null terminator. */
pszString = (char *)malloc( iLength + 7 + 1 );
if ( pszString == NULL )
{
puts( "SETENV: Couldn't allocate Buffer." );
WriteLine( "Set STRING=" );
Quit( 3 );
}
pszBuffer = (char *)malloc( iLength );
if ( pszBuffer == NULL )
{
free( pszString );
puts( "SETENV: Couldn't allocate Buffer." );
WriteLine( "Set STRING=" );
Quit( 3 );
}
cprintf( "%s", pszPrompt );
InputString( pszBuffer, iLength, iEcho );
if ( strlen ( pszBuffer ) == 0 )
{
sprintf( pszString, "Set STRING=%s", pszDefault );
}
else
{
sprintf( pszString, "Set STRING=%s", pszBuffer );
}
WriteLine( pszString );
free( pszBuffer );
free( pszString );
Quit( 0 );
}
void InputString( char * pszBuffer, int iLength, int iEcho )
{
int iI = 0;
char cChar;
while( 1 )
{
cChar = getch();
switch( cChar )
{
case '\r':
{
*( pszBuffer + iI ) = '\0';
putchar( '\n' );
return;
}
case '\b':
{
if ( iI > 0 )
{
iI--;
printf( "\b \b" );
}
break;
}
case ESC:
{
for ( ; iI > 0; iI-- )
{
printf( "\b \b" );
}
break;
}
default:
{
if ( iI < iLength )
{
*( pszBuffer + iI ) = cChar;
iI++;
if ( iEcho )
{
putchar( cChar );
}
else
{
putchar( '*' );
}
}
else
{
printf( "\a" );
}
break;
}
}
}
}
void ParseFilespec( char * pszFilespec )
{
char szDrive[ _MAX_DRIVE ];
char szDir[ _MAX_DIR ];
char szFname[ _MAX_FNAME ];
char szExt[ _MAX_FNAME ];
char szBuffer[ _MAX_PATH + 16 ];
int i;
_splitpath( pszFilespec, szDrive, szDir, szFname, szExt );
/*
Strip the ':' off of the drive
*/
if ( ':' == szDrive[ strlen( szDrive ) - 1 ] )
{
szDrive[ strlen( szDrive ) - 1 ] = 0;
}
/* Strip the ending '\' off of the directory */
if ( '\\' == szDir[ strlen( szDir ) - 1 ] )
{
szDir[ strlen( szDir ) - 1 ] = 0;
}
/* Strip the '.' off of the file extension */
if ( '.' == szExt[ 0 ] )
{
memmove( &szExt[ 1 ], szExt, ( strlen( szExt ) - 1 ) );
}
strcpy( szBuffer, "Set DRIVE=" );
strcat( szBuffer, szDrive );
WriteLine( szBuffer );
strcpy( szBuffer, "Set DIR=" );
strcat( szBuffer, szDir );
WriteLine( szBuffer );
strcpy( szBuffer, "Set FNAME=" );
strcat( szBuffer, szFname );
WriteLine( szBuffer );
strcpy( szBuffer, "Set EXT=" );
strcat( szBuffer, szExt );
WriteLine( szBuffer );
}
char * SubString( char * pszString, int iStart, int iLength )
{
char szBuffer[ 256 ];
if ( iStart > strlen( pszString ) )
{
puts( "SETENV: Starting position is greater than string's length." );
Quit( 1 );
}
strcpy( szBuffer, "Set STRING=" );
strncat( szBuffer, ( pszString + ( iStart - 1 ) ), iLength );
szBuffer[ iLength + 7] = '\0';
WriteLine( szBuffer );
Quit( 0 );
}
void GetOSVersion( void )
{
char szBuffer[ 512 ];
char szValue[ 16 ];
strcpy( szBuffer, "Set OSVer=" );
itoa( _osmajor, szValue, 10 );
strcat( szBuffer, szValue );
WriteLine( szBuffer );
strcpy( szBuffer, "Set OSRel=" );
itoa( ( _osminor / 10 ), szValue, 10 );
strcat( szBuffer, szValue );
WriteLine( szBuffer );
strcpy( szBuffer, "Set OSMod=" );
itoa( ( _osminor % 10 ), szValue, 10 );
strcat( szBuffer, szValue );
WriteLine( szBuffer );
Quit( 0 );
}
void EditPath( char * pszDirectories )
{
char szBuffer[ _MAX_PATH + 2 ];
char * pszDirEnd;
char * pszPath;
int iDirLength;
/*
Initialize the global path variable.
*/
pszPath = getenv( "PATH" );
if ( pszPath != NULL )
{
strcpy( szPath, pszPath );
}
else
{
strcpy( szPath, "" );
}
pszDirEnd = pszDirectories + strlen( pszDirectories );
do
{
iDirLength = strcspn( pszDirectories, ";" );
strncpy( szBuffer, pszDirectories, iDirLength );
szBuffer[ iDirLength ] = 0;
switch( szBuffer[ 0 ] )
{
case '+':
{
AddPath( &szBuffer[ 1 ] );
break;
}
case '-':
{
DelPath( &szBuffer[ 1 ] );
break;
}
default:
{
printf( "SETENV: No '+' or '-' specified with %s.\n", szBuffer );
break;
}
}
pszDirectories = pszDirectories + iDirLength + 1;
} while( pszDirectories < pszDirEnd );
if ( _osmode == DOS_MODE )
{
if ( 123 < strlen( szPath ) )
{
printf( "SETENV: New PATH would exceed 128 characters!" );
Quit( 1 );
}
}
else
{
strcpy( szBuffer, "PATH=" );
strcat( szBuffer, szPath );
WriteLine( szBuffer );
Quit( 0 );
}
}
int AddPath( char * pszDirectory )
{
char szNewPath[ _MAX_PATH + 1 ];
char * pszPath;
/*
If there's not a ';' at the end of the PATH, add one, we'll
need it to use strtok during the search.
*/
if ( 0 < ( strlen( szPath ) ) )
{
if ( ';' != szPath[ strlen( szPath ) - 1 ] )
{
strcat( szPath, ";" );
}
}
/*
Set up the new path by putting the new directory first in line.
*/
strcpy( szNewPath, pszDirectory );
/*
If there's a semi-colon at the end of the new directory, we have
to remove it for the compare...
*/
if ( ';' == *( pszDirectory + strlen( pszDirectory ) - 1 ) )
{
*( pszDirectory + strlen( pszDirectory ) - 1 ) = '\0';
}
/*
...otherwise, add a semi-colon to the end of the new path.
*/
else
{
strcat( szNewPath, ";" );
}
pszPath = strtok( szPath, ";" );
while( pszPath != NULL )
{
/*
If the directory isn't already in the path, add it.
*/
if ( 0 != strcmpi( pszPath, pszDirectory ) )
{
strcat( szNewPath, pszPath );
strcat( szNewPath, ";" );
}
pszPath = strtok( NULL, ";" );
}
strcpy( szPath, szNewPath );
return( 0 );
}
int DelPath( char * pszDirectory )
{
char szNewPath[ _MAX_PATH + 1 ] = "";
char * pszPath;
if ( 0 == strlen( szPath ) )
{
return( 0 );
}
/*
If there's not a ';' at the end of the PATH, add one, we'll
need it to use strtok.
*/
if ( ';' != szPath[ strlen( szPath ) - 1 ] )
{
strcat( szPath, ";" );
}
/*
Remove the ';' at the end of the specified directory, strtok returns
tokens without the delimeters.
*/
if ( ';' == *( pszDirectory + strlen( pszDirectory ) - 1 ) )
{
*( pszDirectory + strlen( pszDirectory ) - 1 ) = '\0';
}
pszPath = strtok( szPath, ";" );
while( pszPath != NULL )
{
/*
If the directory doesn't match, add it.
*/
if ( 0 != strcmpi( pszPath, pszDirectory ) )
{
strcat( szNewPath, pszPath );
strcat( szNewPath, ";" );
}
pszPath = strtok( NULL, ";" );
}
strcpy( szPath, szNewPath );
return( 0 );
}
void GetTimeAndDate( int iFormat )
{
char szTime[] = "TIME=hh:mm:ss ";
char szDate[] = "DATE=mm/dd/yy";
_strtime( &szTime[5] );
if( iFormat == 12 )
{
iFormat = atoi( &szTime[5] );
if ( iFormat > 12 )
{
iFormat -= 12;
szTime[ 13 ] = 'p';
}
else
{
szTime[ 13 ] = 'a';
}
sprintf( &szTime[5], "%02i", iFormat );
szTime[7] = ':';
}
else
{
szTime[13] = 0x0;
}
_strdate( &szDate[5] );
WriteLine( szTime );
WriteLine( szDate );
Quit( 0 );
}
void GetNextDrive( void )
{
int iNextDrive,
iCurrentDrive;
char szNextDrive[] = "Set NEXTDRIVE=A";
iCurrentDrive =_getdrive();
for ( iNextDrive = 1; iNextDrive <= 26; iNextDrive++ )
{
if ( 0 != _chdrive( iNextDrive ) )
{
iNextDrive = 'A' + ( iNextDrive - 1 );
_chdrive( iCurrentDrive );
sprintf( szNextDrive, "Set NEXTDRIVE=%c", iNextDrive );
WriteLine( szNextDrive );
Quit( 0 );
}
}
puts( "SETENV: All drives are in use." );
_chdrive( iCurrentDrive );
Quit( 1 );
}
void WriteLine( char * szLine )
{
char * pszEqual;
if ( iCase != PRESERVE )
{
pszEqual = strchr( szLine, '=' );
pszEqual++;
switch ( iCase )
{
case UPPER:
{
strupr( pszEqual );
break;
}
case LOWER:
{
strlwr( pszEqual );
break;
}
default:
{
break;
}
}
}
printf( "%s\n", szLine );
}
void Quit( int iRC )
{
exit( iRC );
}
void Help( void )
{
printf( "SETENV %d.%d%d by MJ Winkler, %s, %s.\n",
VERSION, RELEASE, MODIFICATION, __DATE__, __TIME__ );
puts( "SETENV will write a series of SET statements to standard output" );
puts( "that will create or modify environment variables." );
puts( "Syntax is: SETENV command [argument(s)] [/U | /L | /P] [/M]" );
puts( " Where: filename = the name of the file to create." );
puts( " command = the SETENV command (see list below).");
puts( " argument(s) = the arguments, if any, to the command." );
puts( "" );
puts( " /U = Make returned environment strings uppercase." );
puts( " /L = Make returned environment strings lowercase." );
puts( " /P = Preserve case (default).\n" );
puts( "ErrorLevel will be set >= 1 if there is an error.\n" );
puts( "Cmd Arguments Description and variable(s) created." );
puts( "--- ----------------------- --------------------------------------------------" );
puts( " ? Displays this help information." );
puts( " E [+ | -]directory; Edit the PATH variable. Use '+' to insert a" );
puts( " directory, '-' to remove one. Separate directories" );
puts( " with a ';'. You may also list multiple directories" );
puts( " at one time (i.e +C:\\123;-C:\\WP51;)." );
puts( " F filespec Splits a filespec into its component parts." );
puts( " DRIVE=drive letter (no ':')" );
puts( " DIR=directory(s) (no ending '\\')" );
puts( " FNAME=file name" );
puts( " EXT=file extension (no '.')" );
puts( " G Gets the next available drive letter." );
puts( " NEXTDRIVE=drive leter (no ':')" );
puts( " P prompt length [default] Prompts for input of specified length and" );
puts( " returns the default if nothing is entered." );
puts( " STRING=string entered or default" );
puts( " Q prompt length [default] Just like P but echos *'s instead" );
puts( " the actual characters--great for" );
puts( " passwords." );
puts( " STRING=string entered or default" );
puts( " S string start length Extracts a substring." );
puts( " STRING=requested portion of string" );
puts( " T 12 or 24 Time (12 or 24 hour format) and Date. If 12 hour" );
puts( " format is selected, 'a' or 'p' will be appended" );
puts( " to TIME." );
puts( " TIME=hh:mm:ss" );
puts( " DATE=mm/dd/yy" );
puts( " V OS version." );
puts( " OSVER=version number" );
puts( " OSMOD=modification number" );
puts( " OSREL=release number" );
Quit( 1 );
}