Skip to content

Ptchsize and MacOS fix #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ if errorlevel 1 goto ende
cd ..

echo.
echo Patching heap size to 8KB
echo Patching heap size
echo.
utils\ptchsize.exe command.com +8KB
utils\ptchsize.exe %CMD_NAME% +8KB
utils\ptchsize.exe command.com %COMPILER% +6KB
utils\ptchsize.exe %CMD_NAME% %COMPILER% +6KB

if %WITH_UPX%x == x goto alldone
if exist command.upx del command.upx >nul
Expand Down
9 changes: 5 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ set -e
WITH_UPX="no"
SED=sed
#workaround for Windows (set to binary mode)
if [ "$(expr substr $(uname -s) 1 5)" == 'MINGW' ]; then
if [ $(uname -s) == "Darwin" ]; then true
# Darwin expr does not support substr, so handle special here
elif [ "$(expr substr $(uname -s) 1 5)" == 'MINGW' ]; then
SED='sed -b'
fi


export SWAP=YES-DXMS-SWAP____________________
# BEGIN Internal stuff for ska -- If one of these three commands
# fail for you, your distribution is broken! Please report.
Expand Down Expand Up @@ -198,9 +199,9 @@ $MAKE all
cd ..

echo
echo Patching heap size to 6KB
echo Patching heap size
echo
utils/ptchsize.exe command.com +6KB
utils/ptchsize.exe command.com $COMPILER +6KB

if [ $WITH_UPX = "yes" ]; then
rm -f command.upx
Expand Down
80 changes: 54 additions & 26 deletions tools/ptchsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Patch heap size into FreeCOM executable

Useage: PTCHSIZE freecom [{ size }]
Useage: PTCHSIZE freecom compiler [{ size }]

Without any argument: Display the internal information
Else: patch the executable
Expand All @@ -21,6 +21,14 @@
#include "../include/res.h"
#include "../include/infores.h"

enum {
COMPILER_NONE,
COMPILER_WATCOM,
COMPILER_TC2,
COMPILER_TURBOCPP,
COMPILER_BC5,
COMPILER_GCC
};

FILE *freecom = 0;
unsigned char *info = 0;
Expand Down Expand Up @@ -127,6 +135,15 @@ void addUns_(unsigned long *minVal, unsigned val)
*minVal += val;
}

int my_stricmp(const char *a, const char *b)
{
while ((*a != '\0') && (toupper(*a) == toupper(*b))) {
a++;
b++;
}
return toupper(*a) - toupper(*b);
}

void addSize(unsigned short *size, char * const Xp)
{ unsigned long n;
char *p = Xp;
Expand Down Expand Up @@ -175,22 +192,35 @@ void addSize(unsigned short *size, char * const Xp)
int main(int argc, char **argv)
{ struct EXE_header exe;
unsigned short tosize;
int compiler = COMPILER_NONE;

if(argc <= 1) {
if(argc <= 2) {
puts("Patch heap size into FreeCOM executable\n"
"\nuseage: PTCHSIZE freecom [{ size }]\n"
"\nuseage: PTCHSIZE freecom compiler [{ size }]\n"
"\nSize may be given hexadecimal (0x###) or decimal.\n"
"If 'KB' is appended to size, it's multiplied with 1024.\n"
"A leading plus sign is ignored.\n"
"A plus sign by its own is treated as estimated minimum size.\n"
"More than one sizes are summed up.\n"
"If size evaluates to 0 (zero), the restriction is disabled.\n"
"\ne.g.: PTCHSIZE freecom + 5K\n"
"means: reserve 5*1024 bytes in addition to minimum size."
"\ne.g.: PTCHSIZE freecom compiler +5K\n"
"means: reserve 5*1024 bytes in addition to minimum size.\n"
"compiler may be one of bc, gcc, tc, tcpp, watcom.\n"
);
return 127;
}


if (!my_stricmp(argv[2], "WATCOM")) compiler = COMPILER_WATCOM;
else if (!my_stricmp(argv[2], "TC")) compiler = COMPILER_TC2;
else if (!my_stricmp(argv[2], "TCPP")) compiler = COMPILER_TURBOCPP;
else if (!my_stricmp(argv[2], "BC")) compiler = COMPILER_BC5;
else if (!my_stricmp(argv[2], "GCC")) compiler = COMPILER_GCC;
else {
printf("Unknown compiler %s\n", argv[2]);
return 78;
}

if(enumFileResources(argv[1], RES_ID_INFO, getInfo, 0) != 1) {
puts("Failed to locate FreeCOM's info resource\n"
"Possible errors:\n"
Expand Down Expand Up @@ -229,10 +259,8 @@ int main(int argc, char **argv)
return 71;
}

if(argc == 2
#ifndef GCC
|| ival.heapPos == ~0
#endif
if(argc == 3
|| (compiler != COMPILER_GCC && ival.heapPos == ~0)
) {
prUns(ival.alias, "Aliases");
prUns(ival.hist, "Command line history");
Expand Down Expand Up @@ -262,7 +290,7 @@ int main(int argc, char **argv)
}

tosize = 0;
argc = 1;
argc = 2;
while(argv[++argc])
addSize(&tosize, argv[argc]);
if(tosize && tosize < minSize) {
Expand All @@ -275,11 +303,11 @@ int main(int argc, char **argv)
, argv[1], tosize);
/* Watcom already has extraMin minimal and dynamically adjusts its MCB*/
if(tosize) {
#ifdef GCC
/* need to adjust SP */
unsigned startbss = 0x10000 - exe.extraMax * 16;
exe.fSP = startbss + ival.extraSpace * 16 + tosize;
#endif
if (compiler == COMPILER_GCC) {
/* need to adjust SP */
unsigned startbss = 0x10000 - exe.extraMax * 16;
exe.fSP = startbss + ival.extraSpace * 16 + tosize;
}
exe.extraMin = exe.extraMax = ival.extraSpace + tosize / 16;
}
else {
Expand All @@ -293,18 +321,18 @@ int main(int argc, char **argv)
return 77;
}

#ifndef GCC
if(fseek(freecom, ival.heapPos, SEEK_SET) != 0) {
printf("Failed to seek to heap size offset in %s\n", argv[1]);
return 42;
}
assert(sizeof(tosize) == 2);
if(fwrite(&tosize, sizeof(tosize), 1, freecom) != 1) {
printf("Failed to patch heap size into FreeCOM executable.\n"
"File most probably corrupted now: %s\n", argv[1]);
return 75;
if (compiler != COMPILER_GCC) {
if(fseek(freecom, ival.heapPos, SEEK_SET) != 0) {
printf("Failed to seek to heap size offset in %s\n", argv[1]);
return 42;
}
assert(sizeof(tosize) == 2);
if(fwrite(&tosize, sizeof(tosize), 1, freecom) != 1) {
printf("Failed to patch heap size into FreeCOM executable.\n"
"File most probably corrupted now: %s\n", argv[1]);
return 75;
}
}
#endif

fflush(freecom);
if(ferror(freecom)) {
Expand Down