Previous patch shall be ignored, as it was an ugly hack. Below is a patch that 
is simpler and fixes expandtilde instead, so it fixes the problem in other 
situations (writing files to ~, for example). The only thing that I'm not sure 
is whether to use getuid or geteuid. Any suggestion / explanation is welcome.

Index: usr.bin/mg/fileio.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.103
diff -u -p -u -p -r1.103 fileio.c
--- usr.bin/mg/fileio.c 28 Jul 2016 21:40:25 -0000      1.103
+++ usr.bin/mg/fileio.c 18 May 2017 17:53:03 -0000
@@ -723,15 +723,12 @@ expandtilde(const char *fn)
                return(ret);
        }
        if (ulen == 0) { /* ~/ or ~ */
-               if ((un = getlogin()) != NULL)
-                       (void)strlcpy(user, un, sizeof(user));
-               else
-                       user[0] = '\0';
+               pw = getpwuid(getuid());
        } else { /* ~user/ or ~user */
                memcpy(user, &fn[1], ulen);
                user[ulen] = '\0';
+               pw = getpwnam(user);
        }
-       pw = getpwnam(user);
        if (pw != NULL) {
                plen = strlcpy(path, pw->pw_dir, sizeof(path));
                if (plen == 0 || path[plen - 1] != '/') {


On 13/05/17 01:32, Lucas Gabriel Vuotto wrote:
Sorry, space got mangled in previous email.

Index: fileio.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.103
diff -u -p -u -p -r1.103 fileio.c
--- fileio.c    28 Jul 2016 21:40:25 -0000      1.103
+++ fileio.c    13 May 2017 04:15:15 -0000
@@ -641,14 +641,15 @@ bkuplocation(const char *fn)
 int
 backuptohomedir(int f, int n)
 {
-       const char      *c = _PATH_MG_DIR;
-       char            *p;
+       char    *home;

        if (bkupdir == NULL) {
-               p = adjustname(c, TRUE);
-               bkupdir = strndup(p, NFILEN);
-               if (bkupdir == NULL)
-                       return(FALSE);
+               if ((home = getenv("HOME")) == NULL || *home == '\0')
+                       return (FALSE);
+               if (asprintf(&bkupdir, _PATH_MG_DIR, home) == -1) {
+                       bkupdir = NULL;
+                       return (FALSE);
+               }

                if (mkdir(bkupdir, 0700) == -1 && errno != EEXIST) {
                        free(bkupdir);
@@ -736,7 +737,7 @@ expandtilde(const char *fn)
                plen = strlcpy(path, pw->pw_dir, sizeof(path));
                if (plen == 0 || path[plen - 1] != '/') {
                        if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
-                               dobeep();                               
+                               dobeep();
                                ewprintf("Path too long");
                                return (NULL);
                        }
Index: pathnames.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/pathnames.h,v
retrieving revision 1.1
diff -u -p -u -p -r1.1 pathnames.h
--- pathnames.h 18 Jun 2012 07:14:55 -0000      1.1
+++ pathnames.h 13 May 2017 04:15:15 -0000
@@ -6,6 +6,6 @@
  *     standard path names
  */

-#define        _PATH_MG_DIR            "~/.mg.d"
+#define        _PATH_MG_DIR            "%s/.mg.d"
 #define        _PATH_MG_STARTUP        "%s/.mg"
 #define        _PATH_MG_TERM           "%s/.mg-%s"


Reply via email to