diff options
-rw-r--r-- | include/sysutils/SocketClient.h | 3 | ||||
-rw-r--r-- | libsysutils/src/SocketClient.cpp | 23 |
2 files changed, 26 insertions, 0 deletions
diff --git a/include/sysutils/SocketClient.h b/include/sysutils/SocketClient.h index 4d7c4fa..85b58ef 100644 --- a/include/sysutils/SocketClient.h +++ b/include/sysutils/SocketClient.h @@ -64,6 +64,9 @@ public: void incRef(); bool decRef(); // returns true at 0 (but note: SocketClient already deleted) + // return a new string in quotes with '\\' and '\"' escaped for "my arg" transmissions + static char *quoteArg(const char *arg); + private: // Send null-terminated C strings int sendMsg(const char *msg); diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index 1533120..4a1227f 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp @@ -107,6 +107,29 @@ int SocketClient::sendCode(int code) { return sendData(buf, sizeof(buf)); } +char *SocketClient::quoteArg(const char *arg) { + int len = strlen(arg); + char *result = (char *)malloc(len * 2 + 3); + char *current = result; + const char *end = arg + len; + + *(current++) = '"'; + while (arg < end) { + switch (*arg) { + case '\\': + case '"': + *(current++) = '\\'; // fallthrough + default: + *(current++) = *(arg++); + } + } + *(current++) = '"'; + *(current++) = '\0'; + result = (char *)realloc(result, current-result); + return result; +} + + int SocketClient::sendMsg(const char *msg) { if (mSocket < 0) { errno = EHOSTUNREACH; |