Here's my take at fixing this in the TODO file:

  reimplement "-P" in a safer way (temp file instead of stdout) - possibly
  security issues.

My solution is to add another option -p that will allow you to
specify a file that the last working directory is written to.

A new function print_last_working_directory was created since
this functionality is needed in two places in main.c.

I also attempted to update the manual page.

Here's a new mc.sh:

mc ()
{
        TMPFILE="`mktemp`"
        /usr/bin/mc -p "$TMPFILE" "$@"
        cd "`cat $TMPFILE`"
        rm -f "$TMPFILE"
        unset TMPFILE;
}

export -f mc

Note that it requires the mktemp command, which is available in
Debian (debianutils 1.7 or later) and OpenBSD 2.1 or later.

At least use the patch as an inspiration... :)

Oskar Liljeblad ([EMAIL PROTECTED])
--- src/main.c.orig     2002-09-05 23:18:11.000000000 +0200
+++ src/main.c  2002-09-05 23:17:35.000000000 +0200
@@ -258,6 +258,7 @@
 
 /* If true, then print on stdout the last directory we were at */
 static int print_last_wd = 0;
+static char *print_last_wd_file = NULL;
 static char *last_wd_string;
 static int print_last_revert = 0;
 
@@ -303,6 +304,56 @@
 /* Used during argument processing */
 int finish_program = 0;
 
+static void
+print_last_working_directory (void)
+{
+#if defined(OS2_NT)
+    /* Note: print_last_wd_file not implemented for OS2/NT */
+
+    if (print_last_wd == 2){
+       FILE *bat_file;
+
+       print_last_wd = 0;
+       bat_file = fopen(batch_file_name, "w");
+       if (bat_file != NULL){
+            /* .ado: \r\n for Win95 */
+           fprintf(bat_file, "@echo off\r\n");
+           if (isalpha(last_wd_string[0]))
+               fprintf(bat_file, "%c:\r\n", last_wd_string[0]);
+           fprintf(bat_file, "cd %s\r\n", last_wd_string);
+           fclose(bat_file);
+       }
+    }
+#endif
+
+    if (print_last_wd || print_last_wd_file) {
+       int fd = -1;
+
+       if (print_last_wd) {
+           fd = stdout_fd;
+       }
+       if (print_last_wd_file) {
+           fd = open(print_last_wd_file, O_WRONLY|O_TRUNC);
+           if (fd < 0) {
+               fprintf(stderr, "%s: cannot open file - %s\n",
+                       print_last_wd_file, strerror(errno));
+           }
+       }
+       if (fd != -1) {
+            if (print_last_revert || edit_one_file || view_one_file)
+               write (fd, ".", 1);
+            else
+               write (fd, last_wd_string, strlen (last_wd_string));
+
+           if (fd != stdout_fd) {
+               close(fd);
+           }
+       }
+
+        g_free (last_wd_string);
+    }
+}
+
 WPanel *
 get_current_panel (void)
 {
@@ -1935,7 +1986,7 @@
     midnight_shutdown = 1;
 
     /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
-    if (print_last_wd) {
+    if (print_last_wd || print_last_wd_file) {
        if (!vfs_current_is_local ())
            last_wd_string = g_strdup (".");
        else
@@ -2154,8 +2205,8 @@
 probably_finish_program (void)
 {
     if (finish_program){
-       if (print_last_wd)
-           printf (".");
+       print_last_revert = 1;  /* to make sure "." is printed */
+       print_last_working_directory ();
        exit (1);
     }
 }
@@ -2268,6 +2319,8 @@
 #endif
     { "printwd",       'P', POPT_ARG_NONE,     &print_last_wd,           0,
       N_("Prints working directory at program exit") },
+    { "printwdtofile",         'p', POPT_ARG_STRING,   &print_last_wd_file,      0,
+      N_("Prints working directory at program exit to a specific file") },
     { "resetsoft",     'k', POPT_ARG_NONE,     &reset_hp_softkeys,       0,
       N_("Resets soft keys on HP terminals") },
     { "slow", 's', POPT_ARG_NONE,              &slow_terminal,           0,
@@ -2595,29 +2648,7 @@
     /* On NT, home_dir is malloced */
     g_free (home_dir);
 #endif
-#if defined(OS2_NT)
-    if (print_last_wd == 2){
-       FILE *bat_file;
-
-       print_last_wd = 0;
-       bat_file = fopen(batch_file_name, "w");
-       if (bat_file != NULL){
-            /* .ado: \r\n for Win95 */
-           fprintf(bat_file, "@echo off\r\n");
-           if (isalpha(last_wd_string[0]))
-               fprintf(bat_file, "%c:\r\n", last_wd_string[0]);
-           fprintf(bat_file, "cd %s\r\n", last_wd_string);
-           fclose(bat_file);
-       }
-    }
-#endif
-    if (print_last_wd) {
-        if (print_last_revert || edit_one_file || view_one_file)
-            write (stdout_fd, ".", 1);
-        else
-           write (stdout_fd, last_wd_string, strlen (last_wd_string));
-       g_free (last_wd_string);
-    }
+    print_last_working_directory ();
 
 #ifndef _OS_NT
     g_free (mc_home);
--- doc/mc.1.in.orig    2002-09-05 23:22:02.000000000 +0200
+++ doc/mc.1.in 2002-09-05 23:21:01.000000000 +0200
@@ -6,7 +6,7 @@
 .\"SKIP_SECTION"
 .SH "USAGE"
 .B mc
-[\-abcCdfhPstuUVx?] [\-l log] [dir1 [dir2]] [-v file]
+[\-abcCdfhpPstuUVx?] [\-l log] [dir1 [dir2]] [-v file]
 .SH "DESCRIPTION"
 .LP
 The Midnight Commander is a directory browser/file manager for
@@ -61,6 +61,10 @@
 .B @prefix@/lib/mc/bin/mc.csh
 (tcsh users) in order to have this function defined.
 .TP
+.I "\-p file"
+Similar to the -P option, but instead of printing the last working
+directory to standard out, it will be written to the specified file.
+.TP
 .I "\-s"
 Turns on the slow terminal mode, in this mode the program will not
 draw expensive line drawing characters and will toggle verbose mode

Reply via email to