From 0e3adea64ee8f6c90db1371016eab460c5b31f60 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Wed, 28 Mar 2012 19:43:21 +0000 Subject: [PATCH] Replace various occurences of "Bstrncpy(buf, src, sz); buf[sz-1]=0;" with Bstrncpyz git-svn-id: https://svn.eduke32.com/eduke32@2559 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/src/build.c | 21 +++++++-------------- polymer/eduke32/build/src/config.c | 4 ++-- polymer/eduke32/build/src/mdsprite.c | 9 ++++----- polymer/eduke32/build/src/polymost.c | 2 +- polymer/eduke32/build/src/sdlayer.c | 9 +++------ polymer/eduke32/source/anim.c | 3 +-- polymer/eduke32/source/astub.c | 28 ++++++++++------------------ polymer/eduke32/source/config.c | 6 +++--- polymer/eduke32/source/game.c | 5 ++--- polymer/eduke32/source/m32exec.c | 3 +-- polymer/eduke32/source/savegame.c | 5 ++--- polymer/eduke32/source/sdlmusic.c | 6 ++---- 12 files changed, 38 insertions(+), 63 deletions(-) diff --git a/polymer/eduke32/build/src/build.c b/polymer/eduke32/build/src/build.c index be9e756c5..bf053c97b 100644 --- a/polymer/eduke32/build/src/build.c +++ b/polymer/eduke32/build/src/build.c @@ -538,10 +538,7 @@ int32_t app_main(int32_t argc, const char **argv) } if (!boardfilename[0]) - { - Bstrncpy(boardfilename, argv[i], BMAX_PATH); - boardfilename[BMAX_PATH-1] = 0; - } + Bstrncpyz(boardfilename, argv[i], BMAX_PATH); } if (boardfilename[0] == 0) { @@ -8532,8 +8529,7 @@ int32_t _getnumber16(const char *namestart, int32_t num, int32_t maxnumber, char // need to have 4+11+2==17 chars room at the end // ("^011", max. string length of an int32, "_ ") - Bstrncpy(ournamestart, namestart, sizeof(ournamestart)); - ournamestart[sizeof(ournamestart)-1] = 0; + Bstrncpyz(ournamestart, namestart, sizeof(ournamestart)); bflushchars(); while (keystatus[0x1] == 0) @@ -8604,8 +8600,7 @@ int32_t _getnumber256(const char *namestart, int32_t num, int32_t maxnumber, cha // need to have 11+2==13 chars room at the end // (max. string length of an int32, "_ ") - Bstrncpy(ournamestart, namestart, sizeof(ournamestart)); - ournamestart[sizeof(ournamestart)-1] = 0; + Bstrncpyz(ournamestart, namestart, sizeof(ournamestart)); bflushchars(); while (keystatus[0x1] == 0) @@ -8795,8 +8790,8 @@ const char *getstring_simple(const char *querystr, const char *defaultstr, int32 if (numcompl<3) { - Bstrncpy(completions[numcompl], cmpstr+len, sizeof(completions[0])); - completions[numcompl][sizeof(completions[0])-1] = 0; + Bstrncpyz(completions[numcompl], cmpstr+len, sizeof(completions[0])); + for (k=0; completions[numcompl][k]; k++) completions[numcompl][k] = Btolower(completions[numcompl][k]); numcompl++; @@ -9411,8 +9406,7 @@ int32_t loadnames(const char *namesfile, int8_t root) int8_t quotes=0, anglebrackets=0; BFILE *fp; - Bstrncpy(buffer, namesfile, sizeof(buffer)); - buffer[sizeof(buffer)-1] = 0; + Bstrncpyz(buffer, namesfile, sizeof(buffer)); fp = fopenfrompath(buffer,"r"); if (!fp) @@ -9516,8 +9510,7 @@ int32_t loadnames(const char *namesfile, int8_t root) if (Bstrlen(name) > 24) initprintf("Warning: Name \"%s\" longer than 24 characters (line %d). Truncating.\n", name, line-1); - Bstrncpy(names[num], name, 24); - names[num][24] = 0; + Bstrncpyz(names[num], name, 25); syms++; diff --git a/polymer/eduke32/build/src/config.c b/polymer/eduke32/build/src/config.c index acae71464..c231f64ec 100644 --- a/polymer/eduke32/build/src/config.c +++ b/polymer/eduke32/build/src/config.c @@ -325,8 +325,8 @@ int32_t loadsetup(const char *fn) // copy script history into OSD history for (i=0; ifn; texidx = &sk->texid[(globalnoeffect)?0:(hictinting[pal].f&HICEFFECTMASK)]; - strncpy(fn,skinfile,BMAX_PATH); + Bstrncpyz(fn, skinfile, BMAX_PATH); //OSD_Printf("Using exact match skin (pal=%d,skinnum=%d,surfnum=%d) %s\n",pal,number,surf,skinfile); break; } @@ -746,7 +746,7 @@ int32_t mdloadskin(md2model_t *m, int32_t number, int32_t pal, int32_t surf) { skinfile = skzero->fn; texidx = &skzero->texid[(globalnoeffect)?0:(hictinting[pal].f&HICEFFECTMASK)]; - strncpy(fn,skinfile,BMAX_PATH); + Bstrncpyz(fn, skinfile, BMAX_PATH); //OSD_Printf("Using def skin 0,0 as fallback, pal=%d\n", pal); } else @@ -754,7 +754,7 @@ int32_t mdloadskin(md2model_t *m, int32_t number, int32_t pal, int32_t surf) if ((unsigned)number >= (unsigned)m->numskins) number = 0; skinfile = m->skinfn + number*64; texidx = &m->texid[ number * (HICEFFECTMASK+1) + (globalnoeffect)?0:(hictinting[pal].f&HICEFFECTMASK)]; - strncpy(fn,m->basepath,BMAX_PATH); + Bstrncpyz(fn, m->basepath, BMAX_PATH); if ((Bstrlen(fn) + Bstrlen(skinfile)) < BMAX_PATH) strcat(fn,skinfile); //OSD_Printf("Using MD2/MD3 skin (%d) %s, pal=%d\n",number,skinfile,pal); @@ -3383,8 +3383,7 @@ mdmodel_t *mdload(const char *filnam) // smuggle the file name into the model struct. // head.nam is unused as far as I can tell - Bstrncpy(vm3->head.nam, filnam, sizeof(vm3->head.nam)); - vm3->head.nam[sizeof(vm3->head.nam)-1] = 0; + Bstrncpyz(vm3->head.nam, filnam, sizeof(vm3->head.nam)); md3postload_common(vm3); diff --git a/polymer/eduke32/build/src/polymost.c b/polymer/eduke32/build/src/polymost.c index 033cd1717..83b65a552 100644 --- a/polymer/eduke32/build/src/polymost.c +++ b/polymer/eduke32/build/src/polymost.c @@ -1294,7 +1294,7 @@ static int32_t LoadCacheOffsets(void) } else { - Bstrncpy(curcacheindex->name, fname, BMAX_PATH); + Bstrncpyz(curcacheindex->name, fname, BMAX_PATH); curcacheindex->offset = foffset; curcacheindex->len = fsize; curcacheindex->next = (texcacheindex *)Bcalloc(1, sizeof(texcacheindex)); diff --git a/polymer/eduke32/build/src/sdlayer.c b/polymer/eduke32/build/src/sdlayer.c index df1f3059b..306fffe9d 100644 --- a/polymer/eduke32/build/src/sdlayer.c +++ b/polymer/eduke32/build/src/sdlayer.c @@ -155,10 +155,7 @@ int32_t wm_ynbox(char *name, char *fmt, ...) void wm_setapptitle(char *name) { if (name) - { - Bstrncpy(apptitle, name, sizeof(apptitle)-1); - apptitle[ sizeof(apptitle)-1 ] = 0; - } + Bstrncpyz(apptitle, name, sizeof(apptitle)); SDL_WM_SetCaption(apptitle, NULL); @@ -493,13 +490,13 @@ int32_t initinput(void) for (i=0; i= BMAX_PATH) diff --git a/polymer/eduke32/source/astub.c b/polymer/eduke32/source/astub.c index 34d59f893..886536cd7 100644 --- a/polymer/eduke32/source/astub.c +++ b/polymer/eduke32/source/astub.c @@ -338,8 +338,7 @@ static void drawgradient(void) static void message_common1(const char *tmpstr) { - Bstrncpy(getmessage,tmpstr,sizeof(getmessage)); - getmessage[sizeof(getmessage)-1] = 0; + Bstrncpyz(getmessage, tmpstr, sizeof(getmessage)); getmessageleng = Bstrlen(getmessage); getmessagetimeoff = totalclock + 120*2 + getmessageleng*(120/30); @@ -964,8 +963,7 @@ int32_t taglab_save(const char *mapname) if (g_taglab.numlabels==0) return 1; - Bstrncpy(buf, mapname, BMAX_PATH); - buf[BMAX_PATH-1] = 0; + Bstrncpyz(buf, mapname, BMAX_PATH); len = Bstrlen(buf); // @@ -1009,8 +1007,7 @@ int32_t taglab_add(const char *label, int16_t tag) if (tag < 0) return -1; - Bstrncpy(buf, label, sizeof(buf)); - buf[sizeof(buf)-1] = 0; + Bstrncpyz(buf, label, sizeof(buf)); // upcase the tag for storage and comparison tstrtoupper(buf); @@ -1063,8 +1060,7 @@ int32_t taglab_gettag(const char *label) { char buf[TAGLAB_MAX]; - Bstrncpy(buf, label, TAGLAB_MAX); - buf[sizeof(buf)-1] = 0; + Bstrncpyz(buf, label, TAGLAB_MAX); // need to upcase since hash_findcase doesn't work as expected: // getting the code is still (necessarily) case-sensitive... @@ -3586,8 +3582,7 @@ static int32_t m32gettile(int32_t idInitialTile) { int32_t i, i0, slen=Bstrlen(searchstr)-1; - Bstrncpy(laststr, searchstr, 25); - laststr[24] = 0; + Bstrncpyz(laststr, searchstr, 25); i0 = localartlookup[iTile]; Bmemcpy(buf[0], laststr, 25); @@ -8428,11 +8423,10 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) { #ifdef USE_OPENGL Bsnprintf(tempbuf,sizeof(tempbuf),"%s/%s",argv[i+1],TEXCACHEFILE); - Bstrncpy(TEXCACHEFILE,tempbuf, sizeof(TEXCACHEFILE)); - TEXCACHEFILE[sizeof(TEXCACHEFILE)-1] = 0; + Bstrncpyz(TEXCACHEFILE, tempbuf, sizeof(TEXCACHEFILE)); #endif - Bstrncpy(g_modDir, argv[i+1], sizeof(g_modDir)); - g_modDir[sizeof(g_modDir)-1] = 0; + Bstrncpyz(g_modDir, argv[i+1], sizeof(g_modDir)); + G_AddPath(argv[i+1]); COPYARG(i); @@ -9247,8 +9241,7 @@ static int32_t osdcmd_endisableevent(const osdfuncparm_t *parm) else if (!Bstrncmp(parm->parms[i], "EVENT_", 6)) { j = hash_find(&h_labels, parm->parms[i]); - Bstrncpy(buf2, parm->parms[i], sizeof(buf2)); - buf2[sizeof(buf2)-1] = '\0'; + Bstrncpyz(buf2, parm->parms[i], sizeof(buf2)); } else { @@ -12654,8 +12647,7 @@ void registerMenuFunction(const char *funcname, int32_t stateidx) // register menu entry named FUNCNAME to call the M32script // state with index STATEIDX - Bstrncpy(fn, funcname, sizeof(fn)); - fn[sizeof(fn)-1] = 0; + Bstrncpyz(fn, funcname, sizeof(fn)); for (i=8; i 10) tempbuf[Bstrlen(tempbuf)-1] = '\0'; - Bstrncpy(szPlayerName,tempbuf,sizeof(szPlayerName)-1); - szPlayerName[sizeof(szPlayerName)-1] = '\0'; + Bstrncpyz(szPlayerName, tempbuf, sizeof(szPlayerName)); if (g_rtsNamePtr == NULL) SCRIPT_GetString(ud.config.scripthandle, "Comm Setup","RTSName",&ud.rtsname[0]); diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index ac988b1e5..02db35a84 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -8665,7 +8665,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) { if (argc > i+1) { - Bstrncpy(g_netPassword, (char *)argv[i+1], sizeof(g_netPassword)-1); + Bstrncpyz(g_netPassword, argv[i+1], sizeof(g_netPassword)); i++; } i++; @@ -9454,8 +9454,7 @@ static void G_Startup(void) while (Bstrlen(OSD_StripColors(tempbuf,tempbuf)) > 10) tempbuf[Bstrlen(tempbuf)-1] = '\0'; - Bstrncpy(szPlayerName,tempbuf,sizeof(szPlayerName)-1); - szPlayerName[sizeof(szPlayerName)-1] = '\0'; + Bstrncpyz(szPlayerName, tempbuf, sizeof(szPlayerName)); } if (CommandMap) diff --git a/polymer/eduke32/source/m32exec.c b/polymer/eduke32/source/m32exec.c index d82e23399..ef8bf7b92 100644 --- a/polymer/eduke32/source/m32exec.c +++ b/polymer/eduke32/source/m32exec.c @@ -2388,8 +2388,7 @@ badindex: int32_t sign = (tw==CON_GETNUMBERFROMUSER) ? Gv_GetVarX(*insptr++) : (max<=0); char buf[64]; // buffers in getnumber* are 80 bytes long - Bstrncpy(buf, quotetext, sizeof(buf)); - buf[sizeof(buf)-1]='\0'; + Bstrncpyz(buf, quotetext, sizeof(buf)); if (max==0) max = INT32_MAX; diff --git a/polymer/eduke32/source/savegame.c b/polymer/eduke32/source/savegame.c index a7dd5e7e5..e304d84e0 100644 --- a/polymer/eduke32/source/savegame.c +++ b/polymer/eduke32/source/savegame.c @@ -1114,8 +1114,7 @@ int32_t sv_saveandmakesnapshot(FILE *fil, int8_t spot, int8_t recdiffsp, int8_t if (spot >= 0) { // savegame - Bstrncpy(h.savename, ud.savegame[spot], sizeof(h.savename)); - h.savename[sizeof(h.savename)-1] = 0; + Bstrncpyz(h.savename, ud.savegame[spot], sizeof(h.savename)); } else { @@ -1124,7 +1123,7 @@ int32_t sv_saveandmakesnapshot(FILE *fil, int8_t spot, int8_t recdiffsp, int8_t const time_t t=time(NULL); struct tm *st; - Bstrncpy(h.savename, "Eduke32 demo", sizeof(h.savename)); + Bstrncpyz(h.savename, "Eduke32 demo", sizeof(h.savename)); if (t>=0 && (st = localtime(&t))) Bsprintf(h.savename, "Edemo32 %04d%02d%02d", st->tm_year+1900, st->tm_mon+1, st->tm_mday); } diff --git a/polymer/eduke32/source/sdlmusic.c b/polymer/eduke32/source/sdlmusic.c index 0da0b6e7c..a64e6200d 100644 --- a/polymer/eduke32/source/sdlmusic.c +++ b/polymer/eduke32/source/sdlmusic.c @@ -70,10 +70,8 @@ static Mix_Music *music_musicchunk = NULL; static void setErrorMessage(const char *msg) { - Bstrncpy(errorMessage, msg, sizeof(errorMessage)); - // strncpy() doesn't add the null char if there isn't room... - errorMessage[sizeof(errorMessage) - 1] = '\0'; -} // setErrorMessage + Bstrncpyz(errorMessage, msg, sizeof(errorMessage)); +} // The music functions...