/* *INDENT-OFF* */ END_TEST /* *INDENT-ON* */ /* --------------------------------------------------------------------------------------------- */ /* @Test */ /* *INDENT-OFF* */ START_TEST (test_mc_mkstemps) /* *INDENT-ON* */ { /* given */ vfs_path_t *pname_vpath = NULL; char *pname = NULL; char *begin_pname; int fd; /* when */ fd = mc_mkstemps (&pname_vpath, "mctest-", NULL); if (fd != -1) pname = vfs_path_to_str (pname_vpath); begin_pname = g_build_filename (mc_tmpdir (), "mctest-", NULL); /* then */ vfs_path_free (pname_vpath); close (fd); mctest_assert_int_ne (fd, -1); fail_unless (g_file_test (pname, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR), "\nNo such file: %s\n", pname); unlink (pname); fail_unless (strncmp (pname, begin_pname, strlen (begin_pname)) == 0, "\nstart of %s should be equal to %s\n", pname, begin_pname); g_free (pname); }
FBUF * f_temp (void) { int fd; FBUF *fs; fs = f_dopen(0); if (fs == NULL) { return NULL; } fd = mc_mkstemps((char **)&fs->name, "mcdiff", NULL); if (fd < 0) { f_free(fs); return NULL; } fs->fd = fd; fs->flags = FILE_FLAG_TEMP; return fs; }
int vfs_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *param_basename) { const char *p; char *suffix, *q; int shift; int fd; /* Strip directories */ p = strrchr (param_basename, PATH_SEP); if (!p) p = param_basename; else p++; /* Protection against very long names */ shift = strlen (p) - (MC_MAXPATHLEN - 16); if (shift > 0) p += shift; suffix = g_malloc (MC_MAXPATHLEN); /* Protection against unusual characters */ q = suffix; while (*p && (*p != '#')) { if (strchr (".-_@", *p) || isalnum ((unsigned char) *p)) *q++ = *p; p++; } *q = 0; fd = mc_mkstemps (pname_vpath, prefix, suffix); g_free (suffix); return fd; }
const char * mc_tmpdir (void) { static char buffer[64]; static const char *tmpdir; const char *sys_tmp; struct passwd *pwd; struct stat st; const char *error = NULL; /* Check if already correctly initialized */ if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) && st.st_uid == getuid () && (st.st_mode & 0777) == 0700) return tmpdir; sys_tmp = getenv ("TMPDIR"); if (!sys_tmp || sys_tmp[0] != '/') { sys_tmp = TMPDIR_DEFAULT; } pwd = getpwuid (getuid ()); if (pwd) g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name); else g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ()); canonicalize_pathname (buffer); if (lstat (buffer, &st) == 0) { /* Sanity check for existing directory */ if (!S_ISDIR (st.st_mode)) error = _("%s is not a directory\n"); else if (st.st_uid != getuid ()) error = _("Directory %s is not owned by you\n"); else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0)) error = _("Cannot set correct permissions for directory %s\n"); } else { /* Need to create directory */ if (mkdir (buffer, S_IRWXU) != 0) { fprintf (stderr, _("Cannot create temporary directory %s: %s\n"), buffer, unix_error_string (errno)); error = ""; } } if (error != NULL) { int test_fd; char *test_fn, *fallback_prefix; int fallback_ok = 0; if (*error) fprintf (stderr, error, buffer); /* Test if sys_tmp is suitable for temporary files */ fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp); test_fd = mc_mkstemps (&test_fn, fallback_prefix, NULL); g_free (fallback_prefix); if (test_fd != -1) { close (test_fd); test_fd = open (test_fn, O_RDONLY); if (test_fd != -1) { close (test_fd); unlink (test_fn); fallback_ok = 1; } } if (fallback_ok) { fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp); g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp); error = NULL; } else { fprintf (stderr, _("Temporary files will not be created\n")); g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/"); } fprintf (stderr, "%s\n", _("Press any key to continue...")); getc (stdin); } tmpdir = buffer; if (!error) g_setenv ("MC_TMPDIR", tmpdir, TRUE); return tmpdir; }
static void exec_extension (const char *filename, const char *data, int *move_dir, int start_line) { char *file_name; int cmd_file_fd; FILE *cmd_file; char *cmd = NULL; int expand_prefix_found = 0; int parameter_found = 0; char prompt[80]; int run_view = 0; int def_hex_mode = default_hex_mode, changed_hex_mode = 0; int def_nroff_flag = default_nroff_flag, changed_nroff_flag = 0; int written_nonspace = 0; int is_cd = 0; char buffer[1024]; char *p = 0; char *localcopy = NULL; int do_local_copy; time_t localmtime = 0; struct stat mystat; quote_func_t quote_func = name_quote; g_return_if_fail (filename != NULL); g_return_if_fail (data != NULL); /* Avoid making a local copy if we are doing a cd */ if (!vfs_file_is_local (filename)) do_local_copy = 1; else do_local_copy = 0; /* * 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 (&file_name, "mcext", SCRIPT_SUFFIX); if (cmd_file_fd == -1) { message (1, MSG_ERROR, _(" Cannot create temporary command file \n %s "), unix_error_string (errno)); return; } cmd_file = fdopen (cmd_file_fd, "w"); fputs ("#! /bin/sh\n", cmd_file); prompt[0] = 0; for (; *data && *data != '\n'; data++) { if (parameter_found) { if (*data == '}') { char *parameter; parameter_found = 0; parameter = input_dialog (_(" Parameter "), prompt, ""); if (!parameter) { /* User canceled */ fclose (cmd_file); unlink (file_name); if (localcopy) { mc_ungetlocalcopy (filename, localcopy, 0); g_free (localcopy); } g_free (file_name); return; } fputs (parameter, cmd_file); written_nonspace = 1; g_free (parameter); } else { size_t len = strlen (prompt); if (len < sizeof (prompt) - 1) { prompt[len] = *data; prompt[len + 1] = 0; } } } else if (expand_prefix_found) { expand_prefix_found = 0; if (*data == '{') parameter_found = 1; else { int i = check_format_view (data); char *v; if (i) { data += i - 1; run_view = 1; } else if ((i = check_format_cd (data)) > 0) { is_cd = 1; quote_func = fake_name_quote; do_local_copy = 0; p = buffer; data += i - 1; } else if ((i = check_format_var (data, &v)) > 0 && v) { fputs (v, cmd_file); g_free (v); data += i; } else { char *text; if (*data == 'f') { if (do_local_copy) { localcopy = mc_getlocalcopy (filename); if (localcopy == NULL) { fclose (cmd_file); unlink (file_name); g_free (file_name); return; } mc_stat (localcopy, &mystat); localmtime = mystat.st_mtime; text = (*quote_func) (localcopy, 0); } else { text = (*quote_func) (filename, 0); } } else text = expand_format (NULL, *data, !is_cd); if (!is_cd) fputs (text, cmd_file); else { strcpy (p, text); p = strchr (p, 0); } g_free (text); written_nonspace = 1; } } } else { if (*data == '%') expand_prefix_found = 1; else { if (*data != ' ' && *data != '\t') written_nonspace = 1; if (is_cd) *(p++) = *data; else fputc (*data, cmd_file); } } } /* for */ /* * 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) { fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name); } fclose (cmd_file); if ((run_view && !written_nonspace) || is_cd) { unlink (file_name); g_free (file_name); file_name = NULL; } else { /* Set executable flag on the command file ... */ chmod (file_name, S_IRWXU); /* ... but don't rely on it - run /bin/sh explicitly */ cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL); } if (run_view) { altered_hex_mode = 0; altered_nroff_flag = 0; if (def_hex_mode != default_hex_mode) changed_hex_mode = 1; if (def_nroff_flag != default_nroff_flag) changed_nroff_flag = 1; /* If we've written whitespace only, then just load filename * into view */ if (written_nonspace) { view (cmd, filename, move_dir, start_line); unlink (file_name); } else { view (0, filename, move_dir, start_line); } if (changed_hex_mode && !altered_hex_mode) default_hex_mode = def_hex_mode; if (changed_nroff_flag && !altered_nroff_flag) default_nroff_flag = def_nroff_flag; repaint_screen (); } else if (is_cd) { char *q; *p = 0; p = buffer; /* while (*p == ' ' && *p == '\t') * p++; */ /* Search last non-space character. Start search at the end in order not to short filenames containing spaces. */ q = p + strlen (p) - 1; while (q >= p && (*q == ' ' || *q == '\t')) q--; q[1] = 0; do_cd (p, cd_parse_command); } else { shell_execute (cmd, EXECUTE_INTERNAL); if (console_flag) { handle_console (CONSOLE_SAVE); if (output_lines && keybar_visible) { show_console_contents (output_start_y, LINES - keybar_visible - output_lines - 1, LINES - keybar_visible - 1); } } } g_free (file_name); g_free (cmd); if (localcopy) { mc_stat (localcopy, &mystat); mc_ungetlocalcopy (filename, localcopy, localmtime != mystat.st_mtime); g_free (localcopy); } }
/* FIXME: recode this routine on version 3.0, it could be cleaner */ static void execute_menu_command (WEdit *edit_widget, const char *commands) { FILE *cmd_file; int cmd_file_fd; int expand_prefix_found = 0; char *parameter = 0; int do_quote = 0; char prompt [80]; int col; char *file_name; int run_view = 0; /* Skip menu entry title line */ commands = strchr (commands, '\n'); if (!commands){ return; } cmd_file_fd = mc_mkstemps (&file_name, "mcusr", SCRIPT_SUFFIX); if (cmd_file_fd == -1){ message (1, MSG_ERROR, _(" Cannot create temporary command file \n %s "), unix_error_string (errno)); 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 == '}'){ char *tmp; *parameter = 0; parameter = input_dialog (_(" Parameter "), prompt, INPUT_LAST_TEXT); if (!parameter){ /* User canceled */ fclose (cmd_file); unlink (file_name); g_free (file_name); return; } if (do_quote) { fputs (tmp = name_quote (parameter, 0), cmd_file); g_free (tmp); } else fputs (parameter, cmd_file); g_free (parameter); parameter = 0; } else { if (parameter < &prompt [sizeof (prompt) - 1]) { *parameter++ = *commands; } } } else if (expand_prefix_found){ expand_prefix_found = 0; if (isdigit ((unsigned char) *commands)) { do_quote = atoi (commands); while (isdigit ((unsigned char) *commands)) commands++; } if (*commands == '{') parameter = 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 = 1; /* Default: Quote expanded macro */ expand_prefix_found = 1; } } else fputc (*commands, cmd_file); } } fclose (cmd_file); chmod (file_name, S_IRWXU); if (run_view) { run_view = 0; mc_internal_viewer (file_name, NULL, &run_view, 0); #ifdef USE_DLGSWITCH dlgswitch_process_pending(); #endif } else { /* execute the command indirectly to allow execution even * on no-exec filesystems. */ char *cmd = g_strconcat("/bin/sh ", file_name, (char *)NULL); shell_execute (cmd, EXECUTE_HIDE); g_free(cmd); } unlink (file_name); g_free (file_name); }
static vfs_path_t * exec_extension (void *target, const vfs_path_t * filename_vpath, const char *lc_data, int start_line) { char *shell_string, *export_variables; vfs_path_t *script_vpath = NULL; int cmd_file_fd; FILE *cmd_file; char *cmd = NULL; g_return_val_if_fail (lc_data != NULL, 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 (&script_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) fprintf (cmd_file, "\n/bin/rm -f %s\n", vfs_path_as_str (script_vpath)); fclose (cmd_file); if ((run_view && !written_nonspace) || is_cd) { exec_cleanup_script (script_vpath); script_vpath = NULL; } else { /* Set executable flag on the command file ... */ mc_chmod (script_vpath, S_IRWXU); /* ... but don't rely on it - run /bin/sh explicitly */ cmd = g_strconcat ("/bin/sh ", vfs_path_as_str (script_vpath), (char *) NULL); } if (run_view) { /* If we've written whitespace only, then just load filename into view */ if (!written_nonspace) exec_extension_view (target, NULL, filename_vpath, start_line); else exec_extension_view (target, cmd, filename_vpath, start_line); } 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: return script_vpath; }
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); }