core: replace calls to sprintf() with snprintf()

This commit is contained in:
Sebastien Helleu 2014-01-05 10:44:53 +01:00
parent a571d599d3
commit d48896cfa4
3 changed files with 94 additions and 67 deletions

View File

@ -360,36 +360,38 @@ completion_list_add_filename_cb (void *data,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
char *path_d, *path_b, *p, *d_name;
char *real_prefix, *prefix;
char *buf;
int buf_len;
char home[3] = { '~', DIR_SEPARATOR_CHAR, '\0' };
char *ptr_home, *pos, buf[PATH_MAX], *real_prefix, *prefix, *path_dir;
char *path_base, *dir_name;
int length_path_base;
DIR *dp;
struct dirent *entry;
struct stat statbuf;
char home[3] = { '~', DIR_SEPARATOR_CHAR, '\0' };
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
buf_len = PATH_MAX;
buf = malloc (buf_len);
if (!buf)
return WEECHAT_RC_OK;
completion->add_space = 0;
if ((strncmp (completion->base_word, home, 2) == 0) && getenv("HOME"))
ptr_home = getenv ("HOME");
real_prefix = NULL;
prefix = NULL;
path_dir = NULL;
path_base = NULL;
dir_name = NULL;
if (ptr_home && (strncmp (completion->base_word, home, 2) == 0))
{
real_prefix = strdup (getenv("HOME"));
real_prefix = strdup (ptr_home);
prefix = strdup (home);
}
else
{
if ((strncmp (completion->base_word, DIR_SEPARATOR, 1) != 0)
|| (strcmp (completion->base_word, "") == 0))
if (!completion->base_word[0]
|| completion->base_word[0] != DIR_SEPARATOR_CHAR)
{
real_prefix = strdup (weechat_home);
prefix = strdup ("");
@ -400,61 +402,82 @@ completion_list_add_filename_cb (void *data,
prefix = strdup (DIR_SEPARATOR);
}
}
if (!real_prefix || !prefix)
goto end;
snprintf (buf, buf_len, "%s", completion->base_word + strlen (prefix));
p = strrchr (buf, DIR_SEPARATOR_CHAR);
if (p)
snprintf (buf, sizeof (buf), "%s", completion->base_word + strlen (prefix));
pos = strrchr (buf, DIR_SEPARATOR_CHAR);
if (pos)
{
p[0] = '\0';
path_d = strdup (buf);
p++;
path_b = strdup (p);
pos[0] = '\0';
path_dir = strdup (buf);
path_base = strdup (pos + 1);
}
else
{
path_d = strdup ("");
path_b = strdup (buf);
path_dir = strdup ("");
path_base = strdup (buf);
}
if (!path_dir || !path_base)
goto end;
sprintf (buf, "%s%s%s", real_prefix, DIR_SEPARATOR, path_d);
d_name = strdup (buf);
dp = opendir (d_name);
if (dp != NULL)
snprintf (buf, sizeof (buf),
"%s%s%s", real_prefix, DIR_SEPARATOR, path_dir);
dir_name = strdup (buf);
if (!dir_name)
goto end;
dp = opendir (dir_name);
if (!dp)
goto end;
length_path_base = strlen (path_base);
while ((entry = readdir (dp)) != NULL)
{
while ((entry = readdir (dp)) != NULL)
if (strncmp (entry->d_name, path_base, length_path_base) != 0)
continue;
/* skip "." and ".." */
if ((strcmp (entry->d_name, ".") == 0)
|| (strcmp (entry->d_name, "..") == 0))
{
if (strncmp (entry->d_name, path_b, strlen (path_b)) == 0)
{
if (strcmp (entry->d_name, ".") == 0 || strcmp (entry->d_name, "..") == 0)
continue;
snprintf (buf, buf_len, "%s%s%s",
d_name, DIR_SEPARATOR, entry->d_name);
if (stat (buf, &statbuf) == -1)
continue;
snprintf (buf, buf_len, "%s%s%s%s%s%s",
prefix,
((strcmp(prefix, "") == 0)
|| strchr(prefix, DIR_SEPARATOR_CHAR)) ? "" : DIR_SEPARATOR,
path_d,
strcmp(path_d, "") == 0 ? "" : DIR_SEPARATOR,
entry->d_name,
S_ISDIR(statbuf.st_mode) ? DIR_SEPARATOR : "");
gui_completion_list_add (completion, buf,
0, WEECHAT_LIST_POS_SORT);
}
continue;
}
closedir (dp);
}
free (d_name);
free (prefix);
free (real_prefix);
free (path_d);
free (path_b);
free (buf);
/* skip entry if not accessible */
snprintf (buf, sizeof (buf), "%s%s%s",
dir_name, DIR_SEPARATOR, entry->d_name);
if (stat (buf, &statbuf) == -1)
continue;
/* build full path name */
snprintf (buf, sizeof (buf),
"%s%s%s%s%s%s",
prefix,
(prefix[0] && !strchr (prefix, DIR_SEPARATOR_CHAR)) ?
DIR_SEPARATOR : "",
path_dir,
(path_dir[0]) ? DIR_SEPARATOR : "",
entry->d_name,
S_ISDIR(statbuf.st_mode) ? DIR_SEPARATOR : "");
/* add path to list of completions */
gui_completion_list_add (completion, buf,
0, WEECHAT_LIST_POS_SORT);
}
closedir (dp);
end:
if (real_prefix)
free (real_prefix);
if (prefix)
free (prefix);
if (path_dir)
free (path_dir);
if (path_base)
free (path_base);
if (dir_name)
free (dir_name);
return WEECHAT_RC_OK;
}

View File

@ -218,8 +218,8 @@ gui_key_default_bindings (int context)
/* bind meta-j + {01..99} to switch to buffers # > 10 */
for (i = 1; i < 100; i++)
{
sprintf (key_str, "meta-j%02d", i);
sprintf (command, "/buffer %d", i);
snprintf (key_str, sizeof (key_str), "meta-j%02d", i);
snprintf (command, sizeof (command), "/buffer %d", i);
BIND(key_str, command);
}
}

View File

@ -1313,8 +1313,8 @@ gui_bar_item_default_hotlist (void *data, struct t_gui_bar_item *item,
*/
break;
}
sprintf (str_hotlist + strlen (str_hotlist),
"%d", ptr_hotlist->buffer->number);
snprintf (str_hotlist + strlen (str_hotlist), 16,
"%d", ptr_hotlist->buffer->number);
numbers_count++;
if (display_name)
@ -1325,14 +1325,18 @@ gui_bar_item_default_hotlist (void *data, struct t_gui_bar_item *item,
strcat (str_hotlist, ":");
strcat (str_hotlist, GUI_COLOR_CUSTOM_BAR_FG);
if (CONFIG_INTEGER(config_look_hotlist_names_length) == 0)
{
snprintf (format, sizeof (format) - 1, "%%s");
}
else
{
snprintf (format, sizeof (format) - 1,
"%%.%ds",
CONFIG_INTEGER(config_look_hotlist_names_length));
sprintf (str_hotlist + strlen (str_hotlist), format,
(CONFIG_BOOLEAN(config_look_hotlist_short_names)) ?
gui_buffer_get_short_name (ptr_hotlist->buffer) : ptr_hotlist->buffer->name);
}
snprintf (str_hotlist + strlen (str_hotlist), 128, format,
(CONFIG_BOOLEAN(config_look_hotlist_short_names)) ?
gui_buffer_get_short_name (ptr_hotlist->buffer) : ptr_hotlist->buffer->name);
}
else
{
@ -1407,8 +1411,8 @@ gui_bar_item_default_hotlist (void *data, struct t_gui_bar_item *item,
*/
break;
}
sprintf (str_hotlist + strlen (str_hotlist),
"%d", ptr_hotlist->count[priority]);
snprintf (str_hotlist + strlen (str_hotlist), 16,
"%d", ptr_hotlist->count[priority]);
}
strcat (str_hotlist, GUI_COLOR_CUSTOM_BAR_DELIM);
strcat (str_hotlist, ")");