mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-26 04:17:33 +00:00
exec.cc: Move the escaping code into standalone function
This commit is contained in:
parent
08f7cd6732
commit
e242fea685
26
src/exec.cc
26
src/exec.cc
@ -48,14 +48,11 @@ struct execi_data {
|
|||||||
execi_data() = default;
|
execi_data() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
// our own implementation of popen, the difference : the value of 'childpid'
|
const int cmd_len = 256;
|
||||||
// will be filled with the pid of the running 'command'. This is useful if want
|
char cmd[cmd_len];
|
||||||
// to kill it when it hangs while reading or writing to it. We have to kill it
|
|
||||||
// because pclose will wait until the process dies by itself
|
static char *escape_quotes(const char *);
|
||||||
static FILE *pid_popen(const char *command, const char *mode, pid_t *child) {
|
static char *escape_quotes(const char *command) {
|
||||||
int ends[2];
|
|
||||||
int parentend, childend;
|
|
||||||
char cmd[256];
|
|
||||||
char *str1 = cmd;
|
char *str1 = cmd;
|
||||||
const char *str2 = command;
|
const char *str2 = command;
|
||||||
int skip = 0;
|
int skip = 0;
|
||||||
@ -73,12 +70,21 @@ static FILE *pid_popen(const char *command, const char *mode, pid_t *child) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (254 > x) {
|
if ((cmd_len - 1) > x) {
|
||||||
*str1++ = *str2;
|
*str1++ = *str2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*str1 = '\0';
|
*str1 = '\0';
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// our own implementation of popen, the difference : the value of 'childpid'
|
||||||
|
// will be filled with the pid of the running 'command'. This is useful if want
|
||||||
|
// to kill it when it hangs while reading or writing to it. We have to kill it
|
||||||
|
// because pclose will wait until the process dies by itself
|
||||||
|
static FILE *pid_popen(const char *command, const char *mode, pid_t *child) {
|
||||||
|
int ends[2];
|
||||||
|
int parentend, childend;
|
||||||
|
|
||||||
// by running pipe after the strcmp's we make sure that we don't have to
|
// by running pipe after the strcmp's we make sure that we don't have to
|
||||||
// create a pipe and close the ends if mode is something illegal
|
// create a pipe and close the ends if mode is something illegal
|
||||||
@ -116,7 +122,7 @@ static FILE *pid_popen(const char *command, const char *mode, pid_t *child) {
|
|||||||
if (fcntl(childend, F_DUPFD, 0) == -1) { perror("fcntl()"); }
|
if (fcntl(childend, F_DUPFD, 0) == -1) { perror("fcntl()"); }
|
||||||
close(childend);
|
close(childend);
|
||||||
|
|
||||||
execl("/bin/sh", "sh", "-c", cmd, (char *)nullptr);
|
execl("/bin/sh", "sh", "-c", escape_quotes(command), (char *)nullptr);
|
||||||
_exit(EXIT_FAILURE); // child should die here, (normally execl will take
|
_exit(EXIT_FAILURE); // child should die here, (normally execl will take
|
||||||
// care of this but it can fail)
|
// care of this but it can fail)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user