diff --git a/.gitignore b/.gitignore index 0bd93cb..fe95aec 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ petratc +obj/ diff --git a/Makefile b/Makefile index 939c4c4..9bbfb11 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,11 @@ CFLAGS = -Wall -Wextra -O2 -pipe LDLIBS = -lncurses -ltinfo TARGET = petratc SRC = petratc.c rat.c $(wildcard games/*.c) +OBJ = $(SRC:%.c=obj/%.o) PREFIX = /usr/local -$(TARGET): $(SRC) - $(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDLIBS) +$(TARGET): $(OBJ) + $(CC) $(CFLAGS) -o $(TARGET) $(OBJ) $(LDLIBS) install: $(TARGET) install -D $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(TARGET) @@ -14,5 +15,9 @@ install: $(TARGET) uninstall: rm -f $(DESTDIR)$(PREFIX)/bin/$(TARGET) +obj/%.o: %.c + @mkdir -p $(@D) + $(CC) $(CFLAGS) -c $< -o $@ + clean: - rm -f $(TARGET) + rm -f $(TARGET) $(OBJ) diff --git a/games/gameclicker.c b/games/gameclicker.c index 4dff0b6..2ac2ca3 100644 --- a/games/gameclicker.c +++ b/games/gameclicker.c @@ -173,7 +173,7 @@ static bignum divmixregbig(bignum a, double regnum){ static char *bignumprint(bignum b) { //32 bytes is more than enough for 124.24 trillion idk - char *nice = malloc(32 * sizeof(char)); + char *nice = malloc(32 * sizeof(char)); if(nice == NULL) return NULL; if(b.y >= 0 && b.y < 12) { @@ -181,7 +181,7 @@ static char *bignumprint(bignum b) { } else{ snprintf(nice, 32, "%.2lfe3*%d", b.x, b.y); } - return nice; + return nice; } @@ -239,8 +239,12 @@ static void drawwin(WINDOW *win, bignum cheeses){ char *scorestr = bignumprint(cheeses); char scoremsg[64]; - snprintf(scoremsg, sizeof(scoremsg), "cheese: %s", scorestr); - free(scorestr); + if(scorestr != NULL) { + snprintf(scoremsg, sizeof(scoremsg), "cheese: %s", scorestr); + free(scorestr); + } else { + snprintf(scoremsg, sizeof(scoremsg), "cheese: ERROR"); + } if(mode == layoutwide){ mvwprintw(win, 1, scorecol, "%s", scoremsg); diff --git a/games/gameguess.c b/games/gameguess.c index fc29839..47a0be9 100644 --- a/games/gameguess.c +++ b/games/gameguess.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "../rat.h" @@ -42,6 +43,7 @@ void gameguess(){ int solved = 0; int range = 100; + srand(time(NULL)); int correct = (rand() % (range + 1)); int history[maxhistory]; diff --git a/games/gamemaze.c b/games/gamemaze.c index 1468475..3ce8737 100644 --- a/games/gamemaze.c +++ b/games/gamemaze.c @@ -64,7 +64,15 @@ void lerw() { char *vis = calloc(total, 1); //visited int *px = malloc(total * sizeof(int)); //path x int *py = malloc(total * sizeof(int)); //path y - int *ip = malloc(total * sizeof(int)); // index of path + int *ip = malloc(total * sizeof(int)); // index of path + + if(vis == NULL || px == NULL || py == NULL || ip == NULL) { + free(vis); + free(px); + free(py); + free(ip); + return; + } vis[0] = 1; grid[1][1] = grid[1][2] = grid[2][1] = grid[2][2] = ' '; //these should be open always int vc = 1; @@ -203,8 +211,20 @@ static void properrestart(cursor *curs, int *W, int *H) { *H = height; grid = malloc(*H * sizeof(char *)); - for (int y = 0; y < *H; y++) + if(grid == NULL) { + running = 0; + return; + } + for (int y = 0; y < *H; y++) { grid[y] = malloc(*W * sizeof(char)); + if(grid[y] == NULL) { + // Free already allocated rows + for(int i = 0; i < y; i++) free(grid[i]); + free(grid); + running = 0; + return; + } + } lerw(); spawncheeses(); @@ -225,9 +245,20 @@ void gamemaze(){ int W = width; int H = height; - grid = malloc(H * sizeof(char *)); //those who allocate memory - for (int y = 0; y < H; y++) - grid[y] = malloc(W * sizeof(char)); + grid = malloc(H * sizeof(char *)); //those who allocate memory + if(grid == NULL) { + stop(); + return; + } + for (int y = 0; y < H; y++) { + grid[y] = malloc(W * sizeof(char)); + if(grid[y] == NULL) { + for(int i = 0; i < y; i++) free(grid[i]); + free(grid); + stop(); + return; + } + } lerw(); spawncheeses(); diff --git a/rat.c b/rat.c index 6c504ba..e713cc4 100644 --- a/rat.c +++ b/rat.c @@ -99,9 +99,10 @@ WINDOW *ratdrawbox(void){ } int wrapprint(WINDOW *win, int row, int col, int maxwidth, const char *msg){ + if(msg == NULL) return row; char copy[256]; - strncpy(copy, msg, sizeof(copy)); + strncpy(copy, msg, sizeof(copy) - 1); copy[sizeof(copy)-1] = '\0'; char line[256] = ""; @@ -117,8 +118,14 @@ int wrapprint(WINDOW *win, int row, int col, int maxwidth, const char *msg){ extra = 0; } - if(linelen > 0) strcat(line, " "); - strcat(line, word); + if(linelen > 0) { + if(strlen(line) + 1 < sizeof(line)) { + strcat(line, " "); + } + } + if(strlen(line) + strlen(word) < sizeof(line)) { + strcat(line, word); + } word = strtok(NULL, " "); }