static void exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line, vfs_path_t * temp_file_name_vpath) { int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0; int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0; mcview_altered_hex_mode = 0; mcview_altered_nroff_flag = 0; if (def_hex_mode != mcview_default_hex_mode) changed_hex_mode = 1; if (def_nroff_flag != mcview_default_nroff_flag) changed_nroff_flag = 1; /* If we've written whitespace only, then just load filename * into view */ if (written_nonspace) { mcview_viewer (cmd, filename_vpath, start_line); mc_unlink (temp_file_name_vpath); } else mcview_viewer (NULL, filename_vpath, start_line); if (changed_hex_mode && !mcview_altered_hex_mode) mcview_default_hex_mode = def_hex_mode; if (changed_nroff_flag && !mcview_altered_nroff_flag) mcview_default_nroff_flag = def_nroff_flag; dialog_switch_process_pending (); }
void edit_symlink_cmd (void) { if (S_ISLNK (selection (current_panel)->st.st_mode)) { char buffer[MC_MAXPATHLEN]; char *p = NULL; int i; char *q; vfs_path_t *p_vpath; p = selection (current_panel)->fname; p_vpath = vfs_path_from_str (p); q = g_strdup_printf (_("Symlink '%s\' points to:"), str_trunc (p, 32)); i = readlink (p, buffer, MC_MAXPATHLEN - 1); if (i > 0) { char *dest; buffer[i] = 0; dest = input_expand_dialog (_("Edit symlink"), q, MC_HISTORY_FM_EDIT_LINK, buffer, INPUT_COMPLETE_FILENAMES); if (dest) { if (*dest && strcmp (buffer, dest)) { save_cwds_stat (); if (mc_unlink (p_vpath) == -1) { message (D_ERROR, MSG_ERROR, _("edit symlink, unable to remove %s: %s"), p, unix_error_string (errno)); } else { vfs_path_t *dest_vpath; dest_vpath = vfs_path_from_str_flags (dest, VPF_NO_CANON); if (mc_symlink (dest_vpath, p_vpath) == -1) message (D_ERROR, MSG_ERROR, _("edit symlink: %s"), unix_error_string (errno)); vfs_path_free (dest_vpath); } update_panels (UP_OPTIMIZE, UP_KEEPSEL); repaint_screen (); } g_free (dest); } } g_free (q); vfs_path_free (p_vpath); } else { message (D_ERROR, MSG_ERROR, _("'%s' is not a symbolic link"), selection (current_panel)->fname); } }
static void exec_cleanup_script (vfs_path_t * script_vpath) { if (script_vpath != NULL) { (void) mc_unlink (script_vpath); vfs_path_free (script_vpath); } }
static void mcview_remove_ext_script (mcview_t * view) { if (view->ext_script != NULL) { mc_unlink (view->ext_script); vfs_path_free (view->ext_script); view->ext_script = NULL; } }
void edit_symlink_cmd (void) { if (S_ISLNK (selection (current_panel)->st.st_mode)) { char buffer[MC_MAXPATHLEN]; char *p = NULL; int i; char *dest, *q; p = selection (current_panel)->fname; q = g_strdup_printf (_("Symlink `%s\' points to:"), str_trunc (p, 32)); i = readlink (p, buffer, MC_MAXPATHLEN - 1); if (i > 0) { buffer[i] = 0; dest = input_expand_dialog (_("Edit symlink"), q, MC_HISTORY_FM_EDIT_LINK, buffer); if (dest) { if (*dest && strcmp (buffer, dest)) { save_cwds_stat (); if (-1 == mc_unlink (p)) { message (D_ERROR, MSG_ERROR, _("edit symlink, unable to remove %s: %s"), p, unix_error_string (errno)); } else { if (-1 == mc_symlink (dest, p)) message (D_ERROR, MSG_ERROR, _("edit symlink: %s"), unix_error_string (errno)); } update_panels (UP_OPTIMIZE, UP_KEEPSEL); repaint_screen (); } g_free (dest); } } g_free (q); } else { message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"), selection (current_panel)->fname); } }
gboolean mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix) { char *backup_path; backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix); if (backup_path == NULL) return FALSE; if (exist_file (backup_path)) mc_unlink (backup_path); g_free (backup_path); return TRUE; }
static void execute_menu_command (WEdit * edit_widget, const char *commands, gboolean show_prompt) { FILE *cmd_file; int cmd_file_fd; int expand_prefix_found = 0; char *parameter = 0; gboolean do_quote = FALSE; char lc_prompt[80]; int col; vfs_path_t *file_name_vpath; int run_view = 0; /* Skip menu entry title line */ commands = strchr (commands, '\n'); if (!commands) { return; } cmd_file_fd = mc_mkstemps (&file_name_vpath, "mcusr", SCRIPT_SUFFIX); if (cmd_file_fd == -1) { message (D_ERROR, MSG_ERROR, _("Cannot create temporary command file\n%s"), unix_error_string (errno)); vfs_path_free (file_name_vpath); return; } cmd_file = fdopen (cmd_file_fd, "w"); fputs ("#! /bin/sh\n", cmd_file); commands++; for (col = 0; *commands; commands++) { if (col == 0) { if (*commands != ' ' && *commands != '\t') break; while (*commands == ' ' || *commands == '\t') commands++; if (*commands == 0) break; } col++; if (*commands == '\n') col = 0; if (parameter) { if (*commands == '}') { *parameter = 0; parameter = input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_FM_MENU_EXEC_PARAM, "", INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES); if (!parameter || !*parameter) { /* User canceled */ fclose (cmd_file); mc_unlink (file_name_vpath); vfs_path_free (file_name_vpath); return; } if (do_quote) { char *tmp; tmp = name_quote (parameter, 0); fputs (tmp, cmd_file); g_free (tmp); } else fputs (parameter, cmd_file); g_free (parameter); parameter = 0; } else { if (parameter < &lc_prompt[sizeof (lc_prompt) - 1]) { *parameter++ = *commands; } } } else if (expand_prefix_found) { expand_prefix_found = 0; if (g_ascii_isdigit ((gchar) * commands)) { do_quote = (atoi (commands) != 0); while (g_ascii_isdigit ((gchar) * commands)) commands++; } if (*commands == '{') parameter = lc_prompt; else { char *text = expand_format (edit_widget, *commands, do_quote); fputs (text, cmd_file); g_free (text); } } else { if (*commands == '%') { int i = check_format_view (commands + 1); if (i) { commands += i; run_view = 1; } else { do_quote = TRUE; /* Default: Quote expanded macro */ expand_prefix_found = 1; } } else fputc (*commands, cmd_file); } } fclose (cmd_file); mc_chmod (file_name_vpath, S_IRWXU); if (run_view) { mcview_viewer (vfs_path_as_str (file_name_vpath), NULL, 0); dialog_switch_process_pending (); } else { /* execute the command indirectly to allow execution even * on no-exec filesystems. */ char *cmd; cmd = g_strconcat ("/bin/sh ", vfs_path_as_str (file_name_vpath), (char *) NULL); if (!show_prompt) { if (system (cmd) == -1) message (D_ERROR, MSG_ERROR, "%s", _("Error calling program")); } else { shell_execute (cmd, EXECUTE_HIDE); } g_free (cmd); } mc_unlink (file_name_vpath); vfs_path_free (file_name_vpath); }
static void exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line) { char *shell_string, *export_variables; vfs_path_t *temp_file_name_vpath = NULL; int cmd_file_fd; FILE *cmd_file; char *cmd = NULL; g_return_if_fail (lc_data != NULL); pbuffer = NULL; localmtime = 0; quote_func = name_quote; run_view = FALSE; is_cd = FALSE; written_nonspace = FALSE; /* Avoid making a local copy if we are doing a cd */ do_local_copy = !vfs_file_is_local (filename_vpath); shell_string = exec_make_shell_string (lc_data, filename_vpath); if (shell_string == NULL) goto ret; if (is_cd) { exec_extension_cd (); g_free (shell_string); goto ret; } /* * All commands should be run in /bin/sh regardless of user shell. * To do that, create temporary shell script and run it. * Sometimes it's not needed (e.g. for %cd and %view commands), * but it's easier to create it anyway. */ cmd_file_fd = mc_mkstemps (&temp_file_name_vpath, "mcext", SCRIPT_SUFFIX); if (cmd_file_fd == -1) { message (D_ERROR, MSG_ERROR, _("Cannot create temporary command file\n%s"), unix_error_string (errno)); goto ret; } cmd_file = fdopen (cmd_file_fd, "w"); fputs ("#! /bin/sh\n\n", cmd_file); export_variables = exec_get_export_variables (filename_vpath); if (export_variables != NULL) { fprintf (cmd_file, "%s\n", export_variables); g_free (export_variables); } fputs (shell_string, cmd_file); g_free (shell_string); /* * Make the script remove itself when it finishes. * Don't do it for the viewer - it may need to rerun the script, * so we clean up after calling view(). */ if (!run_view) { char *file_name; file_name = vfs_path_to_str (temp_file_name_vpath); fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name); g_free (file_name); } fclose (cmd_file); if ((run_view && !written_nonspace) || is_cd) { mc_unlink (temp_file_name_vpath); vfs_path_free (temp_file_name_vpath); temp_file_name_vpath = NULL; } else { char *file_name; file_name = vfs_path_to_str (temp_file_name_vpath); /* Set executable flag on the command file ... */ mc_chmod (temp_file_name_vpath, S_IRWXU); /* ... but don't rely on it - run /bin/sh explicitly */ cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL); g_free (file_name); } if (run_view) exec_extension_view (cmd, filename_vpath, start_line, temp_file_name_vpath); else { shell_execute (cmd, EXECUTE_INTERNAL); if (mc_global.tty.console_flag != '\0') { handle_console (CONSOLE_SAVE); if (output_lines && mc_global.keybar_visible) show_console_contents (output_start_y, LINES - mc_global.keybar_visible - output_lines - 1, LINES - mc_global.keybar_visible - 1); } } g_free (cmd); exec_cleanup_file_name (filename_vpath, TRUE); ret: vfs_path_free (temp_file_name_vpath); }