[Toybox] [PATCH v2] sh.c: add umask builtin

Avery Terrel averymt at proton.me
Wed Mar 11 03:13:40 PDT 2026


POSIX specifies -S, and Bash also has -p, but I haven't encountered
anything that uses them so I didn't implement them.

Signed-off-by: Avery Terrel <avery at averymt.zip>
---
Was messing with the prompt and accidentally committed changes from that
/me facepalms

 toys/pending/sh.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/toys/pending/sh.c b/toys/pending/sh.c
index 664f9cf4..7b1cde14 100644
--- a/toys/pending/sh.c
+++ b/toys/pending/sh.c
@@ -62,6 +62,7 @@ USE_SH(NEWTOY(shift, ">1", TOYFLAG_NOFORK))
 USE_SH(NEWTOY(source, "<1", TOYFLAG_NOFORK))
 USE_SH(OLDTOY(., source, TOYFLAG_NOFORK))
 USE_SH(NEWTOY(trap, "lp", TOYFLAG_NOFORK))
+USE_SH(NEWTOY(umask, ">1", TOYFLAG_NOFORK))
 USE_SH(NEWTOY(unalias, "<1a", TOYFLAG_NOFORK))
 USE_SH(NEWTOY(unset, "fvn[!fv]", TOYFLAG_NOFORK))
 USE_SH(NEWTOY(wait, "n", TOYFLAG_NOFORK))
@@ -345,6 +346,16 @@ config TRAP
     The special signal EXIT gets called before the shell exits, RETURN when
     a function or source returns, and DEBUG is called before each command.

+config UMASK
+bool
+  default n
+  depends on SH
+  help
+    usage: umask [mask]
+
+    Sets the file creation mode mask.
+    An empty mask causes the current mask to be printed.
+
 config UNALIAS
   bool
   default n
@@ -5071,6 +5082,15 @@ void source_main(void)
   TT.ff->arg.c = ii;
 }

+void umask_main(void)
+{
+  if (toys.optc) {
+    toys.old_umask = string_to_mode(*toys.optargs, 0);
+  } else {
+    printf("%04o\n", umask(0));
+  }
+}
+
 #define FOR_unalias
 #include "generated/flags.h"

--
2.51.0


More information about the Toybox mailing list