mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 02:03: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])
|
||||
{
|
||||
char *cmd = strdup(input);
|
||||
|
||||
if (cmd == NULL)
|
||||
exit_toxic_err("failed in parse_command", FATALERR_MEMORY);
|
||||
|
||||
int num_args = 0;
|
||||
int i = 0; /* index of last char in an argument */
|
||||
bool cmd_end = false;
|
||||
|
||||
/* 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 */
|
||||
|
||||
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);
|
||||
args[num_args++][i + qt_ofst] = '\0';
|
||||
|
||||
if (cmd[i])
|
||||
strcpy(cmd, &cmd[i + 1]);
|
||||
else
|
||||
cmd_end = true;
|
||||
if (cmd[i] == '\0') /* no more args */
|
||||
break;
|
||||
|
||||
strcpy(cmd, &cmd[i + 1]);
|
||||
}
|
||||
|
||||
free(cmd);
|
||||
|
@ -199,10 +199,17 @@ void get_file_name(char *namebuf, int bufsize, const char *pathname)
|
||||
int idx = strlen(pathname) - 1;
|
||||
char *path = strdup(pathname);
|
||||
|
||||
if (path == NULL)
|
||||
exit_toxic_err("failed in get_file_name", FATALERR_MEMORY);
|
||||
|
||||
while (idx >= 0 && pathname[idx] == '/')
|
||||
path[idx--] = '\0';
|
||||
|
||||
char *finalname = strdup(path);
|
||||
|
||||
if (finalname == NULL)
|
||||
exit_toxic_err("failed in get_file_name", FATALERR_MEMORY);
|
||||
|
||||
const char *basenm = strrchr(path, '/');
|
||||
|
||||
if (basenm != NULL) {
|
||||
@ -211,7 +218,6 @@ void get_file_name(char *namebuf, int bufsize, const char *pathname)
|
||||
}
|
||||
|
||||
snprintf(namebuf, bufsize, "%s", finalname);
|
||||
|
||||
free(finalname);
|
||||
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
|
||||
* Return 0 stored successfully
|
||||
* Return 0 stored successfully or ignoring data file
|
||||
* Return 1 file path is NULL
|
||||
* Return 2 malloc failed
|
||||
* Return 3 opening path failed
|
||||
@ -383,19 +383,15 @@ int store_data(Tox *m, char *path)
|
||||
if (path == NULL)
|
||||
return 1;
|
||||
|
||||
FILE *fd;
|
||||
int len;
|
||||
char *buf;
|
||||
|
||||
len = tox_size(m);
|
||||
buf = malloc(len);
|
||||
int len = tox_size(m);
|
||||
char *buf = malloc(len);
|
||||
|
||||
if (buf == NULL)
|
||||
return 2;
|
||||
|
||||
tox_save(m, (uint8_t *) buf);
|
||||
|
||||
fd = fopen(path, "wb");
|
||||
FILE *fd = fopen(path, "wb");
|
||||
|
||||
if (fd == NULL) {
|
||||
free(buf);
|
||||
@ -419,15 +415,13 @@ static void load_data(Tox *m, char *path)
|
||||
return;
|
||||
|
||||
FILE *fd;
|
||||
int len;
|
||||
char *buf;
|
||||
|
||||
if ((fd = fopen(path, "rb")) != NULL) {
|
||||
fseek(fd, 0, SEEK_END);
|
||||
len = ftell(fd);
|
||||
int len = ftell(fd);
|
||||
fseek(fd, 0, SEEK_SET);
|
||||
|
||||
buf = malloc(len);
|
||||
char *buf = malloc(len);
|
||||
|
||||
if (buf == NULL) {
|
||||
fclose(fd);
|
||||
@ -526,6 +520,10 @@ static void parse_args(int argc, char *argv[])
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
DATA_FILE = strdup(optarg);
|
||||
|
||||
if (DATA_FILE == NULL)
|
||||
exit_toxic_err("failed in parse_args", FATALERR_MEMORY);
|
||||
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
@ -573,6 +571,9 @@ int main(int argc, char *argv[])
|
||||
if (DATA_FILE == NULL ) {
|
||||
if (config_err) {
|
||||
DATA_FILE = strdup("data");
|
||||
|
||||
if (DATA_FILE == NULL)
|
||||
exit_toxic_err("failed in main", FATALERR_MEMORY);
|
||||
} else {
|
||||
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);
|
||||
tmp[ctx->pos] = '\0';
|
||||
const char *s = strrchr(tmp, ' ');
|
||||
char *sub = malloc(strlen(ubuf) + 1);
|
||||
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) {
|
||||
strcpy(sub, tmp);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user