This diff makes mg behave more like emacs with regards to opening
a new buffer in two ways:
1. If parent directory is read-only, make buffer read-only.
2. If parent doesn't exist; give user a message and create buffer
as readable.
For point 2, emacs acutally suggests that the user use the
"make-directory" command to make the directory, however, mg doesn't
implement that command (yet), so I have amended the message from
emacs.
ok?
-lum
Index: file.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/file.c,v
retrieving revision 1.81
diff -u -p -r1.81 file.c
--- file.c 18 Jun 2012 09:19:21 -0000 1.81
+++ file.c 27 Aug 2012 18:45:18 -0000
@@ -207,8 +207,10 @@ int
readin(char *fname)
{
struct mgwin *wp;
+ struct stat statbuf;
int status, i, ro = FALSE;
PF *ael;
+ char *dp;
/* might be old */
if (bclear(curbp) != TRUE)
@@ -242,13 +244,29 @@ readin(char *fname)
curbp->b_flag &= ~BFCHG;
/*
- * We need to set the READONLY flag after we insert the file,
- * unless the file is a directory.
+ * Set the buffer READONLY flag if any of following are true:
+ * 1. file is a directory.
+ * 2. file is read-only.
+ * 3. file doesn't exist and directory is read-only.
*/
- if (access(fname, W_OK) && errno != ENOENT)
- ro = TRUE;
- if (fisdir(fname) == TRUE)
+ if (fisdir(fname) == TRUE) {
ro = TRUE;
+ } else if (access(fname, W_OK) == -1) {
+ if (errno != ENOENT)
+ ro = TRUE;
+ else if (errno == ENOENT) {
+ dp = dirname(fname);
+ if (stat(dp, &statbuf) == -1 && errno == ENOENT) {
+ /* no read-only; like emacs */
+ ewprintf("Parent directory missing");
+ } else if (access(dp, W_OK) == -1 &&
+ errno == EACCES) {
+ ewprintf("File not found and directory"
+ " write-protected");
+ ro = TRUE;
+ }
+ }
+ }
if (ro == TRUE)
curbp->b_flag |= BFREADONLY;