From 9495aa9923fed16aeb6f49c69ccd249b9e17dba7 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Fri, 15 Feb 2008 14:57:48 +0100 Subject: [PATCH] Using cString::sprintf() instead of asprintf() --- CONTRIBUTORS | 2 + HISTORY | 4 ++ PLUGINS/src/skincurses/HISTORY | 4 ++ PLUGINS/src/skincurses/skincurses.c | 8 +-- channels.c | 12 ++-- config.c | 12 ++-- interface.c | 7 +-- menu.c | 88 +++++++++-------------------- menu.h | 4 +- menuitems.c | 7 +-- plugin.c | 12 ++-- recording.c | 45 ++++++--------- skinclassic.c | 6 +- skinsttng.c | 6 +- svdrp.c | 16 ++---- themes.c | 10 +--- thread.c | 4 +- timers.c | 11 ++-- tools.c | 59 ++++++++++--------- tools.h | 3 +- 20 files changed, 131 insertions(+), 189 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 85a19330..a9f9f5c0 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1689,6 +1689,8 @@ Wolfgang Rohdewald for removing an unnecessary #include from osd.c for reporting a problem with with numerical input to switch channels if Up, Down, Channel+ or Channel- is pressed + for pointing out a possible problem with asprintf() if the return value is not + checked Chad Flynt for suggestions and experiments regarding the buffer reserve in cTransfer diff --git a/HISTORY b/HISTORY index c6889849..ebdc2e4d 100644 --- a/HISTORY +++ b/HISTORY @@ -5616,3 +5616,7 @@ Video Disk Recorder Revision History - Limiting the length of the recording name in timers created via SVDRP in case VDR is run with --vfat, in order to avoid names that are too long for Windows (suggested by Rolf Ahrenberg). +- Using cString::sprintf() instead of asprintf() (thanks to Wolfgang Rohdewald + for pointing out a possible problem if the return value is not checked). + Plugin authors may want to consider doing the same. For convenience there is now + an additional version of cString::sprintf() that accepts a va_list parameter. diff --git a/PLUGINS/src/skincurses/HISTORY b/PLUGINS/src/skincurses/HISTORY index e5bfa49d..2a037012 100644 --- a/PLUGINS/src/skincurses/HISTORY +++ b/PLUGINS/src/skincurses/HISTORY @@ -65,3 +65,7 @@ VDR Plugin 'skincurses' Revision History 2008-01-19: - Updated the Makefile of the skincurses plugin (thanks to Rolf Ahrenberg). + +2008-02-15: + +- Using cString::sprintf() instead of asprintf(). diff --git a/PLUGINS/src/skincurses/skincurses.c b/PLUGINS/src/skincurses/skincurses.c index b9a4fc06..da868e7d 100644 --- a/PLUGINS/src/skincurses/skincurses.c +++ b/PLUGINS/src/skincurses/skincurses.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: skincurses.c 1.18 2007/08/26 20:16:59 kls Exp $ + * $Id: skincurses.c 1.19 2008/02/15 14:52:03 kls Exp $ */ #include @@ -11,7 +11,7 @@ #include #include -static const char *VERSION = "0.1.4"; +static const char *VERSION = "0.1.5"; static const char *DESCRIPTION = trNOOP("A text only skin"); static const char *MAINMENUENTRY = NULL; @@ -384,10 +384,8 @@ void cSkinCursesDisplayMenu::SetEvent(const cEvent *Event) snprintf(t, sizeof(t), "%s %s - %s", *Event->GetDateString(), *Event->GetTimeString(), *Event->GetEndTimeString()); ts.Set(osd, 0, y, ScOsdWidth, ScOsdHeight - y - 2, t, &Font, clrYellow, clrBackground); if (Event->Vps() && Event->Vps() != Event->StartTime()) { - char *buffer; - asprintf(&buffer, " VPS: %s", *Event->GetVpsString()); + cString buffer = cString::sprintf(" VPS: %s", *Event->GetVpsString()); osd->DrawText(ScOsdWidth - Utf8StrLen(buffer), y, buffer, clrBlack, clrYellow, &Font); - free(buffer); } y += ts.Height(); y += 1; diff --git a/channels.c b/channels.c index 4fef3e1f..9730a436 100644 --- a/channels.c +++ b/channels.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: channels.c 1.58 2008/02/10 14:11:47 kls Exp $ + * $Id: channels.c 1.59 2008/02/10 15:45:38 kls Exp $ */ #include "channels.h" @@ -658,12 +658,12 @@ cString cChannel::ToText(const cChannel *Channel) q += sprintf(q, ";%s", Channel->provider); *q = 0; strreplace(FullName, ':', '|'); - char *buffer; + cString buffer; if (Channel->groupSep) { if (Channel->number) - asprintf(&buffer, ":@%d %s\n", Channel->number, FullName); + buffer = cString::sprintf(":@%d %s\n", Channel->number, FullName); else - asprintf(&buffer, ":%s\n", FullName); + buffer = cString::sprintf(":%s\n", FullName); } else { char vpidbuf[32]; @@ -685,9 +685,9 @@ cString cChannel::ToText(const cChannel *Channel) q = caidbuf; q += IntArrayToString(q, Channel->caids, 16); *q = 0; - asprintf(&buffer, "%s:%d:%s:%s:%d:%s:%s:%d:%s:%d:%d:%d:%d\n", FullName, Channel->frequency, *Channel->ParametersToString(), *cSource::ToString(Channel->source), Channel->srate, vpidbuf, apidbuf, Channel->tpid, caidbuf, Channel->sid, Channel->nid, Channel->tid, Channel->rid); + buffer = cString::sprintf("%s:%d:%s:%s:%d:%s:%s:%d:%s:%d:%d:%d:%d\n", FullName, Channel->frequency, *Channel->ParametersToString(), *cSource::ToString(Channel->source), Channel->srate, vpidbuf, apidbuf, Channel->tpid, caidbuf, Channel->sid, Channel->nid, Channel->tid, Channel->rid); } - return cString(buffer, true); + return buffer; } cString cChannel::ToText(void) const diff --git a/config.c b/config.c index 1f5eb720..aa72b06c 100644 --- a/config.c +++ b/config.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: config.c 1.159 2008/02/10 14:10:48 kls Exp $ + * $Id: config.c 1.160 2008/02/10 15:48:17 kls Exp $ */ #include "config.h" @@ -62,9 +62,9 @@ const char *cCommand::Execute(const char *Parameters) { free(result); result = NULL; - char *cmdbuf = NULL; + cString cmdbuf; if (Parameters) - asprintf(&cmdbuf, "%s %s", command, Parameters); + cmdbuf = cString::sprintf("%s %s", command, Parameters); const char *cmd = cmdbuf ? cmdbuf : command; dsyslog("executing command '%s'", cmd); cPipe p; @@ -82,7 +82,6 @@ const char *cCommand::Execute(const char *Parameters) } else esyslog("ERROR: can't open pipe for command '%s'", cmd); - free(cmdbuf); return result; } @@ -322,10 +321,7 @@ void cSetup::Store(const char *Name, const char *Value, const char *Plugin, bool void cSetup::Store(const char *Name, int Value, const char *Plugin) { - char *buffer = NULL; - asprintf(&buffer, "%d", Value); - Store(Name, buffer, Plugin); - free(buffer); + Store(Name, cString::sprintf("%d", Value), Plugin); } bool cSetup::Load(const char *FileName) diff --git a/interface.c b/interface.c index efdf7371..6ad3c59f 100644 --- a/interface.c +++ b/interface.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: interface.c 1.76 2007/08/04 14:39:25 kls Exp $ + * $Id: interface.c 1.77 2008/02/10 15:49:15 kls Exp $ */ #include "interface.h" @@ -86,10 +86,7 @@ bool cInterface::QueryKeys(cRemote *Remote, cSkinDisplayMenu *DisplayMenu) DisplayMenu->SetItem(tr("Phase 2: Learning specific key codes"), 2, false, false); eKeys NewKey = kUp; while (NewKey != kNone) { - char *Prompt; - asprintf(&Prompt, tr("Press key for '%s'"), cKey::ToString(NewKey, true)); - DisplayMenu->SetItem(Prompt, 4, false, false); - free(Prompt); + DisplayMenu->SetItem(cString::sprintf(tr("Press key for '%s'"), cKey::ToString(NewKey, true)), 4, false, false); cRemote::Clear(); DisplayMenu->Flush(); for (eKeys k = NewKey; k == NewKey; ) { diff --git a/menu.c b/menu.c index 9ec407d3..c00dc667 100644 --- a/menu.c +++ b/menu.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: menu.c 1.473 2008/02/10 11:40:49 kls Exp $ + * $Id: menu.c 1.474 2008/02/10 16:02:02 kls Exp $ */ #include "menu.h" @@ -147,12 +147,8 @@ cMenuEditSrcItem::cMenuEditSrcItem(const char *Name, int *Value) void cMenuEditSrcItem::Set(void) { - if (source) { - char *buffer = NULL; - asprintf(&buffer, "%s - %s", *cSource::ToString(source->Code()), source->Description()); - SetValue(buffer); - free(buffer); - } + if (source) + SetValue(cString::sprintf("%s - %s", *cSource::ToString(source->Code()), source->Description())); else cMenuEditIntItem::Set(); } @@ -402,16 +398,16 @@ int cMenuChannelItem::Compare(const cListObject &ListObject) const void cMenuChannelItem::Set(void) { - char *buffer = NULL; + cString buffer; if (!channel->GroupSep()) { if (sortMode == csmProvider) - asprintf(&buffer, "%d\t%s - %s", channel->Number(), channel->Provider(), channel->Name()); + buffer = cString::sprintf("%d\t%s - %s", channel->Number(), channel->Provider(), channel->Name()); else - asprintf(&buffer, "%d\t%s", channel->Number(), channel->Name()); + buffer = cString::sprintf("%d\t%s", channel->Number(), channel->Name()); } else - asprintf(&buffer, "---\t%s ----------------------------------------------------------------", channel->Name()); - SetText(buffer, false); + buffer = cString::sprintf("---\t%s ----------------------------------------------------------------", channel->Name()); + SetText(buffer); } // --- cMenuChannels --------------------------------------------------------- @@ -816,8 +812,7 @@ void cMenuTimerItem::Set(void) strftime(buffer, sizeof(buffer), "%Y%m%d", &tm_r); day = buffer; } - char *buffer = NULL; - asprintf(&buffer, "%c\t%d\t%s%s%s\t%02d:%02d\t%02d:%02d\t%s", + SetText(cString::sprintf("%c\t%d\t%s%s%s\t%02d:%02d\t%02d:%02d\t%s", !(timer->HasFlags(tfActive)) ? ' ' : timer->FirstDay() ? '!' : timer->Recording() ? '#' : '>', timer->Channel()->Number(), *name, @@ -827,8 +822,7 @@ void cMenuTimerItem::Set(void) timer->Start() % 100, timer->Stop() / 100, timer->Stop() % 100, - timer->File()); - SetText(buffer, false); + timer->File())); } // --- cMenuTimers ----------------------------------------------------------- @@ -1092,19 +1086,19 @@ bool cMenuScheduleItem::Update(bool Force) int OldTimerMatch = timerMatch; Timers.GetMatch(event, &timerMatch); if (Force || timerMatch != OldTimerMatch) { - char *buffer = NULL; + cString buffer; char t = TimerMatchChars[timerMatch]; char v = event->Vps() && (event->Vps() - event->StartTime()) ? 'V' : ' '; char r = event->SeenWithin(30) && event->IsRunning() ? '*' : ' '; const char *csn = channel ? channel->ShortName(true) : NULL; cString eds = event->GetDateString(); if (channel && withDate) - asprintf(&buffer, "%d\t%.*s\t%.*s\t%s\t%c%c%c\t%s", channel->Number(), Utf8SymChars(csn, 6), csn, Utf8SymChars(eds, 6), *eds, *event->GetTimeString(), t, v, r, event->Title()); + buffer = cString::sprintf("%d\t%.*s\t%.*s\t%s\t%c%c%c\t%s", channel->Number(), Utf8SymChars(csn, 6), csn, Utf8SymChars(eds, 6), *eds, *event->GetTimeString(), t, v, r, event->Title()); else if (channel) - asprintf(&buffer, "%d\t%.*s\t%s\t%c%c%c\t%s", channel->Number(), Utf8SymChars(csn, 6), csn, *event->GetTimeString(), t, v, r, event->Title()); + buffer = cString::sprintf("%d\t%.*s\t%s\t%c%c%c\t%s", channel->Number(), Utf8SymChars(csn, 6), csn, *event->GetTimeString(), t, v, r, event->Title()); else - asprintf(&buffer, "%.*s\t%s\t%c%c%c\t%s", Utf8SymChars(eds, 6), *eds, *event->GetTimeString(), t, v, r, event->Title()); - SetText(buffer, false); + buffer = cString::sprintf("%.*s\t%s\t%c%c%c\t%s", Utf8SymChars(eds, 6), *eds, *event->GetTimeString(), t, v, r, event->Title()); + SetText(buffer); result = true; } return result; @@ -1325,10 +1319,7 @@ void cMenuSchedule::PrepareScheduleAllThis(const cEvent *Event, const cChannel * { Clear(); SetCols(7, 6, 4); - char *buffer = NULL; - asprintf(&buffer, tr("Schedule - %s"), Channel->Name()); - SetTitle(buffer); - free(buffer); + SetTitle(cString::sprintf(tr("Schedule - %s"), Channel->Name())); if (schedules && Channel) { const cSchedule *Schedule = schedules->GetSchedule(Channel); if (Schedule) { @@ -1346,10 +1337,7 @@ void cMenuSchedule::PrepareScheduleThisThis(const cEvent *Event, const cChannel { Clear(); SetCols(7, 6, 4); - char *buffer = NULL; - asprintf(&buffer, tr("This event - %s"), Channel->Name()); - SetTitle(buffer); - free(buffer); + SetTitle(cString::sprintf(tr("This event - %s"), Channel->Name())); if (schedules && Channel && Event) { const cSchedule *Schedule = schedules->GetSchedule(Channel); if (Schedule) { @@ -1590,17 +1578,11 @@ eOSState cMenuCommands::Execute(void) { cCommand *command = commands->Get(Current()); if (command) { - char *buffer = NULL; bool confirmed = true; - if (command->Confirm()) { - asprintf(&buffer, "%s?", command->Title()); - confirmed = Interface->Confirm(buffer); - free(buffer); - } + if (command->Confirm()) + confirmed = Interface->Confirm(cString::sprintf("%s?", command->Title())); if (confirmed) { - asprintf(&buffer, "%s...", command->Title()); - Skins.Message(mtStatus, buffer); - free(buffer); + Skins.Message(mtStatus, cString::sprintf("%s...", command->Title())); const char *Result = command->Execute(parameters); Skins.Message(mtStatus, NULL); if (Result) @@ -1916,9 +1898,7 @@ void cMenuRecordingItem::IncrementCounter(bool New) totalEntries++; if (New) newEntries++; - char *buffer = NULL; - asprintf(&buffer, "%d\t%d\t%s", totalEntries, newEntries, name); - SetText(buffer, false); + SetText(cString::sprintf("%d\t%d\t%s", totalEntries, newEntries, name)); } // --- cMenuRecordings ------------------------------------------------------- @@ -2036,13 +2016,12 @@ bool cMenuRecordings::Open(bool OpenSubMenus) cMenuRecordingItem *ri = (cMenuRecordingItem *)Get(Current()); if (ri && ri->IsDirectory()) { const char *t = ri->Name(); - char *buffer = NULL; + cString buffer; if (base) { - asprintf(&buffer, "%s~%s", base, t); + buffer = cString::sprintf("%s~%s", base, t); t = buffer; } AddSubMenu(new cMenuRecordings(t, level + 1, OpenSubMenus)); - free(buffer); return true; } return false; @@ -2144,11 +2123,8 @@ eOSState cMenuRecordings::Commands(eKeys Key) if (ri && !ri->IsDirectory()) { cRecording *recording = GetRecording(ri); if (recording) { - char *parameter = NULL; - asprintf(¶meter, "\"%s\"", *strescape(recording->FileName(), "\"$")); cMenuCommands *menu; - eOSState state = AddSubMenu(menu = new cMenuCommands(tr("Recording commands"), &RecordingCommands, parameter)); - free(parameter); + eOSState state = AddSubMenu(menu = new cMenuCommands(tr("Recording commands"), &RecordingCommands, cString::sprintf("\"%s\"", *strescape(recording->FileName(), "\"$")))); if (Key != kNone) state = menu->ProcessKey(Key); return state; @@ -2851,12 +2827,8 @@ cMenuSetupPlugins::cMenuSetupPlugins(void) SetHasHotkeys(); for (int i = 0; ; i++) { cPlugin *p = cPluginManager::GetPlugin(i); - if (p) { - char *buffer = NULL; - asprintf(&buffer, "%s (%s) - %s", p->Name(), p->Version(), p->Description()); - Add(new cMenuSetupPluginItem(hk(buffer), i)); - free(buffer); - } + if (p) + Add(new cMenuSetupPluginItem(hk(cString::sprintf("%s (%s) - %s", p->Name(), p->Version(), p->Description())), i)); else break; } @@ -3098,10 +3070,8 @@ bool cMenuMain::Update(bool Force) } const char *s = NULL; while ((s = cRecordControls::GetInstantId(s)) != NULL) { - char *buffer = NULL; - asprintf(&buffer, "%s%s", tr(STOP_RECORDING), s); cOsdItem *item = new cOsdItem(osStopRecord); - item->SetText(buffer, false); + item->SetText(cString::sprintf("%s%s", tr(STOP_RECORDING), s)); Add(item); if (!stopRecordingItem) stopRecordingItem = item; @@ -3792,7 +3762,6 @@ cRecordControl::cRecordControl(cDevice *Device, cTimer *Timer, bool Pause) cSchedules::Schedules(SchedulesLock); event = NULL; - instantId = NULL; fileName = NULL; recorder = NULL; device = Device; @@ -3802,7 +3771,7 @@ cRecordControl::cRecordControl(cDevice *Device, cTimer *Timer, bool Pause) timer = new cTimer(true, Pause); Timers.Add(timer); Timers.SetModified(); - asprintf(&instantId, cDevice::NumDevices() > 1 ? "%s - %d" : "%s", timer->Channel()->Name(), device->CardIndex() + 1); + instantId = cString::sprintf(cDevice::NumDevices() > 1 ? "%s - %d" : "%s", timer->Channel()->Name(), device->CardIndex() + 1); } timer->SetPending(true); timer->SetRecording(true); @@ -3857,7 +3826,6 @@ cRecordControl::cRecordControl(cDevice *Device, cTimer *Timer, bool Pause) cRecordControl::~cRecordControl() { Stop(); - free(instantId); free(fileName); } diff --git a/menu.h b/menu.h index 1df1c353..1d478d9b 100644 --- a/menu.h +++ b/menu.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: menu.h 1.90 2008/02/10 11:34:43 kls Exp $ + * $Id: menu.h 1.91 2008/02/10 16:01:53 kls Exp $ */ #ifndef __MENU_H @@ -177,7 +177,7 @@ private: cTimer *timer; cRecorder *recorder; const cEvent *event; - char *instantId; + cString instantId; char *fileName; bool GetEvent(void); public: diff --git a/menuitems.c b/menuitems.c index 44c9f892..4aba66c6 100644 --- a/menuitems.c +++ b/menuitems.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: menuitems.c 1.57 2008/02/08 13:48:31 kls Exp $ + * $Id: menuitems.c 1.58 2008/02/10 16:03:30 kls Exp $ */ #include "menuitems.h" @@ -34,9 +34,8 @@ cMenuEditItem::~cMenuEditItem() void cMenuEditItem::SetValue(const char *Value) { - char *buffer = NULL; - asprintf(&buffer, "%s:\t%s", name, Value); - SetText(buffer, false); + cString buffer = cString::sprintf("%s:\t%s", name, Value); + SetText(buffer); cStatus::MsgOsdCurrentItem(buffer); } diff --git a/plugin.c b/plugin.c index a39453cd..9193125e 100644 --- a/plugin.c +++ b/plugin.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: plugin.c 1.26 2007/08/05 12:48:50 kls Exp $ + * $Id: plugin.c 1.27 2008/02/10 16:05:46 kls Exp $ */ #include "plugin.h" @@ -142,11 +142,10 @@ void cPlugin::SetConfigDirectory(const char *Dir) const char *cPlugin::ConfigDirectory(const char *PluginName) { - static char *buffer = NULL; + static cString buffer; if (!cThread::IsMainThread()) esyslog("ERROR: plugin '%s' called cPlugin::ConfigDirectory(), which is not thread safe!", PluginName ? PluginName : ""); - free(buffer); - asprintf(&buffer, "%s/plugins%s%s", configDirectory, PluginName ? "/" : "", PluginName ? PluginName : ""); + buffer = cString::sprintf("%s/plugins%s%s", configDirectory, PluginName ? "/" : "", PluginName ? PluginName : ""); return MakeDirs(buffer, true) ? buffer : NULL; } @@ -317,10 +316,7 @@ void cPluginManager::AddPlugin(const char *Args) char *p = strchr(s, ' '); if (p) *p = 0; - char *buffer = NULL; - asprintf(&buffer, "%s/%s%s%s%s", directory, LIBVDR_PREFIX, s, SO_INDICATOR, APIVERSION); - dlls.Add(new cDll(buffer, Args)); - free(buffer); + dlls.Add(new cDll(cString::sprintf("%s/%s%s%s%s", directory, LIBVDR_PREFIX, s, SO_INDICATOR, APIVERSION), Args)); free(s); } diff --git a/recording.c b/recording.c index e4e2be6a..096e0f85 100644 --- a/recording.c +++ b/recording.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: recording.c 1.158 2008/02/10 12:52:14 kls Exp $ + * $Id: recording.c 1.159 2008/02/10 21:57:09 kls Exp $ */ #include "recording.h" @@ -351,7 +351,7 @@ bool cRecordingInfo::Read(FILE *f) char *p = strchr(t, ' '); if (p) { free(channelName); - asprintf(&channelName, "%s", compactspace(p)); + channelName = strdup(compactspace(p)); *p = 0; // strips optional channel name } if (*t) @@ -525,7 +525,7 @@ cRecording::cRecording(cTimer *Timer, const cEvent *Event) else if (Timer->IsSingleEvent() || !Setup.UseSubtitle) name = strdup(Timer->File()); else - asprintf(&name, "%s~%s", Timer->File(), Subtitle); + name = strdup(cString::sprintf("%s~%s", Timer->File(), Subtitle)); // substitute characters that would cause problems in file names: strreplace(name, '\n', ' '); start = Timer->StartTime(); @@ -566,22 +566,19 @@ cRecording::cRecording(const char *FileName) } GetResume(); // read an optional info file: - char *InfoFileName = NULL; - asprintf(&InfoFileName, "%s%s", fileName, INFOFILESUFFIX); + cString InfoFileName = cString::sprintf("%s%s", fileName, INFOFILESUFFIX); FILE *f = fopen(InfoFileName, "r"); if (f) { if (!info->Read(f)) - esyslog("ERROR: EPG data problem in file %s", InfoFileName); + esyslog("ERROR: EPG data problem in file %s", *InfoFileName); fclose(f); } else if (errno != ENOENT) - LOG_ERROR_STR(InfoFileName); - free(InfoFileName); + LOG_ERROR_STR(*InfoFileName); #ifdef SUMMARYFALLBACK // fall back to the old 'summary.vdr' if there was no 'info.vdr': if (isempty(info->Title())) { - char *SummaryFileName = NULL; - asprintf(&SummaryFileName, "%s%s", fileName, SUMMARYFILESUFFIX); + cString SummaryFileName = cString::sprintf("%s%s", fileName, SUMMARYFILESUFFIX); FILE *f = fopen(SummaryFileName, "r"); if (f) { int line = 0; @@ -627,8 +624,7 @@ cRecording::cRecording(const char *FileName) free(data[i]); } else if (errno != ENOENT) - LOG_ERROR_STR(SummaryFileName); - free(SummaryFileName); + LOG_ERROR_STR(*SummaryFileName); } #endif } @@ -697,7 +693,7 @@ const char *cRecording::FileName(void) const struct tm tm_r; struct tm *t = localtime_r(&start, &tm_r); name = ExchangeChars(name, true); - asprintf(&fileName, NAMEFORMAT, VideoDirectory, name, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, priority, lifetime); + fileName = strdup(cString::sprintf(NAMEFORMAT, VideoDirectory, name, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, priority, lifetime)); name = ExchangeChars(name, false); } return fileName; @@ -716,7 +712,7 @@ const char *cRecording::Title(char Delimiter, bool NewIndicator, int Level) cons s++; else s = name; - asprintf(&titleBuffer, "%02d.%02d.%02d%c%02d:%02d%c%c%s", + titleBuffer = strdup(cString::sprintf("%02d.%02d.%02d%c%02d:%02d%c%c%s", t->tm_mday, t->tm_mon + 1, t->tm_year % 100, @@ -725,7 +721,7 @@ const char *cRecording::Title(char Delimiter, bool NewIndicator, int Level) cons t->tm_min, New, Delimiter, - s); + s)); // let's not display a trailing '~': if (!NewIndicator) stripspace(titleBuffer); @@ -785,16 +781,14 @@ bool cRecording::IsEdited(void) const bool cRecording::WriteInfo(void) { - char *InfoFileName = NULL; - asprintf(&InfoFileName, "%s%s", fileName, INFOFILESUFFIX); + cString InfoFileName = cString::sprintf("%s%s", fileName, INFOFILESUFFIX); FILE *f = fopen(InfoFileName, "w"); if (f) { info->Write(f); fclose(f); } else - LOG_ERROR_STR(InfoFileName); - free(InfoFileName); + LOG_ERROR_STR(*InfoFileName); return true; } @@ -911,8 +905,7 @@ void cRecordings::ScanVideoDir(const char *DirName, bool Foreground, int LinkLev struct dirent *e; while ((Foreground || Running()) && (e = d.Next()) != NULL) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - char *buffer; - asprintf(&buffer, "%s/%s", DirName, e->d_name); + char *buffer = strdup(AddDirectory(DirName, e->d_name)); struct stat st; if (stat(buffer, &st) == 0) { int Link = 0; @@ -1071,9 +1064,7 @@ cMark::~cMark() cString cMark::ToText(void) { - char *buffer; - asprintf(&buffer, "%s%s%s\n", *IndexToHMSF(position, true), comment ? " " : "", comment ? comment : ""); - return cString(buffer, true); + return cString::sprintf("%s%s%s\n", *IndexToHMSF(position, true), comment ? " " : "", comment ? comment : ""); } bool cMark::Parse(const char *s) @@ -1162,11 +1153,9 @@ const char *cRecordingUserCommand::command = NULL; void cRecordingUserCommand::InvokeCommand(const char *State, const char *RecordingFileName) { if (command) { - char *cmd; - asprintf(&cmd, "%s %s \"%s\"", command, State, *strescape(RecordingFileName, "\"$")); - isyslog("executing '%s'", cmd); + cString cmd = cString::sprintf("%s %s \"%s\"", command, State, *strescape(RecordingFileName, "\"$")); + isyslog("executing '%s'", *cmd); SystemExec(cmd); - free(cmd); } } diff --git a/skinclassic.c b/skinclassic.c index 13a6c896..877ef60d 100644 --- a/skinclassic.c +++ b/skinclassic.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: skinclassic.c 1.20 2008/02/10 10:01:13 kls Exp $ + * $Id: skinclassic.c 1.21 2008/02/10 16:18:25 kls Exp $ */ #include "skinclassic.h" @@ -324,12 +324,10 @@ void cSkinClassicDisplayMenu::SetEvent(const cEvent *Event) snprintf(t, sizeof(t), "%s %s - %s", *Event->GetDateString(), *Event->GetTimeString(), *Event->GetEndTimeString()); ts.Set(osd, xl, y, x1 - xl, y3 - y, t, font, Theme.Color(clrMenuEventTime), Theme.Color(clrBackground)); if (Event->Vps() && Event->Vps() != Event->StartTime()) { - char *buffer; - asprintf(&buffer, " VPS: %s ", *Event->GetVpsString()); + cString buffer = cString::sprintf(" VPS: %s ", *Event->GetVpsString()); const cFont *font = cFont::GetFont(fontSml); int w = font->Width(buffer); osd->DrawText(x1 - w, y, buffer, Theme.Color(clrMenuEventVpsFg), Theme.Color(clrMenuEventVpsBg), font, w); - free(buffer); } y += ts.Height(); y += font->Height(); diff --git a/skinsttng.c b/skinsttng.c index 54638864..c03c9a72 100644 --- a/skinsttng.c +++ b/skinsttng.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: skinsttng.c 1.22 2008/02/10 10:05:40 kls Exp $ + * $Id: skinsttng.c 1.23 2008/02/10 16:18:55 kls Exp $ */ // Star Trek: The Next Generation® is a registered trademark of Paramount Pictures @@ -570,15 +570,13 @@ void cSkinSTTNGDisplayMenu::SetEvent(const cEvent *Event) snprintf(t, sizeof(t), "%s %s - %s", *Event->GetDateString(), *Event->GetTimeString(), *Event->GetEndTimeString()); ts.Set(osd, xl, y, x4 - xl, y4 - y, t, font, Theme.Color(clrMenuEventTime), Theme.Color(clrBackground)); if (Event->Vps() && Event->Vps() != Event->StartTime()) { - char *buffer; - asprintf(&buffer, " VPS: %s ", *Event->GetVpsString()); + cString buffer = cString::sprintf(" VPS: %s ", *Event->GetVpsString()); const cFont *font = cFont::GetFont(fontSml); int w = font->Width(buffer); osd->DrawText(x4 - w, y, buffer, Theme.Color(clrMenuEventVps), frameColor, font, w); int yb = y + font->Height(); osd->DrawRectangle(x5, y, x6 - 1, yb - 1, frameColor); osd->DrawEllipse (x6, y, x7 - 1, yb - 1, frameColor, 5); - free(buffer); } y += ts.Height(); y += font->Height(); diff --git a/svdrp.c b/svdrp.c index bce0ec4f..c6e64f87 100644 --- a/svdrp.c +++ b/svdrp.c @@ -10,7 +10,7 @@ * and interact with the Video Disk Recorder - or write a full featured * graphical interface that sits on top of an SVDRP connection. * - * $Id: svdrp.c 1.106 2008/02/10 13:37:18 kls Exp $ + * $Id: svdrp.c 1.107 2008/02/15 14:48:59 kls Exp $ */ #include "svdrp.h" @@ -423,8 +423,8 @@ void cSVDRP::Reply(int Code, const char *fmt, ...) if (Code != 0) { va_list ap; va_start(ap, fmt); - char *buffer; - vasprintf(&buffer, fmt, ap); + cString buffer = cString::sprintf(fmt, ap); + va_end(ap); const char *s = buffer; while (s && *s) { const char *n = strchr(s, '\n'); @@ -437,8 +437,6 @@ void cSVDRP::Reply(int Code, const char *fmt, ...) break; s = n ? n + 1 : NULL; } - free(buffer); - va_end(ap); } else { Reply(451, "Zero return code - looks like a programming error!"); @@ -730,7 +728,7 @@ void cSVDRP::CmdEDIT(const char *Option) void cSVDRP::CmdGRAB(const char *Option) { - char *FileName = NULL; + const char *FileName = NULL; bool Jpeg = true; int Quality = -1, SizeX = -1, SizeY = -1; if (*Option) { @@ -799,10 +797,10 @@ void cSVDRP::CmdGRAB(const char *Option) char RealFileName[PATH_MAX]; if (FileName) { if (grabImageDir) { - char *s = 0; + cString s; char *slash = strrchr(FileName, '/'); if (!slash) { - asprintf(&s, "%s/%s", grabImageDir, FileName); + s = AddDirectory(grabImageDir, FileName); FileName = s; } slash = strrchr(FileName, '/'); // there definitely is one @@ -812,12 +810,10 @@ void cSVDRP::CmdGRAB(const char *Option) if (!r) { LOG_ERROR_STR(FileName); Reply(501, "Invalid file name \"%s\"", FileName); - free(s); return; } strcat(RealFileName, slash); FileName = RealFileName; - free(s); if (strncmp(FileName, grabImageDir, strlen(grabImageDir)) != 0) { Reply(501, "Invalid file name \"%s\"", FileName); return; diff --git a/themes.c b/themes.c index 21294c38..e6ee87e3 100644 --- a/themes.c +++ b/themes.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: themes.c 1.7 2007/08/05 14:11:35 kls Exp $ + * $Id: themes.c 1.8 2008/02/10 16:25:00 kls Exp $ */ #include "themes.h" @@ -285,18 +285,14 @@ void cThemes::SetThemesDirectory(const char *ThemesDirectory) void cThemes::Load(const char *SkinName, const char *ThemeName, cTheme *Theme) { - char *FileName = NULL; - asprintf(&FileName, "%s/%s-%s.theme", themesDirectory, SkinName, ThemeName); + cString FileName = cString::sprintf("%s/%s-%s.theme", themesDirectory, SkinName, ThemeName); if (access(FileName, F_OK) == 0) // the file exists Theme->Load(FileName); - free(FileName); } void cThemes::Save(const char *SkinName, cTheme *Theme) { - char *FileName = NULL; - asprintf(&FileName, "%s/%s-%s.theme", themesDirectory, SkinName, Theme->Name()); + cString FileName = cString::sprintf("%s/%s-%s.theme", themesDirectory, SkinName, Theme->Name()); if (access(FileName, F_OK) != 0) // the file does not exist Theme->Save(FileName); - free(FileName); } diff --git a/thread.c b/thread.c index 95831242..2d84918d 100644 --- a/thread.c +++ b/thread.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: thread.c 1.63 2007/10/19 14:30:13 kls Exp $ + * $Id: thread.c 1.64 2008/02/15 14:17:42 kls Exp $ */ #include "thread.h" @@ -231,7 +231,7 @@ void cThread::SetDescription(const char *Description, ...) if (Description) { va_list ap; va_start(ap, Description); - vasprintf(&description, Description, ap); + description = strdup(cString::sprintf(Description, ap)); va_end(ap); } } diff --git a/timers.c b/timers.c index 3994641d..a0e0da0e 100644 --- a/timers.c +++ b/timers.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: timers.c 1.70 2008/02/10 14:05:49 kls Exp $ + * $Id: timers.c 1.71 2008/02/10 16:26:25 kls Exp $ */ #include "timers.h" @@ -135,18 +135,15 @@ int cTimer::Compare(const cListObject &ListObject) const cString cTimer::ToText(bool UseChannelID) { - char *buffer; strreplace(file, ':', '|'); - asprintf(&buffer, "%u:%s:%s:%04d:%04d:%d:%d:%s:%s\n", flags, UseChannelID ? *Channel()->GetChannelID().ToString() : *itoa(Channel()->Number()), *PrintDay(day, weekdays, true), start, stop, priority, lifetime, file, aux ? aux : ""); + cString buffer = cString::sprintf("%u:%s:%s:%04d:%04d:%d:%d:%s:%s\n", flags, UseChannelID ? *Channel()->GetChannelID().ToString() : *itoa(Channel()->Number()), *PrintDay(day, weekdays, true), start, stop, priority, lifetime, file, aux ? aux : ""); strreplace(file, '|', ':'); - return cString(buffer, true); + return buffer; } cString cTimer::ToDescr(void) const { - char *buffer; - asprintf(&buffer, "%d (%d %04d-%04d %s'%s')", Index() + 1, Channel()->Number(), start, stop, HasFlags(tfVps) ? "VPS " : "", file); - return cString(buffer, true); + return cString::sprintf("%d (%d %04d-%04d %s'%s')", Index() + 1, Channel()->Number(), start, stop, HasFlags(tfVps) ? "VPS " : "", file); } int cTimer::TimeToInt(int t) diff --git a/tools.c b/tools.c index 3ec66a2a..43f5bfad 100644 --- a/tools.c +++ b/tools.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.c 1.141 2008/02/10 12:40:36 kls Exp $ + * $Id: tools.c 1.142 2008/02/15 14:45:05 kls Exp $ */ #include "tools.h" @@ -267,9 +267,7 @@ bool isnumber(const char *s) cString AddDirectory(const char *DirName, const char *FileName) { - char *buf; - asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName); - return cString(buf, true); + return cString::sprintf("%s/%s", DirName && *DirName ? DirName : ".", FileName); } cString itoa(int n) @@ -352,15 +350,14 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks) struct dirent *e; while ((e = d.Next()) != NULL) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - char *buffer; - asprintf(&buffer, "%s/%s", FileName, e->d_name); + cString buffer = AddDirectory(FileName, e->d_name); if (FollowSymlinks) { int size = strlen(buffer) * 2; // should be large enough char *l = MALLOC(char, size); int n = readlink(buffer, l, size); if (n < 0) { if (errno != EINVAL) - LOG_ERROR_STR(buffer); + LOG_ERROR_STR(*buffer); } else if (n < size) { l[n] = 0; @@ -372,10 +369,9 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks) esyslog("ERROR: symlink name length (%d) exceeded anticipated buffer size (%d)", n, size); free(l); } - dsyslog("removing %s", buffer); + dsyslog("removing %s", *buffer); if (remove(buffer) < 0) - LOG_ERROR_STR(buffer); - free(buffer); + LOG_ERROR_STR(*buffer); } } } @@ -405,8 +401,7 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis) struct dirent *e; while ((e = d.Next()) != NULL) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) { - char *buffer; - asprintf(&buffer, "%s/%s", DirName, e->d_name); + cString buffer = AddDirectory(DirName, e->d_name); struct stat st; if (stat(buffer, &st) == 0) { if (S_ISDIR(st.st_mode)) { @@ -417,10 +412,9 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis) empty = false; } else { - LOG_ERROR_STR(buffer); + LOG_ERROR_STR(*buffer); empty = false; } - free(buffer); } } if (RemoveThis && empty) { @@ -445,8 +439,7 @@ int DirSizeMB(const char *DirName) struct dirent *e; while (size >= 0 && (e = d.Next()) != NULL) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - char *buffer; - asprintf(&buffer, "%s/%s", DirName, e->d_name); + cString buffer = AddDirectory(DirName, e->d_name); struct stat st; if (stat(buffer, &st) == 0) { if (S_ISDIR(st.st_mode)) { @@ -460,10 +453,9 @@ int DirSizeMB(const char *DirName) size += st.st_size / MEGABYTE(1); } else { - LOG_ERROR_STR(buffer); + LOG_ERROR_STR(*buffer); size = -1; } - free(buffer); } } return size; @@ -489,13 +481,12 @@ char *ReadLink(const char *FileName) bool SpinUpDisk(const char *FileName) { - char *buf = NULL; for (int n = 0; n < 10; n++) { - free(buf); + cString buf; if (DirectoryOk(FileName)) - asprintf(&buf, "%s/vdr-%06d", *FileName ? FileName : ".", n); + buf = cString::sprintf("%s/vdr-%06d", *FileName ? FileName : ".", n); else - asprintf(&buf, "%s.vdr-%06d", FileName, n); + buf = cString::sprintf("%s.vdr-%06d", FileName, n); if (access(buf, F_OK) != 0) { // the file does not exist timeval tp1, tp2; gettimeofday(&tp1, NULL); @@ -503,21 +494,19 @@ bool SpinUpDisk(const char *FileName) // O_SYNC doesn't work on all file systems if (f >= 0) { if (fdatasync(f) < 0) - LOG_ERROR_STR(buf); + LOG_ERROR_STR(*buf); close(f); remove(buf); gettimeofday(&tp2, NULL); double seconds = (((long long)tp2.tv_sec * 1000000 + tp2.tv_usec) - ((long long)tp1.tv_sec * 1000000 + tp1.tv_usec)) / 1000000.0; if (seconds > 0.5) dsyslog("SpinUpDisk took %.2f seconds", seconds); - free(buf); return true; } else - LOG_ERROR_STR(buf); + LOG_ERROR_STR(*buf); } } - free(buf); esyslog("ERROR: SpinUpDisk failed"); return false; } @@ -888,7 +877,21 @@ cString cString::sprintf(const char *fmt, ...) va_list ap; va_start(ap, fmt); char *buffer; - vasprintf(&buffer, fmt, ap); + if (!fmt || vasprintf(&buffer, fmt, ap) < 0) { + esyslog("error in vasprintf('%s', ...)", fmt); + buffer = strdup("???"); + } + va_end(ap); + return cString(buffer, true); +} + +cString cString::sprintf(const char *fmt, va_list &ap) +{ + char *buffer; + if (!fmt || vasprintf(&buffer, fmt, ap) < 0) { + esyslog("error in vasprintf('%s', ...)", fmt); + buffer = strdup("???"); + } return cString(buffer, true); } @@ -1622,7 +1625,7 @@ cLockFile::cLockFile(const char *Directory) fileName = NULL; f = -1; if (DirectoryOk(Directory)) - asprintf(&fileName, "%s/%s", Directory, LOCKFILENAME); + fileName = strdup(AddDirectory(Directory, LOCKFILENAME)); } cLockFile::~cLockFile() diff --git a/tools.h b/tools.h index 3c804b52..dd93ef02 100644 --- a/tools.h +++ b/tools.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.h 1.110 2008/01/13 11:22:26 kls Exp $ + * $Id: tools.h 1.111 2008/02/15 14:10:11 kls Exp $ */ #ifndef __TOOLS_H @@ -161,6 +161,7 @@ public: cString &operator=(const cString &String); cString &Truncate(int Index); ///< Truncate the string at the given Index (if Index is < 0 it is counted from the end of the string). static cString sprintf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); + static cString sprintf(const char *fmt, va_list &ap); }; ssize_t safe_read(int filedes, void *buffer, size_t size);