diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | CHANGELOG.txt | 8 | ||||
-rw-r--r-- | CMakeLists.txt | 11 | ||||
-rw-r--r-- | Makefile | 38 | ||||
-rw-r--r-- | agbsum/CMakeLists.txt | 42 | ||||
-rw-r--r-- | include/agbsum.h (renamed from agbsum/include/agbsum.h) | 39 | ||||
-rw-r--r-- | src/chkpar.c (renamed from agbsum/src/chkpar.c) | 58 | ||||
-rw-r--r-- | src/exi.c (renamed from agbsum/src/exi.c) | 8 | ||||
-rw-r--r-- | src/getsum.c (renamed from agbsum/src/getsum.c) | 4 | ||||
-rw-r--r-- | src/hlp.c (renamed from agbsum/src/hlp.c) | 6 | ||||
-rw-r--r-- | src/inidat.c (renamed from agbsum/src/inidat.c) | 3 | ||||
-rw-r--r-- | src/main.c (renamed from agbsum/src/main.c) | 23 | ||||
-rw-r--r-- | src/opn.c (renamed from agbsum/src/opn.c) | 10 | ||||
-rw-r--r-- | src/pat.c (renamed from agbsum/src/pat.c) | 11 | ||||
-rw-r--r-- | src/red.c (renamed from agbsum/src/red.c) | 11 |
15 files changed, 145 insertions, 130 deletions
@@ -1,3 +1,4 @@ *.gba +*.o *.sav -/build +/agbsum diff --git a/CHANGELOG.txt b/CHANGELOG.txt index cb38070..42fadc8 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,11 @@ +| A + +- Use generic makefile instead of CMake; +- Update coding style; +- Move include and src directories to root; +- Update gitignore; +- Add install target to makefile; + | 9 - Don't record compilation time; diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index eb2f24d..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -cmake_minimum_required(VERSION 3.8) - -project( - agbsum - VERSION 9 - DESCRIPTION "GBA ROM header checksum patcher." - HOMEPAGE_URL "https://mandelbrot.dk/agbsum" - LANGUAGES C -) - -add_subdirectory(agbsum) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0708815 --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +RM := rm -f + +CFLAGS = \ + -Iinclude \ + -Wall \ + -Wextra \ + -Wmissing-declarations \ + -Wpedantic \ + -o$(@) \ + -std=c99 + +BIN := agbsum + +OBJS := \ + src/chkpar.o \ + src/exi.o \ + src/getsum.o \ + src/hlp.o \ + src/inidat.o \ + src/main.o \ + src/opn.o \ + src/pat.o \ + src/red.o + +$(BIN): $(OBJS) + $(CC) -o$(BIN) $(OBJS) + +.PHONY: clean install purge + +clean: + $(RM) $(OBJS) + +purge: clean + $(RM) $(BIN) + +install: $(BIN) + mkdir -pm755 "$(BINDIR)" + install -m755 "agbsum" "$(BINDIR)" diff --git a/agbsum/CMakeLists.txt b/agbsum/CMakeLists.txt deleted file mode 100644 index 5c104b8..0000000 --- a/agbsum/CMakeLists.txt +++ /dev/null @@ -1,42 +0,0 @@ -cmake_minimum_required(VERSION 3.8) - -set(CMAKE_C_STANDARD 99) - -add_executable( - agbsum - - "src/chkpar.c" - "src/exi.c" - "src/getsum.c" - "src/hlp.c" - "src/inidat.c" - "src/main.c" - "src/opn.c" - "src/pat.c" - "src/red.c" -) - -target_include_directories( - agbsum PRIVATE - - "include" -) - -target_compile_definitions( - agbsum PRIVATE - - _FORTIFY_SOURCE=$<IF:$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">,0,2> -) - -if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang|GNU") - target_compile_options( - agbsum PRIVATE - - $<IF:$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">,-Og,-Ofast> - -Wall - -Wextra - -Wpedantic - -fdiagnostics-color=always - -g - ) -endif() diff --git a/agbsum/include/agbsum.h b/include/agbsum.h index 13c3502..e0f207c 100644 --- a/agbsum/include/agbsum.h +++ b/include/agbsum.h @@ -16,18 +16,15 @@ #if __STDC_VERSION__ > 199901 # include <stdnoreturn.h> +#elif defined(__GNUC__) +# define noreturn __attribute__ ((__noreturn__)) +#elif defined (_MSC_VER) +# define noreturn __declspec (noreturn) #else -# ifdef __GNUC__ -# define _Noreturn __attribute__ ((noreturn)) -# elif defined(_MSC_VER) -# define _Noreturn __declspec (noreturn) -# else -# define _Noreturn -# endif -# define noreturn _Noreturn +# define noreturn #endif -#define agb_rel ((uint64_t)+0x9u) +#define agb_rel ((uint64_t)+0xAu) #define agb_romsrt ((size_t)+0xA0u) #define agb_chksumoff ((size_t)+0xBDu-agb_romsrt) @@ -44,19 +41,27 @@ typedef struct { FILE * rom; } agb_dat; -uint8_t agb_getsum(void const* restrict rom); +uint8_t +agb_getsum (void const* restrict rom); -void agb_pat(FILE * restrict rom,unsigned char chksum); +void +agb_pat (FILE * restrict rom, unsigned char chksum); -void agb_hlp(void); +void +agb_hlp (void); -void agb_chkpar(agb_dat * restrict dat,int argc,char const* const* restrict argv); +void +agb_chkpar (agb_dat * restrict dat, int argc, char const* const* restrict argv); -void agb_inidat(agb_dat * restrict dat); +void +agb_inidat (agb_dat * restrict dat); -FILE * agb_opn(char const* restrict pth); -void agb_red(void * restrict buf,FILE * restrict rom); +FILE * +agb_opn (char const* restrict pth); -noreturn void agb_exi(agb_cnd stat,FILE * rom); +void +agb_red (void * restrict buf, FILE * restrict rom); + +noreturn void agb_exi (agb_cnd stat, FILE * rom); #endif diff --git a/agbsum/src/chkpar.c b/src/chkpar.c index e388305..9ad487d 100644 --- a/agbsum/src/chkpar.c +++ b/src/chkpar.c @@ -13,27 +13,31 @@ #include <stdio.h> #include <string.h> -noreturn static void agb_expparval(char const chrpar) { - fprintf(stderr,"Expected value for character parameter '%c'\n",chrpar); - agb_exi(agb_cnd_err,NULL); +noreturn static void +agb_expparval (char const chrpar) +{ + fprintf (stderr,"Expected value for character parameter '%c'\n", chrpar); + agb_exi (agb_cnd_err, NULL); } -static bool agb_chkchrpar(agb_dat * const restrict dat,char const* const restrict par) { +static bool +agb_chkchrpar (agb_dat * const restrict dat, char const* const restrict par) +{ char const chrpar = par[0x0u]; if (chrpar == '\x0') {return true;} char const* const restrict paramval = &par[0x1]; - + switch (chrpar) { default: - fprintf(stderr,"Invalid character parameter '%c'\n",chrpar); - agb_exi(agb_cnd_err,NULL); + fprintf (stderr,"Invalid character parameter '%c'\n", chrpar); + agb_exi (agb_cnd_err, NULL); case 'h': - agb_hlp(); - agb_exi(agb_cnd_oky,NULL); + agb_hlp (); + agb_exi (agb_cnd_oky, NULL); case 'i': { - if (paramval[0x0u] == '\x0') {agb_expparval(chrpar);} + if (paramval[0x0u] == '\x0') {agb_expparval (chrpar);} dat->pth = paramval; } return true; @@ -46,12 +50,12 @@ static bool agb_chkchrpar(agb_dat * const restrict dat,char const* const restric } } -void agb_chkpar(agb_dat * const restrict dat,int const argc,char const* const* const argv) { +void +agb_chkpar (agb_dat * const restrict dat, int const argc, char const* const* const argv) { if (argc < 0x2) { - agb_hlp(); - agb_exi(agb_cnd_oky,NULL); - } - else { + agb_hlp (); + agb_exi (agb_cnd_oky, NULL); + } else { size_t const numpar = argc; for (size_t pos = 0x1u;pos < numpar;++pos) { @@ -62,33 +66,33 @@ void agb_chkpar(agb_dat * const restrict dat,int const argc,char const* const* c char const* const lngparam = &par[0x2u]; if (lngparam[0x0u] == '\x0') { - fputs("Missing long parameter after '--' sequence\n",stderr); - agb_exi(agb_cnd_err,NULL); + fputs ("Missing long parameter after '--' sequence\n", stderr); + agb_exi (agb_cnd_err, NULL); } - if (!strcmp(lngparam,"help")) { - agb_hlp(); - agb_exi(agb_cnd_oky,NULL); + if (!strcmp (lngparam,"help")) { + agb_hlp (); + agb_exi (agb_cnd_oky, NULL); } - fprintf(stderr,"Invalid long parameter \"%s\"\n",lngparam); - agb_exi(agb_cnd_err,NULL); + fprintf (stderr,"Invalid long parameter \"%s\"\n", lngparam); + agb_exi (agb_cnd_err, NULL); } if (par[0x1u] == '\x0') { - fputs("Missing character parameter after '-'\n",stderr); - agb_exi(agb_cnd_err,NULL); + fputs ("Missing character parameter after '-'\n", stderr); + agb_exi (agb_cnd_err, NULL); } - for (char const* chrpos = &par[0x1u];;++chrpos) {if (agb_chkchrpar(dat,chrpos)) {break;}} + for (char const* chrpos = &par[0x1u];;++chrpos) {if (agb_chkchrpar (dat, chrpos)) {break;}} continue; } } if (dat->pth == NULL) { - fputs("ROM not set (missing character parameter 'i')\n",stderr); - agb_exi(agb_cnd_err,NULL); + fputs ("ROM not set (missing character parameter 'i')\n", stderr); + agb_exi (agb_cnd_err, NULL); } } } diff --git a/agbsum/src/exi.c b/src/exi.c index 4e0a80e..54514c0 100644 --- a/agbsum/src/exi.c +++ b/src/exi.c @@ -12,8 +12,10 @@ #include <stdio.h> #include <stdlib.h> -void agb_exi(agb_cnd const cnd,FILE * restrict rom) { - if (rom != NULL) {fclose(rom);} +noreturn void +agb_exi (agb_cnd const cnd, FILE * restrict rom) +{ + if (rom != NULL) {fclose (rom);} - exit(cnd == agb_cnd_oky ? EXIT_SUCCESS : EXIT_FAILURE); + exit (cnd == agb_cnd_oky ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/agbsum/src/getsum.c b/src/getsum.c index c6ce21b..b92fdef 100644 --- a/agbsum/src/getsum.c +++ b/src/getsum.c @@ -11,7 +11,9 @@ #include <stdint.h> -uint8_t agb_getsum(void const* const restrict romptr) { +uint8_t +agb_getsum (void const* const restrict romptr) +{ uint8_t const* restrict rom = romptr; uint8_t chksum = 0x0u; diff --git a/agbsum/src/hlp.c b/src/hlp.c index f6c87f6..63a5c08 100644 --- a/agbsum/src/hlp.c +++ b/src/hlp.c @@ -12,8 +12,10 @@ #include <inttypes.h> #include <stdio.h> -void agb_hlp(void) { - fprintf(stderr, +void +agb_hlp (void) +{ + fprintf (stderr, "agbsum - Calculate GBA ROM header checksums.\n" "Release #%" PRIX64 ". Copyright 2022-2023 Gabriel Jensen.\n" "\n" diff --git a/agbsum/src/inidat.c b/src/inidat.c index 31f6790..0657c70 100644 --- a/agbsum/src/inidat.c +++ b/src/inidat.c @@ -11,7 +11,8 @@ #include <stddef.h> -void agb_inidat(agb_dat * const restrict dat) { +void agb_inidat (agb_dat * const restrict dat) +{ dat->dopat = false; dat->pth = NULL; dat->sil = false; diff --git a/agbsum/src/main.c b/src/main.c index 0b277ae..44d8a96 100644 --- a/agbsum/src/main.c +++ b/src/main.c @@ -17,30 +17,31 @@ #error "agbsum only support 8-bit byte systems" #endif -int main(int const argc,char const* const* const argv) { +int main (int const argc, char const* const* const argv) +{ agb_dat dat; - agb_inidat(&dat); + agb_inidat (&dat); - agb_chkpar(&dat,argc,argv); + agb_chkpar (&dat, argc, argv); - dat.rom = agb_opn(dat.pth); + dat.rom = agb_opn (dat.pth); unsigned char buf[agb_chksumoff+0x1u]; - agb_red(buf,dat.rom); + agb_red (buf, dat.rom); - unsigned char const chksum = agb_getsum(buf); + unsigned char const chksum = agb_getsum (buf); unsigned char const romchksum = buf[agb_chksumoff]; if (romchksum == chksum || !dat.dopat) { - if (!dat.sil) {printf("\"%s\": %hhX (%hhX in file)\n",dat.pth,chksum,romchksum);} - agb_exi(agb_cnd_oky,dat.rom); + if (!dat.sil) {printf ("\"%s\": %hhX (%hhX in file)\n", dat.pth, chksum, romchksum);} + agb_exi (agb_cnd_oky, dat.rom); } - agb_pat(dat.rom,chksum); + agb_pat (dat.rom, chksum); - if (!dat.sil) {printf("\"%s\" @ %zX: %hhX => %hhX\n",dat.pth,agb_romsrt+agb_chksumoff,romchksum,chksum);} + if (!dat.sil) {printf ("\"%s\" @ %zX: %hhX => %hhX\n", dat.pth, agb_romsrt+agb_chksumoff, romchksum, chksum);} - agb_exi(agb_cnd_oky,dat.rom); + agb_exi (agb_cnd_oky, dat.rom); } diff --git a/agbsum/src/opn.c b/src/opn.c index d876c51..964e14c 100644 --- a/agbsum/src/opn.c +++ b/src/opn.c @@ -11,12 +11,14 @@ #include <stdio.h> -FILE * agb_opn(char const* const restrict pth) { - FILE * rom = fopen(pth,"r+"); +FILE * +agb_opn (char const* const restrict pth) +{ + FILE * rom = fopen (pth,"r+"); if (rom == NULL) { - fputs("Unable to open ROM\n",stderr); - agb_exi(agb_cnd_err,NULL); + fputs ("Unable to open ROM\n", stderr); + agb_exi (agb_cnd_err, NULL); } return rom; diff --git a/agbsum/src/pat.c b/src/pat.c index 12a4627..9313665 100644 --- a/agbsum/src/pat.c +++ b/src/pat.c @@ -11,11 +11,12 @@ #include <stdio.h> -void agb_pat(FILE * const restrict rom,unsigned char chksum) { - fseek(rom,(long)(agb_romsrt+agb_chksumoff),SEEK_SET); +void agb_pat (FILE * const restrict rom, unsigned char chksum) +{ + fseek (rom,(long)(agb_romsrt+agb_chksumoff), SEEK_SET); - if (fwrite(&chksum,0x1u,0x1u,rom) != 0x1u) { - fputs("Unable to patch ROM\n",stderr); - agb_exi(agb_cnd_err,rom); + if (fwrite (&chksum,0x1u,0x1u, rom) != 0x1u) { + fputs ("Unable to patch ROM\n", stderr); + agb_exi (agb_cnd_err, rom); } } diff --git a/agbsum/src/red.c b/src/red.c index 7cb5140..da9b5af 100644 --- a/agbsum/src/red.c +++ b/src/red.c @@ -11,12 +11,13 @@ #include <stdio.h> -void agb_red(void * const restrict buf,FILE * restrict rom) { - fseek(rom,agb_romsrt,SEEK_SET); // We only need to read the part of the ROM that is used for the checksum. +void agb_red (void * const restrict buf, FILE * restrict rom) +{ + fseek (rom, agb_romsrt, SEEK_SET); // We only need to read the part of the ROM that is used for the checksum. size_t const num = agb_chksumoff+0x1u; - if (fread(buf,0x1u,num,rom) != num) { - fputs("Unable to read ROM\n",stderr); - agb_exi(agb_cnd_err,rom); + if (fread (buf,0x1u, num, rom) != num) { + fputs ("Unable to read ROM\n", stderr); + agb_exi (agb_cnd_err, rom); } } |