From 9a500d13055f16b70796a5fce5ec6de4a2844187 Mon Sep 17 00:00:00 2001 From: Kalle Wallin Date: Tue, 13 Jul 2004 21:10:06 +0000 Subject: [PATCH] Use glib's str functions (g_strlcat, g_strlcpy, g_snprintf, g_strdup_vprintf) git-svn-id: https://svn.musicpd.org/ncmpc/trunk@1868 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/command.c | 30 +++++++++++++-------------- src/main.c | 6 +++--- src/screen_file.c | 15 +++++++------- src/screen_help.c | 24 +++++++++++----------- src/screen_keydef.c | 19 +++++++++-------- src/screen_play.c | 7 ++++--- src/screen_utils.c | 7 ++++--- src/strfsong.c | 25 +++++++++-------------- src/support.c | 22 +++++++++++--------- src/utils.c | 7 ++++--- src/wreadln.c | 50 +++++++++++++++++++++++---------------------- src/wreadln.h | 20 +++++++++--------- 12 files changed, 118 insertions(+), 114 deletions(-) diff --git a/src/command.c b/src/command.c index c256d0c..d38402b 100644 --- a/src/command.c +++ b/src/command.c @@ -233,17 +233,17 @@ key2str(int key) for(i=0; i<=63; i++) if( key==KEY_F(i) ) { - snprintf(buf, 32, "F%d", i ); + g_snprintf(buf, 32, "F%d", i ); return buf; } if( !(key & ~037) ) - snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 ); + g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 ); else if( (key & ~037) == 224 ) - snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 ); + g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 ); else if( key>32 && key<256 ) - snprintf(buf, 32, "%c", key); + g_snprintf(buf, 32, "%c", key); else - snprintf(buf, 32, "0x%03X", key); + g_snprintf(buf, 32, "0x%03X", key); } return buf; } @@ -293,14 +293,14 @@ get_key_names(command_t command, int all) int j; static char keystr[80]; - strncpy(keystr, key2str(cmds[i].keys[0]), 80); + g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr)); if( !all ) return keystr; j=1; while( j0 ) { - strcat(keystr, " "); - strcat(keystr, key2str(cmds[i].keys[j])); + g_strlcat(keystr, " ", sizeof(keystr)); + g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr)); j++; } return keystr; @@ -477,14 +477,14 @@ check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize) { if( buf ) #ifdef ENABLE_KEYDEF_SCREEN - snprintf(buf, bufsize, - _("Key %s assigned to %s and %s (press %s for the key editor)"), - key2str(cp[i].keys[j]), - get_key_command_name(cp[i].command), - get_key_command_name(cmd), - get_key_names(CMD_SCREEN_KEYDEF,0)); + g_snprintf(buf, bufsize, + _("Key %s assigned to %s and %s (press %s for the key editor)"), + key2str(cp[i].keys[j]), + get_key_command_name(cp[i].command), + get_key_command_name(cmd), + get_key_names(CMD_SCREEN_KEYDEF,0)); #else - snprintf(buf, bufsize, + g_snprintf(buf, bufsize, _("Error: Key %s assigned to %s and %s !!!\n"), key2str(cp[i].keys[j]), get_key_command_name(cp[i].command), diff --git a/src/main.c b/src/main.c index 1cba667..ece1ad5 100644 --- a/src/main.c +++ b/src/main.c @@ -99,11 +99,11 @@ update_xterm_title(void) strfsong(tmp, BUFSIZE, options.xterm_title_format, song); } else - strncpy(tmp, PACKAGE " version " VERSION, BUFSIZE); + g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE); - if( strcmp(title,tmp) ) + if( strncmp(title,tmp,BUFSIZE) ) { - strncpy(title, tmp, BUFSIZE); + g_strlcpy(title, tmp, BUFSIZE); set_xterm_title(title); } } diff --git a/src/screen_file.c b/src/screen_file.c index 02dc4c4..d25229d 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -182,7 +182,7 @@ static char * list_callback(int index, int *highlight, void *data) { static char buf[BUFSIZE]; - //mpdclient_t *c = (mpdclient_t *) data; + /*mpdclient_t *c = (mpdclient_t *) data;*/ filelist_entry_t *entry; mpd_InfoEntity *entity; @@ -202,7 +202,7 @@ list_callback(int index, int *highlight, void *data) mpd_Directory *dir = entity->info.directory; char *dirname = utf8_to_locale(basename(dir->path)); - snprintf(buf, BUFSIZE, "[%s]", dirname); + g_snprintf(buf, BUFSIZE, "[%s]", dirname); g_free(dirname); return buf; } @@ -219,9 +219,9 @@ list_callback(int index, int *highlight, void *data) char *filename = utf8_to_locale(basename(plf->path)); #ifdef USE_OLD_LAYOUT - snprintf(buf, BUFSIZE, "*%s*", filename); + g_snprintf(buf, BUFSIZE, "*%s*", filename); #else - snprintf(buf, BUFSIZE, " %s", filename); + g_snprintf(buf, BUFSIZE, " %s", filename); #endif g_free(filename); return buf; @@ -289,7 +289,7 @@ handle_delete(screen_t *screen, mpdclient_t *c) filelist_entry_t *entry; mpd_InfoEntity *entity; mpd_PlaylistFile *plf; - char *str, buf[BUFSIZE]; + char *str, *buf; int key; entry=( filelist_entry_t *) g_list_nth_data(filelist->list,lw->selected); @@ -307,9 +307,10 @@ handle_delete(screen_t *screen, mpdclient_t *c) plf = entity->info.playlistFile; str = utf8_to_locale(basename(plf->path)); - snprintf(buf, BUFSIZE, _("Delete playlist %s [%s/%s] ? "), str, YES, NO); + buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO); g_free(str); key = tolower(screen_getch(screen->status_window.w, buf)); + g_free(buf); if( key==KEY_RESIZE ) screen_resize(); if( key != YES[0] ) @@ -526,7 +527,7 @@ browse_close(void) static char * browse_title(char *str, size_t size) { - snprintf(str, size, _("Browse: %s"), basename(filelist->path)); + g_snprintf(str, size, _("Browse: %s"), basename(filelist->path)); return str; } diff --git a/src/screen_help.c b/src/screen_help.c index 68021c6..94fe9ab 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -121,7 +121,7 @@ static list_window_t *lw = NULL; static char * list_callback(int index, int *highlight, void *data) { - static char buf[256]; + static char buf[512]; if( help_text_rows<0 ) { @@ -137,30 +137,30 @@ list_callback(int index, int *highlight, void *data) if( help_text[index].command == CMD_NONE ) { if( help_text[index].text ) - snprintf(buf, 256, "%28s", _(help_text[index].text)); + g_snprintf(buf, sizeof(buf), "%28s", _(help_text[index].text)); else if( help_text[index].highlight == 2 ) { int i; - for(i=3; i0 ) { - snprintf(buf, - BUFSIZE, "%d. %-20s (%d) ", - index+1, - key2str(cmds[subcmd].keys[index]), - cmds[subcmd].keys[index]); + g_snprintf(buf, + BUFSIZE, "%d. %-20s (%d) ", + index+1, + key2str(cmds[subcmd].keys[index]), + cmds[subcmd].keys[index]); return buf; } else if ( index==subcmd_addpos ) { - snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 ); + g_snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 ); return buf; } } @@ -288,7 +289,7 @@ keydef_title(char *str, size_t size) if( subcmd<0 ) return _("Edit key bindings"); - snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name); + g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name); return str; } diff --git a/src/screen_play.c b/src/screen_play.c index e9cb465..307471f 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -178,11 +178,12 @@ handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name) if( code == MPD_ACK_ERROR_EXIST ) { - char buf[256]; + char *buf; int key; - snprintf(buf, 256, _("Replace %s [%s/%s] ? "), filename, YES, NO); + buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO); key = tolower(screen_getch(screen->status_window.w, buf)); + g_free(buf); if( key == YES[0] ) { if( mpdclient_cmd_delete_playlist(c, filename) ) @@ -321,7 +322,7 @@ play_title(char *str, size_t size) if( strcmp(options.host, "localhost") == 0 ) return _("Playlist"); - snprintf(str, size, _("Playlist on %s"), options.host); + g_snprintf(str, size, _("Playlist on %s"), options.host); return str; } diff --git a/src/screen_utils.c b/src/screen_utils.c index 9870fc3..baa371b 100644 --- a/src/screen_utils.c +++ b/src/screen_utils.c @@ -223,13 +223,14 @@ set_xterm_title(char *format, ...) { if( g_getenv("WINDOWID") ) { - char buffer[512]; + char *msg; va_list ap; va_start(ap,format); - vsnprintf(buffer,sizeof(buffer),format,ap); + msg = g_strdup_vprintf(format,ap); va_end(ap); - printf("%c]0;%s%c", '\033', buffer, '\007'); + printf("%c]0;%s%c", '\033', msg, '\007'); + g_free(msg); } else options.enable_xterm_title = FALSE; diff --git a/src/strfsong.c b/src/strfsong.c index a06ae95..bac6d47 100644 --- a/src/strfsong.c +++ b/src/strfsong.c @@ -114,7 +114,7 @@ _strfsong(gchar *s, temp = g_malloc0(max); if( _strfsong(temp, max, p+1, song, &p) >0 ) { - strncat(s, temp, max-length); + g_strlcat(s, temp, max); length = strlen(s); found = TRUE; } @@ -137,17 +137,15 @@ _strfsong(gchar *s, /* pass-through non-escaped portions of the format string */ if (p[0] != '#' && p[0] != '%' && lengthtime != MPD_SONG_NO_TIME) { - gchar s[10]; - snprintf(s, 9, "%d:%d", song->time / 60, - song->time % 60 + 1); - /* nasty hack to use static buffer */ - temp = g_strdup(s); - } + if (song->time != MPD_SONG_NO_TIME) + temp = g_strdup_printf("%d:%d", + song->time / 60, + song->time % 60 + 1); } if( temp == NULL) @@ -202,7 +197,7 @@ _strfsong(gchar *s, but put the real character back in (pseudo-const) */ if( length+templen > max ) templen = max-length; - strncat(s, p, templen); + g_strlcat(s, p,max); length+=templen; } else { @@ -211,7 +206,7 @@ _strfsong(gchar *s, found = TRUE; if( length+templen > max ) templen = max-length; - strncat(s, temp, templen); + g_strlcat(s, temp, max); length+=templen; g_free(temp); } diff --git a/src/support.c b/src/support.c index 3717e6c..254c15e 100644 --- a/src/support.c +++ b/src/support.c @@ -53,8 +53,8 @@ remove_trailing_slash(char *path) char * lowerstr(char *str) { - size_t i; - size_t len = strlen(str); + gsize i; + gsize len = strlen(str); if( str==NULL ) return NULL; @@ -100,8 +100,8 @@ strcasestr(const char *haystack, const char *needle) char * strscroll(char *str, char *separator, int width, scroll_state_t *st) { - char *tmp, *buf; - size_t len; + gchar *tmp, *buf; + gsize len, size; if( st->offset==0 ) { @@ -110,19 +110,21 @@ strscroll(char *str, char *separator, int width, scroll_state_t *st) } /* create a buffer containing the string and the separator */ - tmp = g_malloc(strlen(str)+strlen(separator)+1); - strcpy(tmp, str); - strcat(tmp, separator); + size = strlen(str)+strlen(separator)+1; + tmp = g_malloc(size); + g_strlcpy(tmp, str, size); + g_strlcat(tmp, separator, size); len = strlen(tmp); if( st->offset >= len ) st->offset = 0; /* create the new scrolled string */ - buf = g_malloc(width+1); - strncpy(buf, tmp+st->offset, width); + size = width+1; + buf = g_malloc(size); + g_strlcpy(buf, tmp+st->offset, size); if( strlen(buf) < width ) - strncat(buf, tmp, width-strlen(buf)); + g_strlcat(buf, tmp, size); if( time(NULL)-st->t >= 1 ) { diff --git a/src/utils.c b/src/utils.c index 58340c4..0f63ff1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -101,10 +101,11 @@ gcmp_list_from_path(mpdclient_t *c, gchar *path, GList *list, gint types) { mpd_Directory *dir = entity->info.directory; gchar *tmp = utf8_to_locale(dir->path); + gsize size = strlen(tmp)+2; - name = g_malloc(strlen(tmp)+2); - strcpy(name, tmp); - strcat(name, "/"); + name = g_malloc(size); + g_strlcpy(name, tmp, size); + g_strlcat(name, "/", size); g_free(tmp); } else if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG && diff --git a/src/wreadln.c b/src/wreadln.c index 700769b..930cb54 100644 --- a/src/wreadln.c +++ b/src/wreadln.c @@ -37,27 +37,27 @@ #define WRLN_MAX_LINE_SIZE 1024 #define WRLN_MAX_HISTORY_LENGTH 32 -unsigned int wrln_max_line_size = WRLN_MAX_LINE_SIZE; -unsigned int wrln_max_history_length = WRLN_MAX_HISTORY_LENGTH; +guint wrln_max_line_size = WRLN_MAX_LINE_SIZE; +guint wrln_max_history_length = WRLN_MAX_HISTORY_LENGTH; wrln_wgetch_fn_t wrln_wgetch = NULL; wrln_gcmp_pre_cb_t wrln_pre_completion_callback = NULL; wrln_gcmp_post_cb_t wrln_post_completion_callback = NULL; extern void screen_bell(void); -char * +gchar * wreadln(WINDOW *w, - char *prompt, - char *initial_value, - int x1, + gchar *prompt, + gchar *initial_value, + gint x1, GList **history, GCompletion *gcmp) { GList *hlist = NULL, *hcurrent = NULL; - char *line; - int x0, y, width; - int cursor = 0, start = 0; - int key = 0, i; + gchar *line; + gint x0, y, width; + gint cursor = 0, start = 0; + gint key = 0, i; /* move the cursor one step to the right */ void cursor_move_right(void) { @@ -132,11 +132,11 @@ wreadln(WINDOW *w, if( hlist==hcurrent ) { /* save the current line */ - strncpy(hlist->data, line, wrln_max_line_size); + g_strlcpy(hlist->data, line, wrln_max_line_size); } /* get previous line */ hlist = hlist->prev; - strncpy(line, hlist->data, wrln_max_line_size); + g_strlcpy(line, hlist->data, wrln_max_line_size); } cursor_move_to_eol(); drawline(); @@ -144,7 +144,7 @@ wreadln(WINDOW *w, else if( initial_value ) { /* copy the initial value to the line buffer */ - strncpy(line, initial_value, wrln_max_line_size); + g_strlcpy(line, initial_value, wrln_max_line_size); cursor_move_to_eol(); drawline(); } @@ -166,7 +166,9 @@ wreadln(WINDOW *w, switch (key) { +#ifdef HAVE_GETMOUSE case KEY_MOUSE: /* ignore mouse events */ +#endif case ERR: /* ingnore errors */ break; @@ -193,8 +195,7 @@ wreadln(WINDOW *w, list = g_completion_complete(gcmp, line, &prefix); if( prefix ) { - int len = strlen(prefix); - strncpy(line, prefix, len); + g_strlcpy(line, prefix, wrln_max_line_size); cursor_move_to_eol(); g_free(prefix); } @@ -259,13 +260,12 @@ wreadln(WINDOW *w, if( hlist==hcurrent ) { /* save the current line */ - strncpy(hlist->data, line, wrln_max_line_size); + g_strlcpy(hlist->data, line, wrln_max_line_size); } /* get previous line */ hlist = hlist->prev; - strncpy(line, hlist->data, wrln_max_line_size); + g_strlcpy(line, hlist->data, wrln_max_line_size); } - // if (cursor > strlen(line)) cursor_move_to_eol(); break; case KEY_DOWN: @@ -274,7 +274,7 @@ wreadln(WINDOW *w, { /* get next line */ hlist = hlist->next; - strncpy(line, hlist->data, wrln_max_line_size); + g_strlcpy(line, hlist->data, wrln_max_line_size); } cursor_move_to_eol(); break; @@ -292,12 +292,14 @@ wreadln(WINDOW *w, { if (strlen (line + cursor)) /* if the cursor is */ { /* not at the last pos */ - char *tmp = 0; - tmp = g_malloc0(strlen (line + cursor) + 1); - strcpy (tmp, line + cursor); + gchar *tmp = 0; + gsize size = strlen(line + cursor) + 1; + + tmp = g_malloc0(size); + g_strlcpy (tmp, line + cursor, size); line[cursor] = key; line[cursor + 1] = 0; - strcat (&line[cursor + 1], tmp); + g_strlcat (&line[cursor + 1], tmp, size); g_free(tmp); cursor_move_right(); } @@ -321,7 +323,7 @@ wreadln(WINDOW *w, /* update the current history entry */ size_t size = strlen(line)+1; hcurrent->data = g_realloc(hcurrent->data, size); - strncpy(hcurrent->data, line, size); + g_strlcpy(hcurrent->data, line, size); } else { diff --git a/src/wreadln.h b/src/wreadln.h index 55c24b9..667740c 100644 --- a/src/wreadln.h +++ b/src/wreadln.h @@ -2,10 +2,10 @@ #define WREADLN_H /* max size allocated for a line */ -extern unsigned int wrln_max_line_size; +extern guint wrln_max_line_size; /* max items stored in the history list */ -extern unsigned int wrln_max_history_length; +extern guint wrln_max_history_length; /* custom wgetch function */ typedef int (*wrln_wgetch_fn_t) (WINDOW *w); @@ -21,14 +21,14 @@ extern wrln_gcmp_post_cb_t wrln_post_completion_callback; /* Note, wreadln calls curs_set() and noecho(), to enable cursor and * disable echo. wreadln will not restore these settings when exiting! */ -char *wreadln(WINDOW *w, /* the curses window to use */ - char *prompt, /* the prompt string or NULL */ - char *initial_value, /* initial value or NULL for a empty line - * (char *) -1 => get value from history */ - int x1, /* the maximum x position or 0 */ - GList **history, /* a pointer to a history list or NULL */ - GCompletion *gcmp /* a GCompletion structure or NULL */ - ); +gchar *wreadln(WINDOW *w, /* the curses window to use */ + gchar *prompt, /* the prompt string or NULL */ + gchar *initial_value, /* initial value or NULL for a empty line + * (char *) -1 = get value from history */ + gint x1, /* the maximum x position or 0 */ + GList **history, /* a pointer to a history list or NULL */ + GCompletion *gcmp /* a GCompletion structure or NULL */ + ); #endif -- 2.30.2