strdup + style

This commit is contained in:
taiyu 2015-09-07 15:03:04 -07:00
parent 47ff000697
commit 3eb29ea736
3 changed files with 7 additions and 13 deletions

View file

@ -383,9 +383,8 @@ static bool cmd_mode(int argc, char **argv) {
// Create mode if it doesnt exist // Create mode if it doesnt exist
if (!mode && argc >= 2 && strncmp(argv[1],"{",1) == 0) { if (!mode && argc >= 2 && strncmp(argv[1],"{",1) == 0) {
mode = malloc(sizeof*mode); mode = malloc(sizeof*mode);
mode->name = malloc(strlen(mode_name) + 1); mode->name = strdup(mode_name);
mode->bindings = create_list(); mode->bindings = create_list();
strcpy(mode->name, mode_name);
list_add(config->modes, mode); list_add(config->modes, mode);
} }
if (!mode) { if (!mode) {
@ -834,10 +833,8 @@ static bool cmd_set(int argc, char **argv) {
return false; return false;
} }
struct sway_variable *var = malloc(sizeof(struct sway_variable)); struct sway_variable *var = malloc(sizeof(struct sway_variable));
var->name = malloc(strlen(argv[0]) + 1); var->name = strdup(argv[0]);
strcpy(var->name, argv[0]); var->value = strdup(argv[1]);
var->value = malloc(strlen(argv[1]) + 1);
strcpy(var->value, argv[1]);
list_add(config->symbols, var); list_add(config->symbols, var);
return true; return true;
} }

View file

@ -249,8 +249,7 @@ bool read_config(FILE *file, bool is_active) {
sway_log(L_ERROR, "Invalid command during config ``%s''", line); sway_log(L_ERROR, "Invalid command during config ``%s''", line);
} else if (handler->config_type == CMD_COMPOSITOR_READY && !is_active) { } else if (handler->config_type == CMD_COMPOSITOR_READY && !is_active) {
sway_log(L_DEBUG, "Deferring command ``%s''", line); sway_log(L_DEBUG, "Deferring command ``%s''", line);
char *cmd = malloc(strlen(line) + 1); char *cmd = strdup(line);
strcpy(cmd, line);
list_add(config->cmd_queue, cmd); list_add(config->cmd_queue, cmd);
} else if (!handle_command(line)) { } else if (!handle_command(line)) {
sway_log(L_DEBUG, "Config load failed for line ``%s''", line); sway_log(L_DEBUG, "Config load failed for line ``%s''", line);

View file

@ -17,14 +17,13 @@ char *strip_whitespace(char *_str) {
while (*_str == ' ' || *_str == '\t') { while (*_str == ' ' || *_str == '\t') {
_str++; _str++;
} }
char *str = malloc(strlen(_str) + 1); char *str = strdup(_str);
strcpy(str, _str);
free(strold); free(strold);
int i; int i;
for (i = 0; str[i] != '\0'; ++i); for (i = 0; str[i] != '\0'; ++i);
do { do {
i--; i--;
} while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); } while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
str[i + 1] = '\0'; str[i + 1] = '\0';
return str; return str;
} }
@ -76,9 +75,8 @@ void strip_quotes(char *str) {
list_t *split_string(const char *str, const char *delims) { list_t *split_string(const char *str, const char *delims) {
list_t *res = create_list(); list_t *res = create_list();
char *copy = malloc(strlen(str) + 1); char *copy = strdup(str);
char *token; char *token;
strcpy(copy, str);
token = strtok(copy, delims); token = strtok(copy, delims);
while(token) { while(token) {