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_buffer *buffer,
struct t_gui_completion *completion) struct t_gui_completion *completion)
{ {
char *path_d, *path_b, *p, *d_name; char home[3] = { '~', DIR_SEPARATOR_CHAR, '\0' };
char *real_prefix, *prefix; char *ptr_home, *pos, buf[PATH_MAX], *real_prefix, *prefix, *path_dir;
char *buf; char *path_base, *dir_name;
int buf_len; int length_path_base;
DIR *dp; DIR *dp;
struct dirent *entry; struct dirent *entry;
struct stat statbuf; struct stat statbuf;
char home[3] = { '~', DIR_SEPARATOR_CHAR, '\0' };
/* make C compiler happy */ /* make C compiler happy */
(void) data; (void) data;
(void) completion_item; (void) completion_item;
(void) buffer; (void) buffer;
buf_len = PATH_MAX;
buf = malloc (buf_len);
if (!buf)
return WEECHAT_RC_OK;
completion->add_space = 0; 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); prefix = strdup (home);
} }
else else
{ {
if ((strncmp (completion->base_word, DIR_SEPARATOR, 1) != 0) if (!completion->base_word[0]
|| (strcmp (completion->base_word, "") == 0)) || completion->base_word[0] != DIR_SEPARATOR_CHAR)
{ {
real_prefix = strdup (weechat_home); real_prefix = strdup (weechat_home);
prefix = strdup (""); prefix = strdup ("");
@ -400,61 +402,82 @@ completion_list_add_filename_cb (void *data,
prefix = strdup (DIR_SEPARATOR); prefix = strdup (DIR_SEPARATOR);
} }
} }
if (!real_prefix || !prefix)
goto end;
snprintf (buf, buf_len, "%s", completion->base_word + strlen (prefix)); snprintf (buf, sizeof (buf), "%s", completion->base_word + strlen (prefix));
p = strrchr (buf, DIR_SEPARATOR_CHAR); pos = strrchr (buf, DIR_SEPARATOR_CHAR);
if (p) if (pos)
{ {
p[0] = '\0'; pos[0] = '\0';
path_d = strdup (buf); path_dir = strdup (buf);
p++; path_base = strdup (pos + 1);
path_b = strdup (p);
} }
else else
{ {
path_d = strdup (""); path_dir = strdup ("");
path_b = strdup (buf); path_base = strdup (buf);
} }
if (!path_dir || !path_base)
goto end;
sprintf (buf, "%s%s%s", real_prefix, DIR_SEPARATOR, path_d); snprintf (buf, sizeof (buf),
d_name = strdup (buf); "%s%s%s", real_prefix, DIR_SEPARATOR, path_dir);
dp = opendir (d_name); dir_name = strdup (buf);
if (dp != NULL) 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_b, strlen (path_b)) == 0) if (strncmp (entry->d_name, path_base, length_path_base) != 0)
{
if (strcmp (entry->d_name, ".") == 0 || strcmp (entry->d_name, "..") == 0)
continue; continue;
snprintf (buf, buf_len, "%s%s%s", /* skip "." and ".." */
d_name, DIR_SEPARATOR, entry->d_name); if ((strcmp (entry->d_name, ".") == 0)
|| (strcmp (entry->d_name, "..") == 0))
{
continue;
}
/* skip entry if not accessible */
snprintf (buf, sizeof (buf), "%s%s%s",
dir_name, DIR_SEPARATOR, entry->d_name);
if (stat (buf, &statbuf) == -1) if (stat (buf, &statbuf) == -1)
continue; continue;
snprintf (buf, buf_len, "%s%s%s%s%s%s", /* build full path name */
snprintf (buf, sizeof (buf),
"%s%s%s%s%s%s",
prefix, prefix,
((strcmp(prefix, "") == 0) (prefix[0] && !strchr (prefix, DIR_SEPARATOR_CHAR)) ?
|| strchr(prefix, DIR_SEPARATOR_CHAR)) ? "" : DIR_SEPARATOR, DIR_SEPARATOR : "",
path_d, path_dir,
strcmp(path_d, "") == 0 ? "" : DIR_SEPARATOR, (path_dir[0]) ? DIR_SEPARATOR : "",
entry->d_name, entry->d_name,
S_ISDIR(statbuf.st_mode) ? DIR_SEPARATOR : ""); S_ISDIR(statbuf.st_mode) ? DIR_SEPARATOR : "");
/* add path to list of completions */
gui_completion_list_add (completion, buf, gui_completion_list_add (completion, buf,
0, WEECHAT_LIST_POS_SORT); 0, WEECHAT_LIST_POS_SORT);
} }
}
closedir (dp); closedir (dp);
}
free (d_name); end:
free (prefix); if (real_prefix)
free (real_prefix); free (real_prefix);
free (path_d); if (prefix)
free (path_b); free (prefix);
free (buf); if (path_dir)
free (path_dir);
if (path_base)
free (path_base);
if (dir_name)
free (dir_name);
return WEECHAT_RC_OK; 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 */ /* bind meta-j + {01..99} to switch to buffers # > 10 */
for (i = 1; i < 100; i++) for (i = 1; i < 100; i++)
{ {
sprintf (key_str, "meta-j%02d", i); snprintf (key_str, sizeof (key_str), "meta-j%02d", i);
sprintf (command, "/buffer %d", i); snprintf (command, sizeof (command), "/buffer %d", i);
BIND(key_str, command); BIND(key_str, command);
} }
} }

View File

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