From 503c803b8d97e7997bc2a5c42eb6a2014d013528 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sat, 26 Jan 2002 12:04:32 +0100 Subject: [PATCH] Fixed handling file names that contain single quotes or dollar signs in calls to external commands --- HISTORY | 3 +++ recording.c | 4 ++-- tools.c | 25 ++++++++++++++++++++++++- tools.h | 7 ++++--- vdr.c | 4 ++-- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/HISTORY b/HISTORY index 18b6e5ef..c94ef15e 100644 --- a/HISTORY +++ b/HISTORY @@ -904,3 +904,6 @@ Video Disk Recorder Revision History command to collect the recordings (thanks to Emil Naepflein for his hint about this function). - Improved speed of setting the Help button texts. +- Fixed handling file names that contain single quotes (') or dollar signs ($) + in the call to the shutdown command (option '-s') and the recording command + (option '-r'). diff --git a/recording.c b/recording.c index faabd4ca..6426f330 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.44 2002/01/20 16:47:09 kls Exp $ + * $Id: recording.c 1.45 2002/01/26 11:57:56 kls Exp $ */ #include "recording.h" @@ -640,7 +640,7 @@ void cRecordingUserCommand::InvokeCommand(const char *State, const char *Recordi { if (command) { char *cmd; - asprintf(&cmd, "%s %s '%s'", command, State, RecordingFileName); + asprintf(&cmd, "%s %s \"%s\"", command, State, strescape(RecordingFileName, "\"$")); isyslog(LOG_INFO, "executing '%s'", cmd); SystemExec(cmd); delete cmd; diff --git a/tools.c b/tools.c index de21af50..b18c0ef5 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.51 2002/01/20 15:43:35 kls Exp $ + * $Id: tools.c 1.52 2002/01/26 12:04:32 kls Exp $ */ #include "tools.h" @@ -136,6 +136,29 @@ char *compactspace(char *s) return s; } +const char *strescape(const char *s, const char *chars) +{ + static char *buffer = NULL; + const char *p = s; + char *t = NULL; + while (*p) { + if (strchr(chars, *p)) { + if (!t) { + buffer = (char *)realloc(buffer, 2 * strlen(s) + 1); + t = buffer + (p - s); + s = strcpy(buffer, s); + } + *t++ = '\\'; + } + if (t) + *t++ = *p; + p++; + } + if (t) + *t = 0; + return s; +} + bool startswith(const char *s, const char *p) { while (*p) { diff --git a/tools.h b/tools.h index 933b797e..8bb09699 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.37 2002/01/20 15:39:53 kls Exp $ + * $Id: tools.h 1.38 2002/01/26 11:55:06 kls Exp $ */ #ifndef __TOOLS_H @@ -50,13 +50,14 @@ char *strreplace(char *s, char c1, char c2); char *skipspace(const char *s); char *stripspace(char *s); char *compactspace(char *s); +const char *strescape(const char *s, const char *chars); // returns a statically allocated string! bool startswith(const char *s, const char *p); bool endswith(const char *s, const char *p); bool isempty(const char *s); int time_ms(void); void delay_ms(int ms); bool isnumber(const char *s); -const char *AddDirectory(const char *DirName, const char *FileName); +const char *AddDirectory(const char *DirName, const char *FileName); // returns a statically allocated string! uint FreeDiskSpaceMB(const char *Directory); bool DirectoryOk(const char *DirName, bool LogErrors = false); bool MakeDirs(const char *FileName, bool IsDirectory = false); @@ -64,7 +65,7 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks = false); bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis = false); char *ReadLink(const char *FileName); bool SpinUpDisk(const char *FileName); -const char *DayDateTime(time_t t = 0); +const char *DayDateTime(time_t t = 0); // returns a statically allocated string! class cFile { private: diff --git a/vdr.c b/vdr.c index 264803a5..572e16f1 100644 --- a/vdr.c +++ b/vdr.c @@ -22,7 +22,7 @@ * * The project's page is at http://www.cadsoft.de/people/kls/vdr * - * $Id: vdr.c 1.90 2002/01/13 16:00:47 kls Exp $ + * $Id: vdr.c 1.91 2002/01/26 11:53:11 kls Exp $ */ #include @@ -499,7 +499,7 @@ int main(int argc, char *argv[]) int Channel = timer ? timer->channel : 0; const char *File = timer ? timer->file : ""; char *cmd; - asprintf(&cmd, "%s %ld %ld %d '%s' %d", Shutdown, Next, Delta, Channel, File, UserShutdown); + asprintf(&cmd, "%s %ld %ld %d \"%s\" %d", Shutdown, Next, Delta, Channel, strescape(File, "\"$"), UserShutdown); isyslog(LOG_INFO, "executing '%s'", cmd); SystemExec(cmd); delete cmd;