projects
/
freeglut
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Use vsnprintf instead of the potentially dangerous sprintf to avoid
[freeglut]
/
progs
/
demos
/
Lorenz
/
lorenz.c
diff --git
a/progs/demos/Lorenz/lorenz.c
b/progs/demos/Lorenz/lorenz.c
index
f4372d6
..
98361bc
100644
(file)
--- a/
progs/demos/Lorenz/lorenz.c
+++ b/
progs/demos/Lorenz/lorenz.c
@@
-29,6
+29,7
@@
/* Include Files */
#include <stdio.h>
#include <stdlib.h>
/* Include Files */
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <GL/freeglut.h>
#include <string.h>
#include <math.h>
#include <GL/freeglut.h>
@@
-107,6
+108,16
@@
void advance_in_time ( double time_step, double position[3], double new_position
( deriv0[i] + 2.0 * ( deriv1[i] + deriv2[i] ) + deriv3[i] ) ;
}
( deriv0[i] + 2.0 * ( deriv1[i] + deriv2[i] ) + deriv3[i] ) ;
}
+static void
+checkedFGets ( char *s, int size, FILE *stream )
+{
+ if ( fgets ( s, size, stream ) == NULL ) {
+ fprintf ( stderr, "fgets failed\n");
+ exit ( EXIT_FAILURE );
+ }
+}
+
+
/* GLUT callbacks */
#define INPUT_LINE_LENGTH 80
/* GLUT callbacks */
#define INPUT_LINE_LENGTH 80
@@
-141,16
+152,16
@@
void key_cb ( unsigned char key, int x, int y )
break ;
case 'm' : case 'M' : /* Modify the Lorenz parameters */
break ;
case 'm' : case 'M' : /* Modify the Lorenz parameters */
- printf ( "Please enter new value for <sigma> (default %lf, currently %lf): ", s0, sigma ) ;
- fgets ( inputline, INPUT_LINE_LENGTH-1, stdin ) ;
+ printf ( "Please enter new value for <sigma> (default %f, currently %f): ", s0, sigma ) ;
+ checkedFGets ( inputline, sizeof ( inputline ), stdin ) ;
sscanf ( inputline, "%lf", &sigma ) ;
sscanf ( inputline, "%lf", &sigma ) ;
- printf ( "Please enter new value for <b> (default %lf, currently %lf): ", b0, b ) ;
- fgets ( inputline, INPUT_LINE_LENGTH-1, stdin ) ;
+ printf ( "Please enter new value for <b> (default %f, currently %f): ", b0, b ) ;
+ checkedFGets ( inputline, sizeof ( inputline ), stdin ) ;
sscanf ( inputline, "%lf", &b ) ;
sscanf ( inputline, "%lf", &b ) ;
- printf ( "Please enter new value for <r> (default %lf, currently %lf): ", r0, r ) ;
- fgets ( inputline, INPUT_LINE_LENGTH-1, stdin ) ;
+ printf ( "Please enter new value for <r> (default %f, currently %f): ", r0, r ) ;
+ checkedFGets ( inputline, sizeof ( inputline ), stdin ) ;
sscanf ( inputline, "%lf", &r ) ;
break ;
sscanf ( inputline, "%lf", &r ) ;
break ;
@@
-232,10
+243,19
@@
void draw_curve ( int index, double position [ NUM_POINTS ][3] )
glEnd () ;
}
glEnd () ;
}
-void display_cb ( void )
+void bitmapPrintf (const char *fmt, ...)
{
{
- char string [ 80 ] ;
+ static char buf[256];
+ va_list args;
+
+ va_start(args, fmt);
+ (void) vsnprintf (buf, sizeof(buf), fmt, args);
+ va_end(args);
+ glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
+}
+void display_cb ( void )
+{
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
@@
-257,9
+277,8
@@
void display_cb ( void )
/* Print the distance between the two points */
glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
/* Print the distance between the two points */
glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
- sprintf ( string, "Distance: %10.6lf", distance ) ;
- glRasterPos2i ( 10, 10 ) ;
- glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char *)string ) ;
+ glRasterPos2i ( 1, 1 ) ;
+ bitmapPrintf ( "Distance: %10.6f", distance ) ;
glutSwapBuffers();
}
glutSwapBuffers();
}
@@
-347,7
+366,7
@@
int main ( int argc, char *argv[] )
glutMainLoop () ;
#ifdef WIN32
glutMainLoop () ;
#ifdef WIN32
- /* DUMP MEMORY LEAK INFORMATION */
+ /* DUMP MEMORY LEAK INFORMATION */
_CrtDumpMemoryLeaks () ;
#endif
_CrtDumpMemoryLeaks () ;
#endif