From a39568367ed8e4dede5874f98c7424d745f3d418 Mon Sep 17 00:00:00 2001 From: Timur Bakeyev Date: Wed, 27 Jan 1999 01:08:30 +0000 Subject: [PATCH] Glibing..... (2) Wed Jan 27 03:17:44 1999 Timur Bakeyev * Converted memory managment to Glib. Now we use g_new()/g_malloc()/ g_strdup()/g_free() routings. Also, copy_strings() replaced by g_strconcat(), strcasecmp() -> g_strcasecmp(),and sprintf() by g_snprintf(). * Some sequences of malloc()/sprintf() changed to g_strdup_printf(). * mad.[ch]: Modified, to work with new GLib's memory managment. Fixed a missing #undef for tempnam, which caused dead loop. Add several new functions to emulate GLib memory managment. *main.c, mad.[ch]: Add a new switch "-M", which allows to redirect MAD messages to the file. * util.[ch], utilunix.c: Modified, deleted our variants of strcasecmp() and strdup() - we have g_ equivalences. Remove get_full_name() - it is similar to concat_dir_and_file(). Some other tricks with g_* functions. * global.h: Modified, extended. Now it is main memory mangment include - i.e. all inclusions of , , , "fs.h", "mem.h", "util.h" and "mad.h" done there. This elimanates problem with proper or- der of #include's. * All around the source - changed order of #include's, most of them gone to global.h (see above), minor changes, like "0" -> NULL in string func- tions. --- src/ChangeLog | 29 ++++++ src/achown.c | 8 +- src/background.c | 35 +++---- src/boxes.c | 50 +++++----- src/chmod.c | 11 +- src/chown.c | 8 +- src/cmd.c | 139 +++++++++++++------------- src/color.c | 7 +- src/command.c | 27 +++-- src/complete.c | 102 +++++++++---------- src/cons.handler.c | 4 +- src/cons.saver.c | 12 +-- src/dialog.c | 7 +- src/dir.c | 33 +++--- src/dirhist.c | 16 +-- src/dlg.c | 29 +++--- src/dlg.h | 2 - src/ext.c | 76 +++++++------- src/file.c | 151 ++++++++++++++-------------- src/filegui.c | 45 ++++----- src/filenot.c | 18 ++-- src/fileopctx.h | 2 +- src/find.c | 133 ++++++++++++------------ src/fixhlp.c | 8 +- src/global.h | 26 +++-- src/help.c | 13 +-- src/hotlist.c | 138 +++++++++++++------------ src/info.c | 6 +- src/key.c | 16 ++- src/layout.c | 17 ++-- src/learn.c | 16 ++- src/listmode.c | 13 +-- src/mad.c | 82 ++++++++++----- src/mad.h | 13 ++- src/main.c | 141 +++++++++++++------------- src/mem.h | 2 +- src/menu.c | 18 ++-- src/mouse.c | 8 +- src/panelize.c | 42 ++++---- src/profile.c | 82 ++++++++------- src/rxvt.c | 8 +- src/screen.c | 137 ++++++++++++------------- src/setup.c | 75 +++++++------- src/slint.c | 15 ++- src/subshell.c | 28 +++--- src/tree.c | 44 ++++---- src/treestore.c | 50 +++++----- src/user.c | 42 ++++---- src/util.c | 244 ++++++++++++++++++++------------------------- src/util.h | 23 ++--- src/utilunix.c | 40 ++++---- src/view.c | 98 +++++++++--------- src/widget.c | 130 ++++++++++++------------ src/win.c | 12 +-- src/wtools.c | 13 +-- 55 files changed, 1231 insertions(+), 1313 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 186b1795a..a4af3f414 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,32 @@ +Wed Jan 27 03:17:44 1999 Timur Bakeyev + + * Converted memory managment to Glib. Now we use g_new()/g_malloc()/ + g_strdup()/g_free() routings. Also, copy_strings() replaced by + g_strconcat(), strcasecmp() -> g_strcasecmp(),and sprintf() by + g_snprintf(). + + * Some sequences of malloc()/sprintf() changed to g_strdup_printf(). + + * mad.[ch]: Modified, to work with new GLib's memory managment. Fixed + a missing #undef for tempnam, which caused dead loop. Add several new + functions to emulate GLib memory managment. + + *main.c, mad.[ch]: Add a new switch "-M", which allows to redirect MAD + messages to the file. + + * util.[ch], utilunix.c: Modified, deleted our variants of strcasecmp() + and strdup() - we have g_ equivalences. Remove get_full_name() - it is + similar to concat_dir_and_file(). Some other tricks with g_* functions. + + * global.h: Modified, extended. Now it is main memory mangment include - + i.e. all inclusions of , , , "fs.h", "mem.h", + "util.h" and "mad.h" done there. This elimanates problem with proper or- + der of #include's. + + * All around the source - changed order of #include's, most of them gone + to global.h (see above), minor changes, like "0" -> NULL in string func- + tions. + 1999-01-25 Alexander Savelyev * ext.c (exec_extension): Always use /bin/sh. Not the user diff --git a/src/achown.c b/src/achown.c index bdd45f7f6..eba232262 100644 --- a/src/achown.c +++ b/src/achown.c @@ -28,11 +28,9 @@ #endif #include #include -#include /* For malloc() */ #include /* For errno on SunOS systems */ -#include "mad.h" #include "tty.h" -#include "util.h" /* Needed for the externs */ +#include "global.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -562,7 +560,7 @@ static void init_chown_advanced (void) { int i; - sf_stat = (struct stat *) malloc (sizeof (struct stat)); + sf_stat = g_new (struct stat, 1); do_refresh (); end_chown = need_update = current_file = 0; single_set = (cpanel->marked < 2) ? 2 : 0; @@ -597,7 +595,7 @@ static void init_chown_advanced (void) static void chown_advanced_done (void) { - free (sf_stat); + g_free (sf_stat); if (need_update) update_panels (UP_OPTIMIZE, UP_KEEPSEL); repaint_screen (); diff --git a/src/background.c b/src/background.c index 2bd347910..9216d0b09 100644 --- a/src/background.c +++ b/src/background.c @@ -33,11 +33,11 @@ #endif #include #include -#include #include #include #include #include "tty.h" +#include "global.h" #include "dlg.h" #include "widget.h" #include "wtools.h" @@ -49,12 +49,10 @@ #ifdef USE_NETCODE # include #endif -#include "util.h" #include "dialog.h" #include "fileopctx.h" -#include "mad.h" #include "key.h" /* For add_select_channel(), delete_select_channel() */ -#include "regex.h" +#include "eregex.h" #include "file.h" #include "filegui.h" @@ -84,7 +82,7 @@ register_task_running (FileOpContext *ctx, pid_t pid, int fd, char *info) { TaskList *new; - new = xmalloc (sizeof (TaskList), "note_task_running"); + new = g_new (TaskList, 1); new->pid = pid; new->info = info; new->state = Task_Running; @@ -107,8 +105,8 @@ unregister_task_running (pid_t pid, int fd) prev->next = p->next; else task_list = p->next; - free (p->info); - free (p); + g_free (p->info); + g_free (p); break; } prev = p; @@ -177,7 +175,7 @@ do_background (FileOpContext *ctx, char *info) static char * background_title (char *str) { - char *result = copy_strings (_("Background process:"), str, NULL); + char *result = g_strconcat (_("Background process:"), str, NULL); return result; } @@ -196,7 +194,7 @@ real_message_1s (enum OperationMode mode, int *flags, char *title, char *str1) message (*flags, title, str1); if (title != full_title) - free (full_title); + g_free (full_title); } static void @@ -212,7 +210,7 @@ real_message_2s (enum OperationMode mode, int *flags, char *title, char *str1, c message (*flags, title, str1, str2); if (title != full_title) - free (full_title); + g_free (full_title); } static void @@ -228,7 +226,7 @@ real_message_3s (enum OperationMode mode, int *flags, char *title, char *str1, c message (*flags, title, str1, str2, str3); if (title != full_title) - free (full_title); + g_free (full_title); } /* }}} */ @@ -320,7 +318,7 @@ background_attention (int fd, void *closure) int size; read (fd, &size, sizeof (size)); - data [i] = xmalloc (size+1, "RPC Arguments"); + data [i] = g_malloc (size+1); read (fd, data [i], size); data [i][size] = 0; /* NULL terminate the blocks (they could be strings) */ @@ -398,7 +396,7 @@ background_attention (int fd, void *closure) write (fd, &len, sizeof (len)); if (len){ write (fd, resstr, len); - free (resstr); + g_free (resstr); } } else { len = 0; @@ -406,7 +404,7 @@ background_attention (int fd, void *closure) } } for (i = 0; i < argc; i++) - free (data [i]); + g_free (data [i]); do_refresh (); mc_refresh (); @@ -486,7 +484,7 @@ parent_call_string (void *routine, int argc, ...) read (parent_fd, &i, sizeof (int)); if (!i) return NULL; - str = xmalloc (i + 1, "parent_return"); + str = g_malloc (i + 1); read (parent_fd, str, i); str [i] = 0; return str; @@ -579,12 +577,9 @@ input_dialog_help (char *header, char *text, char *help, char *def_text) void message_1s1d (int flags, char *title, char *str, int d) { - char *p; - - p = xmalloc (strlen (str) + 30, "1s1d"); - sprintf (p, str, d); + char *p = g_strdup_printf (str, d); message_1s (flags, title, p); - free (p); + g_free (p); } /* }}} */ diff --git a/src/boxes.c b/src/boxes.c index 40f86339c..27e0d3b8b 100644 --- a/src/boxes.c +++ b/src/boxes.c @@ -23,16 +23,12 @@ #include "tty.h" #include #include -#include #include #include #include -#include #include #include #include "global.h" -#include "mad.h" /* The great mad */ -#include "util.h" /* Required by panel.h */ #include "win.h" /* Our window tools */ #include "color.h" /* Color definitions */ #include "dlg.h" /* The nice dialog manager */ @@ -227,19 +223,19 @@ display_box (WPanel *panel, char **userp, char **minip, int *use_msformat, int n if (!panel) { p = get_nth_panel_name (num); - panel = (WPanel *) xmalloc (sizeof (WPanel), "temporary panel"); + panel = g_new (WPanel, 1); panel->list_type = list_full; - panel->user_format = strdup (DEFAULT_USER_FORMAT); + panel->user_format = g_strdup (DEFAULT_USER_FORMAT); panel->user_mini_status = 0; for (i = 0; i < LIST_TYPES; i++) - panel->user_status_format[i] = strdup (DEFAULT_USER_FORMAT); - section = copy_strings ("Temporal:", p, 0); + panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT); + section = g_strconcat ("Temporal:", p, NULL); if (!profile_has_section (section, profile_name)) { - free (section); - section = strdup (p); + g_free (section); + section = g_strdup (p); } panel_load_setup (panel, section); - free (section); + g_free (section); } current_mode = panel->list_type; @@ -251,16 +247,16 @@ display_box (WPanel *panel, char **userp, char **minip, int *use_msformat, int n result = -1; if (section) { - free (panel->user_format); + g_free (panel->user_format); for (i = 0; i < LIST_TYPES; i++) - free (panel->user_status_format [i]); - free (panel); + g_free (panel->user_status_format [i]); + g_free (panel); } if (dd->ret_value != B_CANCEL){ result = my_radio->sel; - *userp = strdup (user->buffer); - *minip = strdup (status->buffer); + *userp = g_strdup (user->buffer); + *minip = g_strdup (status->buffer); *use_msformat = check_status->state & C_BOOL; } destroy_dlg (dd); @@ -595,7 +591,7 @@ tree (char *current_dir) run_dlg (dlg); if (dlg->ret_value == B_ENTER) - val = strdup (mytree->selected_ptr->name); + val = g_strdup (mytree->selected_ptr->name); else val = 0; @@ -686,16 +682,16 @@ static QuickDialog confvfs_dlg = void configure_vfs (void) { - char buffer2[15]; + char buffer2[BUF_TINY]; #if defined(USE_NETCODE) - char buffer3[15]; + char buffer3[BUF_TINY]; #endif - sprintf (buffer2, "%i", vfs_timeout); + g_snprintf (buffer2, sizeof (buffer2), "%i", vfs_timeout); confvfs_widgets [3 + VFS_WIDGETBASE].text = buffer2; #if defined(USE_NETCODE) ret_use_netrc = use_netrc; - sprintf(buffer3, "%i", ftpfs_directory_timeout); + g_snprintf(buffer3, sizeof (buffer3), "%i", ftpfs_directory_timeout); confvfs_widgets[5].text = buffer3; confvfs_widgets[7].text = ftpfs_anonymous_passwd; confvfs_widgets[2].text = ftpfs_proxy_host ? ftpfs_proxy_host : ""; @@ -703,18 +699,18 @@ configure_vfs (void) if (quick_dialog (&confvfs_dlg) != B_CANCEL) { vfs_timeout = atoi (ret_timeout); - free (ret_timeout); + g_free (ret_timeout); if (vfs_timeout < 0 || vfs_timeout > 10000) vfs_timeout = 10; #if defined(USE_NETCODE) - free(ftpfs_anonymous_passwd); + g_free (ftpfs_anonymous_passwd); ftpfs_anonymous_passwd = ret_passwd; if (ftpfs_proxy_host) - free(ftpfs_proxy_host); + g_free (ftpfs_proxy_host); ftpfs_proxy_host = ret_ftp_proxy; ftpfs_directory_timeout = atoi(ret_directory_timeout); use_netrc = ret_use_netrc; - free(ret_directory_timeout); + g_free (ret_directory_timeout); #endif } } @@ -840,9 +836,9 @@ jobs_fill_listbox (void) while (tl){ char *s; - s = copy_strings (state_str [tl->state], " ", tl->info, NULL); + s = g_strconcat (state_str [tl->state], " ", tl->info, NULL); listbox_add_item (bg_list, LISTBOX_APPEND_AT_END, 0, s, (void *) tl); - free (s); + g_free (s); tl = tl->next; } } diff --git a/src/chmod.c b/src/chmod.c index b2d65aa74..74f41bb50 100644 --- a/src/chmod.c +++ b/src/chmod.c @@ -30,8 +30,7 @@ # include #endif #include "tty.h" -#include "mad.h" -#include "util.h" +#include "global.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -179,13 +178,13 @@ static void chmod_refresh (void) static int chmod_callback (Dlg_head *h, int Par, int Msg) { - char buffer [10]; + char buffer [BUF_TINY]; switch (Msg) { case DLG_ACTION: if (Par >= BUTTONS - single_set * 2){ c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode; - sprintf (buffer, "%o", c_stat); + g_snprintf (buffer, sizeof (buffer), "%o", c_stat); label_set_text (statl, buffer); chmod_toggle_select (); mode_change = 1; @@ -308,7 +307,7 @@ static void apply_mask (struct stat *sf) void chmod_cmd (void) { - char buffer [10]; + char buffer [BUF_TINY]; char *fname; int i; struct stat sf_stat; @@ -357,7 +356,7 @@ void chmod_cmd (void) add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL)); c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21); add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL)); - sprintf (buffer, "%o", c_stat); + g_snprintf (buffer, sizeof (buffer), "%o", c_stat); statl = label_new (FY+4, FX+2, buffer, NULL); add_widget (ch_dlg, statl); diff --git a/src/chown.c b/src/chown.c index 0b202e2c6..f000406b5 100644 --- a/src/chown.c +++ b/src/chown.c @@ -19,7 +19,6 @@ #include #include #include -#include /* For malloc() */ #include /* For errno on SunOS systems */ #include @@ -32,8 +31,7 @@ #endif #include "tty.h" -#include "mad.h" -#include "util.h" /* Needed for the externs */ +#include "global.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -253,7 +251,7 @@ chown_cmd (void) WLEntry *fe; uid_t new_user; gid_t new_group; - char buffer [15]; + char buffer [BUF_TINY]; #if 0 /* Please no */ @@ -296,7 +294,7 @@ chown_cmd (void) chown_label (0, name_trunc (fname, 15)); chown_label (1, name_trunc (get_owner (sf_stat.st_uid), 15)); chown_label (2, name_trunc (get_group (sf_stat.st_gid), 15)); - sprintf (buffer, "%d", c_fsize); + g_snprintf (buffer, sizeof (buffer), "%d", c_fsize); chown_label (3, buffer); chown_label (4, string_perm (sf_stat.st_mode)); diff --git a/src/cmd.c b/src/cmd.c index b63e879ad..188c4ff5c 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -29,12 +29,10 @@ #endif #include "tty.h" #include -#include /* getenv (), rand */ #include #include #include #include -#include #include #include /* open, O_RDWR */ #include @@ -50,9 +48,8 @@ #ifdef HAVE_MMAP # include #endif -#include "mad.h" +#include "global.h" #include "dir.h" -#include "util.h" #include "panel.h" #include "cmd.h" /* Our definitions */ #include "view.h" /* view() */ @@ -64,7 +61,6 @@ #include "tree.h" #include "subshell.h" /* use_subshell */ #include "cons.saver.h" -#include "global.h" #include "dlg.h" /* required by wtools.h */ #include "widget.h" /* required by wtools.h */ #include "wtools.h" /* listbox */ @@ -75,7 +71,6 @@ #include "view.h" /* view */ #include "key.h" /* get_key_code */ #include "help.h" /* interactive_display */ -#include "fs.h" #include "boxes.h" /* cd_dialog */ #include "color.h" #include "user.h" @@ -173,7 +168,7 @@ int view_file_at_line (char *filename, int plain_view, int internal, int start_l return move_dir; } if (internal){ - char view_entry [32]; + char view_entry [BUF_TINY]; #ifdef HAVE_GNOME if (!gmc_view (filename, start_line)){ @@ -181,7 +176,7 @@ int view_file_at_line (char *filename, int plain_view, int internal, int start_l } #else if (start_line != 0) - sprintf (view_entry, "View:%d", start_line); + g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line); else strcpy (view_entry, "View"); @@ -291,7 +286,7 @@ void view_file_cmd (WPanel *panel) if (!filename) return; view_file (filename, 0, use_internal_view); - free (filename); + g_free (filename); } void view_simple_cmd (WPanel *panel) @@ -311,7 +306,7 @@ void filtered_view_cmd (WPanel *panel) view (command, "", 0, 0); - free (command); + g_free (command); } void filtered_view_cmd_cpanel (void) @@ -398,20 +393,20 @@ void mkdir_cmd (WPanel *panel) return; if (dir [0] && (dir [0] == '/' || dir [0] == '~')) - tempdir = strdup (dir); + tempdir = g_strdup (dir); else tempdir = concat_dir_and_file (panel->cwd, dir); - free (dir); + g_free (dir); save_cwds_stat (); if (my_mkdir (tempdir, 0777) == 0){ update_panels (UP_OPTIMIZE, tempdir); repaint_screen (); select_item (cpanel); - free (tempdir); + g_free (tempdir); return; } - free (tempdir); + g_free (tempdir); message (1, MSG_ERROR, " %s ", unix_error_string (errno)); } @@ -434,13 +429,13 @@ void set_panel_filter_to (WPanel *p, char *allocated_filter_string) { if (p->filter){ - free (p->filter); + g_free (p->filter); p->filter = 0; } if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0)) p->filter = allocated_filter_string; else - free (allocated_filter_string); + g_free (allocated_filter_string); reread_cmd (); x_filter_changed (p); } @@ -548,7 +543,7 @@ void select_cmd_panel (WPanel *panel) c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file); if (c == -1){ message (1, MSG_ERROR, _(" Malformed regular expression ")); - free (reg_exp); + g_free (reg_exp); return; } if (c){ @@ -556,7 +551,7 @@ void select_cmd_panel (WPanel *panel) } } paint_panel (panel); - free (reg_exp); + g_free (reg_exp); } void select_cmd (void) @@ -599,7 +594,7 @@ void unselect_cmd_panel (WPanel *panel) c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file); if (c == -1){ message (1, MSG_ERROR, _(" Malformed regular expression ")); - free (reg_exp); + g_free (reg_exp); return; } if (c){ @@ -607,7 +602,7 @@ void unselect_cmd_panel (WPanel *panel) } } paint_panel (panel); - free (reg_exp); + g_free (reg_exp); } void unselect_cmd (void) @@ -654,11 +649,11 @@ void ext_cmd (void) buffer = concat_dir_and_file (home_dir, MC_USER_EXT); check_for_default (extdir, buffer); do_edit (buffer); - free (buffer); + g_free (buffer); } else if (dir == 1) do_edit (extdir); - free (extdir); + g_free (extdir); flush_extension_file (); } @@ -679,7 +674,7 @@ void menu_edit_cmd (void) switch (dir){ case 0: - buffer = strdup (MC_LOCAL_MENU); + buffer = g_strdup (MC_LOCAL_MENU); check_for_default (menufile, buffer); break; @@ -693,14 +688,14 @@ void menu_edit_cmd (void) break; default: - free (menufile); + g_free (menufile); return; } do_edit (buffer); if (dir == 0) chmod(buffer, 0600); - free (buffer); - free (menufile); + g_free (buffer); + g_free (menufile); } void quick_chdir_cmd (void) @@ -718,7 +713,7 @@ void quick_chdir_cmd (void) #endif if (!do_cd (target, cd_exact)) message (1, MSG_ERROR, _("Could not change directory") ); - free (target); + g_free (target); } #ifdef USE_VFS @@ -732,7 +727,7 @@ void reselect_vfs (void) if (!do_cd (target, cd_exact)) message (1, MSG_ERROR, _("Could not change directory") ); - free (target); + g_free (target); } #endif @@ -843,12 +838,12 @@ compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode) } /* Thorough compare on, do byte-by-byte comparison */ - src_name = get_full_name (panel->cwd, source->fname); - dst_name = get_full_name (other->cwd, target->fname); + src_name = concat_dir_and_file (panel->cwd, source->fname); + dst_name = concat_dir_and_file (other->cwd, target->fname); if (compare_files (src_name, dst_name, source->buf.st_size)) do_file_mark (panel, i, 1); - free (src_name); - free (dst_name); + g_free (src_name); + g_free (dst_name); } } /* for (i ...) */ } @@ -901,7 +896,7 @@ void history_cmd (void) else current = listbox->list->current->data; destroy_dlg (listbox->dlg); - free (listbox); + g_free (listbox); if (!current) return; @@ -1031,14 +1026,14 @@ do_link (int symbolic_link, char *fname) } if (!symbolic_link){ - src = copy_strings (_(" Link "), name_trunc (fname, 46), + src = g_strconcat (_(" Link "), name_trunc (fname, 46), _(" to:"), NULL); dest = input_expand_dialog (_(" Link "), src, ""); - free (src); + g_free (src); if (!dest) return; if (!*dest) { - free (dest); + g_free (dest); return; } save_cwds_stat (); @@ -1067,9 +1062,9 @@ do_link (int symbolic_link, char *fname) #endif if (!dest || !*dest) { if (src) - free (src); + g_free (src); if (dest) - free (dest); + g_free (dest); return; } if (src){ @@ -1079,10 +1074,10 @@ do_link (int symbolic_link, char *fname) message (1, MSG_ERROR, _(" symlink: %s "), unix_error_string (errno)); } - free (src); + g_free (src); } } - free (dest); + g_free (dest); update_panels (UP_OPTIMIZE, UP_KEEPSEL); repaint_screen (); } @@ -1102,7 +1097,7 @@ void edit_symlink_cmd (void) if (S_ISLNK (selection (cpanel)->buf.st_mode)) { char buffer [MC_MAXPATHLEN], *p = selection (cpanel)->fname; int i; - char *dest, *q = copy_strings (_(" Symlink "), name_trunc (p, 32), _(" points to:"), NULL); + char *dest, *q = g_strconcat (_(" Symlink "), name_trunc (p, 32), _(" points to:"), NULL); i = readlink (p, buffer, MC_MAXPATHLEN); if (i > 0) { @@ -1118,10 +1113,10 @@ void edit_symlink_cmd (void) update_panels (UP_OPTIMIZE, UP_KEEPSEL); repaint_screen (); } - free (dest); + g_free (dest); } } - free (q); + g_free (q); } } @@ -1137,7 +1132,7 @@ void other_symlink_cmd (void) p = concat_dir_and_file (cpanel->cwd, selection (cpanel)->fname); r = concat_dir_and_file (opanel->cwd, selection (cpanel)->fname); - q = copy_strings (_(" Link symbolically "), name_trunc (p, 32), _(" to:"), NULL); + q = g_strconcat (_(" Link symbolically "), name_trunc (p, 32), _(" to:"), NULL); dest = input_expand_dialog (_(" Relative symlink "), q, r); if (dest) { if (*dest) { @@ -1153,15 +1148,15 @@ void other_symlink_cmd (void) unix_error_string (errno)); update_panels (UP_OPTIMIZE, UP_KEEPSEL); repaint_screen (); - free (s); + g_free (s); } } } - free (dest); + g_free (dest); } - free (q); - free (p); - free (r); + g_free (q); + g_free (p); + g_free (r); } #endif @@ -1169,7 +1164,7 @@ void help_cmd (void) { char *hlpfile = concat_dir_and_file (mc_home, "mc.hlp"); interactive_display (hlpfile, "[main]"); - free (hlpfile); + g_free (hlpfile); } void view_panel_cmd (void) @@ -1203,7 +1198,7 @@ char *get_random_hint (void) time (&now); if ((now - last) < 60) - return strdup (""); + return g_strdup (""); last = now; #else static int last_sec; @@ -1211,13 +1206,13 @@ char *get_random_hint (void) gettimeofday (&tv, NULL); if (!(tv.tv_sec> last_sec+60)) - return strdup (""); + return g_strdup (""); last_sec = tv.tv_sec; #endif hintfile = concat_dir_and_file (mc_home, MC_HINT); data = load_file (hintfile); - free (hintfile); + g_free (hintfile); if (!data) return 0; @@ -1239,8 +1234,8 @@ char *get_random_hint (void) eol = strchr (&data [start], '\n'); if (eol) *eol = 0; - result = strdup (&data [start]); - free (data); + result = g_strdup (&data [start]); + g_free (data); return result; } @@ -1269,16 +1264,16 @@ static void nice_cd (char *text, char *xtext, char *help, char *prefix, int to_h return; if (strncmp (prefix, machine, strlen (prefix)) == 0) - cd_path = copy_strings (machine, to_home ? "/~/" : NULL, NULL); + cd_path = g_strconcat (machine, to_home ? "/~/" : NULL, NULL); else - cd_path = copy_strings (prefix, machine, to_home ? "/~/" : NULL, NULL); + cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : NULL, NULL); if (do_panel_cd (MENU_PANEL, cd_path, 0)) directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd); else message (1, MSG_ERROR, N_(" Could not chdir to %s "), cd_path); - free (cd_path); - free (machine); + g_free (cd_path); + g_free (machine); } void netlink_cmd (void) @@ -1330,13 +1325,13 @@ void quick_cd_cmd (void) char *p = cd_dialog (); if (p && *p) { - char *q = copy_strings ("cd ", p, NULL); + char *q = g_strconcat ("cd ", p, NULL); do_cd_command (q); - free (q); + g_free (q); } if (p) - free (p); + g_free (p); } void @@ -1369,14 +1364,14 @@ save_setup_cmd (void) save_setup (); sync_profiles (); - str = copy_strings ( _(" Setup saved to ~/"), PROFILE_NAME, NULL); + str = g_strconcat ( _(" Setup saved to ~/"), PROFILE_NAME, NULL); #ifdef HAVE_GNOME set_hintbar (str); #else message (0, _(" Setup "), str); #endif - free (str); + g_free (str); } void @@ -1388,29 +1383,29 @@ configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, p->list_type = view_type; if (view_type == list_user || use_msformat){ - free (p->user_format); + g_free (p->user_format); p->user_format = user; - free (p->user_status_format [view_type]); + g_free (p->user_status_format [view_type]); p->user_status_format [view_type] = status; err = set_panel_formats (p); if (err){ if (err & 0x01){ - free (p->user_format); - p->user_format = strdup (DEFAULT_USER_FORMAT); + g_free (p->user_format); + p->user_format = g_strdup (DEFAULT_USER_FORMAT); } if (err & 0x02){ - free (p->user_status_format [view_type]); - p->user_status_format [view_type] = strdup (DEFAULT_USER_FORMAT); + g_free (p->user_status_format [view_type]); + p->user_status_format [view_type] = g_strdup (DEFAULT_USER_FORMAT); } } } else { - free (user); - free (status); + g_free (user); + g_free (status); } set_panel_formats (p); diff --git a/src/color.c b/src/color.c index e5b757004..d96b6cd40 100644 --- a/src/color.c +++ b/src/color.c @@ -18,9 +18,8 @@ #include #include "tty.h" #include -#include #include -#include "mad.h" +#include "global.h" #include "setup.h" /* For the externs */ #include "color.h" #include "x.h" @@ -200,7 +199,7 @@ void configure_colors_string (char *the_color_string) if (!the_color_string) return; - p = color_string = strdup (the_color_string); + p = color_string = g_strdup (the_color_string); while (color_string && *color_string){ while (*color_string == ' ' || *color_string == '\t') color_string++; @@ -226,7 +225,7 @@ void configure_colors_string (char *the_color_string) color_string++; } } - free (p); + g_free (p); } static void configure_colors (void) diff --git a/src/command.c b/src/command.c index a7a99a018..fce5e6040 100644 --- a/src/command.c +++ b/src/command.c @@ -24,16 +24,12 @@ #include #include #include "tty.h" -#include "fs.h" -#include #include -#include -#include "mad.h" +#include "global.h" /* home_dir */ #include "dlg.h" #include "widget.h" #include "command.h" #include "complete.h" /* completion constants */ -#include "global.h" /* home_dir */ #include "dialog.h" /* message () */ #include "dir.h" /* required by panel.h */ #include "panel.h" /* view_tree enum. Also, needed by main.h */ @@ -61,8 +57,9 @@ static int examine_cd (char *path) { char *p; int result; - char *q = xmalloc (MC_MAXPATHLEN + 10, "examine_cd"), *r, *s, *t, c; + char *q, *r, *s, *t, c; + q = g_malloc (MC_MAXPATHLEN + 10); /* Variable expansion */ for (p = path, r = q; *p && r < q + MC_MAXPATHLEN; ) { if (*p != '$' || (p [1] == '[' || p [1] == '(')) @@ -118,13 +115,13 @@ static int examine_cd (char *path) if (*p) { r = concat_dir_and_file (p, q); result = do_cd (r, cd_parse_command); - free (r); + g_free (r); } *s = c; p = s + 1; } } - free (q); + g_free (q); return result; } @@ -166,7 +163,7 @@ void do_cd_command (char *cmd) char *new; new = concat_dir_and_file (old, cmd+3); sync_tree (new); - free (new); + g_free (new); } } else if (!examine_cd (&cmd [3])) { @@ -202,16 +199,16 @@ static int enter (WCommand *cmdline) return MSG_NOT_HANDLED; } - command = xmalloc (strlen (cmd) + 1, "main, enter"); + command = g_malloc (strlen (cmd) + 1); command [0] = 0; for (i = j = 0; i < strlen (cmd); i ++){ if (cmd [i] == '%'){ i ++; s = expand_format (cmd [i], 1); - command = realloc (command, strlen (command) + strlen (s) + command = g_realloc (command, strlen (command) + strlen (s) + strlen (cmd) - i + 1); strcat (command, s); - free (s); + g_free (s); j = strlen (command); } else { command [j] = cmd [i]; @@ -223,7 +220,7 @@ static int enter (WCommand *cmdline) current_dlg = 0; new_input (input_w (cmdline)); execute (command); - free (command); + g_free (command); #ifdef HAVE_SUBSHELL_SUPPORT if (quit & SUBSHELL_EXIT){ @@ -259,11 +256,11 @@ static int command_callback (Dlg_head *h, WCommand *cmd, int msg, int par) WCommand *command_new (int y, int x, int cols) { WInput *in; - WCommand *cmd = xmalloc (sizeof (WCommand), "command_new"); + WCommand *cmd = g_new (WCommand, 1); in = input_new (y, x, DEFAULT_COLOR, cols, "", "cmdline"); cmd->input = *in; - free (in); + g_free (in); /* Add our hooks */ cmd->old_callback = (callback_fn) cmd->input.widget.callback; diff --git a/src/complete.c b/src/complete.c index b9c3eee91..e3b4066a4 100644 --- a/src/complete.c +++ b/src/complete.c @@ -22,9 +22,7 @@ #include #include "tty.h" #include -#include #include -#include #ifdef HAVE_UNISTD_H # include #endif @@ -60,8 +58,6 @@ #endif #include "global.h" -#include "mad.h" -#include "util.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -97,16 +93,16 @@ filename_completion_function (char *text, int state) char *temp; if (dirname) - free (dirname); + g_free (dirname); if (filename) - free (filename); + g_free (filename); if (users_dirname) - free (users_dirname); + g_free (users_dirname); - filename = strdup (text); + filename = g_strdup (text); if (!*text) text = "."; - dirname = strdup (text); + dirname = g_strdup (text); temp = strrchr (dirname, PATH_SEP); @@ -120,19 +116,19 @@ filename_completion_function (char *text, int state) /* We aren't done yet. We also support the "~user" syntax. */ /* Save the version of the directory that the user typed. */ - users_dirname = strdup (dirname); + users_dirname = g_strdup (dirname); { char *temp_dirname; temp_dirname = tilde_expand (dirname); if (!temp_dirname){ - free (dirname); - free (users_dirname); - free (filename); + g_free (dirname); + g_free (users_dirname); + g_free (filename); dirname = users_dirname = filename = NULL; return NULL; } - free (dirname); + g_free (dirname); dirname = temp_dirname; canonicalize_pathname (dirname); /* Here we should do something with variable expansion @@ -161,7 +157,7 @@ filename_completion_function (char *text, int state) } isdir = 1; isexec = 0; { - char *tmp = xmalloc (3 + strlen (dirname) + NLENGTH (entry), "Filename completion"); + char *tmp = g_malloc (3 + strlen (dirname) + NLENGTH (entry)); struct stat tempstat; strcpy (tmp, dirname); @@ -182,7 +178,7 @@ filename_completion_function (char *text, int state) isexec = 1; } } - free (tmp); + g_free (tmp); } switch (look_for_executables) { @@ -204,15 +200,15 @@ filename_completion_function (char *text, int state) directory = NULL; } if (dirname){ - free (dirname); + g_free (dirname); dirname = NULL; } if (filename){ - free (filename); + g_free (filename); filename = NULL; } if (users_dirname){ - free (users_dirname); + g_free (users_dirname); users_dirname = NULL; } return NULL; @@ -221,7 +217,7 @@ filename_completion_function (char *text, int state) if (users_dirname && (users_dirname[0] != '.' || users_dirname[1])){ int dirlen = strlen (users_dirname); - temp = xmalloc (3 + dirlen + NLENGTH (entry), "Filename completion"); + temp = g_malloc (3 + dirlen + NLENGTH (entry)); strcpy (temp, users_dirname); /* We need a `/' at the end. */ if (users_dirname[dirlen - 1] != PATH_SEP){ @@ -230,7 +226,7 @@ filename_completion_function (char *text, int state) } strcat (temp, entry->d_name); } else { - temp = xmalloc (2 + NLENGTH (entry), "Filename completion"); + temp = g_malloc (2 + NLENGTH (entry)); strcpy (temp, entry->d_name); } if (isdir) @@ -270,7 +266,7 @@ username_completion_function (char *text, int state) endpwent (); return NULL; } else { - char *temp = xmalloc (3 + strlen (entry->pw_name), "Username completion"); + char *temp = g_malloc (3 + strlen (entry->pw_name)); *temp = '~'; strcpy (temp + 1, entry->pw_name); @@ -307,7 +303,7 @@ variable_completion_function (char *text, int state) if (!*env_p) return NULL; else { - char *temp = xmalloc (2 + 2 * isbrace + p - *env_p, "Variable completion"); + char *temp = g_malloc (2 + 2 * isbrace + p - *env_p); *temp = '$'; if (isbrace) @@ -373,7 +369,7 @@ static void fetch_hosts (char *filename) for (start = i; buffer[i] && !cr_whitespace (buffer[i]); i++); if (i - start == 0) continue; - name = (char *) xmalloc (i - start + 1, "Hostname completion"); + name = (char *) g_malloc (i - start + 1); strncpy (name, buffer + start, i - start); name [i - start] = 0; { @@ -382,7 +378,7 @@ static void fetch_hosts (char *filename) if (hosts_p - hosts >= hosts_alloclen){ int j = hosts_p - hosts; - hosts = realloc ((void *)hosts, ((hosts_alloclen += 30) + 1) * sizeof (char *)); + hosts = g_realloc ((void *)hosts, ((hosts_alloclen += 30) + 1) * sizeof (char *)); hosts_p = hosts + j; } for (host_p = hosts; host_p < hosts_p; host_p++) @@ -392,7 +388,7 @@ static void fetch_hosts (char *filename) *(hosts_p++) = name; *hosts_p = NULL; } else - free (name); + g_free (name); } } } @@ -410,10 +406,10 @@ hostname_completion_function (char *text, int state) if (hosts != NULL){ for (host_p = hosts; *host_p; host_p++) - free (*host_p); - free (hosts); + g_free (*host_p); + g_free (hosts); } - hosts = (char **) xmalloc (((hosts_alloclen = 30) + 1) * sizeof (char *), "Hostname completion"); + hosts = g_new (char *, (hosts_alloclen = 30) + 1); *hosts = NULL; hosts_p = hosts; fetch_hosts ((p = getenv ("HOSTFILE")) ? p : "/etc/hosts"); @@ -432,12 +428,12 @@ hostname_completion_function (char *text, int state) if (!*host_p){ for (host_p = hosts; *host_p; host_p++) - free (*host_p); - free (hosts); + g_free (*host_p); + g_free (hosts); hosts = NULL; return NULL; } else { - char *temp = xmalloc (2 + strlen (*host_p), "Hostname completion"); + char *temp = g_malloc (2 + strlen (*host_p)); if (textstart) *temp = '@'; @@ -491,7 +487,7 @@ command_completion_function (char *text, int state) if (!p) path = NULL; else { - path = xmalloc (strlen (p) + 2, "Command completion"); + path = g_malloc (strlen (p) + 2); strcpy (path, p); path [strlen (p) + 1] = 0; p = strchr (path, PATH_ENV_SEP); @@ -515,7 +511,7 @@ command_completion_function (char *text, int state) case 0: /* Reserved words */ while (*words){ if (!strncmp (*words, text, text_len)) - return strdup (*(words++)); + return g_strdup (*(words++)); words++; } phase++; @@ -523,7 +519,7 @@ command_completion_function (char *text, int state) case 1: /* Builtin commands */ while (*words){ if (!strncmp (*words, text, text_len)) - return strdup (*(words++)); + return g_strdup (*(words++)); words++; } phase++; @@ -540,23 +536,23 @@ command_completion_function (char *text, int state) break; expanded = tilde_expand (cur_path); if (!expanded){ - free (path); + g_free (path); path = NULL; return NULL; } p = canonicalize_pathname (expanded); - cur_word = xmalloc (strlen (p) + 2 + text_len, "Command completion"); + cur_word = g_malloc (strlen (p) + 2 + text_len); strcpy (cur_word, p); if (cur_word [strlen (cur_word) - 1] != PATH_SEP) strcat (cur_word, PATH_SEP_STR); strcat (cur_word, text); - free (p); + g_free (p); cur_path = strchr (cur_path, 0) + 1; init_state = state; } found = filename_completion_function (cur_word, state - init_state); if (!found){ - free (cur_word); + g_free (cur_word); cur_word = NULL; } } @@ -565,13 +561,13 @@ command_completion_function (char *text, int state) if (!found){ look_for_executables = 0; if (path) - free (path); + g_free (path); return NULL; } if ((p = strrchr (found, PATH_SEP)) != NULL){ p++; - p = strdup (p); - free (found); + p = g_strdup (p); + g_free (found); return p; } return found; @@ -598,7 +594,7 @@ completion_matches (char *text, CompletionFunction entry_function) int match_list_size; /* The list of matches. */ - char **match_list = (char **) xmalloc (((match_list_size = 30) + 1) * sizeof (char *), "completion match list"); + char **match_list = g_new (char *, (match_list_size = 30) + 1); /* Number of matches actually found. */ int matches = 0; @@ -610,7 +606,7 @@ completion_matches (char *text, CompletionFunction entry_function) while ((string = (*entry_function) (text, matches)) != NULL){ if (matches + 1 == match_list_size) - match_list = (char **) realloc (match_list, ((match_list_size += 30) + 1) * sizeof (char *)); + match_list = (char **) g_realloc (match_list, ((match_list_size += 30) + 1) * sizeof (char *)); match_list[++matches] = string; match_list[matches + 1] = NULL; } @@ -644,7 +640,7 @@ completion_matches (char *text, CompletionFunction entry_function) if (c1 != c2) break; if (!c1 && !match_list [j][si]){ /* Two equal strings */ - free (match_list [j]); + g_free (match_list [j]); j++; if (j > matches) break; @@ -656,12 +652,12 @@ completion_matches (char *text, CompletionFunction entry_function) } matches = i; match_list [matches + 1] = NULL; - match_list[0] = xmalloc (low + 1, "Completion matching list"); + match_list[0] = g_malloc (low + 1); strncpy (match_list[0], match_list[1], low); match_list[0][low] = 0; } } else { /* There were no matches. */ - free (match_list); + g_free (match_list); match_list = NULL; } return match_list; @@ -695,7 +691,7 @@ try_complete (char *text, int *start, int *end, int flags) ignore_filenames = 0; c = text [*end]; text [*end] = 0; - word = strdup (text + *start); + word = g_strdup (text + *start); text [*end] = c; /* Determine if this could be a command word. It is if it appears at @@ -804,7 +800,7 @@ try_complete (char *text, int *start, int *end, int flags) ignore_filenames = 1; matches = completion_matches (r, filename_completion_function); ignore_filenames = 0; - free (r); + g_free (r); } *s = c; cdpath = s + 1; @@ -814,7 +810,7 @@ try_complete (char *text, int *start, int *end, int flags) } if (word) - free (word); + g_free (word); return matches; } @@ -826,8 +822,8 @@ void free_completions (WInput *in) if (!in->completions) return; for (p=in->completions; *p; p++) - free (*p); - free (in->completions); + g_free (*p); + g_free (in->completions); in->completions = NULL; } @@ -840,7 +836,7 @@ static int insert_text (WInput *in, char *text, int len) len = min (len, strlen (text)) + start - end; if (strlen (in->buffer) + len >= in->current_max_len){ /* Expand the buffer */ - char *narea = realloc(in->buffer, in->current_max_len + len + in->field_len); + char *narea = g_realloc (in->buffer, in->current_max_len + len + in->field_len); if (narea){ in->buffer = narea; in->current_max_len += len + in->field_len; diff --git a/src/cons.handler.c b/src/cons.handler.c index ae53464c9..c73a52190 100644 --- a/src/cons.handler.c +++ b/src/cons.handler.c @@ -252,7 +252,7 @@ console_shutdown() } if (screen != NULL) { - free(screen); + g_free (screen); } console_flag = 0; } @@ -340,7 +340,7 @@ console_init() height = vi.mv_rsz; width = vi.mv_csz; - screen = (unsigned short*) xmalloc(height * width * 2,"console_init"); + screen = (unsigned short*) g_malloc (height * width * 2); if (screen == NULL) { console_shutdown(); diff --git a/src/cons.saver.c b/src/cons.saver.c index bb5c48ebe..4f9ba2749 100644 --- a/src/cons.saver.c +++ b/src/cons.saver.c @@ -58,7 +58,7 @@ static int len; static char *buffer = NULL; static int buffer_size = 0; static int columns, rows; -static char vcs_name [40]; +static char vcs_name [BUF_TINY]; static int vcs_fd; static void dwrite (int fd, char *buffer) @@ -86,10 +86,10 @@ static void tty_getsize () inline void tty_cursormove(int y, int x) { - char buffer [20]; + char buffer [BUF_TINY]; /* Standard ANSI escape sequence for cursor positioning */ - sprintf (buffer,"\33[%d;%dH", y + 1, x + 1); + g_snprintf (buffer, sizeof (buffer), "\33[%d;%dH", y + 1, x + 1); dwrite (console_fd, buffer); } @@ -167,7 +167,7 @@ char *detect_console (void) !isdigit(tty_name[len - 1])) return "Doesn't look like console"; - sprintf (vcs_name, "/dev/vcsa%s", tty_name + xlen - 1); + g_snprintf (vcs_name, sizeof (vcs_name), "/dev/vcsa%s", tty_name + xlen - 1); vcs_fd = check_file (vcs_name, 0, &msg); console_fd = check_file (tty_name, 1, &msg); @@ -359,7 +359,7 @@ int main (int argc, char **argv) /* Allocate buffer for screen image */ tty_getsize (); buffer_size = 4 + 2 * columns * rows; - buffer = (char*) malloc (buffer_size); + buffer = (char*) g_malloc (buffer_size); } /* If using /dev/vcs*, we don't need anymore the console fd */ @@ -392,7 +392,7 @@ int main (int argc, char **argv) } /* while (read ...) */ if (buffer) - free (buffer); + g_free (buffer); return 0; } diff --git a/src/dialog.c b/src/dialog.c index 5ad134154..02f85962f 100644 --- a/src/dialog.c +++ b/src/dialog.c @@ -18,14 +18,11 @@ #include #include "tty.h" #include -#include /* For free() */ #include #include #include #include "x.h" -#include "mad.h" #include "global.h" -#include "util.h" #include "dialog.h" #include "color.h" #include "win.h" @@ -43,7 +40,7 @@ void push_refresh (void (*new_refresh)(void *), void *parameter, int flags) { Refresh *new; - new = xmalloc (sizeof (Refresh), "push_refresh"); + new = g_new (Refresh, 1); new->next = (struct Refresh *) refresh_list; new->refresh_fn = new_refresh; new->parameter = parameter; @@ -60,7 +57,7 @@ void pop_refresh (void) else { old = refresh_list; refresh_list = refresh_list->next; - free (old); + g_free (old); } } diff --git a/src/dir.c b/src/dir.c index fdd9f845a..4dbc958e4 100644 --- a/src/dir.c +++ b/src/dir.c @@ -18,17 +18,14 @@ #include #define DIR_H_INCLUDE_HANDLE_DIRENT #include "tty.h" -#include "fs.h" #include #include #include #include #include #include "x.h" -#include "mad.h" #include "global.h" #include "dir.h" -#include "util.h" #include "dialog.h" #include "tree.h" #include "../vfs/vfs.h" @@ -71,7 +68,7 @@ sort_orders_t sort_orders [SORT_TYPES_TOTAL] = { { N_("&Group"), sort_group } }; -#define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : strcasecmp (a,b)) +#define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b)) int unsorted (const file_entry *a, const file_entry *b) @@ -293,7 +290,7 @@ void clean_dir (dir_list *list, int count) int i; for (i = 0; i < count; i++){ - free (list->list [i].fname); + g_free (list->list [i].fname); list->list [i].fname = 0; } } @@ -307,7 +304,7 @@ add_dotdot_to_list (dir_list *list, int index) /* Need to grow the *list? */ if (index == list->size) { - list->list = realloc (list->list, sizeof (file_entry) * + list->list = g_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); if (!list->list) return 0; @@ -315,7 +312,7 @@ add_dotdot_to_list (dir_list *list, int index) } (list->list) [index].fnamelen = 2; - (list->list) [index].fname = strdup (".."); + (list->list) [index].fname = g_strdup (".."); (list->list) [index].f.link_to_dir = 0; (list->list) [index].f.stalled_link = 0; (list->list) [index].f.dir_size_computed = 0; @@ -329,16 +326,16 @@ add_dotdot_to_list (dir_list *list, int index) strcat (buffer, PATH_SEP_STR ".."); p = vfs_canon (buffer); if (mc_stat (p, &((list->list) [index].buf)) != -1){ - free (p); + g_free (p); break; } i = 1; if (!strcmp (p, PATH_SEP_STR)){ - free (p); + g_free (p); return 1; } strcpy (buffer, p); - free (p); + g_free (p); } /* Commented out to preserve a usable '..'. What's the purpose of this @@ -398,7 +395,7 @@ int handle_dirent (dir_list *list, char *filter, struct dirent *dp, /* Need to grow the *list? */ if (next_free == list->size){ - list->list = realloc (list->list, sizeof (file_entry) * + list->list = g_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); if (!list->list) return -1; @@ -437,7 +434,7 @@ int handle_path (dir_list *list, char *path, /* Need to grow the *list? */ if (next_free == list->size){ - list->list = realloc (list->list, sizeof (file_entry) * + list->list = g_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); if (!list->list) return -1; @@ -469,7 +466,7 @@ int do_load_dir(dir_list *list, sortfn *sort, int reverse, int case_sensitive, c if (status == -1) return next_free; list->list [next_free].fnamelen = NLENGTH (dp); - list->list [next_free].fname = strdup (dp->d_name); + list->list [next_free].fname = g_strdup (dp->d_name); list->list [next_free].f.marked = 0; list->list [next_free].f.link_to_dir = link_to_dir; list->list [next_free].f.stalled_link = stalled_link; @@ -525,13 +522,13 @@ static void alloc_dir_copy (int size) for (i = 0; i < dir_copy.size; i++) { if (dir_copy.list [i].fname) - free (dir_copy.list [i].fname); + g_free (dir_copy.list [i].fname); } - free (dir_copy.list); + g_free (dir_copy.list); dir_copy.list = 0; } - dir_copy.list = xmalloc (sizeof (file_entry) * size, "alloc_dir_copy"); + dir_copy.list = g_new (file_entry, size); for (i = 0; i < size; i++) dir_copy.list [i].fname = 0; @@ -601,7 +598,7 @@ int do_reload_dir (dir_list *list, sortfn *sort, int count, int rev, list->list [next_free].f.marked = 0; list->list [next_free].fnamelen = tmp_len; - list->list [next_free].fname = strdup (dp->d_name); + list->list [next_free].fname = g_strdup (dp->d_name); list->list [next_free].f.link_to_dir = link_to_dir; list->list [next_free].f.stalled_link = stalled_link; list->list [next_free].f.dir_size_computed = 0; @@ -641,7 +638,7 @@ sortfn *sort_name_to_type (char *sname) int i; for (i = 0; i < SORT_TYPES; i++) - if (strcasecmp (sort_orders [i].sort_name, sname) == 0) + if ( g_strcasecmp (sort_orders [i].sort_name, sname) == 0) return (sortfn *) sort_orders [i].sort_fn; /* default case */ diff --git a/src/dirhist.c b/src/dirhist.c index 73183754a..3ad271bbd 100644 --- a/src/dirhist.c +++ b/src/dirhist.c @@ -51,12 +51,12 @@ static char *dirhist_name = "Directory history"; void directory_history_load (void) { - char entry_name [20]; + char entry_name [BUF_TINY]; char *value; int i; for (i = 0; i < DIRECTORY_HISTORY_LOAD_COUNT; i++){ - sprintf (entry_name, "%d", i); + g_snprintf (entry_name, sizeof (entry_name), "%d", i); value = get_profile_string (dirhist_name, entry_name, "", profile_name); if (!(value || *value)) continue; @@ -67,7 +67,7 @@ directory_history_load (void) void directory_history_save (void) { - char entry_name [20]; + char entry_name [BUF_TINY]; char *dir; int i; @@ -77,7 +77,7 @@ directory_history_save (void) dir = directory_history_get_next (); if (!dir) break; - sprintf (entry_name, "%d", i); + g_snprintf (entry_name, sizeof (entry_name), "%d", i); WritePrivateProfileString (dirhist_name, entry_name, dir, profile_name); } } @@ -88,8 +88,8 @@ directory_history_delete (struct dirhist_entry *e) if (!e) return; directory_history_delete (e->next); - free (e->directory); - free (e); + g_free (e->directory); + g_free (e); } void @@ -121,10 +121,10 @@ directory_history_add (char *directory) { struct dirhist_entry *p; - p = (struct dirhist_entry *) malloc (sizeof (struct dirhist_entry)); + p = g_new (struct dirhist_entry, 1); if (!p) return; - p->directory = strdup (directory); + p->directory = g_strdup (directory); p->next = base; base = p; } diff --git a/src/dlg.c b/src/dlg.c index a5f27b897..28e35ecd5 100644 --- a/src/dlg.c +++ b/src/dlg.c @@ -20,15 +20,12 @@ /* "$Id$" */ #include #include -#include #include #include "tty.h" #include -#include "mad.h" -#include "x.h" -#include "util.h" -#include "menu.h" #include "global.h" +#include "x.h" +#include "menu.h" #include "win.h" #include "color.h" #include "mouse.h" @@ -236,7 +233,7 @@ Dlg_head *create_dlg (int y1, int x1, int lines, int cols, if ((flags & DLG_TRYUP) && (y1 > 3)) y1 -= 2; - new_d = (Dlg_head *) malloc (sizeof (Dlg_head)); + new_d = g_new (Dlg_head, 1); new_d->current = NULL; new_d->count = 0; new_d->direction = DIR_FORWARD; @@ -292,7 +289,7 @@ int add_widgetl (Dlg_head *where, void *what, WLay layout) if (where->running){ Widget_Item *point = where->current; - where->current = (Widget_Item *) malloc (sizeof (Widget_Item)); + where->current = g_new (Widget_Item, 1); if (point){ where->current->next = point->next; @@ -308,7 +305,7 @@ int add_widgetl (Dlg_head *where, void *what, WLay layout) } } else { back = where->current; - where->current = (Widget_Item *) malloc (sizeof (Widget_Item)); + where->current = g_new (Widget_Item, 1); if (back){ back->prev = where->current; where->current->next = back; @@ -358,7 +355,7 @@ int remove_widget (Dlg_head *h, void *what) h->current = 0; } h->count--; - free (p); + g_free (p); return 1; } p = p->next; @@ -371,7 +368,7 @@ int destroy_widget (Widget *w) send_message (w->parent, w, WIDGET_DESTROY, 0); if (w->destroy) w->destroy (w); - free (w); + g_free (w); return 1; } @@ -613,7 +610,7 @@ static INLINE void dialog_handle_key (Dlg_head *h, int d_key) case KEY_F(1): hlpfile = concat_dir_and_file (mc_home, "mc.hlp"); interactive_display (hlpfile, h->help_ctx); - free (hlpfile); + g_free (hlpfile); do_refresh (); break; @@ -920,14 +917,14 @@ destroy_dlg (Dlg_head *h) if (c->widget->destroy) c->widget->destroy (c->widget); c = c->next; - free (h->current->widget); - free (h->current); + g_free (h->current->widget); + g_free (h->current); h->current = c; } if (h->title) - free (h->title); + g_free (h->title); x_destroy_dlg (h); - free (h); + g_free (h); #ifndef HAVE_X if (refresh_list) @@ -1024,7 +1021,7 @@ int dlg_select_nth_widget (Dlg_head *h, int n) void x_set_dialog_title (Dlg_head *h, char *title) { - h->title = strdup(title); + h->title = g_strdup (title); } #endif diff --git a/src/dlg.h b/src/dlg.h index 4e899b02e..a55b11f23 100644 --- a/src/dlg.h +++ b/src/dlg.h @@ -1,7 +1,6 @@ #ifndef MC_DLG_H #define MC_DLG_H #include "mouse.h" -#include "util.h" /* Color constants */ #define FOCUSC h->color[1] @@ -257,7 +256,6 @@ int default_proc (Dlg_head *h, int Msg, int Par); #endif extern Dlg_head *current_dlg; -extern Hook *idle_hook; int send_message (Dlg_head *h, Widget *w, int msg, int par); int send_message_to (Dlg_head *h, Widget *w, int msg, int par); diff --git a/src/ext.c b/src/ext.c index b4717bb10..f7e7c8b32 100644 --- a/src/ext.c +++ b/src/ext.c @@ -21,7 +21,6 @@ #include #include #include -#include #ifdef NEEDS_IO_H # include #endif @@ -31,19 +30,13 @@ # include #endif #include -#include #include #include +#include "global.h" #include "user.h" #include "main.h" -#include "fs.h" - -#include -#include "util.h" -#include "mad.h" #include "dialog.h" -#include "global.h" #include "ext.h" #include "view.h" #include "main.h" @@ -69,7 +62,7 @@ void flush_extension_file (void) { if (data){ - free (data); + g_free (data); data = NULL; } @@ -91,13 +84,13 @@ quote_block (quote_func_t quote_func, char **quoting_block) temp_len = strlen (temp); current_len += temp_len + 2; - result = realloc (result, current_len); + result = g_realloc (result, current_len); if (!tail) tail = result; strcpy (tail, temp); strcat (tail, " "); tail += temp_len + 1; - free (temp); + g_free (temp); } return result; @@ -137,7 +130,7 @@ exec_extension (char *filename, char *data, char **drops, int *move_dir, int sta /* Note: this has to be done after the getlocalcopy call, * since it uses tmpnam as well */ - file_name = strdup (tmpnam (NULL)); + file_name = g_strdup (tmpnam (NULL)); /* #warning FIXME: this is ugly */ if ((cmd_file_fd = open (file_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600)) == -1){ @@ -146,6 +139,7 @@ exec_extension (char *filename, char *data, char **drops, int *move_dir, int sta return; } cmd_file = fdopen (cmd_file_fd, "w"); + /* FIXME: This is a hack, that makes us sure, we are using the right syntax */ fprintf (cmd_file, "#!/bin/sh\n"); prompt [0] = 0; @@ -162,12 +156,12 @@ exec_extension (char *filename, char *data, char **drops, int *move_dir, int sta if (localcopy) { mc_ungetlocalcopy (filename, localcopy, 0); } - free (file_name); + g_free (file_name); return; } fputs (parameter, cmd_file); written_nonspace = 1; - free (parameter); + g_free (parameter); } else { int len = strlen (prompt); @@ -195,7 +189,7 @@ exec_extension (char *filename, char *data, char **drops, int *move_dir, int sta data += i - 1; } else if ((i = check_format_var (data, &v)) > 0 && v){ fputs (v, cmd_file); - free (v); + g_free (v); data += i; } else { char *text; @@ -206,7 +200,7 @@ exec_extension (char *filename, char *data, char **drops, int *move_dir, int sta if (localcopy == NULL) { fclose(cmd_file); unlink(file_name); - free(file_name); + g_free (file_name); return; } mc_stat (localcopy, &mystat); @@ -225,7 +219,7 @@ exec_extension (char *filename, char *data, char **drops, int *move_dir, int sta strcpy (p, text); p = strchr (p, 0); } - free (text); + g_free (text); written_nonspace = 1; } } @@ -307,7 +301,7 @@ exec_extension (char *filename, char *data, char **drops, int *move_dir, int sta mc_stat (localcopy, &mystat); mc_ungetlocalcopy (filename, localcopy, localmtime != mystat.st_mtime); } - free (file_name); + g_free (file_name); } #ifdef FILE_L @@ -380,13 +374,13 @@ char *regex_command (char *filename, char *action, char **drops, int *move_dir) check_stock_mc_ext: extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT); if ((data = load_file (extension_file)) == NULL) { - free (buffer); + g_free (buffer); return 0; } if (!strstr (data, "default/")) { if (!strstr (data, "regex/") && !strstr (data, "shell/") && !strstr (data, "type/")) { - free (data); + g_free (data); data = NULL; if (extension_file == buffer) { home_error = 1; @@ -394,18 +388,18 @@ check_stock_mc_ext: } else { char *msg; char *msg2; - msg = copy_strings(" ", mc_home, MC_LIB_EXT, _(" file error"), NULL); - msg2 = copy_strings(_("Format of the "), + msg = g_strconcat (" ", mc_home, MC_LIB_EXT, _(" file error"), NULL); + msg2 = g_strconcat (_("Format of the "), mc_home, ("mc.ext file has changed\n\ with version 3.0. It seems that installation\n\ failed. Please fetch a fresh new copy from the\n\ Midnight Commander package or in case you don't\n\ -have any, get it from ftp://ftp.nuclecu.unam.mx."), 0); +have any, get it from ftp://ftp.nuclecu.unam.mx."), NULL); message (1, msg, msg2); - free (msg); - free (msg2); - free (buffer); + g_free (msg); + g_free (msg2); + g_free (buffer); return 0; } } @@ -413,22 +407,22 @@ have any, get it from ftp://ftp.nuclecu.unam.mx."), 0); if (home_error) { char *msg; char *msg2; - msg = copy_strings(" ~/", MC_USER_EXT, _(" file error "), NULL); - msg2 = copy_strings(_("Format of the ~/"), MC_USER_EXT, _(" file has changed\n\ + msg = g_strconcat (" ~/", MC_USER_EXT, _(" file error "), NULL); + msg2 = g_strconcat (_("Format of the ~/"), MC_USER_EXT, _(" file has changed\n\ with version 3.0. You may want either to\n\ copy it from "), mc_home, _("mc.ext or use that\n\ file as an example of how to write it.\n\ -"), mc_home, _("mc.ext will be used for this moment."), 0); +"), mc_home, _("mc.ext will be used for this moment."), NULL); message (1, msg, msg2); - free (msg); - free (msg2); + g_free (msg); + g_free (msg2); } - free (buffer); + g_free (buffer); } mc_stat (filename, &mystat); if (regex_command_title){ - free (regex_command_title); + g_free (regex_command_title); regex_command_title = NULL; } old_patterns = easy_patterns; @@ -498,11 +492,11 @@ file as an example of how to write it.\n\ if (islocal) { char *tmp = name_quote (filename, 0); char *command = - copy_strings (FILE_CMD, tmp, NULL); + g_strconcat (FILE_CMD, tmp, NULL); FILE *f = popen (command, "r"); - free (tmp); - free (command); + g_free (tmp); + g_free (command); if (f != NULL) { hasread = (fgets (content_string, 2047, f) != NULL); @@ -617,7 +611,7 @@ match_file_output: static char *q; if (to_return == NULL) { - to_return = xmalloc (512, "Action list"); + to_return = g_malloc (512); q = to_return; } else *(q++) = '='; /* Mark separator */ @@ -630,12 +624,12 @@ match_file_output: *r = c; c = *q; *q = 0; - to_return = strdup (r + 1); + to_return = g_strdup (r + 1); } else if (!strcmp (p, "Title") && regex_command_title == NULL) { *r = c; c = *q; *q = 0; - regex_command_title = strdup (r + 1); + regex_command_title = g_strdup (r + 1); } else { *r = c; c = *q; @@ -657,10 +651,10 @@ match_file_output: * we get filename as a pointer from cpanel->dir). */ if (p < q) { - char *filename_copy = strdup (filename); + char *filename_copy = g_strdup (filename); exec_extension (filename_copy, r + 1, drops, move_dir, view_at_line_number); - free (filename_copy); + g_free (filename_copy); to_return = "Success"; } diff --git a/src/file.c b/src/file.c index 67b66b5c2..d5526758a 100644 --- a/src/file.c +++ b/src/file.c @@ -64,11 +64,9 @@ #ifdef NEEDS_IO_H # include #endif - #include #include "tty.h" #include -#include #include #ifdef HAVE_UNISTD_H # include @@ -81,9 +79,7 @@ #endif /* SCO_FLAVOR */ #include #include -#include "mad.h" #include "eregex.h" -#include "util.h" #include "dialog.h" #include "global.h" #include "setup.h" @@ -266,7 +262,7 @@ do_transform_source (FileOpContext *ctx, char *source) static char * transform_source (FileOpContext *ctx, char *source) { - char *s = strdup (source); + char *s = g_strdup (source); char *q; /* We remove \n from the filename since regex routines would use \n as an anchor */ @@ -276,7 +272,7 @@ transform_source (FileOpContext *ctx, char *source) *q = ' '; } q = do_transform_source (ctx, s); - free (s); + g_free (s); return q; } @@ -287,7 +283,7 @@ free_linklist (struct link **linklist) for (lp = *linklist; lp != NULL; lp = lp2){ lp2 = lp -> next; - free (lp); + g_free (lp); } *linklist = NULL; } @@ -346,8 +342,8 @@ check_hardlinks (char *src_name, char *dst_name, struct stat *pstat) message_1s (1, MSG_ERROR, _(" Could not make the hardlink ")); return 0; } - lp = (struct link *) xmalloc (sizeof (struct link) + strlen (src_name) - + strlen (dst_name) + 1, "Hardlink cache"); + lp = (struct link *) g_malloc (sizeof (struct link) + strlen (src_name) + + strlen (dst_name) + 1); if (lp){ lp->vfs = my_vfs; lp->ino = ino; @@ -404,30 +400,30 @@ make_symlink (FileOpContext *ctx, char *src_path, char *dst_path) if (ctx->stable_symlinks && *link_target != PATH_SEP) { char *p, *q, *r, *s; - p = strdup (src_path); + p = g_strdup (src_path); r = strrchr (p, PATH_SEP); if (r){ r[1] = 0; if (*dst_path == PATH_SEP) - q = strdup (dst_path); + q = g_strdup (dst_path); else - q = copy_strings (p, dst_path, 0); + q = g_strconcat (p, dst_path, NULL); r = strrchr (q, PATH_SEP); if (r){ r[1] = 0; - s = copy_strings (p, link_target, NULL); + s = g_strconcat (p, link_target, NULL); strcpy (link_target, s); - free (s); + g_free (s); s = diff_two_paths (q, link_target); if (s){ strcpy (link_target, s); - free (s); + g_free (s); } } - free (q); + g_free (q); } - free (p); + g_free (p); } retry_dst_symlink: if (mc_symlink (link_target, dst_path) == 0) @@ -483,8 +479,8 @@ copy_file_file (FileOpContext *ctx, char *src_path, char *dst_path, int ask_over uid_t src_uid; gid_t src_gid; #endif - char *buf = 0; - int buf_size = 8*1024; + char *buf = NULL; + int buf_size = BUF_8K; int src_desc, dest_desc = 0; int n_read, n_written; int src_mode; /* The mode of the source file */ @@ -653,7 +649,7 @@ copy_file_file (FileOpContext *ctx, char *src_path, char *dst_path, int ask_over continue; goto ret; } - buf = (char *) xmalloc (buf_size, "copy_file_file"); + buf = (char *) g_malloc (buf_size); ctx->eta_secs = 0.0; ctx->bps = 0; @@ -763,7 +759,7 @@ copy_file_file (FileOpContext *ctx, char *src_path, char *dst_path, int ask_over ret: if (buf) - free (buf); + g_free (buf); while ((resources & 1) && mc_close (src_desc) < 0){ temp_status = file_error (_(" Cannot close source file \"%s\" \n %s "), src_path); @@ -890,7 +886,7 @@ copy_dir_dir (FileOpContext *ctx, char *s, char *d, int toplevel, return FILE_SKIP; } - lp = xmalloc (sizeof (struct link), "parent_dirs"); + lp = g_new (struct link, 1); lp->vfs = vfs_type (s); lp->ino = cbuf.st_ino; lp->dev = cbuf.st_dev; @@ -903,11 +899,11 @@ copy_dir_dir (FileOpContext *ctx, char *s, char *d, int toplevel, if (move_over){ if (mc_rename (s, d) == 0){ - free (parent_dirs); + g_free (parent_dirs); return FILE_CONT; } } - dest_dir = copy_strings (d, 0); + dest_dir = g_strdup (d); } else { /* * If the destination directory exists, we want to copy the whole @@ -933,7 +929,7 @@ copy_dir_dir (FileOpContext *ctx, char *s, char *d, int toplevel, } else #endif { - dest_dir = copy_strings (d, 0); + dest_dir = g_strdup (d); goto dont_mkdir; } } @@ -945,7 +941,7 @@ copy_dir_dir (FileOpContext *ctx, char *s, char *d, int toplevel, goto ret; } - lp = xmalloc (sizeof (struct link), "dest_dirs"); + lp = g_new (struct link, 1); mc_stat (dest_dir, &buf); lp->vfs = vfs_type (dest_dir); lp->ino = buf.st_ino; @@ -994,17 +990,17 @@ copy_dir_dir (FileOpContext *ctx, char *s, char *d, int toplevel, */ return_status = copy_dir_dir (ctx, path, mdpath, 0, 0, delete, parent_dirs, progress_count, progress_bytes); - free (mdpath); + g_free (mdpath); } else { dest_file = concat_dir_and_file (dest_dir, x_basename (path)); return_status = copy_file_file (ctx, path, dest_file, 1, progress_count, progress_bytes, 0); - free (dest_file); + g_free (dest_file); } if (delete && return_status == FILE_CONT){ if (ctx->erase_at_end){ static struct link *tail; - lp = xmalloc (sizeof (struct link) + strlen (path), "erase_list"); + lp = g_malloc (sizeof (struct link) + strlen (path)); strcpy (lp->name, path); lp->st_mode = buf.st_mode; lp->next = 0; @@ -1026,9 +1022,9 @@ copy_dir_dir (FileOpContext *ctx, char *s, char *d, int toplevel, * next should be freed: .ado */ if (!next) - free (next); + g_free (next); #endif - free (path); + g_free (path); } mc_closedir (reading); @@ -1043,8 +1039,8 @@ copy_dir_dir (FileOpContext *ctx, char *s, char *d, int toplevel, #endif ret: - free (dest_dir); - free (parent_dirs); + g_free (dest_dir); + g_free (parent_dirs); return return_status; } @@ -1180,9 +1176,9 @@ move_dir_dir (FileOpContext *ctx, char *s, char *d, long *progress_count, double mc_stat (s, &sbuf); if (mc_stat (d, &dbuf)) - destdir = copy_strings (d, 0); /* destination doesn't exist */ + destdir = g_strdup (d); /* destination doesn't exist */ else if (!ctx->dive_into_subdirs){ - destdir = copy_strings (d, 0); + destdir = g_strdup (d); move_over = 1; } else destdir = concat_dir_and_file (d, x_basename (s)); @@ -1205,7 +1201,7 @@ move_dir_dir (FileOpContext *ctx, char *s, char *d, long *progress_count, double if (return_status == FILE_RETRY) goto retry_dst_stat; } - free (destdir); + g_free (destdir); return return_status; } @@ -1251,17 +1247,17 @@ move_dir_dir (FileOpContext *ctx, char *s, char *d, long *progress_count, double return_status = erase_file (ctx, erase_list->name, 0, 0, 0); lp = erase_list; erase_list = erase_list->next; - free (lp); + g_free (lp); } } erase_dir_iff_empty (ctx, s); ret: - free (destdir); + g_free (destdir); for ( ; erase_list; ){ lp = erase_list; erase_list = erase_list->next; - free (lp); + g_free (lp); } return return_status; } @@ -1322,7 +1318,7 @@ recursive_erase (FileOpContext *ctx, char *s, long *progress_count, double *prog continue; path = concat_dir_and_file (s, next->d_name); if (mc_lstat (path, &buf)){ - free (path); + g_free (path); return 1; } if (S_ISDIR (buf.st_mode)) @@ -1330,11 +1326,11 @@ recursive_erase (FileOpContext *ctx, char *s, long *progress_count, double *prog != FILE_CONT); else return_status = erase_file (ctx, path, progress_count, progress_bytes, 0); - free (path); + g_free (path); /* .ado: OS/2 returns a block of memory DIR to next and must be freed */ #ifdef __os2__ if (!next) - free (next); + g_free (next); #endif } mc_closedir (reading); @@ -1525,7 +1521,7 @@ compute_dir_size (char *dirname, long *ret_marked, double *ret_total) res = mc_lstat (fullname, &s); if (res != 0){ - free (fullname); + g_free (fullname); continue; } @@ -1541,7 +1537,7 @@ compute_dir_size (char *dirname, long *ret_marked, double *ret_total) (*ret_marked)++; *ret_total += s.st_size; } - free (fullname); + g_free (fullname); } mc_closedir (dir); @@ -1581,7 +1577,7 @@ panel_compute_totals (WPanel *panel, long *ret_marked, double *ret_total) *ret_marked += subdir_count; *ret_total += subdir_bytes; - free (dir_name); + g_free (dir_name); } else { (*ret_marked)++; *ret_total += s->st_size; @@ -1631,7 +1627,7 @@ panel_operate_generate_prompt (char* cmd_buf, WPanel* panel, int operation, int { register char *sp, *cp; register int i; - char format_string [200]; + char format_string [BUF_MEDIUM]; char *dp = format_string; char* source = NULL; @@ -1708,11 +1704,11 @@ panel_operate_generate_prompt (char* cmd_buf, WPanel* panel, int operation, int if (only_one) { i = fmd_xlen - strlen(format_string) - 4; - sprintf (cmd_buf, format_string, name_trunc (source, i)); + g_snprintf (cmd_buf, sizeof (cmd_buf), format_string, name_trunc (source, i)); } else { - sprintf (cmd_buf, format_string, panel->marked); + g_snprintf (cmd_buf, sizeof (cmd_buf), format_string, panel->marked); i = strlen (cmd_buf) + 6 - fmd_xlen; if (i > 0) { @@ -1799,7 +1795,7 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in return 0; } } else if (operation != OP_DELETE){ - ctx->rx.buffer = (char *) xmalloc (MC_MAXPATHLEN, "mask copying"); + ctx->rx.buffer = (char *) g_malloc (MC_MAXPATHLEN); ctx->rx.allocated = MC_MAXPATHLEN; ctx->rx.translate = 0; @@ -1815,23 +1811,23 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in dest = file_mask_dialog (ctx, operation, cmd_buf, dest_dir, only_one, &do_bg); if (!dest){ - free (ctx->rx.buffer); + g_free (ctx->rx.buffer); file_op_context_destroy (ctx); return 0; } if (!*dest){ - free (ctx->rx.buffer); + g_free (ctx->rx.buffer); file_op_context_destroy (ctx); - free (dest); + g_free (dest); return 0; } } else { char *all = "^\\(.*\\)$"; re_compile_pattern (all, strlen (all), &ctx->rx); - ctx->dest_mask = strdup ("*"); + ctx->dest_mask = g_strdup ("*"); do_bg = FALSE; - dest = strdup (thedefault); + dest = g_strdup (thedefault); } } @@ -1840,7 +1836,7 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in if (do_bg){ int v; - v = do_background (ctx, copy_strings (_(op_names [operation]), ": ", panel->cwd, 0)); + v = do_background (ctx, g_strconcat (op_names [operation], ": ", panel->cwd, NULL)); if (v == -1){ message (1, MSG_ERROR, _(" Sorry, I could not put the job in background ")); } @@ -1861,11 +1857,11 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in invalid data. */ if (dest) { if (mc_setctl (dest, MCCTL_WANT_STALE_DATA, NULL)) - save_dest = strdup(dest); + save_dest = g_strdup (dest); } if (panel->cwd) { if (mc_setctl (panel->cwd, MCCTL_WANT_STALE_DATA, NULL)) - save_cwd = strdup(panel->cwd); + save_cwd = g_strdup (panel->cwd); } ftpfs_hint_reread (0); @@ -1897,8 +1893,8 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in if (temp == NULL){ value = transform_error; } else { - temp = get_full_name (dest, temp); - free (dest); + temp = concat_dir_and_file (dest, temp); + g_free (dest); dest = temp; temp = 0; @@ -1988,7 +1984,7 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in #ifdef WITH_FULL_PATHS if (source_with_path) - free (source_with_path); + g_free (source_with_path); source_with_path = concat_dir_and_file (panel->cwd, source); #endif @@ -1999,13 +1995,13 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in value = erase_file (ctx, source_with_path, &count, &bytes, 1); } else { if (temp) - free (temp); + g_free (temp); temp = transform_source (ctx, source_with_path); if (temp == NULL) value = transform_error; else { - temp = get_full_name (dest, temp); + temp = concat_dir_and_file (dest, temp); switch (operation){ case OP_COPY: @@ -2060,11 +2056,11 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in if (save_cwd) { mc_setctl (save_cwd, MCCTL_NO_STALE_DATA, NULL); - free(save_cwd); + g_free (save_cwd); } if (save_dest) { mc_setctl (save_dest, MCCTL_NO_STALE_DATA, NULL); - free(save_dest); + g_free (save_dest); } ftpfs_hint_reread (1); @@ -2072,22 +2068,22 @@ panel_operate (void *source_panel, FileOperation operation, char *thedefault, in free_linklist (&dest_dirs); #if WITH_FULL_PATHS if (source_with_path) - free (source_with_path); + g_free (source_with_path); #endif if (dest) - free (dest); + g_free (dest); if (temp) - free (temp); + g_free (temp); if (ctx->rx.buffer) { - free (ctx->rx.buffer); + g_free (ctx->rx.buffer); ctx->rx.buffer = NULL; } if (ctx->dest_mask) { - free (ctx->dest_mask); + g_free (ctx->dest_mask); ctx->dest_mask = NULL; } @@ -2136,7 +2132,7 @@ real_do_file_error (enum OperationMode mode, char *error) int file_error (char *format, char *file) { - sprintf (cmd_buf, format, + g_snprintf (cmd_buf, sizeof (cmd_buf), format, name_trunc (file, 30), unix_error_string (errno)); return do_file_error (cmd_buf); @@ -2152,7 +2148,8 @@ files_error (char *format, char *file1, char *file2) strcpy (nfile1, name_trunc (file1, 15)); strcpy (nfile2, name_trunc (file2, 15)); - sprintf (cmd_buf, format, nfile1, nfile2, unix_error_string (errno)); + g_snprintf (cmd_buf, sizeof (cmd_buf), format, nfile1, nfile2, + unix_error_string (errno)); return do_file_error (cmd_buf); } @@ -2166,7 +2163,7 @@ real_query_recursive (FileOpContext *ctx, enum OperationMode mode, char *s) char *msg = mode == Foreground ? _("\n Directory not empty. \n Delete it recursively? ") : _("\n Background process: Directory not empty \n Delete it recursively? "); - text = copy_strings (_(" Delete: "), name_trunc (s, 30), " ", 0); + text = g_strconcat (_(" Delete: "), name_trunc (s, 30), " ", NULL); if (know_not_what_am_i_doing) query_set_sel (1); @@ -2177,14 +2174,14 @@ real_query_recursive (FileOpContext *ctx, enum OperationMode mode, char *s) if (ctx->recursive_result != RECURSIVE_ABORT) do_refresh (); - free (text); + g_free (text); if (know_not_what_am_i_doing){ if (ctx->recursive_result == RECURSIVE_YES || ctx->recursive_result == RECURSIVE_ALWAYS){ - text = copy_strings ( + text = g_strconcat ( _(" Type 'yes' if you REALLY want to delete "), ctx->recursive_result == RECURSIVE_YES - ? name_trunc (s, 19) : _("all the directories "), " ", 0); + ? name_trunc (s, 19) : _("all the directories "), " ", NULL); confirm = input_dialog ( mode == Foreground ? _(" Recursive Delete ") : _(" Background process: Recursive Delete "), @@ -2192,8 +2189,8 @@ real_query_recursive (FileOpContext *ctx, enum OperationMode mode, char *s) do_refresh (); if (!confirm || strcmp (confirm, _("yes"))) ctx->recursive_result = RECURSIVE_NEVER; - free (confirm); - free (text); + g_free (confirm); + g_free (text); } } } diff --git a/src/filegui.c b/src/filegui.c index 57527937d..a0c1d88a7 100644 --- a/src/filegui.c +++ b/src/filegui.c @@ -70,7 +70,6 @@ #include #include "tty.h" #include -#include #include #ifdef HAVE_UNISTD_H # include @@ -83,22 +82,18 @@ #endif /* SCO_FLAVOR */ #include #include -#include -#include "mad.h" -#include "regex.h" -#include "setup.h" -#include "util.h" -#include "dialog.h" +#include "eregex.h" #include "global.h" +#include "setup.h" +#include "dialog.h" /* Needed by query_replace */ #include "color.h" #include "win.h" #include "dlg.h" -#include "widget.h" #define WANT_WIDGETS +#include "widget.h" /* Note the sequence */ #include "main.h" /* WANT_WIDGETS-> we get the the_hint def */ #include "layout.h" -#include "widget.h" #include "wtools.h" /* Needed for current_panel, other_panel and WTree */ @@ -351,7 +346,7 @@ static void file_eta_show (FileOpContext *ctx) { int eta_hours, eta_mins, eta_s; - char eta_buffer [30]; + char eta_buffer [BUF_TINY]; FileOpContextUI *ui; ui = ctx->ui; @@ -363,7 +358,7 @@ file_eta_show (FileOpContext *ctx) eta_hours = ctx->eta_secs / (60 * 60); eta_mins = (ctx->eta_secs - (eta_hours * 60 * 60)) / 60; eta_s = ctx->eta_secs - (eta_hours * 60 * 60 + eta_mins * 60); - sprintf (eta_buffer, "ETA %d:%02d.%02d", eta_hours, eta_mins, eta_s); + g_snprintf (eta_buffer, sizeof (eta_buffer), "ETA %d:%02d.%02d", eta_hours, eta_mins, eta_s); } else *eta_buffer = 0; @@ -373,7 +368,7 @@ file_eta_show (FileOpContext *ctx) static void file_bps_show (FileOpContext *ctx) { - char bps_buffer [30]; + char bps_buffer [BUF_TINY]; FileOpContextUI *ui; ui = ctx->ui; @@ -382,11 +377,11 @@ file_bps_show (FileOpContext *ctx) return; if (ctx->bps > 1024*1024) { - sprintf (bps_buffer, "%.2f MB/s", ctx->bps / (1024*1024.0)); + g_snprintf (bps_buffer, sizeof (bps_buffer), "%.2f MB/s", ctx->bps / (1024*1024.0)); } else if (ctx->bps > 1024){ - sprintf (bps_buffer, "%.2f KB/s", ctx->bps / 1024.0); + g_snprintf (bps_buffer, sizeof (bps_buffer), "%.2f KB/s", ctx->bps / 1024.0); } else if (ctx->bps > 1){ - sprintf (bps_buffer, "%ld B/s", ctx->bps); + g_snprintf (bps_buffer, sizeof (bps_buffer), "%ld B/s", ctx->bps); } else *bps_buffer = 0; @@ -591,7 +586,7 @@ rd_widgets [] = rd_widgets [i].layout) #define ADD_RD_LABEL(ui,i,p1,p2)\ - sprintf (buffer, rd_widgets [i].text, p1, p2);\ + g_snprintf (buffer, sizeof (buffer), rd_widgets [i].text, p1, p2);\ add_widgetl (ui->replace_dlg,\ label_new (rd_widgets [i].ypos, rd_widgets [i].xpos, buffer, rd_widgets [i].tkname),\ rd_widgets [i].layout) @@ -600,7 +595,7 @@ static void init_replace (FileOpContext *ctx, enum OperationMode mode) { FileOpContextUI *ui; - char buffer [128]; + char buffer [BUF_SMALL]; static int rd_xlen = 60, rd_trunc = X_TRUNC; #ifdef ENABLE_NLS @@ -957,7 +952,7 @@ ask_file_mask: orig_mask = source_mask; if (!dest_dir || !*dest_dir){ if (source_mask) - free (source_mask); + g_free (source_mask); return dest_dir; } if (source_easy_patterns){ @@ -966,7 +961,7 @@ ask_file_mask: source_mask = convert_pattern (source_mask, match_file, 1); easy_patterns = source_easy_patterns; error = re_compile_pattern (source_mask, strlen (source_mask), &ctx->rx); - free (source_mask); + g_free (source_mask); } else error = re_compile_pattern (source_mask, strlen (source_mask), &ctx->rx); @@ -974,11 +969,11 @@ ask_file_mask: message_3s (1, MSG_ERROR, _("Invalid source pattern `%s' \n %s "), orig_mask, error); if (orig_mask) - free (orig_mask); + g_free (orig_mask); goto ask_file_mask; } if (orig_mask) - free (orig_mask); + g_free (orig_mask); ctx->dest_mask = strrchr (dest_dir, PATH_SEP); if (ctx->dest_mask == NULL) ctx->dest_mask = dest_dir; @@ -990,14 +985,14 @@ ask_file_mask: (ctx->dive_into_subdirs && ((!only_one && !is_wildcarded (ctx->dest_mask)) || (only_one && !mc_stat (dest_dir, &buf) && S_ISDIR (buf.st_mode))))) - ctx->dest_mask = strdup ("*"); + ctx->dest_mask = g_strdup ("*"); else { - ctx->dest_mask = strdup (ctx->dest_mask); + ctx->dest_mask = g_strdup (ctx->dest_mask); *orig_mask = 0; } if (!*dest_dir){ - free (dest_dir); - dest_dir = strdup ("./"); + g_free (dest_dir); + dest_dir = g_strdup ("./"); } if (val == B_USER) *do_background = 1; diff --git a/src/filenot.c b/src/filenot.c index 56f5db35e..548282bdf 100644 --- a/src/filenot.c +++ b/src/filenot.c @@ -10,10 +10,8 @@ #include #include -#include -#include "util.h" -#include "mad.h" #include +#include "global.h" #include "../vfs/vfs.h" static char *get_absolute_name (char *file) @@ -21,9 +19,9 @@ static char *get_absolute_name (char *file) char dir [MC_MAXPATHLEN]; if (file [0] == PATH_SEP) - return strdup (file); + return g_strdup (file); mc_get_current_wd (dir, MC_MAXPATHLEN); - return get_full_name (dir, file); + return concat_dir_and_file (dir, file); } static int @@ -48,12 +46,12 @@ my_mkdir_rec (char *s, mode_t mode) p = concat_dir_and_file (s, ".."); q = vfs_canon (p); - free (p); + g_free (p); if (!(result = my_mkdir_rec (q, mode))) result = mc_mkdir (s, mode); - free (q); + g_free (q); return result; } @@ -72,7 +70,7 @@ my_mkdir (char *s, mode_t mode) char *p = vfs_canon (s); result = my_mkdir_rec (p, mode); - free (p); + g_free (p); } if (result == 0){ s = get_absolute_name (s); @@ -81,7 +79,7 @@ my_mkdir (char *s, mode_t mode) tree_add_entry (tree, s); #endif - free (s); + g_free (s); } return result; } @@ -102,7 +100,7 @@ int my_rmdir (char *s) tree_remove_entry (tree, s); #endif - free (s); + g_free (s); } return result; } diff --git a/src/fileopctx.h b/src/fileopctx.h index 2c45b5cb9..b86f1f05e 100644 --- a/src/fileopctx.h +++ b/src/fileopctx.h @@ -11,7 +11,7 @@ #include #include -#include "regex.h" +#include "eregex.h" /* This structure describes a context for file operations. It is used to update diff --git a/src/find.c b/src/find.c index bb8705cd1..ef179417c 100644 --- a/src/find.c +++ b/src/find.c @@ -25,16 +25,11 @@ #ifdef NEEDS_IO_H # include #endif -#include "fs.h" -#include /* For free() */ #include #include -#include #include #include #include "global.h" -#include "mad.h" -#include "util.h" #include "win.h" #include "color.h" #include "global.h" @@ -196,11 +191,11 @@ find_parameters (char **start_dir, char **pattern, char **content) find_par_start: if (!in_start_dir) - in_start_dir = strdup ("."); + in_start_dir = g_strdup ("."); if (!in_start_name) - in_start_name = strdup (easy_patterns ? "*" : "."); + in_start_name = g_strdup (easy_patterns ? "*" : "."); if (!in_contents) - in_contents = strdup (""); + in_contents = g_strdup (""); find_dlg = create_dlg (0, 0, FIND_Y, FIND_X, dialog_colors, common_dialog_callback, "[Find File]", "findfile", @@ -238,16 +233,16 @@ find_par_start: #ifndef HAVE_GNOME case B_TREE: - temp_dir = strdup (in_start->buffer); + temp_dir = g_strdup (in_start->buffer); destroy_dlg (find_dlg); - free (in_start_dir); + g_free (in_start_dir); if (strcmp (temp_dir, ".") == 0){ - free (temp_dir); - temp_dir = strdup (cpanel->cwd); + g_free (temp_dir); + temp_dir = g_strdup (cpanel->cwd); } in_start_dir = tree (temp_dir); if (in_start_dir) - free (temp_dir); + g_free (temp_dir); else in_start_dir = temp_dir; /* Warning: Dreadful goto */ @@ -257,20 +252,20 @@ find_par_start: default: return_value = 1; - *start_dir = strdup (in_start->buffer); - *pattern = strdup (in_name->buffer); + *start_dir = g_strdup (in_start->buffer); + *pattern = g_strdup (in_name->buffer); - free (in_contents); + g_free (in_contents); if (in_with->buffer [0]){ - *content = strdup (in_with->buffer); - in_contents = strdup (*content); + *content = g_strdup (in_with->buffer); + in_contents = g_strdup (*content); } else *content = in_contents = NULL; - free (in_start_dir); - in_start_dir = strdup (*start_dir); - free (in_start_name); - in_start_name = strdup (*pattern); + g_free (in_start_dir); + in_start_dir = g_strdup (*start_dir); + g_free (in_start_name); + in_start_name = g_strdup (*pattern); } destroy_dlg (find_dlg); @@ -283,8 +278,8 @@ push_directory (char *dir) { dir_stack *new; - new = xmalloc (sizeof (dir_stack), "find: push_directory"); - new->name = strdup (dir); + new = g_new (dir_stack, 1); + new->name = g_strdup (dir); new->prev = dir_stack_base; dir_stack_base = new; } @@ -298,7 +293,7 @@ pop_directory (void) if (dir_stack_base){ name = dir_stack_base->name; next = dir_stack_base->prev; - free (dir_stack_base); + g_free (dir_stack_base); dir_stack_base = next; return name; } else @@ -324,18 +319,18 @@ insert_file (char *dir, char *file) if (old_dir){ if (strcmp (old_dir, dir)){ - free (old_dir); - old_dir = strdup (dir); + g_free (old_dir); + old_dir = g_strdup (dir); dirname = listbox_add_item (find_list, 0, 0, dir, 0); } } else { - old_dir = strdup (dir); + old_dir = g_strdup (dir); dirname = listbox_add_item (find_list, 0, 0, dir, 0); } - tmp_name = copy_strings (" ", file, 0); + tmp_name = g_strconcat (" ", file, NULL); listbox_add_item (find_list, 0, 0, tmp_name, dirname); - free (tmp_name); + g_free (tmp_name); } static void @@ -393,7 +388,7 @@ static void search_content (Dlg_head *h, char *directory, char *filename) { struct stat s; - char buffer [128]; + char buffer [BUF_SMALL]; char *fname, *p; int file_fd, pipe, ignoring; char c; @@ -401,19 +396,19 @@ search_content (Dlg_head *h, char *directory, char *filename) pid_t pid; static char *egrep_path; - fname = get_full_name (directory, filename); + fname = concat_dir_and_file (directory, filename); if (mc_stat (fname, &s) != 0 && !S_ISREG (s.st_mode)){ - free (fname); + g_free (fname); return; } if (!S_ISREG (s.st_mode)){ - free (fname); + g_free (fname); return; } file_fd = mc_open (fname, O_RDONLY); - free (fname); + g_free (fname); if (file_fd == -1) return; @@ -432,7 +427,7 @@ search_content (Dlg_head *h, char *directory, char *filename) return; } - sprintf (buffer, _("Grepping in %s"), name_trunc (filename, FIND2_X_USE)); + g_snprintf (buffer, sizeof (buffer), _("Grepping in %s"), name_trunc (filename, FIND2_X_USE)); label_set_text (status_label, buffer); mc_refresh (); @@ -458,9 +453,9 @@ search_content (Dlg_head *h, char *directory, char *filename) *p = 0; ignoring = 1; - the_name = copy_strings (buffer, ":", filename, NULL); + the_name = g_strconcat (buffer, ":", filename, NULL); find_add_match (h, directory, the_name); - free (the_name); + g_free (the_name); } else { if (p - buffer < (sizeof (buffer)-1) && ISASCII (c) && isdigit (c)) *p++ = c; @@ -523,12 +518,12 @@ do_search (struct Dlg_head *h) } if (find_ignore_dirs){ int found; - char *temp_dir = copy_strings (":", tmp, ":", 0); + char *temp_dir = g_strconcat (":", tmp, ":", NULL); found = strstr (find_ignore_dirs, temp_dir) != 0; - free (temp_dir); + g_free (temp_dir); if (found) - free (tmp); + g_free (tmp); else break; } else @@ -536,12 +531,12 @@ do_search (struct Dlg_head *h) } strcpy (directory, tmp); - free (tmp); + g_free (tmp); if (verbose){ - char buffer [50]; + char buffer [BUF_SMALL]; - sprintf (buffer, _("Searching %s"), name_trunc (directory, FIND2_X_USE)); + g_snprintf (buffer, sizeof (buffer), _("Searching %s"), name_trunc (directory, FIND2_X_USE)); label_set_text (status_label, buffer); } dirp = mc_opendir (directory); @@ -561,7 +556,7 @@ do_search (struct Dlg_head *h) return; } - tmp_name = get_full_name (directory, dp->d_name); + tmp_name = concat_dir_and_file (directory, dp->d_name); if (subdirs_left){ mc_lstat (tmp_name, &tmp_stat); @@ -578,7 +573,7 @@ do_search (struct Dlg_head *h) find_add_match (h, directory, dp->d_name); } - free (tmp_name); + g_free (tmp_name); dp = mc_readdir (dirp); /* Displays the nice dot */ @@ -624,17 +619,17 @@ view_edit_currently_selected_file (int unparsed_view, int edit) line = 0; } if (dir [0] == '.' && dir [1] == 0) - fullname = strdup (filename); + fullname = g_strdup (filename); else if (dir [0] == '.' && dir [1] == PATH_SEP) - fullname = get_full_name (dir+2, filename); + fullname = concat_dir_and_file (dir+2, filename); else - fullname = get_full_name (dir, filename); + fullname = concat_dir_and_file (dir, filename); if (edit) do_edit_at_line (fullname, line); else view_file_at_line (fullname, unparsed_view, use_internal_view, line); - free (fullname); + g_free (fullname); return MSG_HANDLED; } @@ -701,7 +696,7 @@ init_find_vars (void) char *dir; if (old_dir){ - free (old_dir); + g_free (old_dir); old_dir = 0; } count = 0; @@ -709,7 +704,7 @@ init_find_vars (void) /* Remove all the items in the stack */ while ((dir = pop_directory ()) != NULL) - free (dir); + g_free (dir); } static int @@ -810,14 +805,14 @@ find_file (char *start_dir, char *pattern, char *content, char **dirname, char /* Remove all the items in the stack */ while ((dir = pop_directory ()) != NULL) - free (dir); + g_free (dir); listbox_get_current (find_list, &file_tmp, &dir_tmp); if (dir_tmp) - *dirname = strdup (dir_tmp); + *dirname = g_strdup (dir_tmp); if (file_tmp) - *filename = strdup (file_tmp); + *filename = g_strdup (file_tmp); if (return_value == B_PANELIZE && *filename){ int status, link_to_dir, stalled_link; int next_free = 0; @@ -839,26 +834,26 @@ find_file (char *start_dir, char *pattern, char *content, char **dirname, char continue; dir = entry->data; if (dir [0] == '.' && dir [1] == 0) - name = strdup (filename); + name = g_strdup (filename); else if (dir [0] == '.' && dir [1] == PATH_SEP) - name = get_full_name (dir + 2, filename); + name = concat_dir_and_file (dir + 2, filename); else - name = get_full_name (dir, filename); + name = concat_dir_and_file (dir, filename); status = handle_path (list, name, &buf, next_free, &link_to_dir, &stalled_link); if (status == 0) { - free (name); + g_free (name); continue; } if (status == -1) { - free (name); + g_free (name); break; } /* don't add files more than once to the panel */ if (content_pattern && next_free > 0){ if (strcmp (list->list [next_free-1].fname, name) == 0) { - free (name); + g_free (name); continue; } } @@ -896,7 +891,7 @@ find_file (char *start_dir, char *pattern, char *content, char **dirname, char destroy_dlg (find_dlg); do_search (0); /* force do_search to release resources */ if (old_dir){ - free (old_dir); + g_free (old_dir); old_dir = 0; } return return_value; @@ -917,8 +912,8 @@ do_find (void) dirname = filename = NULL; is_start = 0; v = find_file (start_dir, pattern, content, &dirname, &filename); - free (start_dir); - free (pattern); + g_free (start_dir); + g_free (pattern); if (v == B_ENTER){ if (dirname || filename){ @@ -933,16 +928,16 @@ do_find (void) select_item (cpanel); } if (dirname) - free (dirname); + g_free (dirname); if (filename) - free (filename); + g_free (filename); break; } if (content) - free (content); + g_free (content); dir_and_file_set = dirname && filename; - if (dirname) free (dirname); - if (filename) free (filename); + if (dirname) g_free (dirname); + if (filename) g_free (filename); if (v == B_CANCEL) break; diff --git a/src/fixhlp.c b/src/fixhlp.c index db3fa65d4..d3fcf1d24 100644 --- a/src/fixhlp.c +++ b/src/fixhlp.c @@ -23,8 +23,6 @@ #include #include "help.h" -#define BUFFER_SIZE 256 - static int width; /* Output width in characters */ static int col = 0; /* Current output column */ static FILE *toc_file; /* TOC file */ @@ -152,7 +150,7 @@ void print_string (char *buffer) void printf_string (char *format, ...) { va_list args; - char buffer [BUFFER_SIZE]; + char buffer [BUF_LARGE]; va_start (args, format); vsprintf (buffer, format, args); @@ -163,7 +161,7 @@ void printf_string (char *format, ...) int main (int argc, char **argv) { int len; /* Length of input line */ - char buffer [BUFFER_SIZE]; /* Input line */ + char buffer [BUF_LARGE]; /* Input line */ int i, j; char *p; int ignore_newline = 0; @@ -183,7 +181,7 @@ int main (int argc, char **argv) /* Repeat for each input line */ while (!feof (stdin)){ /* Read a line */ - if (!fgets (buffer, BUFFER_SIZE, stdin)){ + if (!fgets (buffer, sizeof(buffer), stdin)){ break; } in_row ++; diff --git a/src/global.h b/src/global.h index c91d5a54a..31ed1aa9b 100644 --- a/src/global.h +++ b/src/global.h @@ -1,6 +1,13 @@ #ifndef __GLOBAL_H #define __GLOBAL_H +#include /* for free() and other usefull routins */ +#include "fs.h" +#include +#include "mem.h" +#include "util.h" +#include "mad.h" + extern char *home_dir; #ifdef min @@ -11,15 +18,22 @@ extern char *home_dir; #undef max #endif -#define min(x,y) (x>y ? y: x) -#define max(x,y) (x>y ? x: y) +#define min(x, y) ((x) > (y) ? (y) : (x)) +#define max(x, y) ((x) > (y) ? (x) : (y)) + +/* Just for keeping Your's brains from invention a proper size of the buffer :-) */ +#define BUF_10K 10240L +#define BUF_8K 8192L +#define BUF_4K 4096L +#define BUF_1K 1024L + +#define BUF_LARGE BUF_1K +#define BUF_MEDIUM 512 +#define BUF_SMALL 128 +#define BUF_TINY 64 void refresh_screen (void *); -#ifndef HAVE_STRDUP -char *strdup (const char *); -#endif - /* AIX compiler doesn't understand '\e' */ #define ESC_CHAR '\033' #define ESC_STR "\033" diff --git a/src/help.c b/src/help.c index f70fe5bd2..20923f073 100644 --- a/src/help.c +++ b/src/help.c @@ -41,14 +41,11 @@ #include #include #include -#include #include -#include "mad.h" +#include "global.h" #include "color.h" -#include "util.h" #include "dialog.h" #include "win.h" -#include "global.h" #include "mouse.h" #include "key.h" /* For mi_getch() */ #include "help.h" @@ -321,7 +318,7 @@ static void start_link_area (int x, int y, char *link_name) message (0, _(" Warning "), _(" Internal bug: Double start of link area ")); /* Allocate memory for a new link area */ - new = (Link_Area*) xmalloc (sizeof (Link_Area), "Help, link_area"); + new = g_new (Link_Area, 1); new->next = link_area; link_area = new; @@ -353,7 +350,7 @@ static void clear_link_areas (void) while (link_area){ current = link_area; link_area = current -> next; - free (current); + g_free (current); } inside_link_area = 0; } @@ -602,7 +599,7 @@ static int md_callback (Dlg_head *h, Widget *w, int msg, int par) static Widget *mousedispatch_new (int y, int x, int yl, int xl) { - Widget *w = xmalloc (sizeof (Widget), "disp_new"); + Widget *w = g_new (Widget, 1); init_widget (w, y, x, yl, xl, (callback_fn) md_callback, 0, (mouse_h) help_event, NULL); @@ -756,7 +753,7 @@ static void interactive_display_finish (void) { clear_link_areas (); - free (data); + g_free (data); } void diff --git a/src/hotlist.c b/src/hotlist.c index c7b4ac517..69fb4f8ea 100644 --- a/src/hotlist.c +++ b/src/hotlist.c @@ -32,7 +32,6 @@ #endif #include #include -#include /* For malloc() */ #include #include #include @@ -49,8 +48,7 @@ # include #endif #include "tty.h" -#include "mad.h" -#include "util.h" /* Needed for the externs */ +#include "global.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -66,7 +64,6 @@ #include "panel.h" /* Needed for the externs */ #include "file.h" #include "main.h" -#include "global.h" #include "hotlist.h" #include "key.h" #include "command.h" @@ -163,7 +160,7 @@ static struct hotlist *new_hotlist (void) { struct hotlist *hl; - hl = xmalloc (sizeof (struct hotlist), "new-hotlist"); + hl = g_new (struct hotlist, 1); hl->type = 0; hl->directory = hl->label = 0; @@ -204,12 +201,12 @@ static INLINE void update_path_name () text = _("Subgroup - press ENTER to see list"); #ifndef HAVE_X - p = copy_strings (" ", current_group->label, " ", (char *)0); + p = g_strconcat (" ", current_group->label, " ", NULL); if (!hotlist_state.moving) label_set_text (pname_group, name_trunc (p, dlg->cols - (UX*2+4))); else label_set_text (movelist_group, name_trunc (p, dlg->cols - (UX*2+4))); - free (p); + g_free (p); #endif } else { text = list->current->text; @@ -227,8 +224,8 @@ do { \ int i; \ \ if ((i = strlen (current->label) + 3) > buflen) { \ - free (buf); \ - buf = xmalloc (buflen = 1024 * (i/1024 + 1), "fill_listbox"); \ + g_free (buf); \ + buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \ } \ buf[0] = '\0'; \ } while (0) @@ -240,7 +237,7 @@ static void fill_listbox (void) static int buflen; if (!buf) - buf = xmalloc (buflen = 1024, "fill_listbox"); + buf = g_malloc (buflen = 1024); buf[0] = '\0'; while (current){ @@ -456,9 +453,9 @@ l1: if (l_hotlist->current->data) { struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data; if (hlp->type == HL_TYPE_ENTRY) { - char *tmp = copy_strings( "cd ", hlp->directory, NULL); + char *tmp = g_strconcat ( "cd ", hlp->directory, NULL); stuff (input_w (cmdline), tmp, 0); - free (tmp); + g_free (tmp); dlg_stop (h); h->ret_value = B_CANCEL; return 1; @@ -663,7 +660,7 @@ static void init_hotlist (int list_type) static void init_movelist (int list_type, struct hotlist *item) { int i; - char *hdr = copy_strings (_("Moving "), item->label, 0); + char *hdr = g_strconcat (_("Moving "), item->label, NULL); int movelist_cols = init_i18n_stuff (list_type, COLS - 6); do_refresh (); @@ -673,7 +670,7 @@ static void init_movelist (int list_type, struct hotlist *item) "movelist", DLG_CENTER|DLG_GRID); x_set_dialog_title (movelist_dlg, hdr); - free (hdr); + g_free (hdr); #define XTRACT(i) BY-4+hotlist_but[i].y, BX+hotlist_but[i].x, hotlist_but[i].ret_cmd, hotlist_but[i].flags, hotlist_but[i].text, hotlist_button_callback, 0, hotlist_but[i].tkname @@ -714,7 +711,7 @@ static void hotlist_done (void) static char * find_group_section (struct hotlist *grp) { - return copy_strings (grp->directory, ".Group", (char *)0); + return g_strconcat (grp->directory, ".Group", NULL); } @@ -768,10 +765,10 @@ add2hotlist (char *label, char *directory, enum HotListType type, int pos) if (hotlist_state.running && type != HL_TYPE_COMMENT) { if (type == HL_TYPE_GROUP) { - char *lbl = copy_strings ("->", new->label, (char *)0); + char *lbl = g_strconcat ("->", new->label, NULL); listbox_add_item (l_hotlist, pos, 0, lbl, new); - free (lbl); + g_free (lbl); } else listbox_add_item (l_hotlist, pos, 0, new->label, new); listbox_select_entry (l_hotlist, l_hotlist->current); @@ -891,9 +888,9 @@ static void add_new_entry_cmd (void) return; if (ret == B_ENTER || ret == B_APPEND) - add2hotlist (strdup (title), strdup (url), HL_TYPE_ENTRY, 2); + add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 2); else - add2hotlist (strdup (title), strdup (url), HL_TYPE_ENTRY, 1); + add2hotlist (g_strdup (title),g_strdup (url), HL_TYPE_ENTRY, 1); hotlist_state.modified = 1; } @@ -979,17 +976,16 @@ void add_new_group_cmd (void) void add2hotlist_cmd (void) { char *prompt, *label; - char* cp = _("Label for \"%s\":"); - int l = strlen(cp); + char *cp = _("Label for \"%s\":"); + int l = strlen (cp); - prompt = xmalloc (strlen (cpanel->cwd) + l, "add2hotlist_cmd"); - sprintf (prompt, cp, name_trunc (cpanel->cwd, COLS-2*UX-(l+8))); + prompt = g_strdup_printf (cp, name_trunc (cpanel->cwd, COLS-2*UX-(l+8))); label = input_dialog (_(" Add to hotlist "), prompt, cpanel->cwd); - free (prompt); + g_free (prompt); if (!label || !*label) return; - add2hotlist (label, strdup (cpanel->cwd), HL_TYPE_ENTRY, 0); + add2hotlist (label,g_strdup (cpanel->cwd), HL_TYPE_ENTRY, 0); hotlist_state.modified = 1; } @@ -1004,10 +1000,10 @@ static void remove_group (struct hotlist *grp) remove_group (current); if (current->label) - free (current->label); + g_free (current->label); if (current->directory) - free (current->directory); - free (current); + g_free (current->directory); + g_free (current); current = next; } @@ -1021,14 +1017,14 @@ static void remove_from_hotlist (struct hotlist *entry) char *header; int result; - header = copy_strings (_(" Remove: "), + header = g_strconcat (_(" Remove: "), name_trunc (entry->label, 30), " ", - 0); + NULL); result = query_dialog (header, _("\n Group not empty.\n Remove it?"), D_ERROR, 2, _("&No"), _("&Yes")); - free (header); + g_free (header); if (!result) return; @@ -1040,10 +1036,10 @@ static void remove_from_hotlist (struct hotlist *entry) unlink_entry (entry); if (entry->label) - free (entry->label); + g_free (entry->label); if (entry->directory) - free (entry->directory); - free (entry); + g_free (entry->directory); + g_free (entry); /* now remove list entry from screen */ listbox_remove_current (l_hotlist, 1); hotlist_state.modified = 1; @@ -1073,9 +1069,9 @@ char *hotlist_cmd (int vfs_or_hotlist) case B_ENTER: if (l_hotlist->current->data) { struct hotlist *hlp = (struct hotlist*) l_hotlist->current->data; - target = strdup (hlp->directory); + target = g_strdup (hlp->directory); } else - target = strdup (l_hotlist->current->text); + target = g_strdup (l_hotlist->current->text); break; } @@ -1099,15 +1095,15 @@ load_group (struct hotlist *grp) while (profile_keys){ profile_keys = profile_iterator_next (profile_keys, &key, &value); - add2hotlist (strdup (value), strdup (key), HL_TYPE_GROUP, 0); + add2hotlist (g_strdup (value), g_strdup (key), HL_TYPE_GROUP, 0); } - free (group_section); + g_free (group_section); profile_keys = profile_init_iterator (grp->directory, profile_name); while (profile_keys){ profile_keys = profile_iterator_next (profile_keys, &key, &value); - add2hotlist (strdup (value), strdup (key), HL_TYPE_ENTRY, 0); + add2hotlist (g_strdup (value),g_strdup (key), HL_TYPE_ENTRY, 0); } for (current = grp->head; current; current = current->next) @@ -1149,8 +1145,8 @@ static int hot_next_token () #define CHECK_BUF() \ do { \ if (tkn_length == tkn_buf_length) \ - tkn_buf = tkn_buf ? (realloc (tkn_buf, tkn_buf_length += 1024)) \ - : (malloc (tkn_buf_length = 1024)); \ + tkn_buf = tkn_buf ? ( g_realloc (tkn_buf, tkn_buf_length += 1024)) \ + : ( g_malloc (tkn_buf_length = 1024)); \ } while (0) tkn_length = 0; @@ -1249,22 +1245,22 @@ hot_load_group (struct hotlist * grp) switch (tkn) { case TKN_GROUP: CHECK_TOKEN(TKN_STRING); - new_grp = add2hotlist (strdup (tkn_buf), 0, HL_TYPE_GROUP, 0); + new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0); SKIP_TO_EOL; hot_load_group (new_grp); current_group = grp; break; case TKN_ENTRY: CHECK_TOKEN(TKN_STRING); - label = strdup (tkn_buf); + label = g_strdup (tkn_buf); CHECK_TOKEN(TKN_URL); CHECK_TOKEN(TKN_STRING); - url = strdup (tkn_buf); + url = g_strdup (tkn_buf); add2hotlist (label, url, HL_TYPE_ENTRY, 0); SKIP_TO_EOL; break; case TKN_COMMENT: - label = strdup (tkn_buf); + label = g_strdup (tkn_buf); add2hotlist (label, 0, HL_TYPE_COMMENT, 0); break; case TKN_EOF: @@ -1297,22 +1293,22 @@ hot_load_file (struct hotlist * grp) switch (tkn) { case TKN_GROUP: CHECK_TOKEN(TKN_STRING); - new_grp = add2hotlist (strdup (tkn_buf), 0, HL_TYPE_GROUP, 0); + new_grp = add2hotlist (g_strdup (tkn_buf), 0, HL_TYPE_GROUP, 0); SKIP_TO_EOL; hot_load_group (new_grp); current_group = grp; break; case TKN_ENTRY: CHECK_TOKEN(TKN_STRING); - label = strdup (tkn_buf); + label = g_strdup (tkn_buf); CHECK_TOKEN(TKN_URL); CHECK_TOKEN(TKN_STRING); - url = strdup (tkn_buf); + url = g_strdup (tkn_buf); add2hotlist (label, url, HL_TYPE_ENTRY, 0); SKIP_TO_EOL; break; case TKN_COMMENT: - label = strdup (tkn_buf); + label = g_strdup (tkn_buf); add2hotlist (label, 0, HL_TYPE_COMMENT, 0); break; case TKN_EOL: @@ -1333,7 +1329,7 @@ clean_up_hotlist_groups (char *section) void *profile_keys; char *key, *value; - grp_section = copy_strings (section, ".Group", (char *)0); + grp_section = g_strconcat (section, ".Group", NULL); if (profile_has_section (section, profile_name)) profile_clean_section (section, profile_name); if (profile_has_section (grp_section, profile_name)) { @@ -1345,7 +1341,7 @@ clean_up_hotlist_groups (char *section) } profile_clean_section (grp_section, profile_name); } - free (grp_section); + g_free (grp_section); } @@ -1370,30 +1366,30 @@ void load_hotlist (void) hotlist = new_hotlist (); hotlist->type = HL_TYPE_GROUP; - hotlist->label = strdup (_(" Top level group ")); + hotlist->label = g_strdup (_(" Top level group ")); hotlist->up = hotlist; /* * compatibility :-( */ - hotlist->directory = strdup ("Hotlist"); + hotlist->directory = g_strdup ("Hotlist"); - grp_section = copy_strings ("Hotlist", ".Group", (char *)0); + grp_section = g_strconcat ("Hotlist", ".Group", NULL); has_old_list = profile_has_section ("Hotlist", profile_name) || profile_has_section (grp_section, profile_name); - free (grp_section); + g_free (grp_section); if ((hotlist_file = fopen (hotlist_file_name, "r")) == 0) { int result; char *msg; - msg = copy_strings (_("Hotlist is now kept in file ~/"), + msg = g_strconcat (_("Hotlist is now kept in file ~/"), HOTLIST_FILENAME, "\n", _("MC will load hotlist from ~/"), PROFILE_NAME, "\n", _("and then delete [Hotlist] section there"), NULL); message (0, _(" Hotlist Load "), msg); - free (msg); + g_free (msg); load_group (hotlist); hotlist_state.loaded = 1; @@ -1408,11 +1404,11 @@ void load_hotlist (void) } else { char *msg; - msg = copy_strings (_("MC was unable to write ~/"), HOTLIST_FILENAME, + msg = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME, _(" file, your old hotlist entries were not deleted"), NULL); message (D_ERROR, _(" Hotlist Load "), msg); - free (msg); + g_free (msg); } } else { hot_load_file (hotlist); @@ -1422,7 +1418,7 @@ void load_hotlist (void) int result; char *msg; - msg = copy_strings ( + msg = g_strconcat ( _("You have ~/"), HOTLIST_FILENAME, _(" file and [Hotlist] section in ~/"), PROFILE_NAME, "\n", _("Your ~/"), HOTLIST_FILENAME, _(" most probably was created\n"), _("by an earlier development version of MC\nand is more actual than ~/"), @@ -1447,7 +1443,7 @@ void load_hotlist (void) old = new_hotlist (); old->type = HL_TYPE_GROUP; - old->label = copy_strings (_(" Entries from ~/"), PROFILE_NAME, NULL); + old->label = g_strconcat (_(" Entries from ~/"), PROFILE_NAME, NULL); old->up = hotlist; old->head = hotlist->head; old->next = grp; @@ -1456,11 +1452,11 @@ void load_hotlist (void) if (!save_hotlist ()){ char *str; - str = copy_strings (_("MC was unable to write ~/"), HOTLIST_FILENAME, + str = g_strconcat (_("MC was unable to write ~/"), HOTLIST_FILENAME, _(" file your old hotlist entries were not deleted"), NULL); message (D_ERROR, _(" Hotlist Load "), str); - free (str); + g_free (str); } else remove_old_list = 1; hotlist_state.modified = 0; @@ -1493,7 +1489,7 @@ save_group (struct hotlist *grp) current->label, profile_name); } - free (group_section); + g_free (group_section); for (current = grp->head; current && current->type == HL_TYPE_GROUP; @@ -1576,7 +1572,7 @@ int save_hotlist (void) struct stat stat_buf; if (!hotlist_state.readonly && hotlist_state.modified && hotlist_file_name) { - char *fbak = copy_strings (hotlist_file_name, ".bak", 0); + char *fbak = g_strconcat (hotlist_file_name, ".bak", NULL); rename (hotlist_file_name, fbak); if ((hotlist_file = fopen (hotlist_file_name, "w")) != 0) { @@ -1593,7 +1589,7 @@ int save_hotlist (void) hotlist_state.modified = 0; } else rename (fbak, hotlist_file_name); - free (fbak); + g_free (fbak); } return saved; @@ -1607,20 +1603,20 @@ void done_hotlist (void) if (hotlist){ if (hotlist->label) - free (hotlist->label); + g_free (hotlist->label); if (hotlist->directory) - free (hotlist->directory); - free (hotlist); + g_free (hotlist->directory); + g_free (hotlist); } if (hotlist_file_name) - free (hotlist_file_name); + g_free (hotlist_file_name); hotlist_file_name = 0; hotlist = current_group = 0; l_hotlist = 0; current_group = 0; if (tkn_buf) - free (tkn_buf); + g_free (tkn_buf); } diff --git a/src/info.c b/src/info.c index e4fd01f82..12d10cc1c 100644 --- a/src/info.c +++ b/src/info.c @@ -22,9 +22,7 @@ #include #include #include -#include -#include "mad.h" -#include "util.h" /* statfs calls */ +#include "global.h" #include "mouse.h" /* Gpm_Event */ #include "color.h" #include "dlg.h" @@ -260,7 +258,7 @@ static int info_callback (Dlg_head *h, WInfo *info, int msg, int par) WInfo *info_new () { - WInfo *info = xmalloc (sizeof (WInfo), "panel_info"); + WInfo *info = g_new (WInfo, 1); init_widget (&info->widget, 0, 0, 0, 0, (callback_fn) info_callback, (destroy_fn) info_destroy, diff --git a/src/key.c b/src/key.c index 007b388c7..78d185fc9 100644 --- a/src/key.c +++ b/src/key.c @@ -23,7 +23,6 @@ #include #include -#include #include #include #ifdef HAVE_UNISTD_H @@ -37,9 +36,6 @@ #include "tty.h" #include #include -#include -#include "util.h" /* For xmalloc prototype */ -#include "mad.h" /* The memory debugger */ #include "global.h" #include "mouse.h" #include "key.h" @@ -107,7 +103,7 @@ void add_select_channel (int fd, select_fn callback, void *info) { SelectList *new; - new = xmalloc (sizeof (SelectList), "add_select_channel"); + new = g_new (SelectList, 1); new->fd = fd; new->callback = callback; new->info = info; @@ -126,7 +122,7 @@ void delete_select_channel (int fd) prev->next = p->next; else select_list = p->next; - free (p); + g_free (p); } prev = p; p = p->next; @@ -340,7 +336,7 @@ static key_def *create_sequence (char *seq, int code, int action) key_def *base, *p, *attach; for (base = attach = NULL; *seq; seq++){ - p = xmalloc (sizeof (key_def), "create_sequence"); + p = g_new (key_def, 1); if (!base) base = p; if (attach) attach->child = p; @@ -919,7 +915,7 @@ char *learn_key (void) nodelay (stdscr, FALSE); #endif *p = 0; - return strdup (buffer); + return g_strdup (buffer); } /* xterm and linux console only: set keypad to numeric or application @@ -999,7 +995,7 @@ void k_dispose (key_def *k) return; k_dispose (k->child); k_dispose (k->next); - free (k); + g_free (k); } void s_dispose (SelectList *sel) @@ -1008,7 +1004,7 @@ void s_dispose (SelectList *sel) return; s_dispose (sel->next); - free (sel); + g_free (sel); } void done_key () diff --git a/src/layout.c b/src/layout.c index 6bf500d6c..1a8c2466c 100644 --- a/src/layout.c +++ b/src/layout.c @@ -20,7 +20,6 @@ */ #include -#include #include #include #include @@ -41,10 +40,8 @@ # include #endif #include -#include #include "tty.h" -#include "mad.h" -#include "util.h" /* Needed for the externs */ +#include "global.h" #include "win.h" #include "color.h" #include "key.h" @@ -901,14 +898,14 @@ void remove_dash (void) char *get_nth_panel_name (int num) { - static char buffer [20]; + static char buffer [BUF_SMALL]; if (!num) return "New Left Panel"; else if (num == 1) return "New Right Panel"; else { - sprintf (buffer, "%ith Panel", num); + g_snprintf (buffer, sizeof (buffer), "%ith Panel", num); return buffer; } } @@ -1102,14 +1099,14 @@ void swap_panels () if (panels [0].type == view_listing) { if (!strcmp (panel1->panel_name, get_nth_panel_name (0))) { - free (panel1->panel_name); - panel1->panel_name = strdup (get_nth_panel_name (1)); + g_free (panel1->panel_name); + panel1->panel_name = g_strdup (get_nth_panel_name (1)); } } if (panels [1].type == view_listing) { if (!strcmp (panel2->panel_name, get_nth_panel_name (1))) { - free (panel2->panel_name); - panel2->panel_name = strdup (get_nth_panel_name (0)); + g_free (panel2->panel_name); + panel2->panel_name = g_strdup (get_nth_panel_name (0)); } } diff --git a/src/learn.c b/src/learn.c index 90efd9e91..c6b5ecd66 100644 --- a/src/learn.c +++ b/src/learn.c @@ -24,14 +24,12 @@ #endif #include #include -#include /* For malloc() */ #include #include #include #include #include "tty.h" -#include "mad.h" -#include "util.h" /* Needed for the externs and convert_controls */ +#include "global.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -102,7 +100,7 @@ _("Please press the %s\n" _(key_name_conv_tab [action - B_USER].longname)); mc_refresh (); if (learnkeys [action - B_USER].sequence != NULL) { - free (learnkeys [action - B_USER].sequence); + g_free (learnkeys [action - B_USER].sequence); learnkeys [action - B_USER].sequence = NULL; } seq = learn_key (); @@ -125,7 +123,7 @@ _("Please press the %s\n" _(" You have entered \"%s\""), seq); } - free (seq); + g_free (seq); } dlg_run_done (d); @@ -235,7 +233,7 @@ static void init_learn (void) { int x, y, i, j; key_code_name_t *key; - char buffer [22]; + char buffer [BUF_TINY]; static int i18n_flag = 0; do_refresh (); @@ -277,7 +275,7 @@ static void init_learn (void) y = UY; for (key = key_name_conv_tab, j = 0; key->name != NULL && strcmp (key->name, "kpleft"); key++, j++); - learnkeys = (learnkey *) xmalloc (sizeof (learnkey) * j, "Learn keys"); + learnkeys = g_new (learnkey, j); x += ((j - 1) / ROWS) * COLSHIFT; y += (j - 1) % ROWS; learn_total = j; @@ -286,7 +284,7 @@ static void init_learn (void) for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) { learnkeys [i].ok = 0; learnkeys [i].sequence = NULL; - sprintf (buffer, "%-16s", _(key->longname)); + g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname)); add_widget (learn_dlg, learnkeys [i].button = (Widget *) button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button, 0, NULL)); add_widget (learn_dlg, learnkeys [i].label = (Widget *) @@ -317,7 +315,7 @@ learn_save (void) { int i; int profile_changed = 0; - char *section = copy_strings ("terminal:", getenv ("TERM"), NULL); + char *section = g_strconcat ("terminal:", getenv ("TERM"), NULL); for (i = 0; i < learn_total; i++) { if (learnkeys [i].sequence != NULL) { diff --git a/src/listmode.c b/src/listmode.c index 6b0cfe9a3..65886c0c8 100644 --- a/src/listmode.c +++ b/src/listmode.c @@ -25,7 +25,6 @@ #endif #include #include -#include /* For malloc() */ #include #include #include @@ -34,8 +33,7 @@ # include #endif #include "tty.h" -#include "mad.h" -#include "util.h" /* Needed for the externs */ +#include "global.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -48,7 +46,6 @@ #include "panel.h" /* Needed for the externs */ #include "file.h" #include "main.h" -#include "global.h" #include "listmode.h" #define UX 5 @@ -267,7 +264,7 @@ collect_new_format (void) char *last; char *text, *extra; - newformat = xmalloc (1024, "collect_new_format"); + newformat = g_malloc (1024); if (radio_genwidth->sel) strcpy (newformat, "full "); else @@ -293,9 +290,9 @@ char *listmode_edit (char *oldlistformat) char *newformat = NULL; char *s; - s = strdup (oldlistformat); + s = g_strdup (oldlistformat); init_listmode (s); - free (s); + g_free (s); while (newformat == NULL) { @@ -306,7 +303,7 @@ char *listmode_edit (char *oldlistformat) switch (listmode_dlg->ret_value) { case B_CANCEL: - newformat = strdup (oldlistformat); + newformat = g_strdup (oldlistformat); break; case B_ADD: diff --git a/src/mad.c b/src/mad.c index 3ffbed910..49affb4ff 100644 --- a/src/mad.c +++ b/src/mad.c @@ -20,6 +20,8 @@ #include #include "mad.h" +#undef tempnam + #undef malloc #undef calloc #undef realloc @@ -28,10 +30,10 @@ #undef free #undef g_malloc +#undef g_malloc0 #undef g_calloc #undef g_realloc #undef g_strdup -#undef g_strconcat #undef g_free #include @@ -64,17 +66,26 @@ typedef struct { } mad_mem_area; static mad_mem_area mem_areas [MAD_MAX_AREAS]; +static FILE *memlog = stderr; + void *watch_free_pointer = 0; +void mad_set_debug (char *file) +{ + if((memlog=fopen (file, "w+")) == NULL) + memlog = stderr; +} + /* This function is only called by the mad_check function */ static void mad_abort (char *message, int area, char *file, int line) { - fprintf (stderr, "MAD: %s in area %d.\r\n", message, area); - fprintf (stderr, " Allocated in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: %s in area %d.\r\n", message, area); + fprintf (memlog, " Allocated in file \"%s\" at line %d.\r\n", mem_areas [area].file, mem_areas [area].line); - fprintf (stderr, " Discovered in file \"%s\" at line %d.\r\n", + fprintf (memlog, " Discovered in file \"%s\" at line %d.\r\n", file, line); - fprintf (stderr, "MAD: Core dumping...\r\n"); + fprintf (memlog, "MAD: Core dumping...\r\n"); + fflush (memlog); kill (getpid (), 3); } @@ -93,6 +104,7 @@ void mad_check (char *file, int line) if (*(mem_areas [i].end_sig) != MAD_SIGNATURE) mad_abort ("Overwrite error: Bad end signature", i, file, line); } + fflush (memlog); } /* Allocates a memory area. Used instead of malloc and calloc. */ @@ -108,10 +120,11 @@ void *mad_alloc (int size, char *file, int line) break; } if (i >= MAD_MAX_AREAS){ - fprintf (stderr, "MAD: Out of memory area handles. Increase the value of MAD_MAX_AREAS.\r\n"); - fprintf (stderr, " Discovered in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Out of memory area handles. Increase the value of MAD_MAX_AREAS.\r\n"); + fprintf (memlog, " Discovered in file \"%s\" at line %d.\r\n", file, line); - fprintf (stderr, "MAD: Aborting...\r\n"); + fprintf (memlog, "MAD: Aborting...\r\n"); + fflush (memlog); abort (); } @@ -119,10 +132,11 @@ void *mad_alloc (int size, char *file, int line) size = (size + 3) & (~3); /* Alignment */ area = (char*) g_malloc (size + 2 * sizeof (long)); if (!area){ - fprintf (stderr, "MAD: Out of memory.\r\n"); - fprintf (stderr, " Discovered in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Out of memory.\r\n"); + fprintf (memlog, " Discovered in file \"%s\" at line %d.\r\n", file, line); - fprintf (stderr, "MAD: Aborting...\r\n"); + fprintf (memlog, "MAD: Aborting...\r\n"); + fflush (memlog); abort (); } @@ -158,20 +172,22 @@ void *mad_realloc (void *ptr, int newsize, char *file, int line) break; } if (i >= MAD_MAX_AREAS){ - fprintf (stderr, "MAD: Attempted to realloc unallocated pointer: %p.\r\n", ptr); - fprintf (stderr, " Discovered in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Attempted to realloc unallocated pointer: %p.\r\n", ptr); + fprintf (memlog, " Discovered in file \"%s\" at line %d.\r\n", file, line); - fprintf (stderr, "MAD: Aborting...\r\n"); + fprintf (memlog, "MAD: Aborting...\r\n"); + fflush (memlog); abort (); } newsize = (newsize + 3) & (~3); /* Alignment */ area = (char*) g_realloc (mem_areas [i].start_sig, newsize + 2 * sizeof (long)); if (!area){ - fprintf (stderr, "MAD: Out of memory.\r\n"); - fprintf (stderr, " Discovered in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Out of memory.\r\n"); + fprintf (memlog, " Discovered in file \"%s\" at line %d.\r\n", file, line); - fprintf (stderr, "MAD: Aborting...\r\n"); + fprintf (memlog, "MAD: Aborting...\r\n"); + fflush (memlog); abort (); } @@ -189,6 +205,16 @@ void *mad_realloc (void *ptr, int newsize, char *file, int line) return mem_areas [i].data; } +/* Allocates a memory area. Used instead of malloc and calloc. */ +void *mad_alloc0 (int size, char *file, int line) +{ + char *t; + + t = (char *) mad_alloc (size, file, line); + memset (t, 0, size); + return (void *) t; +} + /* Duplicates a character string. Used instead of strdup. */ char *mad_strdup (const char *s, char *file, int line) { @@ -207,13 +233,15 @@ void mad_free (void *ptr, char *file, int line) mad_check (file, line); if (watch_free_pointer && ptr == watch_free_pointer){ - fprintf (stderr, "MAD: Watch free pointer found in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Watch free pointer found in file \"%s\" at line %d.\r\n", file, line); + fflush (memlog); } if (ptr == NULL){ - fprintf (stderr, "MAD: Attempted to free a NULL pointer in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Attempted to free a NULL pointer in file \"%s\" at line %d.\r\n", file, line); + fflush (memlog); return; } @@ -224,13 +252,14 @@ void mad_free (void *ptr, char *file, int line) break; } if (i >= MAD_MAX_AREAS){ - fprintf (stderr, "MAD: Attempted to free an unallocated pointer: %p.\r\n", ptr); - fprintf (stderr, " Discovered in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Attempted to free an unallocated pointer: %p.\r\n", ptr); + fprintf (memlog, " Discovered in file \"%s\" at line %d.\r\n", file, line); - fprintf (stderr, "MAD: Aborting...\r\n"); + fprintf (memlog, "MAD: Aborting...\r\n"); + fflush (memlog); abort (); } - + g_free (mem_areas [i].start_sig); mem_areas [i].in_use = 0; } @@ -258,11 +287,12 @@ void mad_finalize (char *file, int line) for (i = 0; i < MAD_MAX_AREAS; i++){ if (! mem_areas [i].in_use) continue; - fprintf (stderr, "MAD: Unfreed pointer: %p.\r\n", mem_areas [i].data); - fprintf (stderr, " Allocated in file \"%s\" at line %d.\r\n", + fprintf (memlog, "MAD: Unfreed pointer: %p.\r\n", mem_areas [i].data); + fprintf (memlog, " Allocated in file \"%s\" at line %d.\r\n", mem_areas [i].file, mem_areas [i].line); - fprintf (stderr, " Discovered in file \"%s\" at line %d.\r\n", + fprintf (memlog, " Discovered in file \"%s\" at line %d.\r\n", file, line); + fflush (memlog); } #endif } diff --git a/src/mad.h b/src/mad.h index a166ed66d..0e8896109 100644 --- a/src/mad.h +++ b/src/mad.h @@ -3,7 +3,7 @@ /* To prevent molesting these files with the malloc/calloc/free macros. */ #include -#include +#include #ifdef HAVE_MAD # define INLINE @@ -35,14 +35,25 @@ #define strdup(x) mad_strdup (x, __FILE__, __LINE__) #define free(x) mad_free (x, __FILE__, __LINE__) +/* This defenitions are grabbed from GLib.h */ +#define g_new(type, count) \ + ((type *) g_malloc ((unsigned) sizeof (type) * (count))) +#define g_new0(type, count) \ + ((type *) g_malloc0 ((unsigned) sizeof (type) * (count))) +#define g_renew(type, mem, count) \ + ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count))) + #define g_malloc(x) mad_alloc (x, __FILE__, __LINE__) +#define g_malloc0(x) mad_alloc0 (x, __FILE__, __LINE__) #define g_calloc(x, y) mad_alloc ((x) * (y), __FILE__, __LINE__) #define g_realloc(x, y) mad_realloc (x, y, __FILE__, __LINE__) #define g_strdup(x) mad_strdup (x, __FILE__, __LINE__) #define g_free(x) mad_free (x, __FILE__, __LINE__) +void mad_set_debug (char *file); void mad_check (char *file, int line); void *mad_alloc (int size, char *file, int line); +void *mad_alloc0 (int size, char *file, int line); void *mad_realloc (void *ptr, int newsize, char *file, int line); char *mad_strdup (const char *s, char *file, int line); void mad_free (void *ptr, char *file, int line); diff --git a/src/main.c b/src/main.c index b91d8d3a6..c3b6cd48d 100644 --- a/src/main.c +++ b/src/main.c @@ -88,11 +88,9 @@ /* Program include files */ #include "x.h" -#include "mad.h" #include "dir.h" #include "color.h" #include "global.h" -#include "util.h" #include "dialog.h" #include "menu.h" #include "file.h" @@ -100,7 +98,6 @@ #include "main.h" #include "win.h" #include "user.h" -#include "mem.h" #include "mouse.h" #include "option.h" #include "tree.h" @@ -402,7 +399,7 @@ reload_panelized (WPanel *panel) do_file_mark (panel, i, 0); } if (mc_lstat (list->list [i].fname, &list->list [i].buf)){ - free (list->list [i].fname); + g_free (list->list [i].fname); continue; } if (list->list [i].f.marked) @@ -429,13 +426,13 @@ update_one_panel_widget (WPanel *panel, int force_update, char *current_file) panel->is_panelized = 0; ftpfs_flushdir (); - bzero (&(panel->dir_stat), sizeof (panel->dir_stat)); + memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat)); } /* If current_file == -1 (an invalid pointer) then preserve selection */ if (current_file == UP_KEEPSEL){ free_pointer = 1; - current_file = strdup (panel->dir.list [panel->selected].fname); + current_file = g_strdup (panel->dir.list [panel->selected].fname); } else free_pointer = 0; @@ -448,7 +445,7 @@ update_one_panel_widget (WPanel *panel, int force_update, char *current_file) panel->dirty = 1; if (free_pointer) - free (current_file); + g_free (current_file); #ifdef HAVE_X paint_panel (panel); @@ -568,13 +565,13 @@ static void parse_control_file (void) return; } #endif - data = (char *) xmalloc (s.st_size+1, "main, parse_control_file"); + data = (char *) g_malloc (s.st_size+1); if (!data){ fclose (file); return; } if (s.st_size != fread (data, 1, s.st_size, file)){ - free (data); + g_free (data); fclose (file); return; } @@ -632,7 +629,7 @@ static void parse_control_file (void) current = strtok (NULL, " \t\n"); } - free (data); + g_free (data); paint_panel (cpanel); paint_panel (opanel); } @@ -706,7 +703,7 @@ do_execute (const char *shell, const char *command, int flags) char *old_vfs_dir = 0; if (!vfs_current_is_local ()) - old_vfs_dir = strdup (vfs_get_current_dir ()); + old_vfs_dir = g_strdup (vfs_get_current_dir ()); #endif save_cwds_stat (); @@ -768,7 +765,7 @@ do_execute (const char *shell, const char *command, int flags) #ifdef USE_VFS if (old_vfs_dir){ mc_chdir (old_vfs_dir); - free (old_vfs_dir); + g_free (old_vfs_dir); } #endif @@ -900,25 +897,23 @@ void directory_history_add (WPanel * panel, char *s) { if (!panel->dir_history) { - panel->dir_history = malloc (sizeof (Hist)); - memset (panel->dir_history, 0, sizeof (Hist)); - panel->dir_history->text = strdup (s); + panel->dir_history = g_new0 (Hist, 1); + panel->dir_history->text = g_strdup (s); return; } if (!strcmp (panel->dir_history->text, s)) return; if (panel->dir_history->next) { if (panel->dir_history->next->text) { - free (panel->dir_history->next->text); + g_free (panel->dir_history->next->text); panel->dir_history->next->text = 0; } } else { - panel->dir_history->next = malloc (sizeof (Hist)); - memset (panel->dir_history->next, 0, sizeof (Hist)); + panel->dir_history->next = g_new0 (Hist, 1); panel->dir_history->next->prev = panel->dir_history; } panel->dir_history = panel->dir_history->next; - panel->dir_history->text = strdup (s); + panel->dir_history->text = g_strdup (s); panel_update_marks (panel); } @@ -953,7 +948,7 @@ _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type) vfsid oldvfsid; struct vfs_stamping *parent; #endif - olddir = strdup (panel->cwd); + olddir = g_strdup (panel->cwd); translated_url = new_dir = vfs_translate_url (new_dir); /* Convert *new_path to a suitable pathname, handle ~user */ @@ -971,11 +966,11 @@ _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type) if (mc_chdir (directory) == -1){ strcpy (panel->cwd, olddir); - free (olddir); - free (translated_url); + g_free (olddir); + g_free (translated_url); return 0; } - free (translated_url); + g_free (translated_url); /* Success: save previous directory, shutdown status of previous dir */ strcpy (panel->lwd, olddir); @@ -1006,7 +1001,7 @@ _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type) load_hint (); panel_update_contents (panel); - free (olddir); + g_free (olddir); return 1; } @@ -1061,7 +1056,7 @@ directory_history_list (WPanel * panel) r = _do_panel_cd (panel, s, cd_exact); if (r) directory_history_add (panel, panel->cwd); - free (s); + g_free (s); } } } @@ -1141,10 +1136,10 @@ set_sort_to (WPanel *p, sortfn *sort_order) if (sort_order == (sortfn *) unsorted){ char *current_file; - current_file = strdup (cpanel->dir.list [cpanel->selected].fname); + current_file = g_strdup (cpanel->dir.list [cpanel->selected].fname); panel_reload (cpanel); try_to_select (cpanel, current_file); - free (current_file); + g_free (current_file); } do_re_sort (p); } @@ -1174,7 +1169,7 @@ tree_box (void) sel_dir = tree (selection (cpanel)->fname); if (sel_dir){ do_cd(sel_dir, cd_exact); - free (sel_dir); + g_free (sel_dir); } } #endif @@ -1186,7 +1181,7 @@ static void char *newmode; newmode = listmode_edit ("half name,|,size:8,|,perm:4+"); message (0, " Listing format edit ", " New mode is \"%s\" ", newmode); - free (newmode); + g_free (newmode); } #endif @@ -1450,7 +1445,7 @@ translated_mc_chdir (char *dir) newdir = vfs_translate_url (dir); mc_chdir (newdir); - free (newdir); + g_free (newdir); } #ifndef PORT_HAS_CREATE_PANELS @@ -1556,7 +1551,7 @@ static void copy_readlink (WPanel *panel) int i; i = mc_readlink (p, buffer, MC_MAXPATHLEN); - free (p); + g_free (p); if (i > 0) { buffer [i] = 0; stuff (input_w (cmdline), buffer, 0); @@ -1590,7 +1585,7 @@ void copy_prog_name (void) } else tmp = name_quote (selection (cpanel)->fname, 1); stuff (input_w (cmdline), tmp, 1); - free (tmp); + g_free (tmp); } static void copy_tagged (WPanel *panel) @@ -1605,12 +1600,12 @@ static void copy_tagged (WPanel *panel) if (panel->dir.list [i].f.marked) { char *tmp = name_quote (panel->dir.list [i].fname, 1); stuff (input_w (cmdline), tmp, 1); - free (tmp); + g_free (tmp); } } else { char *tmp = name_quote (panel->dir.list [panel->selected].fname, 1); stuff (input_w (cmdline), tmp, 1); - free (tmp); + g_free (tmp); } input_enable_update (input_w (cmdline)); } @@ -2085,7 +2080,7 @@ void load_hint () if ((hint = get_random_hint ())){ if (*hint) set_hintbar (hint); - free (hint); + g_free (hint); } else { set_hintbar ("The Midnight Commander " VERSION " (C) 1995-1997 the Free Software Foundation"); @@ -2134,15 +2129,15 @@ prepend_cwd_on_local (char *filename) if (vfs_file_is_local (filename)){ if (*filename == PATH_SEP) /* an absolute pathname */ - return strdup (filename); - d = malloc (MC_MAXPATHLEN + strlen (filename) + 2); + return g_strdup (filename); + d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2); mc_get_current_wd (d, MC_MAXPATHLEN); l = strlen(d); d[l++] = PATH_SEP; strcpy (d + l, filename); return canonicalize_pathname (d); } else - return strdup (filename); + return g_strdup (filename); } #ifdef USE_INTERNAL_EDIT @@ -2171,7 +2166,7 @@ mc_maybe_editor_or_viewer (void) edit (edit_one_file, 1); } #endif - free (path); + g_free (path); midnight_shutdown = 1; done_mc (); return 1; @@ -2203,9 +2198,9 @@ do_nc (void) /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */ if (print_last_wd) { if (!vfs_current_is_local ()) - last_wd_string = strdup ("."); + last_wd_string = g_strdup ("."); else - last_wd_string = strdup (cpanel->cwd); + last_wd_string = g_strdup (cpanel->cwd); } done_mc (); @@ -2337,19 +2332,19 @@ OS_Setup () #endif /* ! HAVE_X */ shell = getenv ("SHELL"); if (!shell || !*shell) - shell = strdup (getpwuid (geteuid ())->pw_shell); + shell = g_strdup (getpwuid (geteuid ())->pw_shell); if (!shell || !*shell) shell = "/bin/sh"; - sprintf (control_file, CONTROL_FILE, getpid ()); + g_snprintf (control_file, sizeof (control_file), CONTROL_FILE, getpid ()); my_putenv ("MC_CONTROL_FILE", control_file); /* This is the directory, where MC was installed, on Unix this is LIBDIR */ /* and can be overriden by the MC_LIBDIR environment variable */ if ((mc_libdir = getenv ("MC_LIBDIR")) != NULL) { - mc_home = strdup(mc_libdir); + mc_home = g_strdup (mc_libdir); } else { - mc_home = strdup(LIBDIR); + mc_home = g_strdup (LIBDIR); } } @@ -2436,22 +2431,25 @@ print_mc_usage (void) #endif "-c, --color Force color mode.\n" "-C, --colors Specify colors (use --help-colors to get a list).\n" + "-d, --nomouse Disable mouse support.\n" #ifdef USE_INTERNAL_EDIT "-e, --edit Startup the internal editor.\n" #endif - "-d, --nomouse Disable mouse support.\n" "-f, --libdir Print configured paths.\n" "-h, --help Shows this help message.\n" "-k, --resetsoft Reset softkeys (HP terminals only) to their terminfo/termcap\n" " default.\n" +#ifdef USE_NETCODE + "-l, --ftplog file Log ftpfs commands to the file.\n" +#endif +#ifdef HAVE_MAD + "-M, --memory file [DEVEL-ONLY: Log MAD messages to the file.]\n" +#endif "-P, --printwd At exit, print the last working directory.\n" "-s, --slow Disables verbose operation (for slow terminals).\n" #if defined(HAVE_SLANG) && !defined(OS2_NT) "-t, --termcap Activate support for the TERMCAP variable.\n" #endif -#ifdef USE_NETCODE - "-l, --ftplog file Log ftpfs commands to the file.\n" -#endif #if defined(HAVE_SLANG) && defined(OS2_NT) "-S, --createcmdile Create command file to set default directory upon exit.\n" #endif @@ -2529,14 +2527,20 @@ process_args (int c, char *option_arg) finish_program = 1; break; +#ifdef USE_NETCODE + case 'l': + ftpfs_set_debug (option_arg); + break; +#endif + case 'm': fprintf (stderr, _("Option -m is obsolete. Please look at Display Bits... in the Option's menu\n")); finish_program = 1; break; - -#ifdef USE_NETCODE - case 'l': - ftpfs_set_debug (option_arg); + +#ifdef HAVE_MAD + case 'M': + mad_set_debug (option_arg); break; #endif @@ -2626,6 +2630,9 @@ static struct poptOption argument_table [] = { { "help-colors", 'H', POPT_ARG_NONE, NULL, 'H' }, #ifdef USE_NETCODE { "ftplog", 'l', POPT_ARG_STRING, NULL, 'l' }, +#endif +#ifdef HAVE_MAD + { "memory", 'M', POPT_ARG_STRING, NULL, 'M' }, #endif { "libdir", 'f', POPT_ARG_NONE, NULL, 'f' }, { NULL, 'm', POPT_ARG_NONE, NULL, 'm' }, @@ -2728,19 +2735,19 @@ handle_args (int argc, char *argv []) if (!STRNCOMP (base, "mce", 3) || !STRCOMP(base, "vi")) { edit_one_file = ""; if (tmp) - edit_one_file = strdup (tmp); + edit_one_file = g_strdup (tmp); } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP(base, "view")) { if (tmp) - view_one_file = strdup (tmp); + view_one_file = g_strdup (tmp); } else { /* sets the current dir and the other dir */ if (tmp) { char buffer[MC_MAXPATHLEN + 2]; - this_dir = strdup (tmp); + this_dir = g_strdup (tmp); mc_get_current_wd (buffer, sizeof (buffer) - 2); if ((tmp = poptGetArg (ctx))) - other_dir = strdup (tmp); + other_dir = g_strdup (tmp); } } @@ -2763,12 +2770,12 @@ static int do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name) { char *full_o_name = concat_dir_and_file (home_dir, o_name); - char *full_n_name = copy_strings (home_dir, MC_BASE, n_name, NULL); + char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, NULL); int move; move = 0 == rename (full_o_name, full_n_name); - free (full_o_name); - free (full_n_name); + g_free (full_o_name); + g_free (full_n_name); return move; } @@ -2807,7 +2814,7 @@ compatibility_move_mc_files (void) char *mc_dir = concat_dir_and_file (home_dir, ".mc"); do_compatibility_move (mc_dir); - free (mc_dir); + g_free (mc_dir); } #endif @@ -2819,7 +2826,7 @@ mc_tree_store_load (void) tree_file = concat_dir_and_file (home_dir, MC_TREE); tree_store_init (); tree_store_load (tree_file); - free (tree_file); + g_free (tree_file); } void @@ -2830,7 +2837,7 @@ mc_tree_store_save (void) printf ("Saving tree!\n"); tree_file = concat_dir_and_file (home_dir, MC_TREE); tree_store_save (tree_file); - free (tree_file); + g_free (tree_file); } int main (int argc, char *argv []) @@ -2917,7 +2924,7 @@ int main (int argc, char *argv []) if (open (ttyname (0), O_RDWR) < 0) if (open ("/dev/tty", O_RDWR) < 0) { /* Try if stderr is not redirected as the last chance */ - char *p = strdup (ttyname (0)); + char *p = g_strdup (ttyname (0)); if (!strcmp (p, ttyname (2))) dup2 (2, 1); @@ -2927,7 +2934,7 @@ int main (int argc, char *argv []) "On some systems you may want to run # `which mc`\n")); exit (1); } - free (p); + g_free (p); } #endif } @@ -3049,7 +3056,7 @@ int main (int argc, char *argv []) #ifdef _OS_NT /* On NT, home_dir is malloced */ - free (home_dir); + g_free (home_dir); #endif #if defined(OS2_NT) if (print_last_wd == 2){ @@ -3072,7 +3079,7 @@ int main (int argc, char *argv []) write (stdout_fd, ".", 1); else write (stdout_fd, last_wd_string, strlen (last_wd_string)); - free (last_wd_string); + g_free (last_wd_string); } #ifdef HAVE_MAD diff --git a/src/mem.h b/src/mem.h index a1ba18fde..e8d0f620f 100644 --- a/src/mem.h +++ b/src/mem.h @@ -9,7 +9,7 @@ # endif /* not STDC_HEADERS and HAVE_MEMORY_H */ # ifndef index -#define index strchr +# define index strchr # endif # ifndef rindex diff --git a/src/menu.c b/src/menu.c index 73ea1ee8e..70aafc2ab 100644 --- a/src/menu.c +++ b/src/menu.c @@ -21,12 +21,9 @@ #include #include #include -#include -#include "mad.h" -#include "util.h" +#include "global.h" #include "menu.h" #include "dialog.h" -#include "global.h" #include "color.h" #include "main.h" #include "mouse.h" @@ -54,13 +51,13 @@ Menu create_menu (char *name, menu_entry *entries, int count) { Menu menu; - menu = (Menu) xmalloc (sizeof (*menu), "create_menu"); + menu = (Menu) g_malloc (sizeof (*menu)); menu->count = count; menu->max_entry_len = 0; menu->entries = entries; #ifdef ENABLE_NLS - if (entries != (menu_entry*) 0) + if (entries != (menu_entry*) NULL) { register menu_entry* mp; for (mp = entries; count--; mp++) @@ -73,7 +70,7 @@ Menu create_menu (char *name, menu_entry *entries, int count) } #endif /* ENABLE_NLS */ - menu->name = strdup( _(name) ); + menu->name = g_strdup ( _(name) ); menu_scan_hotkey(menu); menu->start_x = 0; return menu; @@ -517,15 +514,14 @@ menubar_arrange(WMenu* menubar) void destroy_menu (Menu menu) { - free (menu->name); - free (menu); + g_free (menu->name); + g_free (menu); } WMenu *menubar_new (int y, int x, int cols, Menu menu [], int items) { - WMenu *menubar = (WMenu *) xmalloc (sizeof (WMenu), "menubar_new"); + WMenu *menubar = g_new0 (WMenu, 1); /* FIXME: subsel used w/o being set */ - memset(menubar, 0, sizeof(*menubar)); /* FIXME: subsel used w/o being set */ init_widget (&menubar->widget, y, x, 1, cols, (callback_fn) menubar_callback, (destroy_fn) menubar_destroy, diff --git a/src/mouse.c b/src/mouse.c index 3171ac419..1c4344d6d 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -30,13 +30,9 @@ #if (!defined(__IBMC__) && !defined(__IBMCPP__)) && !defined(HAS_NO_TERMIOS_H) # include #endif -#include #include - -#include "mad.h" +#include "global.h" #include "mouse.h" -#include "global.h" /* ESC_STR */ -#include "util.h" /* xmalloc */ #include "key.h" /* define sequence */ #include "tty.h" /* get ncurses header */ @@ -100,7 +96,7 @@ int _nc_remove_key(struct tries **tree, unsigned short code) } if (ptr->value == code) { *tree = 0; - free(ptr); + g_free (ptr); return TRUE; } ptr = ptr->sibling; diff --git a/src/panelize.c b/src/panelize.c index b4050b32b..c5674afb4 100644 --- a/src/panelize.c +++ b/src/panelize.c @@ -25,7 +25,6 @@ #endif #include #include -#include /* For malloc() */ #include #include #include @@ -38,8 +37,7 @@ # include #endif #include "tty.h" -#include "mad.h" -#include "util.h" /* Needed for the externs */ +#include "global.h" #include "win.h" #include "color.h" #include "dlg.h" @@ -47,7 +45,6 @@ #include "dialog.h" /* For do_refresh() */ #include "setup.h" /* For profile_bname */ #include "profile.h" /* Load/save directories panelize */ -#include "fs.h" /* Needed for the extern declarations of integer parameters */ #define DIR_H_INCLUDE_HANDLE_DIRENT @@ -55,7 +52,6 @@ #include "panel.h" /* Needed for the externs */ #include "file.h" #include "main.h" -#include "global.h" #include "../vfs/vfs.h" #include "panelize.h" @@ -241,13 +237,13 @@ static void add2panelize (char *label, char *command) } if (old == NULL){ - panelize = malloc (sizeof (struct panelize)); + panelize = g_new (struct panelize, 1); panelize->label = label; panelize->command = command; panelize->next = current; } else { struct panelize *new; - new = malloc (sizeof (struct panelize)); + new = g_new (struct panelize, 1); new->label = label; new->command = command; old->next = new; @@ -267,11 +263,11 @@ add2panelize_cmd (void) if (!label) return; if (!*label) { - free (label); + g_free (label); return; } - add2panelize (label, strdup(pname->buffer)); + add2panelize (label, g_strdup (pname->buffer)); } } @@ -289,9 +285,9 @@ static void remove_from_panelize (struct panelize *entry) } } - free (entry->label); - free (entry->command); - free (entry); + g_free (entry->label); + g_free (entry->command); + g_free (entry); } } @@ -328,10 +324,10 @@ external_panelize (void) case B_ENTER: target = pname->buffer; if (target != NULL && *target) { - char *cmd = strdup (target); + char *cmd = g_strdup (target); destroy_dlg (panelize_dlg); do_external_panelize (cmd); - free (cmd); + g_free (cmd); repaint_screen (); return; } @@ -348,18 +344,18 @@ void load_panelize (void) profile_keys = profile_init_iterator (panelize_section, profile_name); - add2panelize (strdup (_("Other command")), strdup ("")); + add2panelize (g_strdup (_("Other command")), g_strdup ("")); if (!profile_keys){ - add2panelize (strdup (_("Find rejects after patching")), strdup ("find . -name \\*.rej -print")); - add2panelize (strdup (_("Find *.orig after patching")), strdup ("find . -name \\*.orig -print")); - add2panelize (strdup (_("Find SUID and SGID programs")), strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print")); + add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print")); + add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print")); + add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print")); return; } while (profile_keys){ profile_keys = profile_iterator_next (profile_keys, &key, &value); - add2panelize (strdup (key), strdup (value)); + add2panelize (g_strdup (key), g_strdup (value)); } } @@ -385,9 +381,9 @@ void done_panelize (void) for (; current; current = next){ next = current->next; - free (current->label); - free (current->command); - free (current); + g_free (current->label); + g_free (current->command); + g_free (current); } } @@ -437,7 +433,7 @@ void do_external_panelize (char *command) if (status == -1) break; list->list [next_free].fnamelen = strlen (name); - list->list [next_free].fname = strdup (name); + list->list [next_free].fname = g_strdup (name); file_mark (cpanel, next_free, 0); list->list [next_free].f.link_to_dir = link_to_dir; list->list [next_free].f.stalled_link = stalled_link; diff --git a/src/profile.c b/src/profile.c index ab8b7801d..5dac0bbfd 100644 --- a/src/profile.c +++ b/src/profile.c @@ -25,10 +25,7 @@ #include #include #include -#include /* For free() and atoi() */ #include -#include "mad.h" -#include "util.h" #include "global.h" #include "profile.h" @@ -64,7 +61,7 @@ static int is_loaded (char *FileName, TSecHeader **section) TProfile *p = Base; while (p){ - if (!strcasecmp (FileName, p->FileName)){ + if (! g_strcasecmp (FileName, p->FileName)){ Current = p; *section = p->Section; return 1; @@ -86,7 +83,7 @@ str_untranslate_newline_dup (char *s) l += (*p == '\n' || *p == TRANSLATION_CHAR); p++; } - q = p = malloc (l + 1); + q = p = g_malloc (l + 1); if (!q) return 0; for (;;) { @@ -116,7 +113,7 @@ static char * str_translate_newline_dup (char *s) { char *p, *q; - q = p = malloc (strlen (s) + 1); + q = p = g_malloc (strlen (s) + 1); if (!q) return 0; while (*s) { @@ -168,7 +165,7 @@ static TSecHeader *load (char *file) if (c == ']' || overflow){ *next = '\0'; next = CharBuffer; - SecHeader->AppName = strdup (CharBuffer); + SecHeader->AppName = g_strdup (CharBuffer); state = IgnoreToEOL; } else *next++ = c; @@ -188,8 +185,7 @@ static TSecHeader *load (char *file) TSecHeader *temp; temp = SecHeader; - SecHeader = (TSecHeader *) xmalloc (sizeof (TSecHeader), - "KeyDef"); + SecHeader = g_new (TSecHeader, 1); SecHeader->link = temp; SecHeader->Keys = 0; state = OnSecHeader; @@ -210,9 +206,9 @@ static TSecHeader *load (char *file) temp = SecHeader->Keys; *next = '\0'; - SecHeader->Keys = (TKeys *) xmalloc (sizeof (TKeys), "KD2"); + SecHeader->Keys =g_new (TKeys, 1); SecHeader->Keys->link = temp; - SecHeader->Keys->KeyName = strdup (CharBuffer); + SecHeader->Keys->KeyName = g_strdup (CharBuffer); state = KeyValue; next = CharBuffer; } else { @@ -250,9 +246,9 @@ static void new_key (TSecHeader *section, char *KeyName, char *Value) { TKeys *key; - key = (TKeys *) xmalloc (sizeof (TKeys), "new_key"); - key->KeyName = strdup (KeyName); - key->Value = strdup (Value); + key = g_new (TKeys, 1); + key->KeyName = g_strdup (KeyName); + key->Value = g_strdup (Value); key->link = section->Keys; section->Keys = key; } @@ -267,9 +263,9 @@ GetSetProfileChar (int set, char *AppName, char *KeyName, TKeys *key; if (!is_loaded (FileName, §ion)){ - New = (TProfile *) xmalloc (sizeof (TProfile), "GetSetProfile"); + New = g_new (TProfile, 1); New->link = Base; - New->FileName = strdup (FileName); + New->FileName = g_strdup (FileName); New->Section = load (FileName); Base = New; section = New->Section; @@ -278,14 +274,14 @@ GetSetProfileChar (int set, char *AppName, char *KeyName, /* Start search */ for (; section; section = section->link){ - if (section->AppName == 0 || strcasecmp (section->AppName, AppName)) + if (section->AppName == 0 || g_strcasecmp (section->AppName, AppName)) continue; for (key = section->Keys; key; key = key->link){ - if (strcasecmp (key->KeyName, KeyName)) + if ( g_strcasecmp (key->KeyName, KeyName)) continue; if (set){ - free (key->Value); - key->Value = strdup (Default); + g_free (key->Value); + key->Value = g_strdup (Default); } return key->Value; } @@ -300,8 +296,8 @@ GetSetProfileChar (int set, char *AppName, char *KeyName, /* Non existent section */ if (set && Default){ - section = (TSecHeader *) xmalloc (sizeof (TSecHeader), "GSP3"); - section->AppName = strdup (AppName); + section = g_new (TSecHeader, 1); + section->AppName = g_strdup (AppName); section->Keys = 0; new_key (section, KeyName, Default); section->link = Current->Section; @@ -350,18 +346,18 @@ int GetProfileString (char * AppName, char * KeyName, char * Default, int GetPrivateProfileInt (char * AppName, char * KeyName, int Default, char * File) { - static char IntBuf [15]; - static char buf [15]; + static char IntBuf [BUF_TINY]; + static char buf [BUF_TINY]; - sprintf (buf, "%d", Default); + g_snprintf (buf, sizeof (buf), "%d", Default); /* Check the exact semantic with the SDK */ - GetPrivateProfileString (AppName, KeyName, buf, IntBuf, 15, File); - if (!strcasecmp (IntBuf, "true")) + GetPrivateProfileString (AppName, KeyName, buf, IntBuf, BUF_TINY, File); + if (! g_strcasecmp (IntBuf, "true")) return 1; - if (!strcasecmp (IntBuf, "yes")) + if (! g_strcasecmp (IntBuf, "yes")) return 1; - return atoi (IntBuf); + return (int) atol (IntBuf); } #if 0 @@ -392,7 +388,7 @@ static void dump_keys (FILE * profile, TKeys * p) dump_keys (profile, p->link); t = str_untranslate_newline_dup (p->Value); fprintf (profile, "%s=%s\n", p->KeyName, t); - free (t); + g_free (t); } static void dump_sections (FILE *profile, TSecHeader *p) @@ -435,9 +431,9 @@ static void free_keys (TKeys *p) if (!p) return; free_keys (p->link); - free (p->KeyName); - free (p->Value); - free (p); + g_free (p->KeyName); + g_free (p->Value); + g_free (p); } static void free_sections (TSecHeader *p) @@ -446,10 +442,10 @@ static void free_sections (TSecHeader *p) return; free_sections (p->link); free_keys (p->Keys); - free (p->AppName); + g_free (p->AppName); p->link = 0; p->Keys = 0; - free (p); + g_free (p); } static void free_profile (TProfile *p) @@ -458,8 +454,8 @@ static void free_profile (TProfile *p) return; free_profile (p->link); free_sections (p->Section); - free (p->FileName); - free (p); + g_free (p->FileName); + g_free (p); } void free_profile_name (char *s) @@ -490,16 +486,16 @@ void *profile_init_iterator (char *appname, char *file) TSecHeader *section; if (!is_loaded (file, §ion)){ - New = (TProfile *) xmalloc (sizeof (TProfile), "GetSetProfile"); + New = g_new (TProfile, 1); New->link = Base; - New->FileName = strdup (file); + New->FileName = g_strdup (file); New->Section = load (file); Base = New; section = New->Section; Current = New; } for (; section; section = section->link){ - if (strcasecmp (section->AppName, appname)) + if ( g_strcasecmp (section->AppName, appname)) continue; return section->Keys; } @@ -531,7 +527,7 @@ void profile_clean_section (char *appname, char *file) /* won't be find by further walks of the structure */ for (; section; section = section->link){ - if (strcasecmp (section->AppName, appname)) + if ( g_strcasecmp (section->AppName, appname)) continue; section->AppName [0] = 0; } @@ -546,7 +542,7 @@ int profile_has_section (char *section_name, char *profile) return 0; } for (; section; section = section->link){ - if (strcasecmp (section->AppName, section_name)) + if ( g_strcasecmp (section->AppName, section_name)) continue; return 1; } @@ -558,7 +554,7 @@ void profile_forget_profile (char *file) TProfile *p; for (p = Base; p; p = p->link){ - if (strcasecmp (file, p->FileName)) + if ( g_strcasecmp (file, p->FileName)) continue; p->FileName [0] = 0; } diff --git a/src/rxvt.c b/src/rxvt.c index 89949645c..001cdc11c 100644 --- a/src/rxvt.c +++ b/src/rxvt.c @@ -18,13 +18,11 @@ #include #include /* read, printf */ -#include /* getenv */ #include #include #ifdef HAVE_UNISTD_H # include #endif -#include /* malloc */ #ifndef SCO_FLAVOR # include /* struct timeval */ @@ -35,7 +33,7 @@ #endif #include "tty.h" /* move, addch */ -#include "util.h" /* is_printable */ +#include "global.h" #include "cons.saver.h" int rxvt_extensions = 0; @@ -101,7 +99,7 @@ void show_rxvt_contents (int starty, unsigned char y1, unsigned char y2) bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */ j = 0; - k = malloc (bytes); + k = g_malloc (bytes); for (;;) { int c; c = rxvt_getc (); @@ -122,6 +120,6 @@ void show_rxvt_contents (int starty, unsigned char y1, unsigned char y2) move (starty + (i / cols), 0); addch (is_printable (k[i]) ? k[i] : ' '); } - free (k); + g_free (k); } diff --git a/src/screen.c b/src/screen.c index 67ded5c40..868074f95 100644 --- a/src/screen.c +++ b/src/screen.c @@ -20,24 +20,15 @@ #include #include "tty.h" -#include "fs.h" #include #include -#include /* For malloc() and free() */ #include #include #ifdef HAVE_UNISTD_H # include /* For chdir(), readlink() and getwd()/getcwd() */ #endif -/*#include "mc.h"*/ - -#include - -#include "mem.h" -#include "mad.h" #include "global.h" #include "dir.h" -#include "util.h" #include "panel.h" #include "color.h" #include "tree.h" @@ -152,7 +143,7 @@ delete_format (format_e *format) while (format){ next = format->next; - free (format); + g_free (format); format = next; } } @@ -272,34 +263,35 @@ string_file_permission (file_entry *fe, int len) char * string_file_nlinks (file_entry *fe, int len) { - static char buffer [20]; + static char buffer [BUF_TINY]; - sprintf (buffer, "%16d", fe->buf.st_nlink); + g_snprintf (buffer, sizeof (buffer), "%16d", fe->buf.st_nlink); return buffer; } char * string_file_size (file_entry *fe, int len) { - static char buffer [16]; + static char buffer [BUF_TINY]; int i; #ifdef HAVE_ST_RDEV if (S_ISBLK (fe->buf.st_mode) || S_ISCHR (fe->buf.st_mode)) - sprintf (buffer, "%3d,%3d", (int) ((fe->buf.st_rdev >> 8) & 0xff), - (int) (fe->buf.st_rdev & 0xff)); + g_snprintf (buffer, sizeof (buffer), "%3d,%3d", + (int) ((fe->buf.st_rdev >> 8) & 0xff), + (int) (fe->buf.st_rdev & 0xff)); else #endif { - sprintf (buffer, "%lu", (unsigned long) fe->buf.st_size); + g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->buf.st_size); if (len && (i = strlen (buffer)) > len) { if (i - 2 > len) { if (i - 5 > len) - sprintf (buffer, "%luG", (unsigned long) ((fe->buf.st_size) >> 30)); + g_snprintf (buffer, sizeof (buffer), "%luG", (unsigned long) ((fe->buf.st_size) >> 30)); else - sprintf (buffer, "%luM", (unsigned long) ((fe->buf.st_size) >> 20)); + g_snprintf (buffer, sizeof (buffer), "%luM", (unsigned long) ((fe->buf.st_size) >> 20)); } else - sprintf (buffer, "%luK", (unsigned long) ((fe->buf.st_size) >> 10)); + g_snprintf (buffer, sizeof (buffer), "%luK", (unsigned long) ((fe->buf.st_size) >> 10)); } } return buffer; @@ -392,36 +384,36 @@ string_marked (file_entry *fe, int len) char * string_file_perm_octal (file_entry *fe, int len) { - static char buffer [9]; + static char buffer [10]; - sprintf (buffer, "0%06o", fe->buf.st_mode); + g_snprintf (buffer, sizeof (buffer), "0%06o", fe->buf.st_mode); return buffer; } char * string_inode (file_entry *fe, int len) { - static char buffer [9]; + static char buffer [10]; - sprintf (buffer, "%ld", (long) fe->buf.st_ino); + g_snprintf (buffer, sizeof (buffer), "%ld", (long) fe->buf.st_ino); return buffer; } char * string_file_ngid (file_entry *fe, int len) { - static char buffer [9]; + static char buffer [10]; - sprintf (buffer, "%d", fe->buf.st_gid); + g_snprintf (buffer, sizeof (buffer), "%d", fe->buf.st_gid); return buffer; } char * string_file_nuid (file_entry *fe, int len) { - static char buffer [9]; + static char buffer [10]; - sprintf (buffer, "%d", fe->buf.st_uid); + g_snprintf (buffer, sizeof (buffer), "%d", fe->buf.st_uid); return buffer; } @@ -657,13 +649,13 @@ display_mini_info (WPanel *panel) /* Status displays total marked size */ if (panel->marked){ - char buffer [100]; + char buffer [BUF_SMALL]; char *p; attrset (MARKED_COLOR); printw ("%*s", panel->widget.cols-2, " "); widget_move (&panel->widget, llines (panel)+3, 1); - sprintf (buffer, _(" %s bytes in %d file%s"), + g_snprintf (buffer, sizeof (buffer), _(" %s bytes in %d file%s"), size_trunc_sep (panel->total), panel->marked, panel->marked == 1 ? "" : "s"); p = buffer; @@ -684,7 +676,7 @@ display_mini_info (WPanel *panel) link = concat_dir_and_file (panel->cwd, panel->dir.list [panel->selected].fname); len = mc_readlink (link, link_target, MC_MAXPATHLEN); - free (link); + g_free (link); if (len > 0){ link_target[len] = 0; printw ("-> %-*s", panel->widget.cols - 5, @@ -868,7 +860,7 @@ Xtry_to_select (WPanel *panel, char *name) for (i = 0; i < panel->count; i++){ if (strcmp (subdir, panel->dir.list [i].fname) == 0) { do_select (panel, i); - free (subdir); + g_free (subdir); return; } } @@ -876,7 +868,7 @@ Xtry_to_select (WPanel *panel, char *name) /* Try to select a file near the file that is missing */ if (panel->selected >= panel->count) do_select (panel, panel->count-1); - free (subdir); + g_free (subdir); } #ifndef PORT_HAS_PANEL_UPDATE_COLS @@ -915,9 +907,9 @@ panel_save_name (WPanel *panel) /* If the program is shuting down */ if ((midnight_shutdown && auto_save_setup) || saving_setup) - return copy_strings (panel->panel_name, 0); + return g_strconcat (panel->panel_name, NULL); else - return copy_strings ("Temporal:", panel->panel_name, 0); + return g_strconcat ("Temporal:", panel->panel_name, NULL); } static void @@ -941,21 +933,21 @@ panel_destroy (WPanel *p) while (current){ old = current; current = current->prev; - free (old->text); - free (old); + g_free (old->text); + g_free (old); } } - free (p->hist_name); + g_free (p->hist_name); delete_format (p->format); delete_format (p->status_format); - free (p->user_format); + g_free (p->user_format); for (i = 0; i < LIST_TYPES; i++) - free (p->user_status_format [i]); - free (p->dir.list); - free (p->panel_name); - free (name); + g_free (p->user_status_format [i]); + g_free (p->dir.list); + g_free (p->panel_name); + g_free (name); } static void @@ -982,8 +974,7 @@ panel_new (char *panel_name) char *section; int i, err; - panel = xmalloc (sizeof (WPanel), "panel_new"); - memset (panel, 0, sizeof (WPanel)); + panel = g_new0 (WPanel, 1); /* No know sizes of the panel at startup */ init_widget (&panel->widget, 0, 0, 0, 0, (callback_fn) @@ -996,11 +987,11 @@ panel_new (char *panel_name) mc_get_current_wd (panel->cwd, sizeof (panel->cwd)-2); strcpy (panel->lwd, "."); - panel->hist_name = copy_strings ("Dir Hist ", panel_name, 0); + panel->hist_name = g_strconcat ("Dir Hist ", panel_name, NULL); panel->dir_history = history_get (panel->hist_name); directory_history_add (panel, panel->cwd); - panel->dir.list = (file_entry *) malloc (MIN_FILES * sizeof (file_entry)); + panel->dir.list = g_new (file_entry, MIN_FILES); panel->dir.size = MIN_FILES; panel->active = 0; panel->filter = 0; @@ -1018,32 +1009,32 @@ panel_new (char *panel_name) panel->status_format = 0; panel->format_modified = 1; - panel->panel_name = strdup (panel_name); - panel->user_format = strdup (DEFAULT_USER_FORMAT); + panel->panel_name = g_strdup (panel_name); + panel->user_format = g_strdup (DEFAULT_USER_FORMAT); for(i = 0; i < LIST_TYPES; i++) - panel->user_status_format [i] = strdup (DEFAULT_USER_FORMAT); + panel->user_status_format [i] = g_strdup (DEFAULT_USER_FORMAT); panel->search_buffer [0] = 0; panel->frame_size = frame_half; - section = copy_strings ("Temporal:", panel->panel_name, 0); + section = g_strconcat ("Temporal:", panel->panel_name, NULL); if (!profile_has_section (section, profile_name)){ - free (section); - section = strdup (panel->panel_name); + g_free (section); + section = g_strdup (panel->panel_name); } panel_load_setup (panel, section); - free (section); + g_free (section); /* Load format strings */ err = set_panel_formats (panel); if (err){ if (err & 0x01){ - free (panel->user_format); - panel->user_format = strdup (DEFAULT_USER_FORMAT); + g_free (panel->user_format); + panel->user_format = g_strdup (DEFAULT_USER_FORMAT); } if (err & 0x02){ - free (panel->user_status_format [panel->list_type]); - panel->user_status_format [panel->list_type] = strdup (DEFAULT_USER_FORMAT); + g_free (panel->user_status_format [panel->list_type]); + panel->user_status_format [panel->list_type] = g_strdup (DEFAULT_USER_FORMAT); } set_panel_formats (panel); } @@ -1078,7 +1069,7 @@ panel_reload (WPanel *panel) strcpy (panel->cwd, PATH_SEP_STR); else *last_slash = 0; - bzero (&(panel->dir_stat), sizeof (panel->dir_stat)); + memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat)); show_dir (panel); } @@ -1223,7 +1214,7 @@ parse_display_format (WPanel *panel, char *format, char **error, int isstatus, i while (*format){ /* format can be an empty string */ int found = 0; - darr = xmalloc (sizeof (format_e), "parse_display_format"); + darr = g_new (format_e, 1); /* I'm so ugly, don't look at me :-) */ if (!home) @@ -1303,7 +1294,7 @@ parse_display_format (WPanel *panel, char *format, char **error, int isstatus, i delete_format (home); old_char = format [pos]; format [pos] = 0; - *error = copy_strings(_("Unknow tag on display format: "), format, 0); + *error = g_strconcat (_("Unknow tag on display format: "), format, NULL); format [pos] = old_char; return 0; } @@ -1399,7 +1390,7 @@ set_panel_formats (WPanel *p) form = use_display_format (p, panel_format (p), &err, 0); if (err){ - free (err); + g_free (err); retcode = 1; } else { @@ -1414,7 +1405,7 @@ set_panel_formats (WPanel *p) form = use_display_format (p, mini_status_format (p), &err, 1); if (err){ - free (err); + g_free (err); retcode += 2; } else { @@ -2061,13 +2052,13 @@ do_enter_on_file_entry (file_entry *fe) #endif { char *tmp = name_quote (fe->fname, 0); - char *cmd = copy_strings (".", PATH_SEP_STR, tmp, 0); + char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, NULL); if (!confirm_execute || (query_dialog (_(" The Midnight Commander "), _(" Do you really want to execute? "), 0, 2, _("&Yes"), _("&No")) == 0)) execute (cmd); - free (tmp); - free (cmd); + g_free (tmp); + g_free (cmd); } #ifdef USE_VFS else { @@ -2080,7 +2071,7 @@ do_enter_on_file_entry (file_entry *fe) if (!mc_setctl (tmp, MCCTL_EXTFS_RUN, NULL)) message (1, _(" Warning "), _(" No action taken ")); - free (tmp); + g_free (tmp); } #endif /* USE_VFS */ return 1; @@ -2128,7 +2119,7 @@ chdir_other_panel (WPanel *panel) move_down (panel); - free (new_dir); + g_free (new_dir); } static void @@ -2161,7 +2152,7 @@ chdir_to_readlink (WPanel *panel) p[1] = 0; } if (*buffer == PATH_SEP) - new_dir = strdup (buffer); + new_dir = g_strdup (buffer); else new_dir = concat_dir_and_file (panel->cwd, buffer); @@ -2171,7 +2162,7 @@ chdir_to_readlink (WPanel *panel) move_down (panel); - free (new_dir); + g_free (new_dir); } } @@ -2568,7 +2559,7 @@ panel_re_sort (WPanel *panel) if (panel == NULL) return; - filename = strdup (selection (panel)->fname); + filename = g_strdup (selection (panel)->fname); unselect_item (panel); do_sort (&panel->dir, panel->sort_type, panel->count-1, panel->reverse, panel->case_sensitive); panel->selected = -1; @@ -2578,7 +2569,7 @@ panel_re_sort (WPanel *panel) break; } } - free (filename); + g_free (filename); panel->top_file = panel->selected - ITEMS (panel)/2; if (panel->top_file < 0) panel->top_file = 0; @@ -2598,10 +2589,10 @@ panel_set_sort_order (WPanel *panel, sortfn *sort_order) if (sort_order == (sortfn *) unsorted){ char *current_file; - current_file = strdup (panel->dir.list [panel->selected].fname); + current_file = g_strdup (panel->dir.list [panel->selected].fname); panel_reload (panel); try_to_select (panel, current_file); - free (current_file); + g_free (current_file); } panel_re_sort (panel); } diff --git a/src/setup.c b/src/setup.c index 504c5c525..e39d85a68 100644 --- a/src/setup.c +++ b/src/setup.c @@ -21,12 +21,9 @@ #include #include #include "tty.h" -#include #include -#include "mad.h" -#include "dir.h" #include "global.h" -#include "util.h" /* Functions and externs */ +#include "dir.h" #include "panel.h" #include "main.h" #include "tree.h" @@ -256,12 +253,12 @@ static struct { void panel_save_setup (WPanel *panel, char *section) { - char buffer [40]; + char buffer [BUF_TINY]; int i; - sprintf (buffer, "%d", panel->reverse); + g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse); save_string (section, "reverse", buffer, profile_name); - sprintf (buffer, "%d", panel->case_sensitive); + g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive); save_string (section, "case_sensitive", buffer, profile_name); for (i = 0; sort_names [i].key; i++) if (sort_names [i].sort_type == (sortfn *) panel->sort_type){ @@ -280,12 +277,12 @@ panel_save_setup (WPanel *panel, char *section) panel->user_format, profile_name); for (i = 0; i < LIST_TYPES; i++){ - sprintf (buffer, "user_status%d", i); + g_snprintf (buffer, sizeof (buffer), "user_status%d", i); save_string (section, buffer, panel->user_status_format [i], profile_name); } - sprintf (buffer, "%d", panel->user_mini_status); + g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status); save_string (section, "user_mini_status", buffer, profile_name); } @@ -295,17 +292,17 @@ save_layout (void) { char *profile; int i; - char buffer [6]; + char buffer [BUF_TINY]; profile = concat_dir_and_file (home_dir, PROFILE_NAME); /* Save integer options */ for (i = 0; layout [i].opt_name; i++){ - sprintf (buffer, "%d", *layout [i].opt_addr); + g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr); save_string ("Layout", layout [i].opt_name, buffer, profile); } - free (profile); + g_free (profile); } void @@ -320,7 +317,7 @@ save_configure (void) for (i = 0; options [i].opt_name; i++) set_int (profile, options [i].opt_name, *options [i].opt_addr); - free (profile); + g_free (profile); } static void @@ -387,7 +384,7 @@ save_setup (void) ftpfs_proxy_host, profile); #endif #endif - free (profile); + g_free (profile); saving_setup = 0; } @@ -395,7 +392,7 @@ void panel_load_setup (WPanel *panel, char *section) { int i; - char buffer [40]; + char buffer [BUF_TINY]; panel->reverse = load_int (section, "reverse", 0); panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT); @@ -404,7 +401,7 @@ panel_load_setup (WPanel *panel, char *section) load_string (section, "sort_order", "name", buffer, sizeof (buffer)); panel->sort_type = (sortfn *) sort_name; for (i = 0; sort_names [i].key; i++) - if (strcasecmp (sort_names [i].key, buffer) == 0){ + if ( g_strcasecmp (sort_names [i].key, buffer) == 0){ panel->sort_type = sort_names [i].sort_type; break; } @@ -413,7 +410,7 @@ panel_load_setup (WPanel *panel, char *section) load_string (section, PORT_LIST_MODE_NAME, PORT_LIST_MODE_DEFAULT, buffer, sizeof (buffer)); panel->list_type = list_full; for (i = 0; list_types [i].key; i++) - if (strcasecmp (list_types [i].key, buffer) == 0){ + if ( g_strcasecmp (list_types [i].key, buffer) == 0){ panel->list_type = list_types [i].list_type; break; } @@ -423,18 +420,18 @@ panel_load_setup (WPanel *panel, char *section) #endif /* User formats */ if (panel->user_format){ - free (panel->user_format); + g_free (panel->user_format); panel->user_format = 0; } - panel->user_format = strdup (get_profile_string (section, "user_format", + panel->user_format = g_strdup (get_profile_string (section, "user_format", DEFAULT_USER_FORMAT, profile_name)); for (i = 0; i < LIST_TYPES; i++){ if (panel->user_status_format [i]) - free (panel->user_status_format [i]); - sprintf (buffer, "user_status%d", i); + g_free (panel->user_status_format [i]); + g_snprintf (buffer, sizeof (buffer), "user_status%d", i); panel->user_status_format [i] = - strdup (get_profile_string (section, buffer, + g_strdup (get_profile_string (section, buffer, DEFAULT_USER_FORMAT, profile_name)); } @@ -466,7 +463,7 @@ load_mode (char *section) load_string (section, "display", "listing", buffer, sizeof (buffer)); for (i = 0; panel_types [i].opt_name; i++) - if (strcasecmp (panel_types [i].opt_name, buffer) == 0){ + if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){ mode = panel_types [i].opt_type; break; } @@ -477,13 +474,13 @@ load_mode (char *section) static char * do_load_string (char *s, char *ss, char *def) { - char *buffer = xmalloc (128, "dls"); + char *buffer = g_malloc (128); char *p; load_string (s, ss, def, buffer, 128); - p = strdup (buffer); - free (buffer); + p = g_strdup (buffer); + g_free (buffer); return p; } @@ -502,12 +499,12 @@ load_setup (void) if (exist_file (buffer)){ profile = buffer; } else if (exist_file (inifile)){ - profile = strdup (inifile); - free (buffer); + profile = g_strdup (inifile); + g_free (buffer); } else { profile = buffer; } - free (inifile); + g_free (inifile); profile_name = profile; @@ -528,13 +525,13 @@ load_setup (void) startup_left_mode = view_listing; if (!other_dir){ - buffer = (char*) malloc (MC_MAXPATHLEN); + buffer = (char*) g_malloc (MC_MAXPATHLEN); load_string ("Dirs", "other_dir", ".", buffer, MC_MAXPATHLEN); if (vfs_file_is_local (buffer)) other_dir = buffer; else - free (buffer); + g_free (buffer); } #ifdef USE_NETCODE ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate"); @@ -545,7 +542,7 @@ load_setup (void) load_string ("Misc", "find_ignore_dirs", "", setup_color_string, sizeof (setup_color_string)); if (setup_color_string [0]) - find_ignore_dirs = copy_strings (":", setup_color_string, ":", 0); + find_ignore_dirs = g_strconcat (":", setup_color_string, ":", NULL); /* The default color and the terminal dependent color */ load_string ("Colors", "base_color", "", setup_color_string, @@ -576,7 +573,7 @@ load_anon_passwd () load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer)); if (buffer [0]) - return strdup (buffer); + return g_strdup (buffer); else return 0; } @@ -585,7 +582,7 @@ load_anon_passwd () void done_setup (void) { - free (profile_name); + g_free (profile_name); done_hotlist (); done_panelize (); /* directory_history_free (); */ @@ -602,10 +599,10 @@ load_keys_from_section (char *terminal, char *profile_name) if (!terminal) return; - section_name = copy_strings ("terminal:", terminal, 0); + section_name = g_strconcat ("terminal:", terminal, NULL); profile_keys = profile_init_iterator (section_name, profile_name); if (!profile_keys){ - free (section_name); + g_free (section_name); return; } @@ -615,9 +612,9 @@ load_keys_from_section (char *terminal, char *profile_name) valcopy = convert_controls (value); if (key_code) define_sequence (key_code, valcopy, MCKEY_NOACTION); - free (valcopy); + g_free (valcopy); } - free (section_name); + g_free (section_name); return; } @@ -632,5 +629,5 @@ void load_key_defs (void) /* We don't want a huge database loaded in core */ free_profile_name (libfile); - free (libfile); + g_free (libfile); } diff --git a/src/slint.c b/src/slint.c index b6f144024..ee46fb751 100644 --- a/src/slint.c +++ b/src/slint.c @@ -27,9 +27,8 @@ #include #include #include "tty.h" -#include "mad.h" +#include "global.h" #include "color.h" -#include "util.h" #include "mouse.h" /* Gpm_Event is required in key.h */ #include "key.h" /* define_sequence */ #include "main.h" /* extern: force_colors */ @@ -274,13 +273,13 @@ slang_reset_softkeys (void) int key; char *send; char *display = " "; - char tmp[100]; + char tmp[BUF_SMALL]; for ( key = 1; key < 9; key++ ) { - sprintf ( tmp, "k%d", key); + g_snprintf (tmp, sizeof (tmp), "k%d", key); send = (char *) SLtt_tgetstr (tmp); if (send) { - sprintf(tmp, "\033&f%dk%dd%dL%s%s", key, + g_snprintf(tmp, sizeof (tmp), "\033&f%dk%dd%dL%s%s", key, strlen(display), strlen(send), display, send); SLtt_write_string (tmp); } @@ -394,11 +393,11 @@ try_alloc_color_pair (char *fg, char *bg) break; p = p->next; } - p->next = malloc (sizeof (c)); + p->next = g_new (struct colors_avail, 1); p = p->next; p->next = 0; - p->fg = fg ? strdup (fg) : 0; - p->bg = bg ? strdup (bg) : 0; + p->fg = fg ? g_strdup (fg) : 0; + p->bg = bg ? g_strdup (bg) : 0; if (!fg) /* Index in color_map array = COLOR_INDEX - 1 */ fg = color_map[EDITOR_NORMAL_COLOR_INDEX - 1].fg; diff --git a/src/subshell.c b/src/subshell.c index 49a3252a6..75d038730 100644 --- a/src/subshell.c +++ b/src/subshell.c @@ -207,7 +207,7 @@ void init_subshell (void) /* {{{ Local variables */ /* This must be remembered across calls to init_subshell() */ - static char pty_name[40]; + static char pty_name[BUF_SMALL]; int pty_slave; /* Braindead tcsh can't redirect output to a file descriptor? */ @@ -264,14 +264,14 @@ void init_subshell (void) /* {{{ Initialise the pty's I/O buffer */ pty_buffer_size = INITIAL_PTY_BUFFER_SIZE; - pty_buffer = (char *) malloc (pty_buffer_size); + pty_buffer = (char *) g_malloc (pty_buffer_size); /* }}} */ /* {{{ Create a pipe for receiving the subshell's CWD */ if (subshell_type == TCSH) { - sprintf (tcsh_fifo, "/tmp/mc.pipe.%d", getpid ()); + g_snprintf (tcsh_fifo, sizeof (tcsh_fifo), "/tmp/mc.pipe.%d", getpid ()); if (mkfifo (tcsh_fifo, 0600) == -1) { perror (__FILE__": mkfifo"); @@ -493,20 +493,20 @@ void init_subshell (void) switch (subshell_type) { - char precmd[80]; + char precmd[BUF_SMALL]; case BASH: - sprintf (precmd, " PROMPT_COMMAND='pwd>&%d;kill -STOP $$'\n", + g_snprintf (precmd, sizeof (precmd), " PROMPT_COMMAND='pwd>&%d;kill -STOP $$'\n", subshell_pipe[WRITE]); goto write_it; case ZSH: - sprintf (precmd, "precmd(){ pwd>&%d;kill -STOP $$ }\n", + g_snprintf (precmd, sizeof (precmd), "precmd(){ pwd>&%d;kill -STOP $$ }\n", subshell_pipe[WRITE]); goto write_it; case TCSH: - sprintf (precmd, "alias precmd 'echo $cwd:q >>%s;kill -STOP $$'\n", tcsh_fifo); + g_snprintf (precmd, sizeof (precmd), "alias precmd 'echo $cwd:q >>%s;kill -STOP $$'\n", tcsh_fifo); write_it: write (subshell_pty, precmd, strlen (precmd)); @@ -617,7 +617,7 @@ int read_subshell_prompt (int how) if (subshell_prompt == NULL) /* First time through */ { - subshell_prompt = (char *) malloc (prompt_size); + subshell_prompt = (char *) g_malloc (prompt_size); *subshell_prompt = '\0'; prompt_pos = 0; } @@ -656,7 +656,7 @@ int read_subshell_prompt (int how) subshell_prompt[prompt_pos++] = pty_buffer[i]; if (prompt_pos == prompt_size) - subshell_prompt = (char *) realloc (subshell_prompt, + subshell_prompt = (char *) g_realloc (subshell_prompt, prompt_size *= 2); } @@ -705,10 +705,10 @@ int exit_subshell (void) else { if (subshell_type == TCSH) - sprintf (pty_buffer, " echo -n Jobs:>/tmp/mc.pipe.%d;jobs>/tmp/" + g_snprintf (pty_buffer, sizeof (pty_buffer), " echo -n Jobs:>/tmp/mc.pipe.%d;jobs>/tmp/" "mc.pipe.%d;kill -STOP $$\n", getpid (), getpid ()); else - sprintf (pty_buffer, " echo -n Jobs:>&%d;jobs>&%d;kill -STOP $$\n", + g_snprintf (pty_buffer, sizeof (pty_buffer), " echo -n Jobs:>&%d;jobs>&%d;kill -STOP $$\n", subshell_pipe[WRITE], subshell_pipe[WRITE]); write (subshell_pty, pty_buffer, strlen (pty_buffer)); @@ -735,7 +735,7 @@ int exit_subshell (void) if (quit && subshell_type == TCSH) { /* We abuse of pty_buffer here, but it doesn't matter at this stage */ - sprintf (pty_buffer, "/tmp/mc.pipe.%d", getpid ()); + g_snprintf (pty_buffer, sizeof (pty_buffer), "/tmp/mc.pipe.%d", getpid ()); if (unlink (pty_buffer) == -1) perror (__FILE__": couldn't remove named pipe /tmp/mc.pipe.NNN"); } @@ -767,7 +767,7 @@ void do_subshell_chdir (char *directory, int do_update, int reset_prompt) if (*directory) { temp = name_quote (directory, 0); write (subshell_pty, temp, strlen (temp)); - free (temp); + g_free (temp); } else { write (subshell_pty, "/", 1); } @@ -1030,7 +1030,7 @@ static int pty_open_master (char *pty_name) ptr = pty_name+9; for (num=0;;num++) { - sprintf(ptr,"%d",num); /* surpriiise ... SCO lacks itoa() */ + g_snprintf(ptr, 9, "%d",num); /* surpriiise ... SCO lacks itoa() */ /* Try to open master */ if ((pty_master = open (pty_name, O_RDWR)) == -1) if (errno == ENOENT) /* Different from EIO */ diff --git a/src/tree.c b/src/tree.c index 6335736c1..98b1cb7ad 100644 --- a/src/tree.c +++ b/src/tree.c @@ -31,9 +31,7 @@ #include #include #include "tty.h" -#include "mad.h" #include "global.h" -#include "util.h" #include "color.h" #include "dialog.h" #include "dir.h" @@ -106,7 +104,7 @@ static tree_entry *tree_append_entry (WTree *tree, char *name) /* We assume the directory is not yet in the list */ - new = xmalloc (sizeof (tree_entry), "tree, tree_entry"); + new = g_new (tree_entry, 1); if (!tree->store->tree_first){ /* Empty list */ tree->store->tree_first = new; @@ -119,7 +117,7 @@ static tree_entry *tree_append_entry (WTree *tree, char *name) tree->store->tree_last = new; /* Calculate attributes */ - new->name = strdup (name); + new->name = g_strdup (name); len = strlen (new->name); new->sublevel = 0; for (i = 0; i < len; i++) @@ -169,7 +167,7 @@ void tree_destroy (WTree *tree) tree_store_destroy (); if (tree->tree_shown){ - free (tree->tree_shown); + g_free (tree->tree_shown); tree->tree_shown = 0; } tree->selected_ptr = NULL; @@ -183,7 +181,7 @@ void load_tree (WTree *tree) filename = concat_dir_and_file (home_dir, MC_TREE); v = tree_store_load (filename); - free (filename); + g_free (filename); tree->selected_ptr = tree->store->tree_first; @@ -198,7 +196,7 @@ void save_tree (WTree *tree) filename = concat_dir_and_file (home_dir, MC_TREE); error = tree_store_save (filename); - free (filename); + g_free (filename); if (error){ fprintf (stderr, _("Can't open the %s file for writing:\n%s\n"), MC_TREE, @@ -260,9 +258,9 @@ void show_tree (WTree *tree) } if (tree->tree_shown) - free (tree->tree_shown); - tree->tree_shown = (tree_entry**)xmalloc (sizeof (tree_entry*)*tree_lines, - "tree, show_tree"); + g_free (tree->tree_shown); + tree->tree_shown = g_new (tree_entry*, tree_lines); + for (i = 0; i < tree_lines; i++) tree->tree_shown [i] = NULL; if (tree->store->tree_first) @@ -646,7 +644,7 @@ void tree_copy (WTree *tree, char *default_dest) if (!tree->selected_ptr) return; - sprintf (cmd_buf, _("Copy \"%s\" directory to:"), + g_snprintf (cmd_buf, sizeof(cmd_buf), _("Copy \"%s\" directory to:"), name_trunc (tree->selected_ptr->name, 50)); dest = input_expand_dialog (_(" Copy "), cmd_buf, default_dest); if (!dest || !*dest){ @@ -658,14 +656,14 @@ void tree_copy (WTree *tree, char *default_dest) copy_dir_dir (ctx, tree->selected_ptr->name, dest, 1, 0, 0, 0, &count, &bytes); file_op_context_destroy (ctx); - free (dest); + g_free (dest); } static void tree_help_cmd (void) { char *hlpfile = concat_dir_and_file (mc_home, "mc.hlp"); interactive_display (hlpfile, "[Directory Tree]"); - free (hlpfile); + g_free (hlpfile); } static int tree_copy_cmd (WTree *tree) @@ -684,7 +682,7 @@ void tree_move (WTree *tree, char *default_dest) if (!tree->selected_ptr) return; - sprintf (cmd_buf, _("Move \"%s\" directory to:"), + g_snprintf (cmd_buf, sizeof (cmd_buf), _("Move \"%s\" directory to:"), name_trunc (tree->selected_ptr->name, 50)); dest = input_expand_dialog (_(" Move "), cmd_buf, default_dest); if (!dest || !*dest){ @@ -693,12 +691,12 @@ void tree_move (WTree *tree, char *default_dest) if (stat (dest, &buf)){ message (1, _(" Error "), _(" Can't stat the destination \n %s "), unix_error_string (errno)); - free (dest); + g_free (dest); return; } if (!S_ISDIR (buf.st_mode)){ message (1, _(" Error "), _(" The destination isn't a directory ")); - free (dest); + g_free (dest); return; } @@ -707,7 +705,7 @@ void tree_move (WTree *tree, char *default_dest) move_dir_dir (ctx, tree->selected_ptr->name, dest, &count, &bytes); file_op_context_destroy (ctx); - free (dest); + g_free (dest); } static int @@ -750,14 +748,12 @@ tree_rmdir_cmd (WTree *tree) if (mc_chdir (PATH_SEP_STR)) return; if (confirm_delete){ - char *cmd_buf; + char *buf; int result; - cmd_buf = xmalloc (strlen (tree->selected_ptr->name) + 20, - "tree, rmdir_cmd"); - sprintf (cmd_buf, _(" Delete %s? "), tree->selected_ptr->name); - result = query_dialog (_(" Delete "), cmd_buf, 3, 2, _("&Yes"), _("&No")); - free (cmd_buf); + buf = g_strdup_printf (_(" Delete %s? "), tree->selected_ptr->name); + result = query_dialog (_(" Delete "), buf, 3, 2, _("&Yes"), _("&No")); + g_free (buf); if (result != 0){ return; } @@ -1059,7 +1055,7 @@ tree_callback (Dlg_head *h, WTree *tree, int msg, int par) WTree * tree_new (int is_panel, int y, int x, int lines, int cols) { - WTree *tree = xmalloc (sizeof (WTree), "tree_new"); + WTree *tree = g_new (WTree, 1); init_widget (&tree->widget, y, x, lines, cols, tcallback, (destroy_fn) tree_destroy, (mouse_h) event_callback, NULL); diff --git a/src/treestore.c b/src/treestore.c index 3413c24d5..181b0edf1 100644 --- a/src/treestore.c +++ b/src/treestore.c @@ -41,14 +41,10 @@ #include #include #include -#include #include -#include -#include "fs.h" -#include "../vfs/vfs.h" -#include "util.h" -#include "mad.h" +#include "global.h" #include "treestore.h" +#include "../vfs/vfs.h" #ifdef OS2_NT # include #endif @@ -155,8 +151,8 @@ tree_store_destroy (void) while (current){ old = current; current = current->next; - free (old->name); - free (old); + g_free (old->name); + g_free (old); } ts.tree_first = NULL; @@ -167,7 +163,7 @@ tree_store_destroy (void) static char * decode (char *buffer) { - char *res = strdup (buffer); + char *res = g_strdup (buffer); char *p, *q; for (p = q = res; *p; p++, q++){ @@ -275,7 +271,7 @@ tree_store_load (char *name) e->scanned = scanned; strcpy (oldname, name); } - free (name); + g_free (name); } fclose (file); } @@ -302,7 +298,7 @@ encode (char *string) special_chars++; } - res = malloc (p - string + special_chars + 1); + res = g_malloc (p - string + special_chars + 1); for (p = string, q = res; *p; p++, q++){ if (*p != '\n' && *p != '\\'){ *q = *p; @@ -346,12 +342,12 @@ tree_store_save (char *name) char *encoded = encode (current->name + common); i = fprintf (file, "%d:%d %s\n", current->scanned, common, encoded); - free (encoded); + g_free (encoded); } else { char *encoded = encode (current->name); i = fprintf (file, "%d:%s\n", current->scanned, encoded); - free (encoded); + g_free (encoded); } if (i == EOF){ @@ -390,7 +386,7 @@ tree_store_add_entry (char *name) return current; /* Already in the list */ /* Not in the list -> add it */ - new = xmalloc (sizeof (tree_entry), "ts, tree_entry"); + new = g_new (tree_entry, 1); if (!current){ /* Append to the end of the list */ if (!ts.tree_first){ @@ -419,7 +415,7 @@ tree_store_add_entry (char *name) } /* Calculate attributes */ - new->name = strdup (name); + new->name = g_strdup (name); len = strlen (new->name); new->sublevel = 0; for (i = 0; i < len; i++) @@ -447,7 +443,7 @@ tree_store_add_entry (char *name) if (new->sublevel > 1){ /* Let's check if the parent directory is in the tree */ - char *parent = strdup (new->name); + char *parent = g_strdup (new->name); int i; for (i = strlen (parent) - 1; i > 1; i--){ @@ -457,7 +453,7 @@ tree_store_add_entry (char *name) break; } } - free (parent); + g_free (parent); } tree_store_dirty (TRUE); @@ -494,8 +490,8 @@ remove_entry (tree_entry *entry) ts.tree_last = entry->prev; /* Free the memory used by the entry */ - free (entry->name); - free (entry); + g_free (entry->name); + g_free (entry); return ret; } @@ -552,7 +548,7 @@ tree_store_mark_checked (const char *subname) (subname [1] == 0 || (subname [1] == '.' && subname [2] == 0))) return; if (ts.check_name [0] == PATH_SEP && ts.check_name [1] == 0) - name = copy_strings (PATH_SEP_STR, subname, 0); + name = g_strconcat (PATH_SEP_STR, subname, NULL); else name = concat_dir_and_file (ts.check_name, subname); @@ -566,7 +562,7 @@ tree_store_mark_checked (const char *subname) current = tree_store_add_entry (name); tree_store_notify_add (current); } - free (name); + g_free (name); /* Clear the deletion mark from the subdirectory and its children */ base = current; @@ -607,12 +603,12 @@ tree_store_start_check (char *path) return NULL; current = tree_store_add_entry (path); - ts.check_name = strdup (path); + ts.check_name = g_strdup (path); return current; } - ts.check_name = strdup (path); + ts.check_name = g_strdup (path); retval = current; @@ -665,7 +661,7 @@ tree_store_end_check (void) remove_entry (old); } - free (ts.check_name); + g_free (ts.check_name); ts.check_name = NULL; } @@ -696,7 +692,7 @@ tree_store_rescan (char *dir) if (S_ISDIR (buf.st_mode)) tree_store_mark_checked (dp->d_name); } - free (full_name); + g_free (full_name); } mc_closedir (dirp); } @@ -778,7 +774,7 @@ tree_store_opendir (char *path) if (entry->next == NULL) return NULL; - scan = xmalloc (sizeof (tree_scan), ""); + scan = g_new (tree_scan, 1); scan->base = entry; scan->current = entry->next; scan->sublevel = entry->next->sublevel; @@ -816,5 +812,5 @@ tree_store_closedir (tree_scan *scanner) { g_assert (scanner != NULL); - free (scanner); + g_free (scanner); } diff --git a/src/user.c b/src/user.c index 4646afb8e..78059cd41 100644 --- a/src/user.c +++ b/src/user.c @@ -21,14 +21,10 @@ # include #endif #include "tty.h" -#include /* For free() */ -#include "fs.h" #include #include #include #include -#include "mad.h" -#include "util.h" #include "global.h" #include "dialog.h" #include "color.h" @@ -152,17 +148,17 @@ int check_format_var (char *p, char **v) } /* Copy the variable name */ - var_name = xmalloc (dots - p, "check_format_var"); + var_name = g_malloc (dots - p); strncpy (var_name, p+4, dots-2 - (p+3)); var_name [dots-2 - (p+3)] = 0; value = getenv (var_name); if (value){ - *v = strdup (value); + *v = g_strdup (value); return q-p; } - free (var_name); - var_name = xmalloc (q - dots + 1, "check_format_var_2"); + g_free (var_name); + var_name = g_malloc (q - dots + 1); strncpy (var_name, dots, q - dots + 1); var_name [q-dots] = 0; *v = var_name; @@ -197,7 +193,7 @@ char *expand_format (char c, int quote) quote_func = fake_name_quote; if (c == '%') - return strdup ("%"); + return g_strdup ("%"); if (islower (c)) panel = cpanel; @@ -205,7 +201,7 @@ char *expand_format (char c, int quote) if (get_other_type () == view_listing){ panel = other_panel; } else - return strdup (""); + return g_strdup (""); } if (!panel) panel = cpanel; @@ -234,12 +230,12 @@ char *expand_format (char c, int quote) if (panel->dir.list [i].f.marked) length += strlen (panel->dir.list [i].fname) + 1; - block = xmalloc (length*2+1, "expand_format"); + block = g_malloc (length*2+1); *block = 0; for (i = 0; i < panel->count; i++) if (panel->dir.list [i].f.marked){ strcat (block, tmp = (*quote_func) (panel->dir.list [i].fname, 0)); - free (tmp); + g_free (tmp); strcat (block, " "); if (c == 'u') do_file_mark (panel, i, 0); @@ -247,7 +243,7 @@ char *expand_format (char c, int quote) return block; } /* sub case block */ } /* switch */ - return strdup (""); + return g_strdup (""); } /* Checks for shell patterns defination */ @@ -505,7 +501,7 @@ execute_menu_command (char *s) #ifdef OS2_NT /* OS/2 and NT requires the command to end in .cmd */ - file_name = copy_strings (file_name, ".cmd", NULL); + file_name = g_strconcat (file_name, ".cmd", NULL); #endif if ((cmd_file_fd = open (file_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600)) == -1){ message (1, MSG_ERROR, _(" Can't create temporary command file \n %s "), @@ -544,10 +540,10 @@ execute_menu_command (char *s) } if (do_quote) { fputs (tmp = name_quote (parameter, 0), cmd_file); - free (tmp); + g_free (tmp); } else fputs (parameter, cmd_file); - free (parameter); + g_free (parameter); } else { int len = strlen (prompt); @@ -569,7 +565,7 @@ execute_menu_command (char *s) else{ char *text = expand_format (*commands, do_quote); fputs (text, cmd_file); - free (text); + g_free (text); } } else { if (*commands == '%') { @@ -630,12 +626,12 @@ void user_menu_cmd (void) return; } - menu = strdup (MC_LOCAL_MENU); + menu = g_strdup (MC_LOCAL_MENU); if (!exist_file (menu) || !menu_file_own (menu)){ - free (menu); + g_free (menu); menu = concat_dir_and_file (home_dir, MC_HOME_MENU); if (!exist_file (menu)){ - free (menu); + g_free (menu); menu = concat_dir_and_file (mc_home, MC_GLOBAL_MENU); } } @@ -643,10 +639,10 @@ void user_menu_cmd (void) if ((data = load_file (menu)) == NULL){ message (1, MSG_ERROR, _(" Can't open file %s \n %s "), menu, unix_error_string (errno)); - free (menu); + g_free (menu); return; } - free (menu); + g_free (menu); max_cols = 0; for (i = 0; i < MAX_ENTRIES; i++) @@ -732,5 +728,5 @@ void user_menu_cmd (void) easy_patterns = old_patterns; do_refresh (); - free (data); + g_free (data); } diff --git a/src/util.c b/src/util.c index 47576147e..ba846a902 100644 --- a/src/util.c +++ b/src/util.c @@ -26,7 +26,6 @@ #if defined(__os2__) /* OS/2 need io.h! .ado */ # include #endif -#include #include #ifdef HAVE_UNISTD_H #include @@ -65,22 +64,13 @@ # include #endif -#include /* For funny new functions */ - -#include "fs.h" #include "mountlist.h" -/* From dialog.h (not wanting to include it as - it requires including a lot of other files, too) */ -int message (int error, char *header, char *text, ...); - -#include "mad.h" #if defined(HAVE_RX_H) && defined(HAVE_REGCOMP) #include #else #include #endif -#include "util.h" #include "global.h" #include "profile.h" #include "user.h" /* expand_format */ @@ -96,15 +86,6 @@ int tilde_trunc = 1; struct mount_entry *mount_list = NULL; -#ifndef HAVE_STRDUP -char *strdup (const char *s) -{ - char *t = malloc (strlen (s)+1); - strcpy (t, s); - return t; -} -#endif - #ifndef VFS_STANDALONE int is_printable (int c) { @@ -175,7 +156,7 @@ name_quote (const char *s, int quote_percent) { char *ret, *d; - d = ret = xmalloc (strlen (s)*2 + 2 + 1, "quote_name"); + d = ret = g_malloc (strlen (s)*2 + 2 + 1); if (*s == '-') { *d++ = '.'; *d++ = '/'; @@ -220,7 +201,7 @@ name_quote (const char *s, int quote_percent) char * fake_name_quote (const char *s, int quote_percent) { - return strdup (s); + return g_strdup (s); } /* If passed an empty txt (this usually means that there is an error) @@ -260,19 +241,19 @@ char *name_trunc (char *txt, int trunc_len) char *size_trunc (long int size) { - static char x [30]; + static char x [BUF_TINY]; long int divisor = 1; char *xtra = ""; if (size > 999999999L){ divisor = 1024; - xtra = "kb"; + xtra = "Kb"; if (size/divisor > 999999999L){ divisor = 1024*1024; xtra = "Mb"; } } - sprintf (x, "%ld%s", (size/divisor), xtra); + g_snprintf (x, sizeof (x), "%ld%s", (size/divisor), xtra); return x; } @@ -420,7 +401,7 @@ char *convert_pattern (char *pattern, int match_type, int do_group) int was_wildcard = 0; if (easy_patterns){ - new_pattern = malloc (MC_MAXPATHLEN); + new_pattern = g_malloc (MC_MAXPATHLEN); d = new_pattern; if (match_type == match_file) *d++ = '^'; @@ -455,7 +436,7 @@ char *convert_pattern (char *pattern, int match_type, int do_group) *d = 0; return new_pattern; } else - return strdup (pattern); + return g_strdup (pattern); } int regexp_match (char *pattern, char *string, int match_type) @@ -468,11 +449,11 @@ int regexp_match (char *pattern, char *string, int match_type) if (!old_pattern || STRCOMP (old_pattern, pattern) || old_type != match_type){ if (old_pattern){ regfree (&r); - free (old_pattern); + g_free (old_pattern); } pattern = convert_pattern (pattern, match_type, 0); if (regcomp (&r, pattern, REG_EXTENDED|REG_NOSUB|MC_ARCH_FLAGS)) { - free (pattern); + g_free (pattern); return -1; } old_pattern = pattern; @@ -531,9 +512,9 @@ int get_int (char *file, char *key, int def) int set_int (char *file, char *key, int value) { - char buffer [30]; + char buffer [BUF_TINY]; - sprintf (buffer, "%d", value); + g_snprintf (buffer, sizeof (buffer), "%d", value); return WritePrivateProfileString (app_text, key, buffer, file); } @@ -555,7 +536,7 @@ char *load_file (char *filename) if ((data_file = fopen (filename, "r")) == NULL){ return 0; } - data = (char *) xmalloc (s.st_size+1, "util, load_file"); + data = (char *) g_malloc (s.st_size+1); read_size = fread (data, 1, s.st_size, data_file); data [read_size] = 0; fclose (data_file); @@ -563,7 +544,7 @@ char *load_file (char *filename) if (read_size > 0) return data; else { - free (data); + g_free (data); return 0; } } @@ -605,7 +586,7 @@ char *file_date_pck (time_t when) char *extract_line (char *s, char *top) { - static char tmp_line [500]; + static char tmp_line [BUF_MEDIUM]; char *t = tmp_line; while (*s && *s != '\n' && (t - tmp_line) < sizeof (tmp_line)-1 && s < top) @@ -648,30 +629,16 @@ char * _icase_search (char *text, char *data, int *lng) char *x_basename (char *s) { char *where; - return ((where = strrchr (s, PATH_SEP)))? where + 1 : s; -} - -char *get_full_name (char *dir, char *file) -{ - int i; - char *d = malloc (strlen (dir) + strlen (file) + 2); - - strcpy (d, dir); - i = strlen (dir); - if (dir [i - 1] != PATH_SEP || dir [i] != 0) - strcat (d, PATH_SEP_STR); - file = x_basename (file); - strcat (d, file); - return d; + return ((where = strrchr (s, PATH_SEP))) ? where + 1 : s; } void my_putenv (char *name, char *data) { char *full; - full = xmalloc (strlen (name) + strlen (data) + 2, "util, my_putenv"); - sprintf (full, "%s=%s", name, data); + full = g_strdup_printf ("%s=%s", name, data); putenv (full); + /* WARNING: NEVER FREE THE full VARIABLE!!!!!!!!!!!!!!!!!!!!!!!! */ /* It is used by putenv. Freeing it will corrupt the environment */ } @@ -683,7 +650,7 @@ static void my_putenv_expand (char *name, char macro_code) data = expand_format (macro_code); my_putenv (name, data); - free (data); + g_free (data); } /* Puts some status information in to the environment so that @@ -703,53 +670,13 @@ static void prepare_environment (void) char *unix_error_string (int error_num) { - static char buffer [256]; - char *error_msg; + static char buffer [BUF_LARGE]; -#ifdef HAVE_STRERROR - error_msg = strerror (error_num); -#else - extern int sys_nerr; - extern char *sys_errlist []; - if ((0 <= error_num) && (error_num < sys_nerr)) - error_msg = sys_errlist[error_num]; - else - error_msg = "strange errno"; -#endif - sprintf (buffer, "%s (%d)", error_msg, error_num); + g_snprintf (buffer, sizeof (buffer), "%s (%d)", + g_strerror (error_num), error_num); return buffer; } -char *copy_strings (const char *first, ...) -{ - va_list ap; - long len; - char *data, *result; - - if (!first) - return 0; - - len = strlen (first) + 1; - va_start (ap, first); - - while ((data = va_arg (ap, char *)) != 0) - len += strlen (data); - - result = g_malloc (len); - - va_end (ap); - - va_start (ap, first); - strcpy (result, first); - - while ((data = va_arg (ap, char *)) != 0) - strcat (result, data); - - va_end (ap); - - return result; -} - #ifndef VFS_STANDALONE long blocks2kilos (int blocks, int bsize) { @@ -819,24 +746,6 @@ char *strip_ctrl_codes (char *s) return s; } -#ifndef HAVE_STRCASECMP -/* At least one version of HP/UX lacks this */ -/* Assumes ASCII encoding */ -int strcasecmp (const char *s, const char *d) -{ - register signed int result; - - while (1){ - if (result = (0x20 | *s) - (0x20 | *d)) - break; - if (!*s) - return 0; - s++; - d++; - } - return result; -} -#endif /* HAVE_STRCASECMP */ #endif /* VFS_STANDALONE */ /* getwd is better than getcwd, the later uses a popen ("pwd"); */ @@ -989,7 +898,7 @@ decompress_command_and_arg (int type, char **cmd, char **flags) /* Hooks */ void add_hook (Hook **hook_list, void (*hook_fn)(void *), void *data) { - Hook *new_hook = xmalloc (sizeof (Hook), "add_hook"); + Hook *new_hook = g_new (Hook, 1); new_hook->hook_fn = hook_fn; new_hook->next = *hook_list; @@ -1021,7 +930,7 @@ void execute_hooks (Hook *hook_list) for (hook_list = p; hook_list;){ p = hook_list; hook_list = hook_list->next; - free (p); + g_free (p); } } @@ -1034,7 +943,7 @@ void delete_hook (Hook **hook_list, void (*hook_fn)(void *)) for (current = *hook_list; current; current = next){ next = current->next; if (current->hook_fn == hook_fn) - free (current); + g_free (current); else add_hook (&new_list, current->hook_fn, current->hook_data); } @@ -1057,14 +966,14 @@ void wipe_password (char *passwd) for (;*p ; p++) *p = 0; - free (passwd); + g_free (passwd); } /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */ /* Returns a newly allocated string */ char *convert_controls (char *s) { - char *valcopy = strdup (s); + char *valcopy = g_strdup (s); char *p, *q; /* Parse the escape special character */ @@ -1119,8 +1028,8 @@ char *resolve_symlinks (char *path) if (*path != PATH_SEP) return NULL; - r = buf = xmalloc (MC_MAXPATHLEN, "resolve symlinks"); - buf2 = xmalloc (MC_MAXPATHLEN, "resolve symlinks"); + r = buf = g_malloc (MC_MAXPATHLEN); + buf2 = g_malloc (MC_MAXPATHLEN); *r++ = PATH_SEP; *r = 0; p = path; @@ -1134,8 +1043,8 @@ char *resolve_symlinks (char *path) c = *q; *q = 0; if (mc_lstat (path, &mybuf) < 0) { - free (buf); - free (buf2); + g_free (buf); + g_free (buf2); *q = c; return NULL; } @@ -1144,8 +1053,8 @@ char *resolve_symlinks (char *path) else { len = mc_readlink (path, buf2, MC_MAXPATHLEN); if (len < 0) { - free (buf); - free (buf2); + g_free (buf); + g_free (buf2); *q = c; return NULL; } @@ -1170,7 +1079,7 @@ char *resolve_symlinks (char *path) strcpy (buf, PATH_SEP_STR); else if (*(r - 1) == PATH_SEP && r != buf + 1) *(r - 1) = 0; - free (buf2); + g_free (buf2); return buf; } @@ -1189,7 +1098,7 @@ char *diff_two_paths (char *first, char *second) if (j) { second = resolve_symlinks (second); if (second == NULL) { - free (first); + g_free (first); return buf; } } @@ -1214,21 +1123,21 @@ char *diff_two_paths (char *first, char *second) currlen = (i + 1) * 3 + strlen (q) + 1; if (j) { if (currlen < prevlen) - free (buf); + g_free (buf); else { - free (first); - free (second); + g_free (first); + g_free (second); return buf; } } - p = buf = xmalloc (currlen, "diff 2 paths"); + p = buf = g_malloc (currlen); prevlen = currlen; for (; i >= 0; i--, p += 3) strcpy (p, "../"); strcpy (p, q); } - free (first); - free (second); + g_free (first); + g_free (second); return buf; } @@ -1271,13 +1180,76 @@ int truncate (const char *path, long size) #endif #endif /* VFS_STANDALONE */ +/* If filename is NULL, then we just append PATH_SEP to the dir */ char * concat_dir_and_file (const char *dir, const char *file) { - int l = strlen (dir); - - if (dir [l-1] == PATH_SEP) - return copy_strings (dir, file, 0); + int i = strlen (dir); + + if (dir [i-1] == PATH_SEP) + return g_strconcat (dir, file, NULL); else - return copy_strings (dir, PATH_SEP_STR, file, 0); + return g_strconcat (dir, PATH_SEP_STR, file, NULL); } + +#ifdef HAVE_MAD +char *mad_strconcat (const char *first, ...) +{ + va_list ap; + long len; + char *data, *result; + + if (!first) + return 0; + + len = strlen (first) + 1; + va_start (ap, first); + + while ((data = va_arg (ap, char *)) != 0) + len += strlen (data); + + result = g_malloc (len); + + va_end (ap); + + va_start (ap, first); + strcpy (result, first); + + while ((data = va_arg (ap, char *)) != 0) + strcat (result, data); + + va_end (ap); + + return result; +} + +/* This two functions grabbed from GLib's gstrfuncs.c */ +char* +mad_strdup_vprintf (const char *format, va_list args1) +{ + char *buffer; + va_list args2; + + G_VA_COPY (args2, args1); + + buffer = g_new (char, g_printf_string_upper_bound (format, args1)); + + vsprintf (buffer, format, args2); + va_end (args2); + + return buffer; +} + +char* +mad_strdup_printf (const char *format, ...) +{ + char *buffer; + va_list args; + + va_start (args, format); + buffer = g_strdup_vprintf (format, args); + va_end (args); + + return buffer; +} +#endif /* HAVE_MAD */ \ No newline at end of file diff --git a/src/util.h b/src/util.h index 7dabdb010..b26719c44 100644 --- a/src/util.h +++ b/src/util.h @@ -7,7 +7,7 @@ /* String managing functions */ #if defined(SCO_FLAVOR) && defined(__GNUC__) -extern char* strdup(const char*); +extern char*g_strdup (const char*); #endif int is_printable (int c); @@ -23,8 +23,6 @@ char *string_perm (mode_t mode_bits); char *strip_home_and_password(char *dir); char *extension (char *); char *split_extension (char *, int pad); -char *get_full_name (char *dir, char *file); - char *concat_dir_and_file (const char *dir, const char *file); char *unix_error_string (int error_num); char *skip_separators (char *s); @@ -37,21 +35,18 @@ char *resolve_symlinks (char *path); char *diff_two_paths (char *first, char *second); int set_nonblocking (int fd); -#ifndef HAVE_STRCASECMP -int strcasecmp (const char *s, const char *d); -#endif - char *x_basename (char *s); extern int align_extensions; + #ifdef HAVE_MAD -char *copy_strings (const char *first, ...); -#else -void *do_xmalloc (int); -#define xmalloc(a,b) g_malloc(a) -#define copy_strings g_strconcat -char *g_strconcat (const char *first, ...); -void *g_malloc (unsigned long); +char *mad_strconcat (const char *first, ...); +char *mad_strdup_printf (const char *format, ...); +char *mad_strdup_vprintf (const char *format, va_list args); + +#define g_strconcat mad_strconcat +#define g_strdup_printf mad_strdup_printf +#define g_strdup_vprintf mad_strdup_vprintf #endif /* Profile managing functions */ diff --git a/src/utilunix.c b/src/utilunix.c index aefc1700a..c7fbaa13f 100644 --- a/src/utilunix.c +++ b/src/utilunix.c @@ -23,7 +23,6 @@ #include #include -#include #include #ifdef HAVE_UNISTD_H #include @@ -62,12 +61,10 @@ #ifdef __QNX__ # include /* exec*() from */ #endif -#include "util.h" #include "global.h" #include "fsusage.h" #include "fsusage.h" #include "mountlist.h" -#include "mad.h" #include "dialog.h" /* message() */ #include "../vfs/vfs.h" /* mc_read() */ #include "x.h" @@ -108,10 +105,11 @@ void init_groups (void) pwd = getpwuid (current_user_uid=getuid ()); - current_user_gid = (pug = xmalloc (sizeof (user_in_groups), "init_groups")); - current_user_gid->gid = getgid (); current_user_gid->next = 0; + current_user_gid = (pug = g_new (user_in_groups, 1)); + current_user_gid->gid = getgid (); + current_user_gid->next = NULL; - if (pwd == 0) + if (pwd == NULL) return; setgrent (); @@ -119,10 +117,10 @@ void init_groups (void) for (i = 0; grp->gr_mem[i]; i++) if (!strcmp (pwd->pw_name,grp->gr_mem[i])) { - cug = xmalloc (sizeof (user_in_groups), "init_groups"); + cug = g_new (user_in_groups, 1); cug->gid = grp->gr_gid; pug->next = cug; - cug->next = 0; + cug->next = NULL; pug = cug; break; } @@ -152,7 +150,7 @@ delete_groups (void) while (cug){ pug = cug->next; - free (cug); + g_free (cug); cug = pug; } } @@ -193,8 +191,8 @@ static void i_cache_add (int id, int_cache *cache, int size, char *text, int *last) { if (cache [*last].string) - free (cache [*last].string); - cache [*last].string = strdup (text); + g_free (cache [*last].string); + cache [*last].string = g_strdup (text); cache [*last].index = id; *last = ((*last)+1) % size; } @@ -202,7 +200,7 @@ static void i_cache_add (int id, int_cache *cache, int size, char *text, char *get_owner (int uid) { struct passwd *pwd; - static char ibuf [8]; + static char ibuf [10]; char *name; static int uid_last; @@ -215,7 +213,7 @@ char *get_owner (int uid) return pwd->pw_name; } else { - sprintf (ibuf, "%d", uid); + g_snprintf (ibuf, sizeof (ibuf), "%d", uid); return ibuf; } } @@ -223,7 +221,7 @@ char *get_owner (int uid) char *get_group (int gid) { struct group *grp; - static char gbuf [8]; + static char gbuf [10]; char *name; static int gid_last; @@ -235,7 +233,7 @@ char *get_group (int gid) i_cache_add (gid, gid_cache, GID_CACHE_SIZE, grp->gr_name, &gid_last); return grp->gr_name; } else { - sprintf (gbuf, "%d", gid); + g_snprintf (gbuf, sizeof (gbuf), "%d", gid); return gbuf; } } @@ -322,7 +320,7 @@ char *tilde_expand (char *directory) int len; if (*directory != '~') - return strdup (directory); + return g_strdup (directory); directory++; @@ -337,11 +335,11 @@ char *tilde_expand (char *directory) p = ""; passwd = getpwnam (directory); } else { - name = xmalloc (p - directory + 1, "tilde_expand"); + name = g_malloc (p - directory + 1); strncpy (name, directory, p - directory); name [p - directory] = 0; passwd = getpwnam (name); - free (name); + g_free (name); } } @@ -350,7 +348,7 @@ char *tilde_expand (char *directory) return 0; len = strlen (passwd->pw_dir) + strlen (p) + 2; - directory = xmalloc (len, "tilde_expand"); + directory = g_malloc (len); strcpy (directory, passwd->pw_dir); strcat (directory, PATH_SEP_STR); strcat (directory, p); @@ -744,7 +742,7 @@ putenv (const char *string) if (*ep == NULL){ static char **last_environ = NULL; - char **new_environ = (char **) malloc ((size + 2) * sizeof (char *)); + char **new_environ = g_new (char *, size + 2); if (new_environ == NULL) return -1; (void) memcpy ((void *) new_environ, (void *) __environ, @@ -752,7 +750,7 @@ putenv (const char *string) new_environ[size] = (char *) string; new_environ[size + 1] = NULL; if (last_environ != NULL) - free ((void *) last_environ); + g_free ((void *) last_environ); last_environ = new_environ; __environ = new_environ; } diff --git a/src/view.c b/src/view.c index 2158cb705..43bf54712 100644 --- a/src/view.c +++ b/src/view.c @@ -46,20 +46,15 @@ # include #endif #include /* For toupper() */ -#include /* atoi() */ -#include #include #include #include -#include "mem.h" -#include "mad.h" -#include "util.h" +#include "global.h" #include "dlg.h" /* Needed by widget.h */ #include "widget.h" /* Needed for buttonbar_new */ #include "color.h" #include "dialog.h" #include "mouse.h" -#include "global.h" #include "help.h" #include "key.h" /* For mi_getch() */ #include "layout.h" @@ -70,7 +65,6 @@ #else # include #endif -#include "fs.h" #include "../vfs/vfs.h" #include "dir.h" #include "panel.h" /* Needed for current_panel and other_panel */ @@ -101,6 +95,8 @@ int max_dirt_limit = 10; #endif +extern Hook *idle_hook; + /* Our callback */ static int view_callback (Dlg_head *h, WView *view, int msg, int par); @@ -170,9 +166,9 @@ free_file (WView *view) /* Block_ptr may be zero if the file was a file with 0 bytes */ if (view->growing_buffer && view->block_ptr){ for (i = 0; i < view->blocks; i++){ - free (view->block_ptr [i].data); + g_free (view->block_ptr [i].data); } - free (view->block_ptr); + g_free (view->block_ptr); } } @@ -187,7 +183,7 @@ view_done (WView *view) /* alex: release core, used to replace mmap */ if (!view->mmapping && !view->growing_buffer && view->data != NULL){ - free(view->data); + g_free (view->data); view->data = NULL; } @@ -195,9 +191,9 @@ view_done (WView *view) if (view->localcopy) mc_ungetlocalcopy (view->filename, view->localcopy, 0); free_file (view); - free (view->filename); + g_free (view->filename); if (view->command) - free (view->command); + g_free (view->command); } view->view_active = 0; default_hex_mode = view->hex_mode; @@ -227,15 +223,15 @@ get_byte (WView *view, int byte_index) if (view->growing_buffer){ if (page > view->blocks){ - tmp = xmalloc (sizeof (block_ptr_t) * page, "get_byte"); + tmp = g_new (block_ptr_t, page); if (view->block_ptr){ bcopy (view->block_ptr, tmp, sizeof (block_ptr_t) * view->blocks); - free (view->block_ptr); + g_free (view->block_ptr); } view->block_ptr = tmp; for (i = view->blocks; i < page; i++){ - char *p = malloc (VIEW_PAGE_SIZE); + char *p = g_malloc (VIEW_PAGE_SIZE); view->block_ptr [i].data = p; if (!p) return '\n'; @@ -338,7 +334,7 @@ put_editkey (WView *view, unsigned char key) } if (!node) { node = (struct hexedit_change_node *) - xmalloc(sizeof(struct hexedit_change_node), "HexEdit"); + g_new (struct hexedit_change_node, 1); if (node) { #ifndef HAVE_MMAP @@ -367,7 +363,7 @@ free_change_list (WView *view) while (n) { view->change_list = n->next; - free (n); + g_free (n); n = view->change_list; } view->file_dirty = 0; @@ -402,11 +398,11 @@ view_ok_to_quit (WView *view) return 1; query_set_sel (1); - text = copy_strings (_("File: \n\n "), view->filename, + text = g_strconcat (_("File: \n\n "), view->filename, _("\n\nhas been modified, do you want to save the changes?\n"), NULL); r = query_dialog (_(" Save changes "), text, 2, 3, _("&Yes"), _("&No"), _("&Cancel")); - free (text); + g_free (text); switch (r) { case 0: @@ -429,7 +425,7 @@ set_view_init_error (WView *view, char *msg) view->last_byte = 0; if (msg){ view->bytes_read = strlen (msg); - return strdup (msg); + return g_strdup (msg); } return 0; } @@ -476,15 +472,15 @@ static char *load_view_file (WView *view, char *filename) { if ((view->file = mc_open (filename, O_RDONLY)) < 0){ set_view_init_error (view, 0); - return (copy_strings (_(" Can't open file \""), + return ( g_strconcat (_(" Can't open file \""), filename, "\"\n ", - unix_error_string (errno), " ", 0)); + unix_error_string (errno), " ", NULL)); } if (mc_fstat (view->file, &view->s) < 0){ set_view_init_error (view, 0); close_view_file (view); - return copy_strings (_(" Can't stat file \n "), - unix_error_string (errno), " ", 0); + return g_strconcat (_(" Can't stat file \n "), + unix_error_string (errno), " ", NULL); } if (S_ISDIR (view->s.st_mode) || S_ISSOCK (view->s.st_mode) || S_ISFIFO (view->s.st_mode)){ @@ -515,12 +511,12 @@ no_mmap: * file into memory (alex@bcs.zaporizhzhe.ua). Also, mmap can fail * for any reason, so we use this as fallback (pavel@ucw.cz) */ - view->data = (unsigned char*) xmalloc (view->s.st_size, "load_view_file"); + view->data = (unsigned char*) g_malloc (view->s.st_size); if (view->data == NULL || mc_lseek(view->file,0,0) != 0 || mc_read(view->file, view->data, view->s.st_size) != view->s.st_size){ if (view->data != NULL) - free(view->data); + g_free (view->data); close_view_file (view); return init_growing_view (view, 0, filename); } @@ -568,8 +564,8 @@ do_view_init (WView *view, char *_command, char *_file, int start_line) int fd; fd = mc_open(_file, O_RDONLY); if (_file[0] && view->viewer_magic_flag && (is_gunzipable (fd, &type)) != 0) - view->filename = copy_strings (_file, decompress_extension(type), NULL); - else view->filename = strdup (_file); + view->filename = g_strconcat (_file, decompress_extension(type), NULL); + else view->filename = g_strdup (_file); mc_close(fd); } @@ -581,14 +577,14 @@ do_view_init (WView *view, char *_command, char *_file, int start_line) if (error){ if (!view->have_frame){ message (1, MSG_ERROR, error); - free (error); + g_free (error); return -1; } } view->view_active = 1; if (_command) - view->command = strdup (_command); + view->command = g_strdup (_command); else view->command = 0; view->search_start = view->start_display = view->start_save = view->first; @@ -779,7 +775,7 @@ display (WView *view) /* Optionally, display a ruler */ if ((!view->hex_mode) && (ruler)){ - char r_buff[4]; + char r_buff[10]; int cl; view_set_color (view, BOLD_COLOR); @@ -797,7 +793,7 @@ display (WView *view) r_buff[0] = '*'; view_add_character (view, r_buff[0]); if ((cl != 0) && (cl % 10) == 0){ - sprintf(r_buff, "%03d", cl); + g_snprintf(r_buff, sizeof (r_buff), "%03d", cl); if (ruler == 1) widget_move (view, row + 1, c - 1); else @@ -828,7 +824,7 @@ display (WView *view) for (;row < height && from < view->last_byte; row++){ /* Print the hex offset */ - sprintf (hex_buff, "%05X", (int) (from - view->first)); + g_snprintf (hex_buff, sizeof (hex_buff), "%05X", (int) (from - view->first)); widget_move (view, row, frame_shift); view_add_string (view, hex_buff); @@ -1346,10 +1342,10 @@ grow_string_buffer (char *text, int *size) /* The grow steps */ *size += 160; - new = xmalloc (*size, "grow_string_buffer"); + new = g_malloc (*size); if (text){ strncpy (new, text, old_size); - free (text); + g_free (text); } else { *new = 0; } @@ -1466,7 +1462,7 @@ search (WView *view, char *text, int (*search)(WView *, char *, char *, int)) search_update_steps (view); update_activate = 0; - for (; ; isatbeg = 1, free (s)){ + for (; ; isatbeg = 1, g_free (s)){ #ifdef PORT_HAS_FLUSH_EVENTS static int count; @@ -1527,7 +1523,7 @@ search (WView *view, char *text, int (*search)(WView *, char *, char *, int)) view->start_display = t; } - free (s); + g_free (s); break; } disable_interrupt_key (); @@ -1687,7 +1683,7 @@ static int regexp_view_search (WView *view, char *pattern, char *string, int mat if (!old_pattern || strcmp (old_pattern, pattern) || old_type != match_type){ if (old_pattern){ regfree (&r); - free (old_pattern); + g_free (old_pattern); old_pattern = 0; } for (i = 0; pattern[i] != 0; i++){ @@ -1701,7 +1697,7 @@ static int regexp_view_search (WView *view, char *pattern, char *string, int mat message (1, MSG_ERROR, _(" Invalid regular expression ")); return -1; } - old_pattern = strdup (pattern); + old_pattern = g_strdup (pattern); old_type = match_type; } if (regexec (&r, string, 1, pmatch, 0) != 0) @@ -1744,7 +1740,7 @@ static void help_cmd (void) { char *hlpfile = concat_dir_and_file (mc_home, "mc.hlp"); interactive_display (hlpfile, "[Internal File Viewer]"); - free (hlpfile); + g_free (hlpfile); /* view_refresh (0); */ @@ -1820,7 +1816,7 @@ toggle_hexedit_mode(WView *view) void goto_line (WView *view) { - char *line, prompt [100]; + char *line, prompt [BUF_SMALL]; int i, oldline = 1; int saved_wrap_mode = view->wrap_mode; @@ -1828,15 +1824,15 @@ goto_line (WView *view) for (i = view->first; i < view->start_display; i++) if (get_byte (view, i) == '\n') oldline ++; - sprintf (prompt, _(" The current line number is %d.\n" + g_snprintf (prompt, sizeof (prompt), _(" The current line number is %d.\n" " Enter the new line number:"), oldline); line = input_dialog (_(" Goto line "), prompt, ""); if (line){ if (*line){ move_to_top (view); - view_move_forward (view, atoi (line) - 1); + view_move_forward (view, atol (line) - 1); } - free (line); + g_free (line); } view->dirty++; view->wrap_mode = saved_wrap_mode; @@ -1863,7 +1859,7 @@ regexp_search (WView *view, int direction) return; } if (old) - free (old); + g_free (old); old = regexp; #if 0 /* Mhm, do we really need to load all the file in the core? */ @@ -1895,7 +1891,7 @@ normal_search (WView *view, int direction) return; } if (old) - free (old); + g_free (old); old = exp; view->direction = direction; @@ -1919,17 +1915,17 @@ change_viewer (WView *view) if (*view->filename) { altered_magic_flag = 1; view->viewer_magic_flag = !view->viewer_magic_flag; - s = strdup (view->filename); + s = g_strdup (view->filename); if (view->command) - t = strdup (view->command); + t = g_strdup (view->command); else t = 0; view_done (view); view_init (view, t, s, 0); - free (s); + g_free (s); if (t) - free (t); + g_free (t); view_labels (view); view->dirty++; view_update (view); @@ -2431,7 +2427,7 @@ view_callback (Dlg_head *h, WView *v, int msg, int par) WView * view_new (int y, int x, int cols, int lines, int is_panel) { - WView *view = xmalloc (sizeof (WView), "view_new"); + WView *view = g_new (WView, 1); init_widget (&view->widget, y, x, lines, cols, (callback_fn) view_callback, diff --git a/src/widget.c b/src/widget.c index fe1e4c54f..6a1b496a3 100644 --- a/src/widget.c +++ b/src/widget.c @@ -28,12 +28,9 @@ #include #include #include -#include #include "tty.h" #include -#include "mad.h" #include "global.h" -#include "util.h" #include "color.h" #include "mouse.h" #include "dlg.h" @@ -78,7 +75,7 @@ static int button_callback (Dlg_head *h, WButton *b, int Msg, int Par) { #ifndef HAVE_X - char *txt, buf[256]; + char *txt, buf[BUF_SMALL]; #endif int stop = 0; int off = 0; @@ -136,15 +133,15 @@ button_callback (Dlg_head *h, WButton *b, int Msg, int Par) switch (b->flags){ case DEFPUSH_BUTTON: - sprintf (buf, "[< %s >]", b->text); + g_snprintf (buf, sizeof(buf), "[< %s >]", b->text); off = 3; break; case NORMAL_BUTTON: - sprintf (buf, "[ %s ]", b->text); + g_snprintf (buf, sizeof(buf), "[ %s ]", b->text); off = 2; break; case NARROW_BUTTON: - sprintf (buf, "[%s]", b->text); + g_snprintf (buf, sizeof(buf), "[%s]", b->text); off = 1; break; case HIDDEN_BUTTON: @@ -196,7 +193,7 @@ static void button_destroy (WButton *b) { x_destroy_cmd (b); - free (b->text); + g_free (b->text); } static int @@ -246,7 +243,7 @@ WButton * button_new (int y, int x, int action, int flags, char *text, int (*callback)(int, void *), void *callback_data, char *tkname) { - WButton *b = xmalloc (sizeof (WButton), "new_button"); + WButton *b = g_new (WButton, 1); init_widget (&b->widget, y, x, 1, button_len (text, flags), (callback_fn) button_callback, @@ -255,7 +252,7 @@ button_new (int y, int x, int action, int flags, char *text, b->action = action; b->flags = flags; b->selected = 0; - b->text = strdup (text); + b->text = g_strdup (text); b->callback = callback; b->callback_data = callback_data; widget_want_hotkey (b->widget, 1); @@ -269,8 +266,8 @@ button_new (int y, int x, int action, int flags, char *text, void button_set_text (WButton *b, char *text) { - free (b->text); - b->text = strdup (text); + g_free (b->text); + b->text = g_strdup (text); b->widget.cols = button_len (text, b->flags); button_scan_hotkey(b); #ifdef HAVE_X @@ -419,7 +416,7 @@ radio_event (Gpm_Event *event, WRadio *r) WRadio * radio_new (int y, int x, int count, char **texts, int use_hotkey, char *tkname) { - WRadio *r = xmalloc (sizeof (WRadio), "radio_new"); + WRadio *r = g_new (WRadio, 1); int i, max, m; /* Compute the longest string */ @@ -518,20 +515,20 @@ static void check_destroy (WCheck *c) { x_destroy_cmd (c); - free (c->text); + g_free (c->text); } WCheck * check_new (int y, int x, int state, char *text, char *tkname) { - WCheck *c = xmalloc (sizeof (WCheck), "check_new"); + WCheck *c = g_new (WCheck, 1); char *s, *t; init_widget (&c->widget, y, x, 1, strlen (text), (callback_fn)check_callback, (destroy_fn)check_destroy, (mouse_h) check_event, tkname); c->state = state ? C_BOOL : 0; - c->text = strdup (text); + c->text = g_strdup (text); c->hotkey = 0; c->hotpos = -1; widget_want_hotkey (c->widget, 1); @@ -607,10 +604,10 @@ label_set_text (WLabel *label, char *text) return; /* Flickering is not nice */ if (label->text){ - free (label->text); + g_free (label->text); } if (text){ - label->text = strdup (text); + label->text = g_strdup (text); if (label->auto_adjust_cols) { newcols = strlen (text); if (newcols > label->widget.cols) @@ -634,18 +631,18 @@ label_destroy (WLabel *l) { x_destroy_cmd (l); if (l->text) - free (l->text); + g_free (l->text); } WLabel * label_new (int y, int x, char *text, char *tkname) { - WLabel *l = xmalloc (sizeof (WLabel), "label_new"); + WLabel *l = g_new (WLabel, 1); init_widget (&l->widget, y, x, 1, 1, (callback_fn) label_callback, (destroy_fn) label_destroy, NULL, tkname); - l->text = text ? strdup (text) : 0; + l->text = text ? g_strdup (text) : 0; l->auto_adjust_cols = 1; l->transparent = 0; widget_want_cursor (l->widget, 0); @@ -745,7 +742,7 @@ gauge_destroy (WGauge *g) WGauge * gauge_new (int y, int x, int shown, int max, int current, char *tkname) { - WGauge *g = xmalloc (sizeof (WGauge), "gauge_new"); + WGauge *g = g_new (WGauge, 1); init_widget (&g->widget, y, x, 1, gauge_len, (callback_fn) gauge_callback, @@ -896,9 +893,11 @@ int num_history_items_recorded = 60; Hist *history_get (char *input_name) { int i; - Hist *old = 0, *new = 0; + Hist *old, *new; char *profile; + old = new = NULL; + if (!num_history_items_recorded) /* this is how to disable */ return 0; if (!input_name) @@ -907,21 +906,20 @@ Hist *history_get (char *input_name) return 0; profile = concat_dir_and_file (home_dir, HISTORY_FILE_NAME); for (i = 0;; i++) { - char key_name[32]; - char this_entry[1024]; - sprintf (key_name, "%d", i); + char key_name[BUF_TINY]; + char this_entry[BUF_LARGE]; + g_snprintf (key_name, sizeof (key_name), "%d", i); GetPrivateProfileString (input_name, key_name, "", this_entry, sizeof (this_entry), profile); if (!*this_entry) break; - new = xmalloc (sizeof (Hist), "history_get"); - memset (new, 0, sizeof (Hist)); - new->text = strdup (this_entry); + new = g_new0 (Hist, 1); + new->text = g_strdup (this_entry); new->prev = old; /* set up list pointers */ if (old) old->next = new; old = new; } - free (profile); + g_free (profile); return new; /* return pointer to last entry in list */ } @@ -961,14 +959,14 @@ void history_put (char *input_name, Hist *h) /* probably aren't any null entries, but lets be sure */ if (*(h->text)){ - char key_name[32]; - sprintf (key_name, "%d", i++); + char key_name[BUF_TINY]; + g_snprintf (key_name, sizeof(key_name), "%d", i++); WritePrivateProfileString (input_name, key_name, h->text, profile); } } h = h->next; } - free (profile); + g_free (profile); } #else void history_put (char *input_name, Hist *h) @@ -1073,7 +1071,7 @@ char *show_hist (Hist *history, int widget_x, int widget_y) if (query_dlg->ret_value != B_CANCEL) { listbox_get_current (query_list, &q, NULL); if (q) - r = strdup (q); + r = g_strdup (q); } destroy_dlg (query_dlg); return r; @@ -1085,7 +1083,7 @@ static void do_show_hist (WInput * in) r = show_hist (in->history, in->widget.x, in->widget.y); if (r) { assign_text (in, r); - free (r); + g_free (r); } } @@ -1112,15 +1110,15 @@ input_destroy (WInput *in) while (current){ old = current; current = current->prev; - free (old->text); - free (old); + g_free (old->text); + g_free (old); } } x_destroy_cmd (in); - free (in->buffer); + g_free (in->buffer); free_completions (in); if (in->history_name) - free (in->history_name); + g_free (in->history_name); } static char disable_update = 0; @@ -1152,14 +1150,14 @@ push_history (WInput *in, char *text) in->history = in->history->next; if (!strcmp (in->history->text, text)) return 1; - new = xmalloc (sizeof (Hist), "push_history"); + new = g_new (Hist, 1); in->history->next = new; } else - new = xmalloc (sizeof (Hist), "push_history"); + new = g_new (Hist, 1); in->need_push = 0; new->next = 0; new->prev = in->history; - new->text = strdup (text); + new->text = g_strdup (text); in->history = new; return 2; } @@ -1189,14 +1187,14 @@ insert_char (WInput *in, int c_code) in->need_push = 1; if (strlen (in->buffer)+1 == in->current_max_len){ /* Expand the buffer */ - char *narea = xmalloc(in->current_max_len + in->field_len, "string expansion"); + char *narea = g_malloc (in->current_max_len + in->field_len); if (narea){ char *p = in->buffer; strcpy (narea, in->buffer); in->buffer = narea; in->current_max_len += in->field_len; - free (p); + g_free (p); } } if (strlen (in->buffer)+1 < in->current_max_len){ @@ -1315,9 +1313,9 @@ copy_region (WInput *in, int x_first, int x_last) return; if (kill_buffer) - free (kill_buffer); + g_free (kill_buffer); - kill_buffer = xmalloc (last-first + 1, "copy_region"); + kill_buffer = g_malloc (last-first + 1); strncpy (kill_buffer, in->buffer+first, last-first); kill_buffer [last-first] = 0; } @@ -1398,8 +1396,8 @@ static void kill_line (WInput *in) { if (kill_buffer) - free (kill_buffer); - kill_buffer = strdup (&in->buffer [in->point]); + g_free (kill_buffer); + kill_buffer = g_strdup (&in->buffer [in->point]); in->buffer [in->point] = 0; } @@ -1407,8 +1405,8 @@ void assign_text (WInput *in, char *text) { free_completions (in); - free (in->buffer); - in->buffer = strdup (text); /* was in->buffer->text */ + g_free (in->buffer); + in->buffer = g_strdup (text); /* was in->buffer->text */ in->current_max_len = strlen (in->buffer) + 1; in->point = strlen (in->buffer); in->mark = 0; @@ -1684,7 +1682,7 @@ input_event (Gpm_Event *event, WInput *in) WInput * input_new (int y, int x, int color, int len, char *def_text, char *tkname) { - WInput *in = xmalloc (sizeof (WInput), "input_new"); + WInput *in = g_new (WInput, 1); int initial_buffer_len; init_widget (&in->widget, y, x, 1, len, @@ -1696,7 +1694,7 @@ input_new (int y, int x, int color, int len, char *def_text, char *tkname) in->history_name = 0; if (tkname && PORT_WIDGET_WANTS_HISTORY){ if (*tkname) { - in->history_name = strdup (tkname); + in->history_name = g_strdup (tkname); in->history = history_get (tkname); } } @@ -1713,7 +1711,7 @@ input_new (int y, int x, int color, int len, char *def_text, char *tkname) INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES; in->current_max_len = initial_buffer_len; - in->buffer = xmalloc (initial_buffer_len, "create_input: in->buffer"); + in->buffer = g_malloc (initial_buffer_len); in->color = color; in->field_len = len; in->first = 1; @@ -1895,8 +1893,8 @@ listbox_remove_list (WListbox *l) while (l->count--) { q = p->next; - free (p->text); - free (p); + g_free (p->text); + g_free (p); p = q; } l->pos = l->count = 0; @@ -1949,8 +1947,8 @@ listbox_remove_current (WListbox *l, int force) l->list = l->top = l->current = 0; } - free (p->text); - free (p); + g_free (p->text); + g_free (p); } /* Makes *e the selected entry (sets current and pos) */ @@ -2194,8 +2192,8 @@ listbox_destroy (WListbox *l) x_destroy_cmd (l); for (i = 0; i < l->count; i++){ n = p->next; - free (p->text); - free (p); + g_free (p->text); + g_free (p); p = n; } } @@ -2204,7 +2202,7 @@ WListbox * listbox_new (int y, int x, int width, int height, int action, lcback callback, char *tkname) { - WListbox *l = xmalloc (sizeof (WListbox), "listbox_new"); + WListbox *l = g_new (WListbox, 1); extern int slow_terminal; init_widget (&l->widget, y, x, height, width, @@ -2279,8 +2277,8 @@ listbox_add_item (WListbox *l, enum append_pos pos, int hotkey, char *text, if (listbox_search_text (l, text)) return 0; - entry = xmalloc (sizeof (WLEntry), "listbox_add_item"); - entry->text = strdup (text); + entry = g_new (WLEntry, 1); + entry->text = g_strdup (text); entry->data = data; entry->hotkey = hotkey; @@ -2379,7 +2377,7 @@ buttonbar_destroy (WButtonBar *bb) for (i = 0; i < 10; i++){ if (bb->labels [i].text) - free (bb->labels [i].text); + g_free (bb->labels [i].text); } } @@ -2404,7 +2402,7 @@ WButtonBar * buttonbar_new (int visible) { int i; - WButtonBar *bb = xmalloc (sizeof (WButtonBar), "buttonbar_new"); + WButtonBar *bb = g_new (WButtonBar, 1); init_widget (&bb->widget, LINES-1, 0, 1, COLS, (callback_fn) buttonbar_callback, @@ -2425,9 +2423,9 @@ void set_label_text (WButtonBar *bb, int index, char *text) { if (bb->labels [index-1].text) - free (bb->labels [index-1].text); + g_free (bb->labels [index-1].text); - bb->labels [index-1].text = strdup (text); + bb->labels [index-1].text = g_strdup (text); } /* paneletc is either the panel widget, or info or view or tree widget */ diff --git a/src/win.c b/src/win.c index 08bff460e..3791352ff 100644 --- a/src/win.c +++ b/src/win.c @@ -18,22 +18,18 @@ #include #include "tty.h" #include -#include /* For free() */ #include #if (!defined(__IBMC__) && !defined(__IBMCPP__)) && !defined(HAS_NO_TERMIOS_H) # include #endif -#include "mad.h" +#include "global.h" #include "color.h" #include "mouse.h" -#include "util.h" /* For xmalloc() */ - #include "dlg.h" #include "widget.h" #include "win.h" #include "key.h" /* XCTRL and ALT macros */ #include "layout.h" -#include "global.h" /* "$Id$" */ @@ -60,9 +56,9 @@ void sprint_bytesize (char *buffer, int size, int scale) } } if (scale > 0) - sprintf (buffer, "%4d %cb", size, scales[scale]); + g_snprintf (buffer, 10, "%4d %cb", size, scales[scale]); else - sprintf (buffer, "%4d b ", size); + g_snprintf (buffer, 10, "%4d b ", size); } void print_bytesize (int size, int scale) @@ -283,7 +279,7 @@ int lookup_key (char *keyname) int i; for (i = 0; key_name_conv_tab [i].code; i++){ - if (strcasecmp (key_name_conv_tab [i].name, keyname)) + if ( g_strcasecmp (key_name_conv_tab [i].name, keyname)) continue; return key_name_conv_tab [i].code; } diff --git a/src/wtools.c b/src/wtools.c index 65b16a8a5..685386c2a 100644 --- a/src/wtools.c +++ b/src/wtools.c @@ -33,12 +33,9 @@ #include #include #include -#include #include "tty.h" #include -#include "mad.h" #include "global.h" -#include "util.h" #include "win.h" #include "color.h" #include "mouse.h" @@ -110,7 +107,7 @@ static int listbox_callback (Dlg_head *h, int id, int msg) Listbox *create_listbox_window (int cols, int lines, char *title, char *help) { int xpos, ypos, len; - Listbox *listbox = xmalloc (sizeof (Listbox), "create_listbox_window"); + Listbox *listbox = g_new (Listbox, 1); char* cancel_string = _("&Cancel"); /* Adjust sizes */ @@ -157,7 +154,7 @@ int run_listbox (Listbox *l) else val = l->list->pos; destroy_dlg (l->dlg); - free (l); + g_free (l); return val; } @@ -369,7 +366,7 @@ Chooser *new_chooser (int lines, int cols, char *help, int flags) Chooser *c; int button_lines; - c = (Chooser *) xmalloc (sizeof (Chooser), "new_chooser"); + c =g_new (Chooser, 1); c->dialog = create_dlg (0, 0, lines, cols, dialog_colors, common_dialog_callback, help, "chooser", DLG_CENTER | DLG_GRID); @@ -535,7 +532,7 @@ int quick_dialog_skip (QuickDialog *qd, int nskip) break; case quick_input: - *qw->str_result = strdup (((WInput *) qw->the_widget)->buffer); + *qw->str_result = g_strdup (((WInput *) qw->the_widget)->buffer); break; } } @@ -704,7 +701,7 @@ char *input_expand_dialog (char *header, char *text, char *def_text) if (result){ expanded = tilde_expand (result); if (expanded){ - free (result); + g_free (result); return expanded; } else return result;