mg bug under WSL?

I am not a fan of the emacs default behaviour of storing backup files in the same directory as the original file, suffixed with a ~. mg, being a lightweight emacs, does the same, but this behaviour can be changed with the command backup-to-home-directory, which puts the backup files in ${HOME}/.mg.d.

I created the mg rc file, .mg with the contents:

  backup-to-home-directory
  global-set-key "\^cg" goto-line

However, this did not change the behaviour of mg. Backups were still being placed in the directory containing the edited file. I suspected this might be some side-effect of running inside WSL, so I needed the source code of mg to investigate. This can be obtained via the Debian packages repository.

The culprit turned out to be the function expandtilde, which uses getlogin to obtain the user name. However, under WSL, the function returned NULL. My guess this is because the controlling terminal is invoked by the preferred terminal emulator, wsltty, or its helper wslbridge. I followed the advice on the getlogin man page and used getenv to grab the value of LOGNAME>. The following unified diff shows the patch:

   --- fileio_orig.c  2018-03-30 12:00:37.169010300 +0100
   +++ fileio.c       2018-03-30 12:03:56.435206400 +0100
   @@ -726,7 +726,7 @@
                  return(ret);
          }
          if (ulen == 0) { /* ~/ or ~ */
  -               if ((un = getlogin()) != NULL)
  +               if ((un = getenv("LOGNAME")) != NULL)
                          (void)strlcpy(user, un, sizeof(user));
                  else
                          user[0] = '\0';

Note that mg obtains the user's home directory from /etc/passwd, so home/mark is the location for .mg.d, rather than using my faked setting of ${HOME} (see previous journal entry).