1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00

Using cString::sprintf() instead of asprintf()

This commit is contained in:
Klaus Schmidinger 2008-02-15 14:57:48 +01:00
parent c8584521f7
commit 9495aa9923
20 changed files with 131 additions and 189 deletions

View File

@ -1689,6 +1689,8 @@ Wolfgang Rohdewald <wolfgang@rohdewald.de>
for removing an unnecessary #include from osd.c for removing an unnecessary #include from osd.c
for reporting a problem with with numerical input to switch channels if Up, Down, for reporting a problem with with numerical input to switch channels if Up, Down,
Channel+ or Channel- is pressed Channel+ or Channel- is pressed
for pointing out a possible problem with asprintf() if the return value is not
checked
Chad Flynt <hoochster@sofnet.com> Chad Flynt <hoochster@sofnet.com>
for suggestions and experiments regarding the buffer reserve in cTransfer for suggestions and experiments regarding the buffer reserve in cTransfer

View File

@ -5616,3 +5616,7 @@ Video Disk Recorder Revision History
- Limiting the length of the recording name in timers created via SVDRP in case VDR - 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 is run with --vfat, in order to avoid names that are too long for Windows
(suggested by Rolf Ahrenberg). (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.

View File

@ -65,3 +65,7 @@ VDR Plugin 'skincurses' Revision History
2008-01-19: 2008-01-19:
- Updated the Makefile of the skincurses plugin (thanks to Rolf Ahrenberg). - Updated the Makefile of the skincurses plugin (thanks to Rolf Ahrenberg).
2008-02-15:
- Using cString::sprintf() instead of asprintf().

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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 <ncurses.h> #include <ncurses.h>
@ -11,7 +11,7 @@
#include <vdr/plugin.h> #include <vdr/plugin.h>
#include <vdr/skins.h> #include <vdr/skins.h>
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 *DESCRIPTION = trNOOP("A text only skin");
static const char *MAINMENUENTRY = NULL; 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()); 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); ts.Set(osd, 0, y, ScOsdWidth, ScOsdHeight - y - 2, t, &Font, clrYellow, clrBackground);
if (Event->Vps() && Event->Vps() != Event->StartTime()) { if (Event->Vps() && Event->Vps() != Event->StartTime()) {
char *buffer; cString buffer = cString::sprintf(" VPS: %s", *Event->GetVpsString());
asprintf(&buffer, " VPS: %s", *Event->GetVpsString());
osd->DrawText(ScOsdWidth - Utf8StrLen(buffer), y, buffer, clrBlack, clrYellow, &Font); osd->DrawText(ScOsdWidth - Utf8StrLen(buffer), y, buffer, clrBlack, clrYellow, &Font);
free(buffer);
} }
y += ts.Height(); y += ts.Height();
y += 1; y += 1;

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "channels.h"
@ -658,12 +658,12 @@ cString cChannel::ToText(const cChannel *Channel)
q += sprintf(q, ";%s", Channel->provider); q += sprintf(q, ";%s", Channel->provider);
*q = 0; *q = 0;
strreplace(FullName, ':', '|'); strreplace(FullName, ':', '|');
char *buffer; cString buffer;
if (Channel->groupSep) { if (Channel->groupSep) {
if (Channel->number) if (Channel->number)
asprintf(&buffer, ":@%d %s\n", Channel->number, FullName); buffer = cString::sprintf(":@%d %s\n", Channel->number, FullName);
else else
asprintf(&buffer, ":%s\n", FullName); buffer = cString::sprintf(":%s\n", FullName);
} }
else { else {
char vpidbuf[32]; char vpidbuf[32];
@ -685,9 +685,9 @@ cString cChannel::ToText(const cChannel *Channel)
q = caidbuf; q = caidbuf;
q += IntArrayToString(q, Channel->caids, 16); q += IntArrayToString(q, Channel->caids, 16);
*q = 0; *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 cString cChannel::ToText(void) const

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "config.h"
@ -62,9 +62,9 @@ const char *cCommand::Execute(const char *Parameters)
{ {
free(result); free(result);
result = NULL; result = NULL;
char *cmdbuf = NULL; cString cmdbuf;
if (Parameters) if (Parameters)
asprintf(&cmdbuf, "%s %s", command, Parameters); cmdbuf = cString::sprintf("%s %s", command, Parameters);
const char *cmd = cmdbuf ? cmdbuf : command; const char *cmd = cmdbuf ? cmdbuf : command;
dsyslog("executing command '%s'", cmd); dsyslog("executing command '%s'", cmd);
cPipe p; cPipe p;
@ -82,7 +82,6 @@ const char *cCommand::Execute(const char *Parameters)
} }
else else
esyslog("ERROR: can't open pipe for command '%s'", cmd); esyslog("ERROR: can't open pipe for command '%s'", cmd);
free(cmdbuf);
return result; 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) void cSetup::Store(const char *Name, int Value, const char *Plugin)
{ {
char *buffer = NULL; Store(Name, cString::sprintf("%d", Value), Plugin);
asprintf(&buffer, "%d", Value);
Store(Name, buffer, Plugin);
free(buffer);
} }
bool cSetup::Load(const char *FileName) bool cSetup::Load(const char *FileName)

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #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); DisplayMenu->SetItem(tr("Phase 2: Learning specific key codes"), 2, false, false);
eKeys NewKey = kUp; eKeys NewKey = kUp;
while (NewKey != kNone) { while (NewKey != kNone) {
char *Prompt; DisplayMenu->SetItem(cString::sprintf(tr("Press key for '%s'"), cKey::ToString(NewKey, true)), 4, false, false);
asprintf(&Prompt, tr("Press key for '%s'"), cKey::ToString(NewKey, true));
DisplayMenu->SetItem(Prompt, 4, false, false);
free(Prompt);
cRemote::Clear(); cRemote::Clear();
DisplayMenu->Flush(); DisplayMenu->Flush();
for (eKeys k = NewKey; k == NewKey; ) { for (eKeys k = NewKey; k == NewKey; ) {

88
menu.c
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "menu.h"
@ -147,12 +147,8 @@ cMenuEditSrcItem::cMenuEditSrcItem(const char *Name, int *Value)
void cMenuEditSrcItem::Set(void) void cMenuEditSrcItem::Set(void)
{ {
if (source) { if (source)
char *buffer = NULL; SetValue(cString::sprintf("%s - %s", *cSource::ToString(source->Code()), source->Description()));
asprintf(&buffer, "%s - %s", *cSource::ToString(source->Code()), source->Description());
SetValue(buffer);
free(buffer);
}
else else
cMenuEditIntItem::Set(); cMenuEditIntItem::Set();
} }
@ -402,16 +398,16 @@ int cMenuChannelItem::Compare(const cListObject &ListObject) const
void cMenuChannelItem::Set(void) void cMenuChannelItem::Set(void)
{ {
char *buffer = NULL; cString buffer;
if (!channel->GroupSep()) { if (!channel->GroupSep()) {
if (sortMode == csmProvider) 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 else
asprintf(&buffer, "%d\t%s", channel->Number(), channel->Name()); buffer = cString::sprintf("%d\t%s", channel->Number(), channel->Name());
} }
else else
asprintf(&buffer, "---\t%s ----------------------------------------------------------------", channel->Name()); buffer = cString::sprintf("---\t%s ----------------------------------------------------------------", channel->Name());
SetText(buffer, false); SetText(buffer);
} }
// --- cMenuChannels --------------------------------------------------------- // --- cMenuChannels ---------------------------------------------------------
@ -816,8 +812,7 @@ void cMenuTimerItem::Set(void)
strftime(buffer, sizeof(buffer), "%Y%m%d", &tm_r); strftime(buffer, sizeof(buffer), "%Y%m%d", &tm_r);
day = buffer; day = buffer;
} }
char *buffer = NULL; SetText(cString::sprintf("%c\t%d\t%s%s%s\t%02d:%02d\t%02d:%02d\t%s",
asprintf(&buffer, "%c\t%d\t%s%s%s\t%02d:%02d\t%02d:%02d\t%s",
!(timer->HasFlags(tfActive)) ? ' ' : timer->FirstDay() ? '!' : timer->Recording() ? '#' : '>', !(timer->HasFlags(tfActive)) ? ' ' : timer->FirstDay() ? '!' : timer->Recording() ? '#' : '>',
timer->Channel()->Number(), timer->Channel()->Number(),
*name, *name,
@ -827,8 +822,7 @@ void cMenuTimerItem::Set(void)
timer->Start() % 100, timer->Start() % 100,
timer->Stop() / 100, timer->Stop() / 100,
timer->Stop() % 100, timer->Stop() % 100,
timer->File()); timer->File()));
SetText(buffer, false);
} }
// --- cMenuTimers ----------------------------------------------------------- // --- cMenuTimers -----------------------------------------------------------
@ -1092,19 +1086,19 @@ bool cMenuScheduleItem::Update(bool Force)
int OldTimerMatch = timerMatch; int OldTimerMatch = timerMatch;
Timers.GetMatch(event, &timerMatch); Timers.GetMatch(event, &timerMatch);
if (Force || timerMatch != OldTimerMatch) { if (Force || timerMatch != OldTimerMatch) {
char *buffer = NULL; cString buffer;
char t = TimerMatchChars[timerMatch]; char t = TimerMatchChars[timerMatch];
char v = event->Vps() && (event->Vps() - event->StartTime()) ? 'V' : ' '; char v = event->Vps() && (event->Vps() - event->StartTime()) ? 'V' : ' ';
char r = event->SeenWithin(30) && event->IsRunning() ? '*' : ' '; char r = event->SeenWithin(30) && event->IsRunning() ? '*' : ' ';
const char *csn = channel ? channel->ShortName(true) : NULL; const char *csn = channel ? channel->ShortName(true) : NULL;
cString eds = event->GetDateString(); cString eds = event->GetDateString();
if (channel && withDate) 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) 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 else
asprintf(&buffer, "%.*s\t%s\t%c%c%c\t%s", Utf8SymChars(eds, 6), *eds, *event->GetTimeString(), t, v, r, event->Title()); 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, false); SetText(buffer);
result = true; result = true;
} }
return result; return result;
@ -1325,10 +1319,7 @@ void cMenuSchedule::PrepareScheduleAllThis(const cEvent *Event, const cChannel *
{ {
Clear(); Clear();
SetCols(7, 6, 4); SetCols(7, 6, 4);
char *buffer = NULL; SetTitle(cString::sprintf(tr("Schedule - %s"), Channel->Name()));
asprintf(&buffer, tr("Schedule - %s"), Channel->Name());
SetTitle(buffer);
free(buffer);
if (schedules && Channel) { if (schedules && Channel) {
const cSchedule *Schedule = schedules->GetSchedule(Channel); const cSchedule *Schedule = schedules->GetSchedule(Channel);
if (Schedule) { if (Schedule) {
@ -1346,10 +1337,7 @@ void cMenuSchedule::PrepareScheduleThisThis(const cEvent *Event, const cChannel
{ {
Clear(); Clear();
SetCols(7, 6, 4); SetCols(7, 6, 4);
char *buffer = NULL; SetTitle(cString::sprintf(tr("This event - %s"), Channel->Name()));
asprintf(&buffer, tr("This event - %s"), Channel->Name());
SetTitle(buffer);
free(buffer);
if (schedules && Channel && Event) { if (schedules && Channel && Event) {
const cSchedule *Schedule = schedules->GetSchedule(Channel); const cSchedule *Schedule = schedules->GetSchedule(Channel);
if (Schedule) { if (Schedule) {
@ -1590,17 +1578,11 @@ eOSState cMenuCommands::Execute(void)
{ {
cCommand *command = commands->Get(Current()); cCommand *command = commands->Get(Current());
if (command) { if (command) {
char *buffer = NULL;
bool confirmed = true; bool confirmed = true;
if (command->Confirm()) { if (command->Confirm())
asprintf(&buffer, "%s?", command->Title()); confirmed = Interface->Confirm(cString::sprintf("%s?", command->Title()));
confirmed = Interface->Confirm(buffer);
free(buffer);
}
if (confirmed) { if (confirmed) {
asprintf(&buffer, "%s...", command->Title()); Skins.Message(mtStatus, cString::sprintf("%s...", command->Title()));
Skins.Message(mtStatus, buffer);
free(buffer);
const char *Result = command->Execute(parameters); const char *Result = command->Execute(parameters);
Skins.Message(mtStatus, NULL); Skins.Message(mtStatus, NULL);
if (Result) if (Result)
@ -1916,9 +1898,7 @@ void cMenuRecordingItem::IncrementCounter(bool New)
totalEntries++; totalEntries++;
if (New) if (New)
newEntries++; newEntries++;
char *buffer = NULL; SetText(cString::sprintf("%d\t%d\t%s", totalEntries, newEntries, name));
asprintf(&buffer, "%d\t%d\t%s", totalEntries, newEntries, name);
SetText(buffer, false);
} }
// --- cMenuRecordings ------------------------------------------------------- // --- cMenuRecordings -------------------------------------------------------
@ -2036,13 +2016,12 @@ bool cMenuRecordings::Open(bool OpenSubMenus)
cMenuRecordingItem *ri = (cMenuRecordingItem *)Get(Current()); cMenuRecordingItem *ri = (cMenuRecordingItem *)Get(Current());
if (ri && ri->IsDirectory()) { if (ri && ri->IsDirectory()) {
const char *t = ri->Name(); const char *t = ri->Name();
char *buffer = NULL; cString buffer;
if (base) { if (base) {
asprintf(&buffer, "%s~%s", base, t); buffer = cString::sprintf("%s~%s", base, t);
t = buffer; t = buffer;
} }
AddSubMenu(new cMenuRecordings(t, level + 1, OpenSubMenus)); AddSubMenu(new cMenuRecordings(t, level + 1, OpenSubMenus));
free(buffer);
return true; return true;
} }
return false; return false;
@ -2144,11 +2123,8 @@ eOSState cMenuRecordings::Commands(eKeys Key)
if (ri && !ri->IsDirectory()) { if (ri && !ri->IsDirectory()) {
cRecording *recording = GetRecording(ri); cRecording *recording = GetRecording(ri);
if (recording) { if (recording) {
char *parameter = NULL;
asprintf(&parameter, "\"%s\"", *strescape(recording->FileName(), "\"$"));
cMenuCommands *menu; cMenuCommands *menu;
eOSState state = AddSubMenu(menu = new cMenuCommands(tr("Recording commands"), &RecordingCommands, parameter)); eOSState state = AddSubMenu(menu = new cMenuCommands(tr("Recording commands"), &RecordingCommands, cString::sprintf("\"%s\"", *strescape(recording->FileName(), "\"$"))));
free(parameter);
if (Key != kNone) if (Key != kNone)
state = menu->ProcessKey(Key); state = menu->ProcessKey(Key);
return state; return state;
@ -2851,12 +2827,8 @@ cMenuSetupPlugins::cMenuSetupPlugins(void)
SetHasHotkeys(); SetHasHotkeys();
for (int i = 0; ; i++) { for (int i = 0; ; i++) {
cPlugin *p = cPluginManager::GetPlugin(i); cPlugin *p = cPluginManager::GetPlugin(i);
if (p) { if (p)
char *buffer = NULL; Add(new cMenuSetupPluginItem(hk(cString::sprintf("%s (%s) - %s", p->Name(), p->Version(), p->Description())), i));
asprintf(&buffer, "%s (%s) - %s", p->Name(), p->Version(), p->Description());
Add(new cMenuSetupPluginItem(hk(buffer), i));
free(buffer);
}
else else
break; break;
} }
@ -3098,10 +3070,8 @@ bool cMenuMain::Update(bool Force)
} }
const char *s = NULL; const char *s = NULL;
while ((s = cRecordControls::GetInstantId(s)) != NULL) { while ((s = cRecordControls::GetInstantId(s)) != NULL) {
char *buffer = NULL;
asprintf(&buffer, "%s%s", tr(STOP_RECORDING), s);
cOsdItem *item = new cOsdItem(osStopRecord); cOsdItem *item = new cOsdItem(osStopRecord);
item->SetText(buffer, false); item->SetText(cString::sprintf("%s%s", tr(STOP_RECORDING), s));
Add(item); Add(item);
if (!stopRecordingItem) if (!stopRecordingItem)
stopRecordingItem = item; stopRecordingItem = item;
@ -3792,7 +3762,6 @@ cRecordControl::cRecordControl(cDevice *Device, cTimer *Timer, bool Pause)
cSchedules::Schedules(SchedulesLock); cSchedules::Schedules(SchedulesLock);
event = NULL; event = NULL;
instantId = NULL;
fileName = NULL; fileName = NULL;
recorder = NULL; recorder = NULL;
device = Device; device = Device;
@ -3802,7 +3771,7 @@ cRecordControl::cRecordControl(cDevice *Device, cTimer *Timer, bool Pause)
timer = new cTimer(true, Pause); timer = new cTimer(true, Pause);
Timers.Add(timer); Timers.Add(timer);
Timers.SetModified(); 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->SetPending(true);
timer->SetRecording(true); timer->SetRecording(true);
@ -3857,7 +3826,6 @@ cRecordControl::cRecordControl(cDevice *Device, cTimer *Timer, bool Pause)
cRecordControl::~cRecordControl() cRecordControl::~cRecordControl()
{ {
Stop(); Stop();
free(instantId);
free(fileName); free(fileName);
} }

4
menu.h
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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 #ifndef __MENU_H
@ -177,7 +177,7 @@ private:
cTimer *timer; cTimer *timer;
cRecorder *recorder; cRecorder *recorder;
const cEvent *event; const cEvent *event;
char *instantId; cString instantId;
char *fileName; char *fileName;
bool GetEvent(void); bool GetEvent(void);
public: public:

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "menuitems.h"
@ -34,9 +34,8 @@ cMenuEditItem::~cMenuEditItem()
void cMenuEditItem::SetValue(const char *Value) void cMenuEditItem::SetValue(const char *Value)
{ {
char *buffer = NULL; cString buffer = cString::sprintf("%s:\t%s", name, Value);
asprintf(&buffer, "%s:\t%s", name, Value); SetText(buffer);
SetText(buffer, false);
cStatus::MsgOsdCurrentItem(buffer); cStatus::MsgOsdCurrentItem(buffer);
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "plugin.h"
@ -142,11 +142,10 @@ void cPlugin::SetConfigDirectory(const char *Dir)
const char *cPlugin::ConfigDirectory(const char *PluginName) const char *cPlugin::ConfigDirectory(const char *PluginName)
{ {
static char *buffer = NULL; static cString buffer;
if (!cThread::IsMainThread()) if (!cThread::IsMainThread())
esyslog("ERROR: plugin '%s' called cPlugin::ConfigDirectory(), which is not thread safe!", PluginName ? PluginName : "<no name given>"); esyslog("ERROR: plugin '%s' called cPlugin::ConfigDirectory(), which is not thread safe!", PluginName ? PluginName : "<no name given>");
free(buffer); buffer = cString::sprintf("%s/plugins%s%s", configDirectory, PluginName ? "/" : "", PluginName ? PluginName : "");
asprintf(&buffer, "%s/plugins%s%s", configDirectory, PluginName ? "/" : "", PluginName ? PluginName : "");
return MakeDirs(buffer, true) ? buffer : NULL; return MakeDirs(buffer, true) ? buffer : NULL;
} }
@ -317,10 +316,7 @@ void cPluginManager::AddPlugin(const char *Args)
char *p = strchr(s, ' '); char *p = strchr(s, ' ');
if (p) if (p)
*p = 0; *p = 0;
char *buffer = NULL; dlls.Add(new cDll(cString::sprintf("%s/%s%s%s%s", directory, LIBVDR_PREFIX, s, SO_INDICATOR, APIVERSION), Args));
asprintf(&buffer, "%s/%s%s%s%s", directory, LIBVDR_PREFIX, s, SO_INDICATOR, APIVERSION);
dlls.Add(new cDll(buffer, Args));
free(buffer);
free(s); free(s);
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "recording.h"
@ -351,7 +351,7 @@ bool cRecordingInfo::Read(FILE *f)
char *p = strchr(t, ' '); char *p = strchr(t, ' ');
if (p) { if (p) {
free(channelName); free(channelName);
asprintf(&channelName, "%s", compactspace(p)); channelName = strdup(compactspace(p));
*p = 0; // strips optional channel name *p = 0; // strips optional channel name
} }
if (*t) if (*t)
@ -525,7 +525,7 @@ cRecording::cRecording(cTimer *Timer, const cEvent *Event)
else if (Timer->IsSingleEvent() || !Setup.UseSubtitle) else if (Timer->IsSingleEvent() || !Setup.UseSubtitle)
name = strdup(Timer->File()); name = strdup(Timer->File());
else 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: // substitute characters that would cause problems in file names:
strreplace(name, '\n', ' '); strreplace(name, '\n', ' ');
start = Timer->StartTime(); start = Timer->StartTime();
@ -566,22 +566,19 @@ cRecording::cRecording(const char *FileName)
} }
GetResume(); GetResume();
// read an optional info file: // read an optional info file:
char *InfoFileName = NULL; cString InfoFileName = cString::sprintf("%s%s", fileName, INFOFILESUFFIX);
asprintf(&InfoFileName, "%s%s", fileName, INFOFILESUFFIX);
FILE *f = fopen(InfoFileName, "r"); FILE *f = fopen(InfoFileName, "r");
if (f) { if (f) {
if (!info->Read(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); fclose(f);
} }
else if (errno != ENOENT) else if (errno != ENOENT)
LOG_ERROR_STR(InfoFileName); LOG_ERROR_STR(*InfoFileName);
free(InfoFileName);
#ifdef SUMMARYFALLBACK #ifdef SUMMARYFALLBACK
// fall back to the old 'summary.vdr' if there was no 'info.vdr': // fall back to the old 'summary.vdr' if there was no 'info.vdr':
if (isempty(info->Title())) { if (isempty(info->Title())) {
char *SummaryFileName = NULL; cString SummaryFileName = cString::sprintf("%s%s", fileName, SUMMARYFILESUFFIX);
asprintf(&SummaryFileName, "%s%s", fileName, SUMMARYFILESUFFIX);
FILE *f = fopen(SummaryFileName, "r"); FILE *f = fopen(SummaryFileName, "r");
if (f) { if (f) {
int line = 0; int line = 0;
@ -627,8 +624,7 @@ cRecording::cRecording(const char *FileName)
free(data[i]); free(data[i]);
} }
else if (errno != ENOENT) else if (errno != ENOENT)
LOG_ERROR_STR(SummaryFileName); LOG_ERROR_STR(*SummaryFileName);
free(SummaryFileName);
} }
#endif #endif
} }
@ -697,7 +693,7 @@ const char *cRecording::FileName(void) const
struct tm tm_r; struct tm tm_r;
struct tm *t = localtime_r(&start, &tm_r); struct tm *t = localtime_r(&start, &tm_r);
name = ExchangeChars(name, true); 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); name = ExchangeChars(name, false);
} }
return fileName; return fileName;
@ -716,7 +712,7 @@ const char *cRecording::Title(char Delimiter, bool NewIndicator, int Level) cons
s++; s++;
else else
s = name; 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_mday,
t->tm_mon + 1, t->tm_mon + 1,
t->tm_year % 100, t->tm_year % 100,
@ -725,7 +721,7 @@ const char *cRecording::Title(char Delimiter, bool NewIndicator, int Level) cons
t->tm_min, t->tm_min,
New, New,
Delimiter, Delimiter,
s); s));
// let's not display a trailing '~': // let's not display a trailing '~':
if (!NewIndicator) if (!NewIndicator)
stripspace(titleBuffer); stripspace(titleBuffer);
@ -785,16 +781,14 @@ bool cRecording::IsEdited(void) const
bool cRecording::WriteInfo(void) bool cRecording::WriteInfo(void)
{ {
char *InfoFileName = NULL; cString InfoFileName = cString::sprintf("%s%s", fileName, INFOFILESUFFIX);
asprintf(&InfoFileName, "%s%s", fileName, INFOFILESUFFIX);
FILE *f = fopen(InfoFileName, "w"); FILE *f = fopen(InfoFileName, "w");
if (f) { if (f) {
info->Write(f); info->Write(f);
fclose(f); fclose(f);
} }
else else
LOG_ERROR_STR(InfoFileName); LOG_ERROR_STR(*InfoFileName);
free(InfoFileName);
return true; return true;
} }
@ -911,8 +905,7 @@ void cRecordings::ScanVideoDir(const char *DirName, bool Foreground, int LinkLev
struct dirent *e; struct dirent *e;
while ((Foreground || Running()) && (e = d.Next()) != NULL) { while ((Foreground || Running()) && (e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
char *buffer; char *buffer = strdup(AddDirectory(DirName, e->d_name));
asprintf(&buffer, "%s/%s", DirName, e->d_name);
struct stat st; struct stat st;
if (stat(buffer, &st) == 0) { if (stat(buffer, &st) == 0) {
int Link = 0; int Link = 0;
@ -1071,9 +1064,7 @@ cMark::~cMark()
cString cMark::ToText(void) cString cMark::ToText(void)
{ {
char *buffer; return cString::sprintf("%s%s%s\n", *IndexToHMSF(position, true), comment ? " " : "", comment ? comment : "");
asprintf(&buffer, "%s%s%s\n", *IndexToHMSF(position, true), comment ? " " : "", comment ? comment : "");
return cString(buffer, true);
} }
bool cMark::Parse(const char *s) bool cMark::Parse(const char *s)
@ -1162,11 +1153,9 @@ const char *cRecordingUserCommand::command = NULL;
void cRecordingUserCommand::InvokeCommand(const char *State, const char *RecordingFileName) void cRecordingUserCommand::InvokeCommand(const char *State, const char *RecordingFileName)
{ {
if (command) { if (command) {
char *cmd; cString cmd = cString::sprintf("%s %s \"%s\"", command, State, *strescape(RecordingFileName, "\"$"));
asprintf(&cmd, "%s %s \"%s\"", command, State, *strescape(RecordingFileName, "\"$")); isyslog("executing '%s'", *cmd);
isyslog("executing '%s'", cmd);
SystemExec(cmd); SystemExec(cmd);
free(cmd);
} }
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #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()); 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)); ts.Set(osd, xl, y, x1 - xl, y3 - y, t, font, Theme.Color(clrMenuEventTime), Theme.Color(clrBackground));
if (Event->Vps() && Event->Vps() != Event->StartTime()) { if (Event->Vps() && Event->Vps() != Event->StartTime()) {
char *buffer; cString buffer = cString::sprintf(" VPS: %s ", *Event->GetVpsString());
asprintf(&buffer, " VPS: %s ", *Event->GetVpsString());
const cFont *font = cFont::GetFont(fontSml); const cFont *font = cFont::GetFont(fontSml);
int w = font->Width(buffer); int w = font->Width(buffer);
osd->DrawText(x1 - w, y, buffer, Theme.Color(clrMenuEventVpsFg), Theme.Color(clrMenuEventVpsBg), font, w); osd->DrawText(x1 - w, y, buffer, Theme.Color(clrMenuEventVpsFg), Theme.Color(clrMenuEventVpsBg), font, w);
free(buffer);
} }
y += ts.Height(); y += ts.Height();
y += font->Height(); y += font->Height();

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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 // 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()); 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)); ts.Set(osd, xl, y, x4 - xl, y4 - y, t, font, Theme.Color(clrMenuEventTime), Theme.Color(clrBackground));
if (Event->Vps() && Event->Vps() != Event->StartTime()) { if (Event->Vps() && Event->Vps() != Event->StartTime()) {
char *buffer; cString buffer = cString::sprintf(" VPS: %s ", *Event->GetVpsString());
asprintf(&buffer, " VPS: %s ", *Event->GetVpsString());
const cFont *font = cFont::GetFont(fontSml); const cFont *font = cFont::GetFont(fontSml);
int w = font->Width(buffer); int w = font->Width(buffer);
osd->DrawText(x4 - w, y, buffer, Theme.Color(clrMenuEventVps), frameColor, font, w); osd->DrawText(x4 - w, y, buffer, Theme.Color(clrMenuEventVps), frameColor, font, w);
int yb = y + font->Height(); int yb = y + font->Height();
osd->DrawRectangle(x5, y, x6 - 1, yb - 1, frameColor); osd->DrawRectangle(x5, y, x6 - 1, yb - 1, frameColor);
osd->DrawEllipse (x6, y, x7 - 1, yb - 1, frameColor, 5); osd->DrawEllipse (x6, y, x7 - 1, yb - 1, frameColor, 5);
free(buffer);
} }
y += ts.Height(); y += ts.Height();
y += font->Height(); y += font->Height();

16
svdrp.c
View File

@ -10,7 +10,7 @@
* and interact with the Video Disk Recorder - or write a full featured * and interact with the Video Disk Recorder - or write a full featured
* graphical interface that sits on top of an SVDRP connection. * 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" #include "svdrp.h"
@ -423,8 +423,8 @@ void cSVDRP::Reply(int Code, const char *fmt, ...)
if (Code != 0) { if (Code != 0) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
char *buffer; cString buffer = cString::sprintf(fmt, ap);
vasprintf(&buffer, fmt, ap); va_end(ap);
const char *s = buffer; const char *s = buffer;
while (s && *s) { while (s && *s) {
const char *n = strchr(s, '\n'); const char *n = strchr(s, '\n');
@ -437,8 +437,6 @@ void cSVDRP::Reply(int Code, const char *fmt, ...)
break; break;
s = n ? n + 1 : NULL; s = n ? n + 1 : NULL;
} }
free(buffer);
va_end(ap);
} }
else { else {
Reply(451, "Zero return code - looks like a programming error!"); 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) void cSVDRP::CmdGRAB(const char *Option)
{ {
char *FileName = NULL; const char *FileName = NULL;
bool Jpeg = true; bool Jpeg = true;
int Quality = -1, SizeX = -1, SizeY = -1; int Quality = -1, SizeX = -1, SizeY = -1;
if (*Option) { if (*Option) {
@ -799,10 +797,10 @@ void cSVDRP::CmdGRAB(const char *Option)
char RealFileName[PATH_MAX]; char RealFileName[PATH_MAX];
if (FileName) { if (FileName) {
if (grabImageDir) { if (grabImageDir) {
char *s = 0; cString s;
char *slash = strrchr(FileName, '/'); char *slash = strrchr(FileName, '/');
if (!slash) { if (!slash) {
asprintf(&s, "%s/%s", grabImageDir, FileName); s = AddDirectory(grabImageDir, FileName);
FileName = s; FileName = s;
} }
slash = strrchr(FileName, '/'); // there definitely is one slash = strrchr(FileName, '/'); // there definitely is one
@ -812,12 +810,10 @@ void cSVDRP::CmdGRAB(const char *Option)
if (!r) { if (!r) {
LOG_ERROR_STR(FileName); LOG_ERROR_STR(FileName);
Reply(501, "Invalid file name \"%s\"", FileName); Reply(501, "Invalid file name \"%s\"", FileName);
free(s);
return; return;
} }
strcat(RealFileName, slash); strcat(RealFileName, slash);
FileName = RealFileName; FileName = RealFileName;
free(s);
if (strncmp(FileName, grabImageDir, strlen(grabImageDir)) != 0) { if (strncmp(FileName, grabImageDir, strlen(grabImageDir)) != 0) {
Reply(501, "Invalid file name \"%s\"", FileName); Reply(501, "Invalid file name \"%s\"", FileName);
return; return;

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "themes.h"
@ -285,18 +285,14 @@ void cThemes::SetThemesDirectory(const char *ThemesDirectory)
void cThemes::Load(const char *SkinName, const char *ThemeName, cTheme *Theme) void cThemes::Load(const char *SkinName, const char *ThemeName, cTheme *Theme)
{ {
char *FileName = NULL; cString FileName = cString::sprintf("%s/%s-%s.theme", themesDirectory, SkinName, ThemeName);
asprintf(&FileName, "%s/%s-%s.theme", themesDirectory, SkinName, ThemeName);
if (access(FileName, F_OK) == 0) // the file exists if (access(FileName, F_OK) == 0) // the file exists
Theme->Load(FileName); Theme->Load(FileName);
free(FileName);
} }
void cThemes::Save(const char *SkinName, cTheme *Theme) void cThemes::Save(const char *SkinName, cTheme *Theme)
{ {
char *FileName = NULL; cString FileName = cString::sprintf("%s/%s-%s.theme", themesDirectory, SkinName, Theme->Name());
asprintf(&FileName, "%s/%s-%s.theme", themesDirectory, SkinName, Theme->Name());
if (access(FileName, F_OK) != 0) // the file does not exist if (access(FileName, F_OK) != 0) // the file does not exist
Theme->Save(FileName); Theme->Save(FileName);
free(FileName);
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "thread.h"
@ -231,7 +231,7 @@ void cThread::SetDescription(const char *Description, ...)
if (Description) { if (Description) {
va_list ap; va_list ap;
va_start(ap, Description); va_start(ap, Description);
vasprintf(&description, Description, ap); description = strdup(cString::sprintf(Description, ap));
va_end(ap); va_end(ap);
} }
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "timers.h"
@ -135,18 +135,15 @@ int cTimer::Compare(const cListObject &ListObject) const
cString cTimer::ToText(bool UseChannelID) cString cTimer::ToText(bool UseChannelID)
{ {
char *buffer;
strreplace(file, ':', '|'); 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, '|', ':'); strreplace(file, '|', ':');
return cString(buffer, true); return buffer;
} }
cString cTimer::ToDescr(void) const cString cTimer::ToDescr(void) const
{ {
char *buffer; return cString::sprintf("%d (%d %04d-%04d %s'%s')", Index() + 1, Channel()->Number(), start, stop, HasFlags(tfVps) ? "VPS " : "", file);
asprintf(&buffer, "%d (%d %04d-%04d %s'%s')", Index() + 1, Channel()->Number(), start, stop, HasFlags(tfVps) ? "VPS " : "", file);
return cString(buffer, true);
} }
int cTimer::TimeToInt(int t) int cTimer::TimeToInt(int t)

59
tools.c
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "tools.h"
@ -267,9 +267,7 @@ bool isnumber(const char *s)
cString AddDirectory(const char *DirName, const char *FileName) cString AddDirectory(const char *DirName, const char *FileName)
{ {
char *buf; return cString::sprintf("%s/%s", DirName && *DirName ? DirName : ".", FileName);
asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName);
return cString(buf, true);
} }
cString itoa(int n) cString itoa(int n)
@ -352,15 +350,14 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
struct dirent *e; struct dirent *e;
while ((e = d.Next()) != NULL) { while ((e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
char *buffer; cString buffer = AddDirectory(FileName, e->d_name);
asprintf(&buffer, "%s/%s", FileName, e->d_name);
if (FollowSymlinks) { if (FollowSymlinks) {
int size = strlen(buffer) * 2; // should be large enough int size = strlen(buffer) * 2; // should be large enough
char *l = MALLOC(char, size); char *l = MALLOC(char, size);
int n = readlink(buffer, l, size); int n = readlink(buffer, l, size);
if (n < 0) { if (n < 0) {
if (errno != EINVAL) if (errno != EINVAL)
LOG_ERROR_STR(buffer); LOG_ERROR_STR(*buffer);
} }
else if (n < size) { else if (n < size) {
l[n] = 0; 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); esyslog("ERROR: symlink name length (%d) exceeded anticipated buffer size (%d)", n, size);
free(l); free(l);
} }
dsyslog("removing %s", buffer); dsyslog("removing %s", *buffer);
if (remove(buffer) < 0) if (remove(buffer) < 0)
LOG_ERROR_STR(buffer); LOG_ERROR_STR(*buffer);
free(buffer);
} }
} }
} }
@ -405,8 +401,7 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
struct dirent *e; struct dirent *e;
while ((e = d.Next()) != NULL) { while ((e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) {
char *buffer; cString buffer = AddDirectory(DirName, e->d_name);
asprintf(&buffer, "%s/%s", DirName, e->d_name);
struct stat st; struct stat st;
if (stat(buffer, &st) == 0) { if (stat(buffer, &st) == 0) {
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
@ -417,10 +412,9 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
empty = false; empty = false;
} }
else { else {
LOG_ERROR_STR(buffer); LOG_ERROR_STR(*buffer);
empty = false; empty = false;
} }
free(buffer);
} }
} }
if (RemoveThis && empty) { if (RemoveThis && empty) {
@ -445,8 +439,7 @@ int DirSizeMB(const char *DirName)
struct dirent *e; struct dirent *e;
while (size >= 0 && (e = d.Next()) != NULL) { while (size >= 0 && (e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
char *buffer; cString buffer = AddDirectory(DirName, e->d_name);
asprintf(&buffer, "%s/%s", DirName, e->d_name);
struct stat st; struct stat st;
if (stat(buffer, &st) == 0) { if (stat(buffer, &st) == 0) {
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
@ -460,10 +453,9 @@ int DirSizeMB(const char *DirName)
size += st.st_size / MEGABYTE(1); size += st.st_size / MEGABYTE(1);
} }
else { else {
LOG_ERROR_STR(buffer); LOG_ERROR_STR(*buffer);
size = -1; size = -1;
} }
free(buffer);
} }
} }
return size; return size;
@ -489,13 +481,12 @@ char *ReadLink(const char *FileName)
bool SpinUpDisk(const char *FileName) bool SpinUpDisk(const char *FileName)
{ {
char *buf = NULL;
for (int n = 0; n < 10; n++) { for (int n = 0; n < 10; n++) {
free(buf); cString buf;
if (DirectoryOk(FileName)) if (DirectoryOk(FileName))
asprintf(&buf, "%s/vdr-%06d", *FileName ? FileName : ".", n); buf = cString::sprintf("%s/vdr-%06d", *FileName ? FileName : ".", n);
else 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 if (access(buf, F_OK) != 0) { // the file does not exist
timeval tp1, tp2; timeval tp1, tp2;
gettimeofday(&tp1, NULL); gettimeofday(&tp1, NULL);
@ -503,21 +494,19 @@ bool SpinUpDisk(const char *FileName)
// O_SYNC doesn't work on all file systems // O_SYNC doesn't work on all file systems
if (f >= 0) { if (f >= 0) {
if (fdatasync(f) < 0) if (fdatasync(f) < 0)
LOG_ERROR_STR(buf); LOG_ERROR_STR(*buf);
close(f); close(f);
remove(buf); remove(buf);
gettimeofday(&tp2, NULL); 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; 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) if (seconds > 0.5)
dsyslog("SpinUpDisk took %.2f seconds", seconds); dsyslog("SpinUpDisk took %.2f seconds", seconds);
free(buf);
return true; return true;
} }
else else
LOG_ERROR_STR(buf); LOG_ERROR_STR(*buf);
} }
} }
free(buf);
esyslog("ERROR: SpinUpDisk failed"); esyslog("ERROR: SpinUpDisk failed");
return false; return false;
} }
@ -888,7 +877,21 @@ cString cString::sprintf(const char *fmt, ...)
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
char *buffer; 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); return cString(buffer, true);
} }
@ -1622,7 +1625,7 @@ cLockFile::cLockFile(const char *Directory)
fileName = NULL; fileName = NULL;
f = -1; f = -1;
if (DirectoryOk(Directory)) if (DirectoryOk(Directory))
asprintf(&fileName, "%s/%s", Directory, LOCKFILENAME); fileName = strdup(AddDirectory(Directory, LOCKFILENAME));
} }
cLockFile::~cLockFile() cLockFile::~cLockFile()

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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 #ifndef __TOOLS_H
@ -161,6 +161,7 @@ public:
cString &operator=(const cString &String); 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). 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, ...) __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); ssize_t safe_read(int filedes, void *buffer, size_t size);