Should this be "if (fname != NULL)"? Some other callers of adjustname() check for a NULL pointer. strlen(NULL) would crash by SIGSEGV; strlen("") would return 0, because "" is a non-NULL pointer to an ASCII NUL '\0'.
I agree. Amended diff below. Index: def.h =================================================================== RCS file: /cvs/src/usr.bin/mg/def.h,v retrieving revision 1.168 diff -u -p -r1.168 def.h --- def.h 1 Mar 2021 10:51:14 -0000 1.168 +++ def.h 19 Mar 2021 06:02:05 -0000 @@ -363,6 +363,7 @@ int ask_makedir(void); /* dired.c */ struct buffer *dired_(char *); +int dired_jump(int, int); int do_dired(char *); /* file.c X */ Index: dired.c =================================================================== RCS file: /cvs/src/usr.bin/mg/dired.c,v retrieving revision 1.98 diff -u -p -r1.98 dired.c --- dired.c 5 Mar 2021 16:16:53 -0000 1.98 +++ dired.c 19 Mar 2021 06:02:05 -0000 @@ -53,6 +53,7 @@ static int d_refreshbuffer(int, int); static int d_filevisitalt(int, int); static int d_gotofile(int, int); static void reaper(int); +static int gotofile(char*); static struct buffer *refreshbuffer(struct buffer *); static int createlist(struct buffer *); static void redelete(struct buffer *); @@ -1091,13 +1092,38 @@ createlist(struct buffer *bp) } int +dired_jump(int f, int n) +{ + char dname[NFILEN], *fname; + struct buffer *bp; + int ret; + + if (getbufcwd(dname, sizeof(dname)) != TRUE) + return (FALSE); + + fname = curbp->b_fname; + + if ((bp = dired_(dname)) == NULL) + return (FALSE); + curbp = bp; + + ret = showbuffer(bp, curwp, WFFULL | WFMODE); + if (ret != TRUE) + return ret; + + fname = adjustname(fname, TRUE); + if (fname != NULL) + gotofile(fname); + + return (TRUE); +} + +int d_gotofile(int f, int n) { - struct line *lp, *nlp; size_t lenfpath; - char fpath[NFILEN], fname[NFILEN]; - char *p, *fpth, *fnp = NULL; - int tmp; + char fpath[NFILEN]; + char *fpth, *fnp = NULL; if (getbufcwd(fpath, sizeof(fpath)) != TRUE) fpath[0] = '\0'; @@ -1114,8 +1140,18 @@ d_gotofile(int f, int n) ewprintf("No file to find"); /* Current directory given so */ return (TRUE); /* return at present location. */ } + return gotofile(fpth); +} + +int +gotofile(char *fpth) +{ + struct line *lp, *nlp; + char fname[NFILEN]; + char *p; + int tmp; + (void)xbasename(fname, fpth, NFILEN); - curbp = curwp->w_bufp; tmp = 0; for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) { tmp++; Index: funmap.c =================================================================== RCS file: /cvs/src/usr.bin/mg/funmap.c,v retrieving revision 1.59 diff -u -p -r1.59 funmap.c --- funmap.c 11 Jul 2019 18:20:18 -0000 1.59 +++ funmap.c 19 Mar 2021 06:02:05 -0000 @@ -98,6 +98,7 @@ static struct funmap functnames[] = { {desckey, "describe-key-briefly", 1}, {diffbuffer, "diff-buffer-with-file", 0}, {digit_argument, "digit-argument", 1}, + {dired_jump, "dired-jump", 1}, {lowerregion, "downcase-region", 0}, {lowerword, "downcase-word", 0}, {showversion, "emacs-version", 0}, Index: keymap.c =================================================================== RCS file: /cvs/src/usr.bin/mg/keymap.c,v retrieving revision 1.58 diff -u -p -r1.58 keymap.c --- keymap.c 29 Dec 2015 19:44:32 -0000 1.58 +++ keymap.c 19 Mar 2021 06:02:05 -0000 @@ -129,7 +129,8 @@ static PF cXcB[] = { ctrlg /* ^G */ }; -static PF cXcL[] = { +static PF cXcJ[] = { + dired_jump, /* ^J */ lowerregion, /* ^L */ rescan, /* ^M */ rescan, /* ^N */ @@ -198,7 +199,7 @@ struct KEYMAPE (6) cXmap = { CCHR('B'), CCHR('G'), cXcB, NULL }, { - CCHR('L'), CCHR('X'), cXcL, NULL + CCHR('J'), CCHR('X'), cXcJ, NULL }, { '(', ')', cXlp, NULL Index: mg.1 =================================================================== RCS file: /cvs/src/usr.bin/mg/mg.1,v retrieving revision 1.120 diff -u -p -r1.120 mg.1 --- mg.1 23 Feb 2021 18:45:33 -0000 1.120 +++ mg.1 19 Mar 2021 06:02:05 -0000 @@ -198,6 +198,8 @@ list-buffers save-buffers-kill-emacs .It C-x C-f find-file +.It C-x C-j +dired-jump .It C-x C-g keyboard-quit .It C-x C-l @@ -523,6 +525,8 @@ Display the name of the function current View the differences between buffer and its associated file. .It digit-argument Process a numerical argument for keyboard-invoked functions. +.It dired-jump +Open a dired buffer containing the current buffer's directory location. .It downcase-region Set all characters in the region to lower case. .It downcase-word