SETMODE(3)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… SETMODE(3) β€” the mode-bits glow-up page (Library Functions Manual)

okay bestie, so this is a libc manpage, manual section 3, and it's literally about getmode and setmode β€” the little duo that lets you mess with mode bits without doing the whole chmod thing by hand. sit down, hydrate, let's go 🫢


✨ NAME ✨

getmode, setmode β€” modify mode bits πŸ’–

πŸ“š LIBRARY πŸ“š

Standard C Library β€” that's libc, link it with -lc, no cap.

πŸŽ€ SYNOPSIS πŸŽ€

#include <unistd.h>

mode_t
getmode(const void *set, mode_t mode);

void *
setmode(const char *mode_str);

🌸 DESCRIPTION 🌸

so getmode() gives you back a copy of the file permission bits (that's the mode argument, bestie) as altered by whatever values set is pointing at. and like β€” heads up β€” only the mode bits get altered, but other parts of the file mode may still get examined. allegedly. iykyk πŸ‘€

then there's setmode(). it takes an absolute (octal) value or a symbolic value β€” literally the same vibe as described in chmod(1) β€” and hands back a pointer to mode values that you then feed to getmode(). iconic teamwork.

⚠️ ok important: because some of those symbolic values are relative to the file creation mask, setmode() may call umask(2). and if it does? it restores the file creation mask before setmode() returns, so you don't get caught lacking. BUT β€” and this is the part people always forget β€” if your calling program changes the value of its file creation mask after calling setmode(), you gotta call setmode() again, otherwise getmode() won't modify future file modes correctly. periodt.

if the mode you passed to setmode() is invalid, or if memory can't be allocated for the return value, setmode() returns NULL. 😭

also: the value returned from setmode() comes from malloc(), so you gotta give it back to the system with free() when your program is done with it β€” generally after a call to getmode(). don't be the person who leaks, please, i'm begging πŸ™ˆ

πŸ’€ ERRORS πŸ’€

setmode() may fail and set errno for any of the errors specified for the library routines malloc(3) or strtol(3). on top of that, setmode() will fail and set errno to:

  • [EINVAL] β€” the mode argument does not represent a valid mode. fake mode, not a real one, rejected 🚫

🌈 SEE ALSO 🌈

chmod(1), stat(2), umask(2), malloc(3) β€” go read these, they're the supporting cast and they ate ✨

πŸ‘‘ HISTORY πŸ‘‘

getmode() and setmode() first appeared in 4.4BSD. vintage. long-running icon behavior, honestly.

πŸ”₯ BUGS πŸ”₯

  • setmode() is not thread safe. that's not a vibe.
seriously though β€” files created in other threads while setmode() is being called may be created with a umask of 0. yes, zero. as in wide open. as in the whole world can see it. so like… be careful, bestie πŸ’€

okay that's the page!! source is Debian, version dated July 18, 2019, and that's the whole manual β€” go free() your memory and stay hydrated πŸ¦‹πŸ’œπŸŽ€

πŸ“¦ where this page comes from
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms