From 0b08fca9f229b40275542544eda14195e85ea8ce Mon Sep 17 00:00:00 2001 From: bentley Date: Thu, 14 Jan 2010 23:17:22 -0700 Subject: [PATCH] xlink: replace awkward fatalerror() with standard errx(3) fatalerror() prints a given string to stdout and exits(5). It cannot format text, so there was a string temptext used with sprintf() to format text and pass it to fatalerror(). errx() can format text, and it prints to stderr. As a bonus, this fixes the linking warnings from use of sprintf(). --- include/link/main.h | 2 -- src/link/assign.c | 71 ++++++++++++--------------------------------- src/link/library.c | 4 +-- src/link/main.c | 32 ++++++-------------- src/link/mapfile.c | 4 +-- src/link/object.c | 42 ++++++++++++--------------- src/link/patch.c | 22 +++++--------- src/link/symbol.c | 12 +++----- 8 files changed, 63 insertions(+), 126 deletions(-) diff --git a/include/link/main.h b/include/link/main.h index 29fc6f1d..b43103eb 100644 --- a/include/link/main.h +++ b/include/link/main.h @@ -4,8 +4,6 @@ #include "link/types.h" extern void PrintUsage(void); -extern void fatalerror(char *s); -extern char temptext[1024]; extern SLONG fillchar; extern char smartlinkstartsymbol[256]; diff --git a/src/link/assign.c b/src/link/assign.c index 59826da1..f53254e0 100644 --- a/src/link/assign.c +++ b/src/link/assign.c @@ -1,3 +1,4 @@ +#include #include #include @@ -71,7 +72,7 @@ area_AllocAbs(struct sFreeArea ** ppArea, SLONG org, SLONG size) return (org); } else - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); } } } @@ -164,7 +165,7 @@ AssignCodeSections(void) pSection->oAssigned = 1; DOMAXBANK(pSection->nBank); } else - fatalerror("Unable to place CODE section anywhere"); + errx(5, "Unable to place CODE section anywhere"); } } @@ -185,7 +186,7 @@ GBROM_AssignSections(void) BankFree[i] = malloc(sizeof *BankFree[i]); if (!BankFree[i]) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); if (i == 0) { BankFree[i]->nOrg = 0x0000; @@ -243,10 +244,7 @@ GBROM_AssignSections(void) if (area_AllocAbs (&BankFree[BANK_BSS], pSection->nOrg, pSection->nByteSize) != pSection->nOrg) { - sprintf(temptext, - "Unable to load fixed BSS section at $%lX", - pSection->nOrg); - fatalerror(temptext); + errx(5, "Unable to load fixed BSS section at $%lX", pSection->nOrg); } pSection->oAssigned = 1; pSection->nBank = BANK_BSS; @@ -255,10 +253,7 @@ GBROM_AssignSections(void) if (area_AllocAbs (&BankFree[BANK_HRAM], pSection->nOrg, pSection->nByteSize) != pSection->nOrg) { - sprintf(temptext, - "Unable to load fixed HRAM section at $%lX", - pSection->nOrg); - fatalerror(temptext); + errx(5, "Unable to load fixed HRAM section at $%lX", pSection->nOrg); } pSection->oAssigned = 1; pSection->nBank = BANK_HRAM; @@ -267,10 +262,7 @@ GBROM_AssignSections(void) if (area_AllocAbs (&BankFree[BANK_VRAM], pSection->nOrg, pSection->nByteSize) != pSection->nOrg) { - sprintf(temptext, - "Unable to load fixed VRAM section at $%lX", - pSection->nOrg); - fatalerror(temptext); + errx(5, "Unable to load fixed VRAM section at $%lX", pSection->nOrg); } pSection->oAssigned = 1; pSection->nBank = BANK_VRAM; @@ -279,10 +271,7 @@ GBROM_AssignSections(void) if (area_AllocAbs (&BankFree[BANK_HOME], pSection->nOrg, pSection->nByteSize) != pSection->nOrg) { - sprintf(temptext, - "Unable to load fixed HOME section at $%lX", - pSection->nOrg); - fatalerror(temptext); + errx(5, "Unable to load fixed HOME section at $%lX", pSection->nOrg); } pSection->oAssigned = 1; pSection->nBank = BANK_HOME; @@ -327,26 +316,13 @@ GBROM_AssignSections(void) pSection-> nByteSize) != pSection->nOrg) { - sprintf - (temptext, - "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX", - pSection-> - nOrg, - pSection-> - nBank); - fatalerror - (temptext); + errx(5, "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); } DOMAXBANK(pSection-> nBank); pSection->oAssigned = 1; } else { - sprintf(temptext, - "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX", - pSection->nOrg, - pSection-> - nBank); - fatalerror(temptext); + errx(5, "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); } } @@ -372,18 +348,12 @@ GBROM_AssignSections(void) if ((pSection->nOrg = area_Alloc(&BankFree[pSection->nBank], pSection->nByteSize)) == -1) { - sprintf(temptext, - "Unable to load fixed CODE/DATA section into bank $%02lX", - pSection->nBank); - fatalerror(temptext); + errx(5, "Unable to load fixed CODE/DATA section into bank $%02lX", pSection->nBank); } pSection->oAssigned = 1; DOMAXBANK(pSection->nBank); } else { - sprintf(temptext, - "Unable to load fixed CODE/DATA section into bank $%02lX", - pSection->nBank); - fatalerror(temptext); + errx(5, "Unable to load fixed CODE/DATA section into bank $%02lX", pSection->nBank); } } pSection = pSection->pNext; @@ -405,10 +375,7 @@ GBROM_AssignSections(void) area_AllocAbsCODEAnyBank(pSection->nOrg, pSection->nByteSize)) == -1) { - sprintf(temptext, - "Unable to load fixed CODE/DATA section at $%lX into any bank", - pSection->nOrg); - fatalerror(temptext); + errx(5, "Unable to load fixed CODE/DATA section at $%lX into any bank", pSection->nOrg); } pSection->oAssigned = 1; DOMAXBANK(pSection->nBank); @@ -430,7 +397,7 @@ GBROM_AssignSections(void) if ((pSection->nOrg = area_Alloc(&BankFree[BANK_BSS], pSection->nByteSize)) == -1) { - fatalerror("BSS section too large\n"); + errx(5, "BSS section too large"); } pSection->nBank = BANK_BSS; pSection->oAssigned = 1; @@ -439,7 +406,7 @@ GBROM_AssignSections(void) if ((pSection->nOrg = area_Alloc(&BankFree[BANK_HRAM], pSection->nByteSize)) == -1) { - fatalerror("HRAM section too large"); + errx(5, "HRAM section too large"); } pSection->nBank = BANK_HRAM; pSection->oAssigned = 1; @@ -448,7 +415,7 @@ GBROM_AssignSections(void) if ((pSection->nOrg = area_Alloc(&BankFree[BANK_VRAM], pSection->nByteSize)) == -1) { - fatalerror("VRAM section too large"); + errx(5, "VRAM section too large"); } pSection->nBank = BANK_VRAM; pSection->oAssigned = 1; @@ -457,7 +424,7 @@ GBROM_AssignSections(void) if ((pSection->nOrg = area_Alloc(&BankFree[BANK_HOME], pSection->nByteSize)) == -1) { - fatalerror("HOME section too large"); + errx(5, "HOME section too large"); } pSection->nBank = BANK_HOME; pSection->oAssigned = 1; @@ -465,7 +432,7 @@ GBROM_AssignSections(void) case SECT_CODE: break; default: - fatalerror("(INTERNAL) Unknown section type!"); + errx(5, "(INTERNAL) Unknown section type!"); break; } } @@ -482,7 +449,7 @@ PSION2_AssignSections(void) BankFree[0] = malloc(sizeof *BankFree[0]); if (!BankFree[0]) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); BankFree[0]->nOrg = 0x0000; BankFree[0]->nSize = 0x10000; diff --git a/src/link/library.c b/src/link/library.c index c60da2eb..18ed2b0f 100644 --- a/src/link/library.c +++ b/src/link/library.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -91,9 +92,8 @@ AddNeededModules(void) } if (options & OPT_SMART_C_LINK) { if (!addmodulecontaining(smartlinkstartsymbol)) { - sprintf(temptext, "Can't find start symbol '%s'", + errx(5, "Can't find start symbol '%s'", smartlinkstartsymbol); - fatalerror(temptext); } else printf("Smart linking with symbol '%s'\n", smartlinkstartsymbol); diff --git a/src/link/main.c b/src/link/main.c index 0ca72430..a25b0df9 100644 --- a/src/link/main.c +++ b/src/link/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -31,17 +32,6 @@ enum eOutputType outputtype = OUTPUT_GBROM; char temptext[1024]; char smartlinkstartsymbol[256]; -/* - * Print out an errormessage - * - */ - -void -fatalerror(char *s) -{ - printf("*ERROR* : %s\n", s); - exit(5); -} /* * Print the usagescreen * @@ -76,8 +66,7 @@ ProcessLinkfile(char *tzLinkfile) pLinkfile = fopen(tzLinkfile, "rt"); if (!pLinkfile) { - sprintf(temptext, "Unable to find linkfile '%s'\n", tzLinkfile); - fatalerror(temptext); + errx(5, "Unable to find linkfile '%s'", tzLinkfile); } while (!feof(pLinkfile)) { char tzLine[256]; @@ -99,10 +88,9 @@ ProcessLinkfile(char *tzLinkfile) CurrentBlock = BLOCK_COMMENT; else { fclose(pLinkfile); - sprintf(temptext, - "Unknown block '%s'\n", + errx(5, + "Unknown block '%s'", tzLine); - fatalerror(temptext); } } else { switch (CurrentBlock) { @@ -167,9 +155,8 @@ main(int argc, char *argv[]) outputtype = OUTPUT_PSION2; break; default: - sprintf(temptext, "Unknown option 't%c'\n", + errx(5, "Unknown option 't%c'", opt); - fatalerror(temptext); break; } break; @@ -184,12 +171,12 @@ main(int argc, char *argv[]) sscanf(argv[argn - 1] + 2, "%lx", &fillchar); if (!((result == EOF) || (result == 1))) { - fatalerror - ("Invalid argument for option 'z'\n"); + errx(5, + "Invalid argument for option 'z'"); } } } else { - fatalerror("Invalid argument for option 'z'\n"); + errx(5, "Invalid argument for option 'z'"); } break; case 's': @@ -197,8 +184,7 @@ main(int argc, char *argv[]) strcpy(smartlinkstartsymbol, argv[argn - 1] + 2); break; default: - sprintf(temptext, "Unknown option '%c'\n", opt); - fatalerror(temptext); + errx(5, "Unknown option '%c'", opt); break; } } diff --git a/src/link/mapfile.c b/src/link/mapfile.c index 4b2a53ef..90c2102b 100644 --- a/src/link/mapfile.c +++ b/src/link/mapfile.c @@ -19,7 +19,7 @@ SetMapfileName(char *name) mf = fopen(name, "wt"); if (!mf) - fatalerror("Unable to open mapfile for writing"); + errx(5, "Unable to open mapfile for writing"); } void @@ -28,7 +28,7 @@ SetSymfileName(char *name) sf = fopen(name, "wt"); if (!sf) - fatalerror("Unable to open symfile for writing"); + errx(5, "Unable to open symfile for writing"); fprintf(sf, ";File generated by xLink v" LINK_VERSION "\n\n"); } diff --git a/src/link/object.c b/src/link/object.c index e399935e..364fdeaf 100644 --- a/src/link/object.c +++ b/src/link/object.c @@ -3,6 +3,7 @@ * */ +#include #include #include #include @@ -79,7 +80,7 @@ AllocSection(void) *ppSections = malloc(sizeof **ppSections); if (!*ppSections) { - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); return NULL; } (*ppSections)->tSymbols = tSymbols; @@ -101,12 +102,12 @@ obj_ReadSymbol(FILE * f) pSym = malloc(sizeof *pSym); if (!pSym) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); readasciiz(s, f); pSym->pzName = malloc(strlen(s) + 1); if (!pSym->pzName) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); strcpy(pSym->pzName, s); if ((pSym->Type = (enum eSymbolType) fgetc(f)) != SYM_IMPORT) { @@ -145,7 +146,7 @@ obj_ReadRGB0Section(FILE * f) if (pSection->nByteSize) { pSection->pData = malloc(pSection->nByteSize); if (!pSection->pData) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); SLONG nNumberOfPatches; struct sPatch **ppPatch, *pPatch; @@ -163,14 +164,14 @@ obj_ReadRGB0Section(FILE * f) while (nNumberOfPatches--) { pPatch = malloc(sizeof *pPatch); if (!pPatch) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); *ppPatch = pPatch; readasciiz(s, f); pPatch->pzFilename = malloc(strlen(s) + 1); if (!pPatch->pzFilename) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); strcpy(pPatch->pzFilename, s); @@ -185,7 +186,7 @@ obj_ReadRGB0Section(FILE * f) if ((pPatch->nRPNSize = readlong(f)) > 0) { pPatch->pRPN = malloc(pPatch->nRPNSize); if (!pPatch->pRPN) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); fread(pPatch->pRPN, sizeof(UBYTE), pPatch->nRPNSize, f); @@ -218,7 +219,7 @@ obj_ReadRGB0(FILE * pObjfile) if (nNumberOfSymbols) { tSymbols = malloc(nNumberOfSymbols * sizeof(struct sSymbol *)); if (!tSymbols) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); for (i = 0; i < nNumberOfSymbols; i += 1) tSymbols[i] = obj_ReadSymbol(pObjfile); @@ -293,7 +294,7 @@ obj_ReadRGB1Section(FILE * f) if (pSection->nByteSize) { pSection->pData = malloc(pSection->nByteSize); if (!pSection->pData) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); SLONG nNumberOfPatches; struct sPatch **ppPatch, *pPatch; @@ -311,13 +312,13 @@ obj_ReadRGB1Section(FILE * f) while (nNumberOfPatches--) { pPatch = malloc(sizeof *pPatch); if (!pPatch) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); *ppPatch = pPatch; readasciiz(s, f); pPatch->pzFilename = malloc(strlen(s) + 1); if (!pPatch->pzFilename) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); strcpy(pPatch->pzFilename, s); pPatch->nLineNo = readlong(f); @@ -326,7 +327,7 @@ obj_ReadRGB1Section(FILE * f) if ((pPatch->nRPNSize = readlong(f)) > 0) { pPatch->pRPN = malloc(pPatch->nRPNSize); if (!pPatch->pRPN) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); fread(pPatch->pRPN, sizeof(UBYTE), pPatch->nRPNSize, f); @@ -359,7 +360,7 @@ obj_ReadRGB1(FILE * pObjfile) if (nNumberOfSymbols) { tSymbols = malloc(nNumberOfSymbols * sizeof *tSymbols); if (!tSymbols) - fatalerror("Out of memory!"); + errx(5, "Out of memory!"); for (i = 0; i < nNumberOfSymbols; i += 1) tSymbols[i] = obj_ReadSymbol(pObjfile); @@ -422,14 +423,12 @@ obj_ReadOpenFile(FILE * pObjfile, char *tzObjectfile) obj_ReadRGB1(pObjfile); break; default: - sprintf(temptext, "'%s' is an unsupported version\n", + errx(5, "'%s' is an unsupported version", tzObjectfile); - fatalerror(temptext); break; } } else { - sprintf(temptext, "'%s' is not a valid object\n", tzObjectfile); - fatalerror(temptext); + errx(5, "'%s' is not a valid object", tzObjectfile); } } @@ -445,8 +444,7 @@ obj_Readfile(char *tzObjectfile) pObjfile = fopen(tzObjectfile, "rb"); if (!pObjfile) { - sprintf(temptext, "Unable to open '%s'\n", tzObjectfile); - fatalerror(temptext); + errx(5, "Unable to open '%s'", tzObjectfile); } obj_ReadOpenFile(pObjfile, tzObjectfile); fclose(pObjfile); @@ -496,8 +494,7 @@ lib_Readfile(char *tzLibfile) pObjfile = fopen(tzLibfile, "rb"); if (!pObjfile) { - sprintf(temptext, "Unable to open '%s'\n", tzLibfile); - fatalerror(temptext); + errx(5, "Unable to open '%s'", tzLibfile); } char tzHeader[5]; @@ -506,9 +503,8 @@ lib_Readfile(char *tzLibfile) if (strcmp(tzHeader, "XLB0") == 0) lib_ReadXLB0(pObjfile); else { - sprintf(temptext, "'%s' is an invalid library\n", + errx(5, "'%s' is an invalid library", tzLibfile); - fatalerror(temptext); } fclose(pObjfile); } diff --git a/src/link/patch.c b/src/link/patch.c index de344867..3d10cf15 100644 --- a/src/link/patch.c +++ b/src/link/patch.c @@ -1,3 +1,4 @@ +#include #include #include @@ -45,8 +46,7 @@ getsymvalue(SLONG symid) default: break; } - fatalerror("*INTERNAL* UNKNOWN SYMBOL TYPE"); - return (0); + errx(5, "*INTERNAL* UNKNOWN SYMBOL TYPE"); } SLONG @@ -63,8 +63,7 @@ getsymbank(SLONG symid) default: break; } - fatalerror("*INTERNAL* UNKNOWN SYMBOL TYPE"); - return (0); + errx(5, "*INTERNAL* UNKNOWN SYMBOL TYPE"); } SLONG @@ -158,20 +157,18 @@ calcrpn(struct sPatch * pPatch) t = rpnpop(); rpnpush(t & 0xFF); if (t < 0 || (t > 0xFF && t < 0xFF00) || t > 0xFFFF) { - sprintf(temptext, + errx(5, "%s(%ld) : Value must be in the HRAM area", pPatch->pzFilename, pPatch->nLineNo); - fatalerror(temptext); } break; case RPN_PCEZP: t = rpnpop(); rpnpush(t & 0xFF); if (t < 0x2000 || t > 0x20FF) { - sprintf(temptext, + errx(5, "%s(%ld) : Value must be in the ZP area", pPatch->pzFilename, pPatch->nLineNo); - fatalerror(temptext); } break; case RPN_CONST: @@ -216,11 +213,10 @@ calcrpn(struct sPatch * pPatch) high |= (*rpn++) << 24; t = rpnpop(); if (t < low || t > high) { - sprintf(temptext, + errx(5, "%s(%ld) : Value must be in the range [%ld;%ld]", pPatch->pzFilename, pPatch->nLineNo, low, high); - fatalerror(temptext); } rpnpush(t); size -= 8; @@ -254,11 +250,10 @@ Patch(void) pSect->pData[pPatch->nOffset] = (UBYTE) t; } else { - sprintf(temptext, + errx(5, "%s(%ld) : Value must be 8-bit\n", pPatch->pzFilename, pPatch->nLineNo); - fatalerror(temptext); } break; case PATCH_WORD_L: @@ -279,11 +274,10 @@ Patch(void) 1] = t & 0xFF; } } else { - sprintf(temptext, + errx(5, "%s(%ld) : Value must be 16-bit\n", pPatch->pzFilename, pPatch->nLineNo); - fatalerror(temptext); } break; case PATCH_LONG_L: diff --git a/src/link/symbol.c b/src/link/symbol.c index a3196797..faff4713 100644 --- a/src/link/symbol.c +++ b/src/link/symbol.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -53,9 +54,7 @@ sym_GetValue(char *tzName) } } - sprintf(temptext, "Unknown symbol '%s'", tzName); - fatalerror(temptext); - return (0); + errx(5, "Unknown symbol '%s'", tzName); } } @@ -73,9 +72,7 @@ sym_GetBank(char *tzName) } } - sprintf(temptext, "Unknown symbol '%s'", tzName); - fatalerror(temptext); - return (0); + errx(5, "Unknown symbol '%s'", tzName); } void @@ -95,10 +92,9 @@ sym_CreateSymbol(char *tzName, SLONG nValue, SBYTE nBank) if (nBank == -1) return; - sprintf(temptext, + errx(5, "Symbol '%s' defined more than once\n", tzName); - fatalerror(temptext); } }