Skip to content

Commit

Permalink
- Fix many unsafe printing and formatting.
Browse files Browse the repository at this point in the history
- Fix some compiler warnings.
  • Loading branch information
patlefort committed Apr 24, 2021
1 parent 1f36624 commit cfcb3fd
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 192 deletions.
11 changes: 6 additions & 5 deletions bot.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,7 @@ static void change_level(void)
reinit_area();

LEVEL=load_level(level_number);
snprintf(txt,256,"Loading level \"%s\".\n",LEVEL);
ERROR(txt);
ERROR("Loading level \"%s\".\n",LEVEL);
snprintf(txt,256,"%s%s%s",DATA_PATH,LEVEL,LEVEL_SPRITES_SUFFIX);
load_sprites(txt);
snprintf(txt,256,"%s%s%s",DATA_PATH,LEVEL,STATIC_DATA_SUFFIX);
Expand Down Expand Up @@ -879,6 +878,8 @@ static int process_packet(char *packet,int l)
else printf("Game terminated by %s.\n",packet+1);
n=2+strlen(packet+1);
shut_down(1);

[[fallthrough]];

case P_INFO:
if (l<=5)break;
Expand Down Expand Up @@ -1138,12 +1139,12 @@ int main(int argc,char **argv)
#endif

ERROR("Resolving server address ...\n");
if ((m=find_server(host,port))){ERROR(m);EXIT(1);}
if ((m=find_server(host,port))){ERROR("%s", m);EXIT(1);}
ERROR("Initializing socket ...\n");
if ((m=init_socket())){ERROR(m);EXIT(1);}
if ((m=init_socket())){ERROR("%s", m);EXIT(1);}
ERROR("Contacting server ...\n");
color=(random()%15)+1;
if ((m=contact_server(color,select_name()))){ERROR(m);EXIT(1);}
if ((m=contact_server(color,select_name()))){ERROR("%s", m);EXIT(1);}

connected=1;
ERROR("OK, connected.\nPlaying...\n");
Expand Down
8 changes: 4 additions & 4 deletions cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ extern int tri_d;
#undef ERROR
#ifdef SERVER
extern int consoleApp;
#define ERROR(a) {if (!console_ok)c_shutdown();if(consoleApp)fprintf(stderr,a);}
#define ERROR(...) {if (!console_ok)c_shutdown();if(consoleApp)fprintf(stderr,__VA_ARGS__);}
#define EXIT(a) {if (!consoleApp)ReportStatusToSCMgr(SERVICE_STOPPED, NO_ERROR, 0);exit(a);}
#else
#define ERROR(a) {if (!console_ok)c_shutdown();fprintf(stderr,a);}
#define ERROR(...) {if (!console_ok)c_shutdown();fprintf(stderr,__VA_ARGS__);}
#define EXIT(a) {exit(a);}
#endif
#define random rand
Expand All @@ -163,9 +163,9 @@ extern int tri_d;

#define long_long long long
#ifdef CLIENT
#define ERROR(a) {if (!console_ok)c_shutdown();fprintf(stderr,a);}
#define ERROR(...) {if (!console_ok)c_shutdown();fprintf(stderr,__VA_ARGS__);}
#else
#define ERROR(a) {fprintf(stderr,a);}
#define ERROR(...) {fprintf(stderr,__VA_ARGS__);}
#endif

#include <stdlib.h>
Expand Down
6 changes: 2 additions & 4 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@ void print_error(char *text)
/* draw initial screen */
void menu_screen(struct config *cfg)
{
char txt[8];
char txt[32];
int sprite,anim=0,title_anim=0,bulge_anim=0;
unsigned long_long t;
char port[MAX_PORT_LEN+1];
Expand All @@ -1759,11 +1759,9 @@ void menu_screen(struct config *cfg)
snprintf(txt, sizeof(txt), "hero%d",cfg->color);
if (find_sprite(txt,&sprite))
{
char msg[256];
mem_free(banner);
shut_down(0);
snprintf(msg,256,"Error: Can't find sprite \"%s\".\n",txt);
ERROR(msg);
ERROR("Error: Can't find sprite \"%s\".\n",txt);
EXIT(1);
}

Expand Down
29 changes: 14 additions & 15 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ void my_print(char *str)
void c_refresh(void)
{
#ifdef SCREEN_BUFFERING
ssize_t ret = 0;
ret = write(1,screen_buffer,screen_buffer_pos);
write(1,screen_buffer,screen_buffer_pos);
screen_buffer_pos=0;
#else
fflush(stdout);
Expand Down Expand Up @@ -154,42 +153,42 @@ void c_shutdown(void)
/* move cursor to [x,y] */
void c_goto(int x,int y)
{
char txt[16];
char txt[32];

#ifdef TRI_D
if (TRI_D_ON&&tri_d)x+=screen_width;
#endif
snprintf(txt,16,"\033[%d;%dH",y+1,x+1);
snprintf(txt,32,"\033[%d;%dH",y+1,x+1);
my_print(txt);
}


/* set complete foreground color */
void c_setcolor(unsigned char a)
{
char txt[16];
char txt[32];
a&=15;
snprintf(txt,16,"\033[%c;%dm",(a>>3)+'0',30+(a&7));
snprintf(txt,32,"\033[%c;%dm",(a>>3)+'0',30+(a&7));
my_print(txt);
}


/* set complete foreground color and background color */
void c_setcolor_bg(unsigned char fg,unsigned char bg)
{
char txt[16];
char txt[32];
fg&=15;
if (!(fg>>3)&&!bg) snprintf(txt,16,"\033[%c;%dm",(fg>>3)+'0',30+(fg&7));
else snprintf(txt,16,"\033[%c;%d;%dm",(fg>>3)+'0',40+(bg&7),30+(fg&7));
if (!(fg>>3)&&!bg) snprintf(txt,32,"\033[%c;%dm",(fg>>3)+'0',30+(fg&7));
else snprintf(txt,32,"\033[%c;%d;%dm",(fg>>3)+'0',40+(bg&7),30+(fg&7));
my_print(txt);
}


/* set background color */
void c_setbgcolor(unsigned char a)
{
char txt[16];
snprintf(txt,16,"\033[%dm",40+(a&7));
char txt[32];
snprintf(txt,32,"\033[%dm",40+(a&7));
my_print(txt);
}

Expand All @@ -204,9 +203,9 @@ void c_sethlt(unsigned char a)
/* set highlight and background color */
void c_sethlt_bg(unsigned char hlt,unsigned char bg)
{
char txt[16];
char txt[32];

snprintf(txt,16,"\033[%d;%dm",hlt&1,40+(bg&7));
snprintf(txt,32,"\033[%d;%dm",hlt&1,40+(bg&7));
my_print(txt);
}

Expand All @@ -223,9 +222,9 @@ void c_setcolor_3b(unsigned char a)
/* set 3 bit foreground color and background color*/
void c_setcolor_3b_bg(unsigned char fg,unsigned char bg)
{
char txt[16];
char txt[32];

snprintf(txt,16,"\033[%d;%dm",30+(fg&7),40+(bg&7));
snprintf(txt,32,"\033[%d;%dm",30+(fg&7),40+(bg&7));
my_print(txt);
}

Expand Down
16 changes: 4 additions & 12 deletions data.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ void load_data(char * filename)
if (!fopen_s(&stream,filename,"rb"))
#endif
{
char msg[256];
snprintf(msg,256,"Can't open file \"%s\"!\n",filename);
ERROR(msg);
ERROR("Can't open file \"%s\"!\n",filename);
EXIT(1);
}
while(fgets(line,1024,stream))
Expand All @@ -217,9 +215,7 @@ void load_data(char * filename)
_skip_ws(&p);
if ((t=_convert_type(*p))<0)
{
char msg[256];
snprintf(msg,256,"Unknown object type '%c'.\n",*p);
ERROR(msg);
ERROR("Unknown object type '%c'.\n",*p);
EXIT(1);
}
p++;
Expand All @@ -229,9 +225,7 @@ void load_data(char * filename)
y=strtol(q,&p,0);
if (find_sprite(name,&n))
{
char msg[256];
snprintf(msg,256,"Unknown bitmap name \"%s\"!\n",name);
ERROR(msg);
ERROR("Unknown bitmap name \"%s\"!\n",name);
EXIT(1);
}
_put_sprite(AREA_X,AREA_Y,area,area_a,x,y,sprites[n].positions,t,0);
Expand All @@ -256,9 +250,7 @@ void load_sprites(char * filename)
if((err = fopen_s(&stream,filename,"rb")) != 0)
#endif
{
char msg[256];
snprintf(msg,256,"Can't open file \"%s\"!\n",filename);
ERROR(msg);
ERROR("Can't open file \"%s\"!\n",filename);
EXIT(1);
}
while(fgets(line,1024,stream))
Expand Down
16 changes: 4 additions & 12 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ void load_room(char * filename)

if (!(stream=fopen(filename,"rb")))
{
char msg[256];
snprintf(msg,sizeof(msg),"Can't open file \"%s\"!\n",filename);
ERROR(msg);
ERROR("Can't open file \"%s\"!\n",filename);
EXIT(1);
}
while(fgets(line,1024,stream))
Expand All @@ -141,9 +139,7 @@ void load_room(char * filename)
y=strtol(q,&p,0);
if (find_sprite(name,&n))
{
char msg[256];
snprintf(msg,sizeof(msg),"Unknown bitmap name \"%s\"!\n",name);
ERROR(msg);
ERROR("Unknown bitmap name \"%s\"!\n",name);
EXIT(1);
}
new_obj(id,t,0,n,0,0,x,y,0,0,0);
Expand Down Expand Up @@ -272,9 +268,7 @@ void save_data(void)
data=fopen(txt,"w");
if (!data)
{
char msg[256];
snprintf(msg,256,"Can't create file \"%s\"\n",txt);
ERROR(msg);
ERROR("Can't create file \"%s\"\n",txt);
EXIT(1);
}

Expand All @@ -283,9 +277,7 @@ void save_data(void)
dynamic=fopen(txt,"w");
if (!dynamic)
{
char msg[256];
snprintf(msg,256,"Can't create file \"%s\"\n",txt);
ERROR(msg);
ERROR("Can't create file \"%s\"\n",txt);
EXIT(1);
}

Expand Down
3 changes: 1 addition & 2 deletions kbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ int os2_read_mon(unsigned char *data, int len)
int my_getchar(void)
{
unsigned char a;
ssize_t ret = 0;
ret = read(0,&a,1);
read(0,&a,1);
return a;
}

Expand Down
Loading

0 comments on commit cfcb3fd

Please sign in to comment.