Skip to content

Commit

Permalink
wmake: fix backward compatibility with previous versions of wmake
Browse files Browse the repository at this point in the history
remove copy command wmake interception and move this copy functionality to %copy wmake internal command
  • Loading branch information
jmalak committed Apr 28, 2024
1 parent 669c6e4 commit 21e4775
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 180 deletions.
323 changes: 160 additions & 163 deletions bld/wmake/c/mexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ STATIC const char * const dosInternals[] = { /* COMMAND.COM commands */
static const char * const percentCmds[] = {
"ABORT",
"APPEND",
"COPY",
"CREATE",
"ERASE",
"MAKE",
Expand All @@ -174,6 +175,7 @@ static const char * const percentCmds[] = {
enum {
PER_ABORT,
PER_APPEND,
PER_COPY,
PER_CREATE,
PER_ERASE,
PER_MAKE,
Expand All @@ -197,6 +199,21 @@ STATIC NKLIST *noKeepList; /* contains the list of files that
needs to be cleaned when wmake
exits */

static bool IsDotOrDotDot( const char *fname )
/*********************************************
* return true if fname is "." or "..", false otherwise
*/
{
return( fname[0] == '.' && ( fname[1] == NULLCHAR || ( fname[1] == '.' && fname[2] == NULLCHAR ) ) );
}

static bool chk_is_dir( const char *name )
{
struct stat s;

return( stat( name, &s ) == 0 && S_ISDIR( s.st_mode ) );
}

char *CmdGetFileName( char *src, char **fname, bool osname )
/**********************************************************/
{
Expand Down Expand Up @@ -802,6 +819,146 @@ STATIC bool percentRename( char *arg )
return( rename( fn1, fn2 ) == 0 );
}

static FILE *open_file( const char *name, const char *mode )
/**********************************************************/
{
FILE *fp;

fp = fopen( name, mode );
if( fp == NULL )
PrtMsg( ERR | ERROR_OPENING_FILE, name );
return( fp );
}

static bool close_file( FILE *fp, const char *name )
/**************************************************/
{
if( fp != NULL ) {
if( fclose( fp ) ) {
PrtMsg( ERR | ERROR_CLOSING_FILE, name );
return( false );
}
}
return( true );
}

static size_t read_block( void *buf, size_t len, FILE *fp, const char *name )
/***************************************************************************/
{
size_t readlen;

readlen = fread( buf, 1, len, fp );
if( ferror( fp ) )
PrtMsg( ERR | READ_ERROR, name );
return( readlen );
}

static size_t write_block( const void *buf, size_t len, FILE *fp, const char *name )
/**********************************************************************************/
{
size_t writelen;

writelen = fwrite( buf, 1, len, fp );
if( ferror( fp ) )
PrtMsg( ERR | ERROR_WRITING_FILE, name );
return( writelen );
}

STATIC bool oneCopy( const char *src, const char *dst )
/*****************************************************/
{
FILE *fps;
FILE *fpd;
bool ok;
char buf[FILE_BUFFER_SIZE];
size_t len;
struct stat st;
struct utimbuf dsttimes;

ok = false;
fps = open_file( src, "rb" );
if( fps != NULL ) {
fpd = open_file( dst, "wb" );
if( fpd != NULL ) {
while( (len = read_block( buf, FILE_BUFFER_SIZE, fps, src )) == FILE_BUFFER_SIZE ) {
if( len != write_block( buf, len, fpd, dst ) ) {
break;
}
}
if( len != FILE_BUFFER_SIZE ) {
ok = ( len == write_block( buf, len, fpd, dst ) );
}
ok &= close_file( fpd, dst );

stat( src, &st );
dsttimes.actime = st.st_atime;
dsttimes.modtime = st.st_mtime;
utime( dst, &dsttimes );
/*
* copy permissions: mostly necessary for the "x" bit
* some files is copied with the read-only permission
*/
chmod( dst, st.st_mode );
}
ok &= close_file( fps, src );
}
return( ok );
}

STATIC bool processCopy( const char *src, const char *dst )
/*********************************************************/
{
char buf[FILE_BUFFER_SIZE];
pgroup2 pg;

if( chk_is_dir( dst ) ) {
_splitpath2( src, pg.buffer, NULL, NULL, &pg.fname, &pg.ext );
_makepath( buf, NULL, dst, pg.fname, pg.ext );
dst = strcpy( pg.buffer, buf );
}
return( oneCopy( src, dst ) );
}

STATIC bool percentCopy( char *arg )
/***********************************
* "%COPY" {ws}+ <source file> {ws}+ <destination file>
*/
{
char *p;
char *fn1, *fn2;

assert( arg != NULL );

if( Glob.noexec ) {
return( true );
}
/*
* Get first LFN
*/
p = CmdGetFileName( arg, &fn1, false );
if( *p == NULLCHAR || !cisws( *p ) ) {
PrtMsg( ERR | SYNTAX_ERROR_IN, percentCmds[PER_COPY] );
PrtMsg( INF | PRNTSTR, "First file" );
PrtMsg( INF | PRNTSTR, fn1 );
return( false );
}
*p++ = NULLCHAR; /* terminate first file name */
/*
* skip ws after first and before second file name
*/
p = SkipWS( p );
/*
* Get second LFN as well
*/
p = CmdGetFileName( p, &fn2, false );
if( *p != NULLCHAR && !cisws( *p ) ) {
PrtMsg( ERR | SYNTAX_ERROR_IN, percentCmds[PER_COPY] );
return( false );
}
*p = NULLCHAR; /* terminate second file name */
return( processCopy( fn1, fn2 ) != 0 );
}

STATIC bool percentCmd( const char *cmdname, char *arg )
/*******************************************************
* handle our special percent commands
Expand Down Expand Up @@ -831,6 +988,9 @@ STATIC bool percentCmd( const char *cmdname, char *arg )
case PER_APPEND:
ok = percentWrite( arg, WR_APPEND );
break;
case PER_COPY:
ok = percentCopy( arg );
break;
case PER_CREATE:
ok = percentWrite( arg, WR_CREATE );
break;
Expand Down Expand Up @@ -1499,21 +1659,6 @@ STATIC bool remove_item( const char *name, const rm_flags *flags, bool dir )
return( rc == 0 );
}

static bool IsDotOrDotDot( const char *fname )
/*********************************************
* return true if fname is "." or "..", false otherwise
*/
{
return( fname[0] == '.' && ( fname[1] == NULLCHAR || ( fname[1] == '.' && fname[2] == NULLCHAR ) ) );
}

static bool chk_is_dir( const char *name )
{
struct stat s;

return( stat( name, &s ) == 0 && S_ISDIR( s.st_mode ) );
}

static bool doRM( const char *fullpath, const rm_flags *flags )
{
iolist *tmp;
Expand Down Expand Up @@ -1808,153 +1953,6 @@ STATIC RET_T handleMkdir( char *cmd )
return( RET_SUCCESS );
}

static FILE *open_file( const char *name, const char *mode )
/**********************************************************/
{
FILE *fp;

fp = fopen( name, mode );
if( fp == NULL )
PrtMsg( ERR | ERROR_OPENING_FILE, name );
return( fp );
}

static bool close_file( FILE *fp, const char *name )
/**************************************************/
{
if( fp != NULL ) {
if( fclose( fp ) ) {
PrtMsg( ERR | ERROR_CLOSING_FILE, name );
return( false );
}
}
return( true );
}

static size_t read_block( void *buf, size_t len, FILE *fp, const char *name )
/***************************************************************************/
{
size_t readlen;

readlen = fread( buf, 1, len, fp );
if( ferror( fp ) )
PrtMsg( ERR | READ_ERROR, name );
return( readlen );
}

static size_t write_block( const void *buf, size_t len, FILE *fp, const char *name )
/**********************************************************************************/
{
size_t writelen;

writelen = fwrite( buf, 1, len, fp );
if( ferror( fp ) )
PrtMsg( ERR | ERROR_WRITING_FILE, name );
return( writelen );
}

STATIC bool oneCopy( const char *src, const char *dst )
/*****************************************************/
{
FILE *fps;
FILE *fpd;
bool ok;
char buf[FILE_BUFFER_SIZE];
size_t len;
struct stat st;
struct utimbuf dsttimes;

ok = false;
fps = open_file( src, "rb" );
if( fps != NULL ) {
fpd = open_file( dst, "wb" );
if( fpd != NULL ) {
while( (len = read_block( buf, FILE_BUFFER_SIZE, fps, src )) == FILE_BUFFER_SIZE ) {
if( len != write_block( buf, len, fpd, dst ) ) {
break;
}
}
if( len != FILE_BUFFER_SIZE ) {
ok = ( len == write_block( buf, len, fpd, dst ) );
}
ok &= close_file( fpd, dst );

stat( src, &st );
dsttimes.actime = st.st_atime;
dsttimes.modtime = st.st_mtime;
utime( dst, &dsttimes );
/*
* copy permissions: mostly necessary for the "x" bit
* some files is copied with the read-only permission
*/
chmod( dst, st.st_mode );
}
ok &= close_file( fps, src );
}
return( ok );
}

STATIC bool processCopy( const char *src, const char *dst )
/*********************************************************/
{
char buf[FILE_BUFFER_SIZE];
pgroup2 pg;

if( chk_is_dir( dst ) ) {
_splitpath2( src, pg.buffer, NULL, NULL, &pg.fname, &pg.ext );
_makepath( buf, NULL, dst, pg.fname, pg.ext );
dst = strcpy( pg.buffer, buf );
}
return( oneCopy( src, dst ) );
}

STATIC RET_T handleCopy( char *arg )
/***********************************
* "COPY" {ws}+ <source file> {ws}+ <destination file>
*/
{
char *p;
char *fn1, *fn2;

assert( arg != NULL );

if( Glob.noexec ) {
return( RET_SUCCESS );
}
/*
* Get first LFN
*
* skip "COPY"
*/
p = arg + 4;
p = SkipWS( p );
p = CmdGetFileName( p, &fn1, false );
if( *p == NULLCHAR || !cisws( *p ) ) {
PrtMsg( ERR | SYNTAX_ERROR_IN, dosInternals[COM_COPY] );
PrtMsg( INF | PRNTSTR, "First file" );
PrtMsg( INF | PRNTSTR, fn1 );
return( RET_ERROR );
}
*p++ = NULLCHAR; /* terminate first file name */
/*
* skip ws after first and before second file name
*/
p = SkipWS( p );
/*
* Get second LFN as well
*/
p = CmdGetFileName( p, &fn2, false );
if( *p != NULLCHAR && !cisws( *p ) ) {
PrtMsg( ERR | SYNTAX_ERROR_IN, dosInternals[COM_COPY] );
return( RET_ERROR );
}
*p = NULLCHAR; /* terminate second file name */
if( processCopy( fn1, fn2 ) )
return( RET_SUCCESS );
return( RET_ERROR );
}


STATIC RET_T handleRmdir( char *cmd )
/************************************
* RMDIR {ws}+ <dir>
Expand Down Expand Up @@ -2235,7 +2233,6 @@ STATIC RET_T shellSpawn( char *cmd, shell_flags flags )
case COM_RM: my_ret = handleRM( cmd ); break;
case COM_MKDIR: my_ret = handleMkdir( cmd ); break;
case COM_RMDIR: my_ret = handleRmdir( cmd ); break;
case COM_COPY: my_ret = handleCopy( cmd ); break;
#if defined( __OS2__ ) || defined( __NT__ ) || defined( __UNIX__ ) || defined( __RDOS__ )
case COM_CD:
case COM_CHDIR: my_ret = handleCD( cmd ); break;
Expand Down
Loading

0 comments on commit 21e4775

Please sign in to comment.