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

Avery Terrel averymt at proton.me
Wed Mar 11 03:00:43 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>
---
 toys/pending/sh.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/toys/pending/sh.c b/toys/pending/sh.c
index 664f9cf4..4b326467 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
@@ -3767,6 +3778,8 @@ static void do_prompt(char *prompt)
         pp += snprintf(pp, len, "%ld", TT.ff->lineno);
         continue;
       }
+    } else if (c=='$') {
+
     } else if (c=='\\') {
       cc = *(prompt++);
       if (!cc) {
@@ -5071,6 +5084,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