summaryrefslogtreecommitdiff
path: root/bin/ksh
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2005-10-06 06:39:37 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2005-10-06 06:39:37 +0000
commit65c4205b299c9f25669fee2e37fae88dede57b82 (patch)
tree4bd1e61bfa82e6136fea29686cb176c30fd229a4 /bin/ksh
parent09ef6f24b12e5f04eef67780b8ee7f3e95c2c9a2 (diff)
Introducing mknod as a built-in. It might be against the unix
philosophy, but in this case it's worth it. ok deraadt@
Diffstat (limited to 'bin/ksh')
-rw-r--r--bin/ksh/Makefile7
-rw-r--r--bin/ksh/c_sh.c56
-rw-r--r--bin/ksh/ksh.119
-rw-r--r--bin/ksh/mknod.c105
-rw-r--r--bin/ksh/proto.h5
-rw-r--r--bin/ksh/sh.119
6 files changed, 204 insertions, 7 deletions
diff --git a/bin/ksh/Makefile b/bin/ksh/Makefile
index c89691b695b..f25b597ec60 100644
--- a/bin/ksh/Makefile
+++ b/bin/ksh/Makefile
@@ -1,9 +1,10 @@
-# $OpenBSD: Makefile,v 1.24 2005/03/30 17:16:37 deraadt Exp $
+# $OpenBSD: Makefile,v 1.25 2005/10/06 06:39:35 otto Exp $
PROG= ksh
SRCS= alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c \
- exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c misc.c path.c \
- shf.c syn.c table.c trap.c tree.c tty.c var.c version.c vi.c
+ exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c mknod.c \
+ misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
+ version.c vi.c
DEFS= -Wall
CFLAGS+=${DEFS} -I. -I${.CURDIR}
diff --git a/bin/ksh/c_sh.c b/bin/ksh/c_sh.c
index ebea7ef17dd..e731fdc1e5b 100644
--- a/bin/ksh/c_sh.c
+++ b/bin/ksh/c_sh.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: c_sh.c,v 1.29 2005/03/30 17:16:37 deraadt Exp $ */
+/* $OpenBSD: c_sh.c,v 1.30 2005/10/06 06:39:35 otto Exp $ */
/*
* built-in Bourne commands
@@ -835,6 +835,59 @@ c_exec(char **wp)
return 0;
}
+static int
+c_mknod(char **wp)
+{
+ int argc, optc, ismkfifo = 0, ret;
+ char **argv;
+ void *set = NULL;
+ mode_t mode = 0, oldmode;
+
+ while ((optc = ksh_getopt(wp, &builtin_opt, "m:")) != EOF) {
+ switch (optc) {
+ case 'm':
+ set = setmode(builtin_opt.optarg);
+ if (set == NULL) {
+ bi_errorf("invalid file mode");
+ return 1;
+ }
+ mode = getmode(set, DEFFILEMODE);
+ free(set);
+ break;
+ default:
+ goto usage;
+ }
+ }
+ argv = &wp[builtin_opt.optind];
+ if (argv[0] == '\0')
+ goto usage;
+ for (argc = 0; argv[argc]; argc++)
+ ;
+ if (argc == 2 && argv[1][0] == 'p') {
+ ismkfifo = 1;
+ argc--;
+ } else if (argc != 4)
+ goto usage;
+
+ if (set)
+ oldmode = umask(0);
+ else
+ mode = DEFFILEMODE;
+
+ if (ismkfifo)
+ ret = domkfifo(argc, argv, mode);
+ else
+ ret = domknod(argc, argv, mode);
+
+ if (set)
+ umask(oldmode);
+ return ret;
+usage:
+ bi_errorf("usage: mknod [-m mode] name [b | c] major minor");
+ bi_errorf("usage: mknod [-m mode] name p");
+ return 1;
+}
+
/* dummy function, special case in comexec() */
int
c_builtin(char **wp)
@@ -873,5 +926,6 @@ const struct builtin shbuiltins [] = {
{"ulimit", c_ulimit},
{"+umask", c_umask},
{"*=unset", c_unset},
+ {"mknod", c_mknod},
{NULL, NULL}
};
diff --git a/bin/ksh/ksh.1 b/bin/ksh/ksh.1
index 819ab7892fe..d76ec3fad69 100644
--- a/bin/ksh/ksh.1
+++ b/bin/ksh/ksh.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: ksh.1,v 1.103 2005/09/07 07:38:51 jmc Exp $
+.\" $OpenBSD: ksh.1,v 1.104 2005/10/06 06:39:35 otto Exp $
.\"
.\" Public Domain
.\"
@@ -3261,6 +3261,23 @@ is syntactic sugar for
.No let \&" Ns Ar expr Ns \&" .
.Pp
.It Xo
+.Ic mknod
+.Op Fl m Ar mode
+.Ar name
+.Op Cm c | Cm b
+.Ar major minor
+.Xc
+.It Xo
+.Ic mknod
+.Op Fl m Ar mode
+.Ar name
+.Cm p
+.Xc
+Equivalent to the
+.Xr mknod 8
+command.
+.Pp
+.It Xo
.Ic print
.Oo Fl nprsu Ns Oo Ar n Oc \*(Ba
.Fl R Op Fl en Oc
diff --git a/bin/ksh/mknod.c b/bin/ksh/mknod.c
new file mode 100644
index 00000000000..a336a867047
--- /dev/null
+++ b/bin/ksh/mknod.c
@@ -0,0 +1,105 @@
+/* $OpenBSD: mknod.c,v 1.1 2005/10/06 06:39:36 otto Exp $ */
+/* $NetBSD: mknod.c,v 1.8 1995/08/11 00:08:18 jtc Exp $ */
+
+/*
+ * Copyright (c) 1989, 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Kevin Fall.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+static const char copyright[] =
+"@(#) Copyright (c) 1989, 1993\n\
+ The Regents of the University of California. All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)mknod.c 8.1 (Berkeley) 6/5/93";
+#else
+static const char rcsid[] = "$OpenBSD: mknod.c,v 1.1 2005/10/06 06:39:36 otto Exp $";
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include "sh.h"
+
+int
+domknod(int argc, char **argv, mode_t mode)
+{
+ dev_t dev;
+ char *endp;
+ u_int major, minor;
+
+ if (argv[1][0] == 'c')
+ mode |= S_IFCHR;
+ else if (argv[1][0] == 'b')
+ mode |= S_IFBLK;
+ else {
+ bi_errorf("node must be type 'b' or 'c'.");
+ return 1;
+ }
+
+ major = (long)strtoul(argv[2], &endp, 0);
+ if (endp == argv[2] || *endp != '\0') {
+ bi_errorf("non-numeric major number.");
+ return 1;
+ }
+ minor = (long)strtoul(argv[3], &endp, 0);
+ if (endp == argv[3] || *endp != '\0') {
+ bi_errorf("non-numeric minor number.");
+ return 1;
+ }
+ dev = makedev(major, minor);
+ if (major(dev) != major || minor(dev) != minor) {
+ bi_errorf("major or minor number too large");
+ return 1;
+ }
+ if (mknod(argv[0], mode, dev) < 0) {
+ bi_errorf("%s: %s", argv[0], strerror(errno));
+ return 1;
+ }
+ return 0;
+}
+
+int
+domkfifo(int argc, char **argv, mode_t mode)
+{
+ int rv = 0;
+
+ if (mkfifo(argv[0], mode) < 0) {
+ bi_errorf("%s: %s", argv[0], strerror(errno));
+ rv = 1;
+ }
+ return(rv);
+}
+
diff --git a/bin/ksh/proto.h b/bin/ksh/proto.h
index 38596de07b2..38d33b91b1e 100644
--- a/bin/ksh/proto.h
+++ b/bin/ksh/proto.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: proto.h,v 1.26 2005/03/28 21:28:22 deraadt Exp $ */
+/* $OpenBSD: proto.h,v 1.27 2005/10/06 06:39:36 otto Exp $ */
/*
* prototypes for PD-KSH
@@ -195,6 +195,9 @@ int strip_nuls(char *, int);
int blocking_read(int, char *, int);
int reset_nonblock(int);
char *ksh_get_wd(char *, int);
+/* mknod.c */
+int domknod(int, char **, mode_t);
+int domkfifo(int, char **, mode_t);
/* path.c */
int make_path(const char *, const char *, char **, XString *, int *);
void simplify_path(char *);
diff --git a/bin/ksh/sh.1 b/bin/ksh/sh.1
index a77f32679df..7ca34ba65a0 100644
--- a/bin/ksh/sh.1
+++ b/bin/ksh/sh.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: sh.1,v 1.69 2005/09/07 07:38:51 jmc Exp $
+.\" $OpenBSD: sh.1,v 1.70 2005/10/06 06:39:36 otto Exp $
.\"
.\" Public Domain
.\"
@@ -2546,6 +2546,23 @@ If no arguments are specified, a list of all the signals, their numbers, and
a short description of them are printed.
.Pp
.It Xo
+.Ic mknod
+.Op Fl m Ar mode
+.Ar name
+.Op Cm c | Cm b
+.Ar major minor
+.Xc
+.It Xo
+.Ic mknod
+.Op Fl m Ar mode
+.Ar name
+.Cm p
+.Xc
+Equivalent to the
+.Xr mknod 8
+command.
+.Pp
+.It Xo
.Ic print
.Oo Fl nrsu Ns Oo Ar n Oc \*(Ba
.Fl R Op Fl en Oc