Files
oasis-linux-mirror/pkg/rc/patch/0003-implement-umask-builtin-on-unix.patch

128 lines
2.6 KiB
Diff

From 0f88ba02c7bbcb91b428bfb294831769e521a81b Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Sun, 19 Apr 2026 14:11:15 -0700
Subject: [PATCH] implement umask builtin on unix
---
exec.h | 2 +-
fns.h | 1 +
simple.c | 2 +-
unix.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/exec.h b/exec.h
index 69b1004..ee8edab 100644
--- a/exec.h
+++ b/exec.h
@@ -77,7 +77,7 @@ extern void (*builtinfunc(char *name))(void);
void execcd(void), execwhatis(void), execeval(void), execexec(void);
int execforkexec(void);
void execexit(void), execshift(void);
-void execwait(void), execumask(void), execdot(void), execflag(void);
+void execwait(void), execdot(void), execflag(void);
void execfunc(var*), execcmds(io*, char*, var*, redir*);
void startfunc(var*, word*, var*, redir*);
diff --git a/fns.h b/fns.h
index 339f08b..1644d1e 100644
--- a/fns.h
+++ b/fns.h
@@ -45,6 +45,7 @@ int havewaitpid(int);
int idchr(int);
void inttoascii(char*, int);
void kinit(void);
+int mapfd(int);
int match(char*, char*, int);
char* makepath(char*, char*);
void panic(char*, int);
diff --git a/simple.c b/simple.c
index 362b77b..1a9d27d 100644
--- a/simple.c
+++ b/simple.c
@@ -287,7 +287,7 @@ execshift(void)
poplist();
}
-static int
+int
mapfd(int fd)
{
redir *rp;
diff --git a/unix.c b/unix.c
index 05d883c..cbb2054 100644
--- a/unix.c
+++ b/unix.c
@@ -12,9 +12,12 @@
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
+#include <limits.h>
+#include <sys/stat.h>
#include <sys/wait.h>
static void execfinit(void);
+static void execumask(void);
builtin Builtin[] = {
"cd", execcd,
@@ -24,6 +27,7 @@ builtin Builtin[] = {
"exit", execexit,
"shift", execshift,
"wait", execwait,
+ "umask", execumask,
".", execdot,
"flag", execflag,
"finit", execfinit,
@@ -81,6 +85,48 @@ execfinit(void)
start(rdfns, 2, runq->local, runq->redir);
}
+static int
+octal(char *s)
+{
+ int n = 0;
+
+ while(n<INT_MAX/8 && '0'<=*s && *s<='7')
+ n = n*8+*s++-'0';
+ return *s=='\0' ? n : -1;
+}
+
+static void
+execumask(void)
+{
+ int m;
+ struct io *out;
+ char *e;
+
+ switch(count(runq->argv->words)){
+ case 2:
+ m = octal(runq->argv->words->next->word);
+ if(m>=0 && m<=07777) {
+ umask(m);
+ break;
+ }
+ /* fallthrough */
+ default:
+ pfmt(err, "Usage: umask [umask]\n");
+ setstatus("umask usage");
+ poplist();
+ return;
+ case 1:
+ umask(m = umask(0));
+ out = openiofd(mapfd(1));
+ pfmt(out, "%o\n", m);
+ flushio(out);
+ free(closeiostr(out));
+ break;
+ }
+ setstatus("");
+ poplist();
+}
+
static int
cmpenv(const void *aa, const void *ab)
{
--
2.49.0