SETMODE(3)
π 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 callumask(2). and if it does? it restores the file creation mask beforesetmode()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 callingsetmode(), you gotta callsetmode()again, otherwisegetmode()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 whilesetmode()is being called may be created with a umask of0. 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
- Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms