Skip to content

Commit

Permalink
Migrate main to WinMain - resolves #1
Browse files Browse the repository at this point in the history
  • Loading branch information
schnotzler committed Mar 11, 2023
1 parent f81337c commit 4fdb46e
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
#include <stdio.h>
#include <unistd.h>
#include <windows.h>

//run contents of winrun.cfg line by line on command interpreter
int main(int argc, char *argv[])
{
//create local variables
FILE * fp;
char * linebuf[ 256+1 ] = { 0 };
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
FILE* fp;
char linebuf[256 + 1] = { 0 };
char path[MAX_PATH];

//get and chdir executable dir
char *dirsep = strrchr( argv[0], '\\' );
if ( NULL != dirsep )
{
*dirsep=0;
if ( 0 > chdir( argv[0] ) )
{
GetModuleFileName( NULL, path, MAX_PATH );
char* dirsep = strrchr( path, '\\' );
if ( NULL != dirsep ) {
*dirsep = 0;
if ( 0 > chdir( path ) ) {
perror( "chdir() not successful" );
return -1;
}
}

//open cfg file
fp = fopen("winrun.cfg", "r");
if ( NULL == fp )
{
perror("Could not open file winrun.cfg");
fp = fopen( "winrun.cfg", "r" );
if ( NULL == fp ) {
perror( "Could not open file winrun.cfg" );
return -2;
}

//read and execute from file line by line
while( 0 < fscanf( fp , "%256[A-Za-z0-9 -\\:]\n", &linebuf ) )
{
printf("> %s\n", linebuf);
if ( -1 == system( linebuf ))
{
perror("ERROR Executing system()");
while ( 0 < fscanf( fp, "%256[A-Za-z0-9 -\\:]\n", linebuf ) ) {
printf( "> %s\n", linebuf );
if ( -1 == system( linebuf ) ) {
perror( "ERROR Executing system()" );
fclose( fp );
return -3;
}
}

//cleanup and close
// cleanup and close
fclose( fp );
exit( 0 );
}

0 comments on commit 4fdb46e

Please # to comment.