$NetBSD: patch-ac,v 1.3 2012/12/23 18:43:47 dholland Exp $ - use standard headers - declare functions before calling them - use c89 function declarations - give functions return types, including sometimes "void" - silence clang and gcc warnings about assignments inside conditionals - silence clang warnings about braces - silence gcc warnings about unused/uninitialized variables - fix insecure-temporary-files - use snprintf() instead of sprintf() - call free() correctly - call qsort() correctly - pass gcc -Wall -Werror (with gcc 4.5) --- mapit.c.orig 1998-03-13 19:38:03.000000000 +0000 +++ mapit.c @@ -149,11 +149,18 @@ fix parse_status to handle retreat with mutilple alternate destinations */ #ifdef MACINTOSH -#include #include #include +#else +#include #endif +#include +#include +#include +#include + + #ifdef STRDUP_MISSING #ifdef ANSI char *strdup(char *s1); @@ -162,7 +169,7 @@ char *strdup(); #endif #endif -#ifdef STRRSTR_MISSING +#ifdef STRSTR_MISSING #ifdef ANSI char *strrstr(char *s1,char *s2); #else @@ -170,11 +177,13 @@ char *strrstr(); #endif #endif -#include -#include -#include - -extern char *strstr(); +#ifdef STRRSTR_MISSING +#ifdef ANSI +char *strrstr(char *s1,char *s2); +#else +char *strrstr(); +#endif +#endif #define MIN(x,y) (x ownerlist[i]) ownerlist[i] = ownerlist[i+1]; else ownerlist[i+1] = ownerlist[i]; + } + } - if(!(nstdout = fopen(tempfilename, "r"))) + if(!(nstdout = fdopen(fd, "r"))) fprintf(stderr,"Unable to reopen file.\n"); else { while(fgets(line, BUFSIZ, nstdout)) @@ -532,6 +560,7 @@ main(argc,argv) remove(tempfilename); } } + return 0; } @@ -555,12 +584,13 @@ main(argc,argv) location nick | full_name */ -parse_movement() +static void +parse_movement(void) { char line[BUFSIZ]; /* the current order being processed */ char copy[BUFSIZ]; /* a temp copy */ char *p0,*p1,*p2, *p3; /* some char pointers */ - int i,j,k; /* misc counters */ + int i; /* misc counters */ char country[BUFSIZ]; char country2[BUFSIZ]; @@ -587,13 +617,11 @@ parse_movement() char *msg; /* current bouce/cut message */ int fail; /* flag to indicate success of order */ - int x,y; /* map coordinate of support/convoy arrow */ int error; /* true if a syntax (or malloc) error */ for (i=0; i 0))) + ((p1=strrstr(p0," C "))!=NULL && (strncmp(p0,"OFF BOARD", 9) > 0))) order = CONVOY; - else if (p1=strstr(p0," PROXY GIVEN TO ")) + else if ((p1=strstr(p0," PROXY GIVEN TO ")) != NULL) order = PROXY; - else if ((p1=strstr(p0," -> ")) || - (p1=strstr(p0," - ")) || - (p1=strstr(p0," M ")) || - (p1=strstr(p0," TO ")) || - (p1=strstr(p0," MOVE"))) /* or MOVES */ + else if ((p1=strstr(p0," -> "))!=NULL || + (p1=strstr(p0," - "))!=NULL || + (p1=strstr(p0," M "))!=NULL || + (p1=strstr(p0," TO "))!=NULL || + (p1=strstr(p0," MOVE"))!=NULL) /* or MOVES */ order = MOVE; - else if ((p1=strstr(p0," HOLD")) || /* or HOLDS */ - (p1=strstr(p0," H."))) + else if ((p1=strstr(p0," HOLD"))!=NULL || /* or HOLDS */ + (p1=strstr(p0," H."))!=NULL) order = HOLD; - else if ((p1=strstr(p0,", NO ORDER PROCESSED")) || - (p1=strstr(p0,", NO MOVE RECEIVED")) || - (p1=strstr(p0," NMR"))) + else if ((p1=strstr(p0,", NO ORDER PROCESSED"))!=NULL || + (p1=strstr(p0,", NO MOVE RECEIVED"))!=NULL || + (p1=strstr(p0," NMR"))!=NULL) order = NMR; if (order == UNKNOWN) { @@ -774,33 +802,35 @@ parse_movement() fprintf(stderr,"movement: unable to parse destination location in convoy command: %s\n",p1); break; } - sprintf(new," %c %.3s C %.3s - %-6.6s %.9s", + snprintf(new, sizeof(new), " %c %.3s C %.3s - %-6.6s %.9s", unit,map[si].nick,map[di].nick,map[di2].nick,msg); if (fail) - sprintf(newg,"FailedOrder %d %d %d %d %d %d ArrowConvoy OkOrder", + snprintf(newg, sizeof(newg), + "FailedOrder %d %d %d %d %d %d ArrowConvoy OkOrder", map[si].x,map[si].y, map[di].x,map[di].y, map[di2].x,map[di2].y); else - sprintf(newg,"%d %d %d %d %d %d ArrowConvoy", + snprintf(newg, sizeof(newg), + "%d %d %d %d %d %d ArrowConvoy", map[si].x,map[si].y, map[di].x,map[di].y, map[di2].x,map[di2].y); break; case HOLD: - sprintf(new," %c %.3s H %.9s", + snprintf(new, sizeof(new), " %c %.3s H %.9s", unit,map[si].nick,msg); break; - case PROXY: - strcpy(country2, strstr(p1, "TO ") + 3); - sprintf(new," %c %.3s PROXY TO %s %.9s", - unit,map[si].nick,country2,msg); + case PROXY: + strcpy(country2, strstr(p1, "TO ") + 3); + snprintf(new, sizeof(new), " %c %.3s PROXY TO %s %.9s", + unit,map[si].nick,country2,msg); - break; + break; case NMR: - sprintf(new," %c %.3s NMR %.9s", + snprintf(new, sizeof(new), " %c %.3s NMR %.9s", unit,map[si].nick,msg); break; @@ -824,8 +854,7 @@ parse_movement() p0 = NULL; /* couldn't find unit type */ p1 = p2; /* assume no nationality either */ } else { - if (free(p2) == 0) - ; + free(p2); } } @@ -856,14 +885,15 @@ parse_movement() fprintf(stderr,"movement: unable to parse target of support command: %s\n",p0); break; } - sprintf(new," %c %.3s S %.3s H %.9s", + snprintf(new, sizeof(new), " %c %.3s S %.3s H %.9s", unit,map[si].nick,map[di].nick,msg); if (fail) - sprintf(newg,"FailedOrder %d %d %d %d ArrowHold OkOrder", + snprintf(newg, sizeof(newg), + "FailedOrder %d %d %d %d ArrowHold OkOrder", map[si].x,map[si].y, map[di].x,map[di].y); else - sprintf(newg,"%d %d %d %d ArrowHold", + snprintf(newg, sizeof(newg), "%d %d %d %d ArrowHold", map[si].x,map[si].y, map[di].x,map[di].y); } else { /* support a unit moving */ @@ -878,15 +908,16 @@ parse_movement() fprintf(stderr,"movement: unable to parse second target of support command: %s\n",p1); break; } - sprintf(new," %c %.3s S %.3s - %-6.6s %.9s", + snprintf(new, sizeof(new), " %c %.3s S %.3s - %-6.6s %.9s", unit,map[si].nick,map[di].nick,map[di2].nick,msg); if (fail) - sprintf(newg,"FailedOrder %d %d %d %d %d %d ArrowSupport OkOrder", + snprintf(newg, sizeof(newg), + "FailedOrder %d %d %d %d %d %d ArrowSupport OkOrder", map[si].x,map[si].y, map[di].x,map[di].y, map[di2].x,map[di2].y); else - sprintf(newg,"%d %d %d %d %d %d ArrowSupport", + snprintf(newg, sizeof(newg), "%d %d %d %d %d %d ArrowSupport", map[si].x,map[si].y, map[di].x,map[di].y, map[di2].x,map[di2].y); @@ -899,16 +930,17 @@ parse_movement() fprintf(stderr,"movement: unable to parse destination of move command: %s\n",p1); break; } - sprintf(new," %c %.3s - %-6.6s %.9s", + snprintf(new, sizeof(new), " %c %.3s - %-6.6s %.9s", unit,map[si].nick,map[di].nick,msg); if (fail) - sprintf(newg,"FailedOrder %d %d %d %d ArrowMove OkOrder", + snprintf(newg, sizeof(newg), + "FailedOrder %d %d %d %d ArrowMove OkOrder", map[si].x,map[si].y, map[di].x,map[di].y); else { units[unitcount].loc = di; - sprintf(newg,"%d %d %d %d ArrowMove", + snprintf(newg, sizeof(newg), "%d %d %d %d ArrowMove", map[si].x,map[si].y, map[di].x,map[di].y); } @@ -948,16 +980,14 @@ parse_movement() fprintf(nstdout,"OrderReport\n"); for (i = 0; i0) { for (i = 0; i instead. */ -parse_fall_report() +static void +parse_fall_report(void) { char line[BUFSIZ],copy[BUFSIZ],temp[BUFSIZ]; char report[BUFSIZ]; @@ -1331,7 +1363,7 @@ parse_fall_report() char country[BUFSIZ],save[BUFSIZ]; int centers,units; int ci; /* index of center in map database */ - int i,j,k; + int i; /* first we have ownership report */ @@ -1346,7 +1378,7 @@ parse_fall_report() fprintf(stderr,"ownership: expecting continuation line\n\t%s\n",line); break; } - sprintf(temp,"%s %s",save,line); + snprintf(temp, sizeof(temp), "%s %s",save,line); copyline(copy,temp); } else copyline(copy,line); @@ -1363,7 +1395,7 @@ parse_fall_report() strcpy(country,p0); if (report[0]!='\0') fprintf(nstdout,"(%s) WriteOwner\n",report); - sprintf(report,"%-10s ",country); + snprintf(report, sizeof(report), "%-10s ", country); } if (p1==NULL || *p1=='\0') continue; /* just a country token, get next line */ @@ -1439,17 +1471,16 @@ If it is a Status Report, it might inclu [country:] unit location [can retreat to location [or location]] */ -parse_start(report) - int report; +static void +parse_start(int report) { char line[BUFSIZ]; /* the current order being processed */ char copy[BUFSIZ]; /* a temp copy */ char *p0,*p1,*p2; /* some char pointers */ - int i,j,k; /* misc counters */ + int i; /* misc counters */ char country[BUFSIZ]; char unit; /* unit type, "A" or "F" */ - int order; /* one of the following: */ int si; /* index of source location) */ #define MAXORDERS MAXUNITS @@ -1457,12 +1488,10 @@ parse_start(report) char new[BUFSIZ]; /* current order being formed */ int ordernum; /* count of orders */ - int x,y; /* map coordinate of support/convoy arrow */ int error; /* true if a syntax (or malloc) error */ for (i=0; i ???)",unit,map[si].nick); + snprintf(new, sizeof(new), + " %c %s (-> %s)", unit, map[si].nick, "??" "?"); else - sprintf(new," %c %s (-> %s)",unit,map[si].nick,p2); + snprintf(new, sizeof(new), " %c %s (-> %s)", unit, map[si].nick, p2); else { - sprintf(new," %c %s",unit,map[si].nick); + snprintf(new, sizeof(new), " %c %s",unit,map[si].nick); /* remember that we have a unit at this location */ units[unitcount].country = (char *) strdup(country); @@ -1600,15 +1630,14 @@ parse_start(report) fprintf(nstdout,"OrderReport\n"); for (i = 0; icountry,u2->country); } -drawmap(title,message) - char *title,*message; +static void +drawmap(char *title, char *message) { int i; @@ -1666,9 +1698,10 @@ drawmap(title,message) } /* given the new unit positions, draw a new map */ -drawnew() +static void +drawnew(void) { - int i,j,k; + int i; char country[BUFSIZ]; if (unitcount==0) @@ -1703,15 +1736,14 @@ drawnew() if (units[i].loc>0) fprintf(nstdout,"( %c %s) WriteOrder\n", units[i].type,map[units[i].loc].nick); - if (free(units[i].country) == 0) - ; + free(units[i].country); } unitcount= 0; } /* copy s2 to s1, uppercase, squeeze whitespace */ -copyline(s1,s2) - char *s1,*s2; +static void +copyline(char *s1, const char *s2) { int white; /* white space seen */ @@ -1724,8 +1756,8 @@ copyline(s1,s2) } } else { white = 0; - if (islower(*s2)) - *(s1++) = toupper(*s2); + if (islower((unsigned char)*s2)) + *(s1++) = toupper((unsigned char)*s2); else *(s1++) = *s2; } @@ -1736,8 +1768,8 @@ copyline(s1,s2) } -string_check(s,fatal) - char *s; int fatal; +static int +string_check(const char *s, int fatal) { if (s==NULL) { fprintf(stderr,"?: unexpected error, null string pointer\n"); @@ -1753,13 +1785,14 @@ string_check(s,fatal) } /* init reads the 2 files described above... */ -init() +static int +init(void) { char *fn; FILE *f; char line[BUFSIZ],copy[BUFSIZ],nick[BUFSIZ],owner[BUFSIZ], name[BUFSIZ],nicks[BUFSIZ]; int x,y; - int i,j,k; + int i,j; unitcount = 0; nationcount = 0; @@ -1771,7 +1804,7 @@ init() fn = (char *)getenv("MAPPS"); if (fn == NULL) fn = DEFAULT_PS; - if (f = fopen(fn,"r")) { + if ((f = fopen(fn,"r")) != NULL) { while (fgets(line,BUFSIZ,f)) fprintf(nstdout,"%s",line); fclose(f); @@ -1795,7 +1828,7 @@ init() fn = (char *)getenv("MAPINFO"); if (fn == NULL) fn = DEFAULT_INFO; - if (f = fopen(fn,"r")) { + if ((f = fopen(fn,"r")) != NULL) { j = 0; while (fgets(line,BUFSIZ,f)) { /* first section is just a list of nations */ if (line[0]=='#' || line[0]==' ') @@ -1848,13 +1881,13 @@ init() return 0; } -mapi(name) /* return index of map table entry */ - char *name; +static int +mapi(char *name) /* return index of map table entry */ { - int i,j,k; + int i; char t[BUFSIZ]; - sprintf(t,"|%s|",name); + snprintf(t, sizeof(t), "|%s|", name); for ( i=1; i