mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 01:53:02 +01:00
cleanup/error checks
This commit is contained in:
parent
b5f34f42a8
commit
e61d070def
@ -82,12 +82,15 @@ static struct cmd_func chat_commands[] = {
|
|||||||
static int parse_command(WINDOW *w, ToxWindow *self, const char *input, char (*args)[MAX_STR_SIZE])
|
static int parse_command(WINDOW *w, ToxWindow *self, const char *input, char (*args)[MAX_STR_SIZE])
|
||||||
{
|
{
|
||||||
char *cmd = strdup(input);
|
char *cmd = strdup(input);
|
||||||
|
|
||||||
|
if (cmd == NULL)
|
||||||
|
exit_toxic_err("failed in parse_command", FATALERR_MEMORY);
|
||||||
|
|
||||||
int num_args = 0;
|
int num_args = 0;
|
||||||
int i = 0; /* index of last char in an argument */
|
int i = 0; /* index of last char in an argument */
|
||||||
bool cmd_end = false;
|
|
||||||
|
|
||||||
/* characters wrapped in double quotes count as one arg */
|
/* characters wrapped in double quotes count as one arg */
|
||||||
while (!cmd_end && num_args < MAX_NUM_ARGS) {
|
while (num_args < MAX_NUM_ARGS) {
|
||||||
int qt_ofst = 0; /* set to 1 to offset index for quote char at end of arg */
|
int qt_ofst = 0; /* set to 1 to offset index for quote char at end of arg */
|
||||||
|
|
||||||
if (*cmd == '\"') {
|
if (*cmd == '\"') {
|
||||||
@ -107,10 +110,10 @@ static int parse_command(WINDOW *w, ToxWindow *self, const char *input, char (*a
|
|||||||
memcpy(args[num_args], cmd, i + qt_ofst);
|
memcpy(args[num_args], cmd, i + qt_ofst);
|
||||||
args[num_args++][i + qt_ofst] = '\0';
|
args[num_args++][i + qt_ofst] = '\0';
|
||||||
|
|
||||||
if (cmd[i])
|
if (cmd[i] == '\0') /* no more args */
|
||||||
strcpy(cmd, &cmd[i + 1]);
|
break;
|
||||||
else
|
|
||||||
cmd_end = true;
|
strcpy(cmd, &cmd[i + 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(cmd);
|
free(cmd);
|
||||||
|
@ -199,10 +199,17 @@ void get_file_name(char *namebuf, int bufsize, const char *pathname)
|
|||||||
int idx = strlen(pathname) - 1;
|
int idx = strlen(pathname) - 1;
|
||||||
char *path = strdup(pathname);
|
char *path = strdup(pathname);
|
||||||
|
|
||||||
|
if (path == NULL)
|
||||||
|
exit_toxic_err("failed in get_file_name", FATALERR_MEMORY);
|
||||||
|
|
||||||
while (idx >= 0 && pathname[idx] == '/')
|
while (idx >= 0 && pathname[idx] == '/')
|
||||||
path[idx--] = '\0';
|
path[idx--] = '\0';
|
||||||
|
|
||||||
char *finalname = strdup(path);
|
char *finalname = strdup(path);
|
||||||
|
|
||||||
|
if (finalname == NULL)
|
||||||
|
exit_toxic_err("failed in get_file_name", FATALERR_MEMORY);
|
||||||
|
|
||||||
const char *basenm = strrchr(path, '/');
|
const char *basenm = strrchr(path, '/');
|
||||||
|
|
||||||
if (basenm != NULL) {
|
if (basenm != NULL) {
|
||||||
@ -211,7 +218,6 @@ void get_file_name(char *namebuf, int bufsize, const char *pathname)
|
|||||||
}
|
}
|
||||||
|
|
||||||
snprintf(namebuf, bufsize, "%s", finalname);
|
snprintf(namebuf, bufsize, "%s", finalname);
|
||||||
|
|
||||||
free(finalname);
|
free(finalname);
|
||||||
free(path);
|
free(path);
|
||||||
}
|
}
|
||||||
|
25
src/toxic.c
25
src/toxic.c
@ -369,7 +369,7 @@ static void load_friendlist(Tox *m)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Store Messenger to given location
|
* Store Messenger to given location
|
||||||
* Return 0 stored successfully
|
* Return 0 stored successfully or ignoring data file
|
||||||
* Return 1 file path is NULL
|
* Return 1 file path is NULL
|
||||||
* Return 2 malloc failed
|
* Return 2 malloc failed
|
||||||
* Return 3 opening path failed
|
* Return 3 opening path failed
|
||||||
@ -383,19 +383,15 @@ int store_data(Tox *m, char *path)
|
|||||||
if (path == NULL)
|
if (path == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
FILE *fd;
|
int len = tox_size(m);
|
||||||
int len;
|
char *buf = malloc(len);
|
||||||
char *buf;
|
|
||||||
|
|
||||||
len = tox_size(m);
|
|
||||||
buf = malloc(len);
|
|
||||||
|
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
tox_save(m, (uint8_t *) buf);
|
tox_save(m, (uint8_t *) buf);
|
||||||
|
|
||||||
fd = fopen(path, "wb");
|
FILE *fd = fopen(path, "wb");
|
||||||
|
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
free(buf);
|
free(buf);
|
||||||
@ -419,15 +415,13 @@ static void load_data(Tox *m, char *path)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
int len;
|
|
||||||
char *buf;
|
|
||||||
|
|
||||||
if ((fd = fopen(path, "rb")) != NULL) {
|
if ((fd = fopen(path, "rb")) != NULL) {
|
||||||
fseek(fd, 0, SEEK_END);
|
fseek(fd, 0, SEEK_END);
|
||||||
len = ftell(fd);
|
int len = ftell(fd);
|
||||||
fseek(fd, 0, SEEK_SET);
|
fseek(fd, 0, SEEK_SET);
|
||||||
|
|
||||||
buf = malloc(len);
|
char *buf = malloc(len);
|
||||||
|
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
@ -526,6 +520,10 @@ static void parse_args(int argc, char *argv[])
|
|||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'f':
|
case 'f':
|
||||||
DATA_FILE = strdup(optarg);
|
DATA_FILE = strdup(optarg);
|
||||||
|
|
||||||
|
if (DATA_FILE == NULL)
|
||||||
|
exit_toxic_err("failed in parse_args", FATALERR_MEMORY);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'x':
|
case 'x':
|
||||||
@ -573,6 +571,9 @@ int main(int argc, char *argv[])
|
|||||||
if (DATA_FILE == NULL ) {
|
if (DATA_FILE == NULL ) {
|
||||||
if (config_err) {
|
if (config_err) {
|
||||||
DATA_FILE = strdup("data");
|
DATA_FILE = strdup("data");
|
||||||
|
|
||||||
|
if (DATA_FILE == NULL)
|
||||||
|
exit_toxic_err("failed in main", FATALERR_MEMORY);
|
||||||
} else {
|
} else {
|
||||||
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
|
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
|
||||||
|
|
||||||
|
@ -235,9 +235,13 @@ int complete_line(ChatContext *ctx, const void *list, int n_items, int size)
|
|||||||
snprintf(tmp, sizeof(tmp), "%s", ubuf);
|
snprintf(tmp, sizeof(tmp), "%s", ubuf);
|
||||||
tmp[ctx->pos] = '\0';
|
tmp[ctx->pos] = '\0';
|
||||||
const char *s = strrchr(tmp, ' ');
|
const char *s = strrchr(tmp, ' ');
|
||||||
char *sub = malloc(strlen(ubuf) + 1);
|
|
||||||
int n_endchrs = 1; /* 1 = append space to end of match, 2 = append ": " */
|
int n_endchrs = 1; /* 1 = append space to end of match, 2 = append ": " */
|
||||||
|
|
||||||
|
char *sub = malloc(strlen(ubuf) + 1);
|
||||||
|
|
||||||
|
if (sub == NULL)
|
||||||
|
exit_toxic_err("failed in complete_line", FATALERR_MEMORY);
|
||||||
|
|
||||||
if (!s) {
|
if (!s) {
|
||||||
strcpy(sub, tmp);
|
strcpy(sub, tmp);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user