aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Kiagiadakis <george.kiagiadakis@collabora.com>2021-08-03 10:12:14 +0300
committerGeorge Kiagiadakis <george.kiagiadakis@collabora.com>2021-08-03 10:35:11 +0300
commit6f1c0e8a4d312b598620184619bfa06aaad8a5e7 (patch)
treeec1b3456840cd5b11592182b6ae000632b9efd1e
parent6f93de72365499f3431e6de4e81755feb277a103 (diff)
client: rework to avoid sscanf() and print help on invalid commands
Signed-off-by: George Kiagiadakis <george.kiagiadakis@collabora.com>
-rw-r--r--src/icipc-client.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/icipc-client.c b/src/icipc-client.c
index 9f47ba4..922f5b4 100644
--- a/src/icipc-client.c
+++ b/src/icipc-client.c
@@ -8,6 +8,7 @@
#include <stdio.h>
#include <string.h>
+#include <limits.h>
#include <pthread.h>
#include <assert.h>
#include <icipc.h>
@@ -47,6 +48,7 @@ static void reply_handler(
int main(int argc, char *argv[]) {
ClientData data;
+ char str[LINE_MAX];
if (argc < 2) {
printf("usage: <server-path>\n");
@@ -60,23 +62,16 @@ int main(int argc, char *argv[]) {
data.reply_received = false;
while (true) {
- char str[1024];
-
+ memset(str, 0, sizeof(str));
printf("> ");
- fgets(str, 1023, stdin);
+ fgets(str, sizeof(str), stdin);
- if (strncmp(str, "help", 4) == 0) {
- printf("help\tprints this message\n");
- printf("quit\texits the client\n");
- printf("send\tsends a request, usage: send <request-name>\n");
- } else if (strncmp(str, "quit", 4) == 0) {
+ if (strncmp(str, "quit", 4) == 0) {
printf("exiting...\n");
break;
- } else if (strncmp(str, "send", 4) == 0) {
- char request_name[1024];
- int n =
- sscanf(str, "send %s", request_name);
- if (n <= 0)
+ } else if (strncmp(str, "send ", 5) == 0) {
+ const char *request_name = str + 5;
+ if (*request_name == '\0')
continue;
/* send request */
@@ -89,6 +84,10 @@ int main(int argc, char *argv[]) {
while (!data.reply_received)
pthread_cond_wait(&data.cond, &data.mutex);
pthread_mutex_unlock(&data.mutex);
+ } else {
+ printf("help\tprints this message\n");
+ printf("quit\texits the client\n");
+ printf("send\tsends a request, usage: send [SUSPEND|RESUME]\n");
}
}