Skip to content

Commit

Permalink
* Use fill_pathname_join where possible instead of snprintf
Browse files Browse the repository at this point in the history
* Use strlcpy where possible instead of snprintf
* Get rid of 'bad CDRIP'warning whenever an audio track cannot be found
  • Loading branch information
LibretroAdmin committed Jun 12, 2022
1 parent a273599 commit e04da40
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 253 deletions.
69 changes: 33 additions & 36 deletions common/bgmusic.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
#include "snd_codec.h"
#include "bgmusic.h"

#define MUSIC_DIRNAME "music"
#include <compat/strl.h>
#include <file/file_path.h>

qboolean bgmloop;
cvar_t bgm_extmusic = {"bgm_extmusic", "1", false};

static qboolean no_extmusic= false;
static float old_volume = -1.0f;

typedef enum _bgm_player
{
Expand All @@ -60,16 +59,16 @@ typedef struct music_handler_s

static music_handler_t wanted_handlers[] =
{
{ CODECTYPE_VORBIS,BGM_STREAMER,-1, "ogg", MUSIC_DIRNAME, NULL },
{ CODECTYPE_OPUS, BGM_STREAMER, -1, "opus", MUSIC_DIRNAME, NULL },
{ CODECTYPE_MP3, BGM_STREAMER, -1, "mp3", MUSIC_DIRNAME, NULL },
{ CODECTYPE_FLAC, BGM_STREAMER, -1, "flac", MUSIC_DIRNAME, NULL },
{ CODECTYPE_WAV, BGM_STREAMER, -1, "wav", MUSIC_DIRNAME, NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "it", MUSIC_DIRNAME, NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "s3m", MUSIC_DIRNAME, NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "xm", MUSIC_DIRNAME, NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "mod", MUSIC_DIRNAME, NULL },
{ CODECTYPE_UMX, BGM_STREAMER, -1, "umx", MUSIC_DIRNAME, NULL },
{ CODECTYPE_VORBIS,BGM_STREAMER,-1, "ogg", "music", NULL },
{ CODECTYPE_OPUS, BGM_STREAMER, -1, "opus", "music", NULL },
{ CODECTYPE_MP3, BGM_STREAMER, -1, "mp3", "music", NULL },
{ CODECTYPE_FLAC, BGM_STREAMER, -1, "flac", "music", NULL },
{ CODECTYPE_WAV, BGM_STREAMER, -1, "wav", "music", NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "it", "music", NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "s3m", "music", NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "xm", "music", NULL },
{ CODECTYPE_MOD, BGM_STREAMER, -1, "mod", "music", NULL },
{ CODECTYPE_UMX, BGM_STREAMER, -1, "umx", "music", NULL },
{ CODECTYPE_NONE, BGM_NONE, -1, NULL, NULL, NULL }
};

Expand Down Expand Up @@ -141,9 +140,6 @@ qboolean BGM_Init (void)
Cmd_AddCommand("music_loop", BGM_Loop_f);
Cmd_AddCommand("music_stop", BGM_Stop_f);

if (COM_CheckParm("-noextmusic") != 0)
no_extmusic = true;

bgmloop = true;

for (i = 0; wanted_handlers[i].type != CODECTYPE_NONE; i++)
Expand Down Expand Up @@ -261,7 +257,7 @@ void BGM_Play (const char *filename)
Con_Printf("Unhandled extension for %s\n", filename);
return;
}
snprintf(tmp, sizeof(tmp), "%s/%s", handler->dir, filename);
fill_pathname_join(tmp, handler->dir, filename, sizeof(tmp));
switch (handler->player)
{
case BGM_MIDIDRV:
Expand Down Expand Up @@ -300,33 +296,31 @@ void BGM_PlayCDtrack (byte track, qboolean looping)
return; /* success */
if (music_handlers == NULL)
return;
if (no_extmusic || !bgm_extmusic.value)
if (!bgm_extmusic.value)
return;

type = 0;
ext = NULL;
type = 0;
ext = NULL;
handler = music_handlers;
while (handler)
{
if (! handler->is_available)
goto _next;
if (! CDRIPTYPE(handler->type))
goto _next;
snprintf(tmp, sizeof(tmp), "%s/track%02d.%s",
MUSIC_DIRNAME, (int)track, handler->ext);
snprintf(tmp, sizeof(tmp), "music/track%02d.%s",
(int)track, handler->ext);
if (! COM_FileExists(tmp))
goto _next;
type = handler->type;
ext = handler->ext;
_next:
type = handler->type;
ext = handler->ext;
_next:
handler = handler->next;
}
if (ext == NULL)
Con_Printf("Couldn't find a cdrip for track %d\n", (int)track);
else
if (ext)
{
snprintf(tmp, sizeof(tmp), "%s/track%02d.%s",
MUSIC_DIRNAME, (int)track, ext);
snprintf(tmp, sizeof(tmp), "music/track%02d.%s",
(int)track, ext);
bgmstream = S_CodecOpenStreamType(tmp, type);
if (! bgmstream)
Con_Printf("Couldn't handle music file %s\n", tmp);
Expand Down Expand Up @@ -394,9 +388,9 @@ static void BGM_UpdateStream (void)
fileBytes = fileSamples * (bgmstream->info.width * bgmstream->info.channels);
if (fileBytes > (int) sizeof(raw))
{
fileBytes = (int) sizeof(raw);
fileBytes = (int) sizeof(raw);
fileSamples = fileBytes /
(bgmstream->info.width * bgmstream->info.channels);
(bgmstream->info.width * bgmstream->info.channels);
}

/* Read */
Expand All @@ -409,10 +403,11 @@ static void BGM_UpdateStream (void)

if (res > 0) /* data: add to raw buffer */
{
S_RawSamples(fileSamples, bgmstream->info.rate,
bgmstream->info.width,
bgmstream->info.channels,
raw, bgmvolume.value);
S_RawSamples(fileSamples,
bgmstream->info.rate,
bgmstream->info.width,
bgmstream->info.channels,
raw, bgmvolume.value);
}
else if (res == 0) /* EOF */
{
Expand Down Expand Up @@ -443,6 +438,8 @@ static void BGM_UpdateStream (void)

void BGM_Update (void)
{
static float old_volume = -1.0f;

if (old_volume != bgmvolume.value)
{
if (bgmvolume.value < 0)
Expand Down
13 changes: 7 additions & 6 deletions common/cl_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "bgmusic.h"
#include "sys.h"

#include <compat/strl.h>

//=============================================================================

/*
Expand Down Expand Up @@ -200,7 +202,7 @@ CL_ParseServerInfo(void)
{
char *level;
const char *mapname;
int i, maxlen;
int i;
int nummodels, numsounds;
char **model_precache = malloc(sizeof(char*) * MAX_MODELS);
char **sound_precache = malloc(sizeof(char*) * MAX_SOUNDS);
Expand Down Expand Up @@ -235,8 +237,7 @@ CL_ParseServerInfo(void)

/* parse signon message */
level = cl.levelname;
maxlen = sizeof(cl.levelname);
snprintf(level, maxlen, "%s", MSG_ReadString());
strlcpy(level, MSG_ReadString(), sizeof(cl.levelname));

/* seperate the printfs so the server message can have a color */
Con_Printf("\n\n\35\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36"
Expand Down Expand Up @@ -285,7 +286,7 @@ CL_ParseServerInfo(void)

/* copy the naked name of the map file to the cl structure */
mapname = COM_SkipPath(model_precache[1]);
snprintf(cl.mapname, sizeof(cl.mapname), "%s", mapname);
strlcpy(cl.mapname, mapname, sizeof(cl.mapname));
COM_StripExtension(cl.mapname);

/* now we try to load everything else until a cache allocation fails */
Expand Down Expand Up @@ -953,7 +954,7 @@ CL_ParseServerMessage(void)
if (i >= MAX_LIGHTSTYLES)
Sys_Error("svc_lightstyle > MAX_LIGHTSTYLES");
s = MSG_ReadString();
snprintf(cl_lightstyle[i].map, MAX_STYLESTRING, "%s", s);
strlcpy(cl_lightstyle[i].map, s, MAX_STYLESTRING);
cl_lightstyle[i].length = strlen(cl_lightstyle[i].map);
break;

Expand All @@ -972,7 +973,7 @@ CL_ParseServerMessage(void)
if (i >= cl.maxclients)
Host_Error("%s: svc_updatename > MAX_SCOREBOARD", __func__);
s = MSG_ReadString();
snprintf(cl.players[i].name, MAX_SCOREBOARDNAME, "%s", s);
strlcpy(cl.players[i].name, s, MAX_SCOREBOARDNAME);
break;

case svc_updatefrags:
Expand Down
35 changes: 9 additions & 26 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <sys/types.h>
#include <errno.h>

#include <compat/strl.h>
#include <file/file_path.h>

#ifdef NQ_HACK
#include "quakedef.h"
#include "host.h"
Expand Down Expand Up @@ -72,10 +75,6 @@ int rfgetc(RFILE* stream);
static const char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
static const char *argvdummy = " ";

static const char *safeargvs[NUM_SAFE_ARGVS] = {
"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-dibonly"
};

cvar_t registered = { "registered", "0" };
#ifdef NQ_HACK
static cvar_t cmdline = { "cmdline", "0", false, true };
Expand Down Expand Up @@ -1008,7 +1007,6 @@ COM_InitArgv
*/
void COM_InitArgv(int argc, const char **argv)
{
qboolean safe;
int i;
#ifdef NQ_HACK
int j, n = 0;
Expand All @@ -1027,22 +1025,9 @@ void COM_InitArgv(int argc, const char **argv)
com_cmdline[n] = 0;
#endif

safe = false;

for (com_argc = 0; (com_argc < MAX_NUM_ARGVS) && (com_argc < argc);
com_argc++) {
largv[com_argc] = argv[com_argc];
if (!strcmp("-safe", argv[com_argc]))
safe = true;
}

if (safe) {
// force all the safe-mode switches. Note that we reserved extra space in
// case we need to add these, so we don't need an overflow check
for (i = 0; i < NUM_SAFE_ARGVS; i++) {
largv[com_argc] = safeargvs[i];
com_argc++;
}
}

largv[com_argc] = argvdummy;
Expand Down Expand Up @@ -1244,7 +1229,6 @@ int COM_FOpenFile(const char *filename, RFILE **file)
char path[MAX_OSPATH];
pack_t *pak;
int i;
int findtime;

file_from_pak = 0;

Expand Down Expand Up @@ -1276,9 +1260,8 @@ int COM_FOpenFile(const char *filename, RFILE **file)
if (strchr(filename, '/') || strchr(filename, '\\'))
continue;
}
snprintf(path, sizeof(path), "%s/%s", search->filename, filename);
findtime = Sys_FileTime(path);
if (findtime == -1)
fill_pathname_join(path, search->filename, filename, sizeof(path));
if (Sys_FileTime(path) == -1)
continue;

*file = rfopen(path, "rb");
Expand Down Expand Up @@ -1333,7 +1316,7 @@ qboolean COM_FileExists (const char *filename)
if (strchr(filename, '/') || strchr(filename, '\\'))
continue;
}
snprintf(path, sizeof(path), "%s/%s", search->filename, filename);
fill_pathname_join(path, search->filename, filename, sizeof(path));
findtime = Sys_FileTime(path);
if (findtime == -1)
continue;
Expand Down Expand Up @@ -1445,7 +1428,7 @@ void COM_ScanDir(struct stree_root *root, const char *path, const char *pfx,
COM_ScanDirPak(root, search->pack, path, pfx, ext, stripext);
else
{
snprintf(fullpath, MAX_OSPATH, "%s/%s", search->filename, path);
fill_pathname_join(fullpath, search->filename, path, MAX_OSPATH);
fullpath[MAX_OSPATH - 1] = '\0';
dir = retro_opendir(fullpath);

Expand Down Expand Up @@ -1597,7 +1580,7 @@ static pack_t *COM_LoadPackFile(const char *packfile)
/* parse the directory */
for (i = 0; i < numfiles; i++)
{
snprintf(mfiles[i].name, sizeof(mfiles[i].name), "%s", dfiles[i].name);
strlcpy(mfiles[i].name, dfiles[i].name, sizeof(mfiles[i].name));
#ifdef MSB_FIRST
mfiles[i].filepos = LittleLong(dfiles[i].filepos);
mfiles[i].filelen = LittleLong(dfiles[i].filelen);
Expand All @@ -1619,7 +1602,7 @@ static pack_t *COM_LoadPackFile(const char *packfile)
if (!pack)
goto error;

snprintf(pack->filename, sizeof(pack->filename), "%s", packfile);
strlcpy(pack->filename, packfile, sizeof(pack->filename));
strcpy(pack->filename, packfile);
pack->numfiles = numfiles;
pack->files = mfiles;
Expand Down
54 changes: 0 additions & 54 deletions common/host_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,61 +62,8 @@ Host_Quit_f(void)
}
CL_Disconnect();
Host_ShutdownServer(false);

//Sys_Quit();
}


/*
==================
Host_Status_f
==================
*/
void
Host_Status_f(void)
{
client_t *client;
int seconds;
int minutes;
int hours = 0;
int j;
void (*print)(const char *fmt, ...);

if (cmd_source == src_command) {
if (!sv.active) {
Cmd_ForwardToServer();
return;
}
print = Con_Printf;
} else
print = SV_ClientPrintf;

print("host: %s\n", Cvar_VariableString("hostname"));
print("version: TyrQuake-%s\n", stringify(TYR_VERSION));
if (tcpipAvailable)
print("tcp/ip: %s\n", my_tcpip_address);
print("map: %s\n", sv.name);
print("players: %i active (%i max)\n\n", net_activeconnections,
svs.maxclients);
for (j = 0, client = svs.clients; j < svs.maxclients; j++, client++) {
if (!client->active)
continue;
seconds = (int)(net_time - client->netconnection->connecttime);
minutes = seconds / 60;
if (minutes) {
seconds -= (minutes * 60);
hours = minutes / 60;
if (hours)
minutes -= (hours * 60);
} else
hours = 0;
print("#%-2u %-16.16s %3i %2i:%02i:%02i\n", j + 1, client->name,
(int)client->edict->v.frags, hours, minutes, seconds);
print(" %s\n", client->netconnection->address);
}
}


/*
==================
Host_God_f
Expand Down Expand Up @@ -1436,7 +1383,6 @@ Host_InitCommands
void
Host_InitCommands(void)
{
Cmd_AddCommand("status", Host_Status_f);
Cmd_AddCommand("quit", Host_Quit_f);
Cmd_AddCommand("god", Host_God_f);
Cmd_AddCommand("notarget", Host_Notarget_f);
Expand Down
Loading

0 comments on commit e04da40

Please sign in to comment.