do not abuse dirent.
sprinkle snprintf around while we're at it.
This commit is contained in:
parent
24c4a06162
commit
5786079d91
@ -1,12 +1,96 @@
|
||||
$OpenBSD: patch-moon_lander_c,v 1.1.1.1 2003/04/15 16:01:42 espie Exp $
|
||||
--- moon_lander.c.orig Wed Aug 22 15:52:28 2001
|
||||
+++ moon_lander.c Thu Apr 10 09:06:35 2003
|
||||
@@ -37,7 +37,7 @@
|
||||
$OpenBSD: patch-moon_lander_c,v 1.2 2003/04/16 10:31:24 espie Exp $
|
||||
--- moon_lander.c.orig Wed Aug 22 22:52:28 2001
|
||||
+++ moon_lander.c Wed Apr 16 12:28:42 2003
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
+#include <string.h>
|
||||
#include <math.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
@@ -37,7 +38,7 @@
|
||||
#define YSIZE 480
|
||||
#define TERRAIN_YSIZE (YSIZE / 2)
|
||||
#define FPS (1000 / 35)
|
||||
-#define DATAPATH ""
|
||||
+#define DATAPATH "%%PREFIX%%/share/moonlander/"
|
||||
+#define DATAPATH "/usr/local/share/moonlander/"
|
||||
|
||||
#define FRESHRUN 0
|
||||
#define GAMEOVER 1
|
||||
@@ -175,9 +176,10 @@ void get_new_background(Game *game) {
|
||||
|
||||
char filename[1024];
|
||||
DIR *dir;
|
||||
- struct dirent *files[100];
|
||||
- int done = 0;
|
||||
+ char *bg[100];
|
||||
+ struct dirent *d;
|
||||
int count = 0;
|
||||
+ int i;
|
||||
|
||||
/* read images/backgrounds dir and choose a random image from there.
|
||||
* put it's filename in image_file
|
||||
@@ -185,27 +187,20 @@ void get_new_background(Game *game) {
|
||||
|
||||
|
||||
|
||||
- sprintf(filename, "%simages/backgrounds", DATAPATH);
|
||||
+ snprintf(filename, sizeof filename, "%simages/backgrounds", DATAPATH);
|
||||
|
||||
if ( !(dir = opendir(filename)) ){
|
||||
/* error */
|
||||
printf("cannot open dir %s\n", filename);
|
||||
exit(0);
|
||||
}
|
||||
-
|
||||
- while (!done){
|
||||
- if ( files[count] = readdir(dir) ){
|
||||
-
|
||||
- //printf("I see - %d %s\n", count, files[count]->d_name);
|
||||
- count++;
|
||||
- }
|
||||
- else{
|
||||
- done = 1;
|
||||
- }
|
||||
-
|
||||
- if (count > 99) {
|
||||
- done = 1;
|
||||
- }
|
||||
+
|
||||
+ while ( (d = readdir(dir)) != NULL){
|
||||
+ if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
+ continue;
|
||||
+ bg[count++] = strdup(d->d_name);
|
||||
+ if (count >= 100)
|
||||
+ break;
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
@@ -221,12 +216,12 @@ void get_new_background(Game *game) {
|
||||
|
||||
game->back_no++;
|
||||
|
||||
- if (game->back_no < 2){
|
||||
- game->back_no = 2;
|
||||
+ if (game->back_no < 0){
|
||||
+ game->back_no = 0;
|
||||
}
|
||||
|
||||
if (game->back_no >= count){
|
||||
- game->back_no = 2;
|
||||
+ game->back_no = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -240,7 +235,9 @@ void get_new_background(Game *game) {
|
||||
|
||||
//printf("about to get new background: %d\n", game->back_no );
|
||||
|
||||
- sprintf(filename, "%simages/backgrounds/%s", DATAPATH, files[game->back_no]->d_name);
|
||||
+ snprintf(filename, sizeof(filename), "%simages/backgrounds/%s", DATAPATH, bg[game->back_no]);
|
||||
+ for (i = 0; i < count; i++)
|
||||
+ free(bg[i]);
|
||||
|
||||
// printf("got %s\n", filename);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user