aboutsummaryrefslogtreecommitdiff
path: root/sway/commands.c
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2015-08-10 16:21:04 -0400
committerDrew DeVault <sir@cmpwn.com>2015-08-10 16:21:04 -0400
commitcaee41b7775d6e7eacc37837b31d92808e1a34de (patch)
treeaeb799f4d613ca9ddb406cf9fdf5e1eaa23a04ef /sway/commands.c
parent54374d81b560d572fef5b1e5a38704349f31998e (diff)
parent820dda1be6b5792507325da84a88e579daff9fc0 (diff)
Merge pull request #10 from taiyu-len/master
fixed small memory leak. v2
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c31
1 files changed, 18 insertions, 13 deletions
diff --git a/sway/commands.c b/sway/commands.c
index 3f9cc73f..2ce24fa4 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -346,9 +346,10 @@ struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *lin
int handle_command(struct sway_config *config, char *exec) {
sway_log(L_INFO, "Handling command '%s'", exec);
char *ptr, *cmd;
+ int ret;
+
if ((ptr = strchr(exec, ' ')) == NULL) {
- cmd = malloc(strlen(exec) + 1);
- strcpy(cmd, exec);
+ cmd = exec;
} else {
int index = ptr - exec;
cmd = malloc(index + 1);
@@ -358,18 +359,22 @@ int handle_command(struct sway_config *config, char *exec) {
struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd);
if (handler == NULL) {
sway_log(L_ERROR, "Unknown command '%s'", cmd);
- return 0; // TODO: return error, probably
- }
- int argc;
- char **argv = split_directive(exec + strlen(handler->command), &argc);
- int ret = handler->handle(config, argc, argv);
- int i;
- for (i = 0; i < argc; ++i) {
- free(argv[i]);
+ ret = 0; // TODO: return error, probably
+ } else {
+ int argc;
+ char **argv = split_directive(exec + strlen(handler->command), &argc);
+ int i;
+ ret = handler->handle(config, argc, argv);
+ for (i = 0; i < argc; ++i) {
+ free(argv[i]);
+ }
+ free(argv);
+ if (ret != 0) {
+ sway_log(L_ERROR, "Command failed: %s", cmd);
+ }
}
- free(argv);
- if (ret != 0) {
- sway_log(L_ERROR, "Command failed: %s", cmd);
+ if(ptr) {
+ free(cmd);
}
return ret;
}