summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/mg/def.h3
-rw-r--r--usr.bin/mg/dir.c76
-rw-r--r--usr.bin/mg/file.c7
-rw-r--r--usr.bin/mg/funmap.c3
-rw-r--r--usr.bin/mg/mg.16
5 files changed, 87 insertions, 8 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index 8a0df64e3e3..65e565b38b0 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.130 2012/11/27 19:45:01 jasper Exp $ */
+/* $OpenBSD: def.h,v 1.131 2012/11/27 19:46:46 jasper Exp $ */
/* This file is in the public domain. */
@@ -335,6 +335,7 @@ void dirinit(void);
int changedir(int, int);
int showcwdir(int, int);
int getcwdir(char *, size_t);
+int makedir(int, int);
/* dired.c */
struct buffer *dired_(char *);
diff --git a/usr.bin/mg/dir.c b/usr.bin/mg/dir.c
index 235277398ad..56867a57615 100644
--- a/usr.bin/mg/dir.c
+++ b/usr.bin/mg/dir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dir.c,v 1.19 2008/06/13 20:07:40 kjell Exp $ */
+/* $OpenBSD: dir.c,v 1.20 2012/11/27 19:46:46 jasper Exp $ */
/* This file is in the public domain. */
@@ -9,6 +9,8 @@
* Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987
*/
+#include <sys/stat.h>
+
#include "def.h"
static char mgcwd[NFILEN];
@@ -75,3 +77,75 @@ getcwdir(char *buf, size_t len)
return (TRUE);
}
+
+/* Create the directory and it's parents. */
+/* ARGSUSED */
+int
+makedir(int f, int n)
+{
+ struct stat sb;
+ int finished, ishere;
+ mode_t dir_mode, mode, oumask;
+ char bufc[NFILEN], path[MAXPATHLEN];
+ char *slash, *tpath, *fpath;
+
+ if (getbufcwd(bufc, sizeof(bufc)) != TRUE)
+ return (ABORT);
+ if ((tpath = eread("Make directory: ", bufc, NFILEN,
+ EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
+ return (ABORT);
+ else if (tpath[0] == '\0')
+ return (FALSE);
+
+ if ((fpath = expandtilde(tpath)) == NULL)
+ return (FALSE);
+
+ (void)strlcpy(path, fpath, sizeof(path));
+ free(fpath);
+
+ slash = path;
+ oumask = umask(0);
+ mode = 0777 & ~oumask;
+ dir_mode = mode | S_IWUSR | S_IXUSR;
+
+ for (;;) {
+ slash += strspn(slash, "/");
+ slash += strcspn(slash, "/");
+
+ finished = (*slash == '\0');
+ *slash = '\0';
+
+ ishere = !stat(path, &sb);
+ if (!finished && ishere && S_ISDIR(sb.st_mode)) {
+ *slash = '/';
+ continue;
+ }
+
+ if (mkdir(path, finished ? mode : dir_mode) == 0) {
+ if (mode > 0777 && chmod(path, mode) < 0) {
+ umask(oumask);
+ return (ABORT);
+ }
+ } else {
+ if (!ishere || !S_ISDIR(sb.st_mode)) {
+ if (!ishere)
+ ewprintf("Creating directory: "
+ "permission denied, %s", path);
+ else
+ eerase();
+
+ umask(oumask);
+ return (FALSE);
+ }
+ }
+
+ if (finished)
+ break;
+
+ *slash = '/';
+ }
+
+ eerase();
+ umask(oumask);
+ return (TRUE);
+}
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c
index 8c1297b586f..30d8b974aaf 100644
--- a/usr.bin/mg/file.c
+++ b/usr.bin/mg/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.84 2012/08/30 21:36:48 lum Exp $ */
+/* $OpenBSD: file.c,v 1.85 2012/11/27 19:46:46 jasper Exp $ */
/* This file is in the public domain. */
@@ -258,13 +258,14 @@ readin(char *fname)
dp = dirname(fname);
if (stat(dp, &statbuf) == -1 && errno == ENOENT) {
/* no read-only; like emacs */
- ewprintf("Parent directory missing");
+ ewprintf("Use M-x make-directory RET RET to "
+ "create the directory and it's parents");
} else if (access(dp, W_OK) == -1 &&
errno == EACCES) {
ewprintf("File not found and directory"
" write-protected");
ro = TRUE;
- }
+ }
}
}
if (ro == TRUE)
diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c
index 9d25fe530a3..80e2e2b4148 100644
--- a/usr.bin/mg/funmap.c
+++ b/usr.bin/mg/funmap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: funmap.c,v 1.42 2012/11/20 13:10:16 florian Exp $ */
+/* $OpenBSD: funmap.c,v 1.43 2012/11/27 19:46:46 jasper Exp $ */
/* This file is in the public domain */
@@ -122,6 +122,7 @@ static struct funmap functnames[] = {
{localbind, "local-set-key",},
{localunbind, "local-unset-key",},
{makebkfile, "make-backup-files",},
+ {makedir, "make-directory",},
{markbuffer, "mark-whole-buffer",},
{do_meta, "meta-key-mode",}, /* better name, anyone? */
{negative_argument, "negative-argument",},
diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1
index 42411c6d12e..300c28519dc 100644
--- a/usr.bin/mg/mg.1
+++ b/usr.bin/mg/mg.1
@@ -1,7 +1,7 @@
-.\" $OpenBSD: mg.1,v 1.71 2012/11/13 22:03:44 florian Exp $
+.\" $OpenBSD: mg.1,v 1.72 2012/11/27 19:46:46 jasper Exp $
.\" This file is in the public domain.
.\"
-.Dd $Mdocdate: November 13 2012 $
+.Dd $Mdocdate: November 27 2012 $
.Dt MG 1
.Os
.Sh NAME
@@ -662,6 +662,8 @@ Bind a key mapping in the local (topmost) mode.
Unbind a key mapping in the local (topmost) mode.
.It make-backup-files
Toggle generation of backup files.
+.It make-directory
+Prompt the user for a path or directory name which is then created.
.It mark-whole-buffer
Marks whole buffer as a region by putting dot at the beginning and mark
at the end of buffer.