xref: /netbsd-src/lib/libc/gen/setmode.3 (revision d1b0f9f059f9b113a757b58d6c3eec416149050b)
1.\"	$NetBSD: setmode.3,v 1.24 2024/04/28 22:43:30 rillig Exp $
2.\"
3.\" Copyright (c) 1989, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)setmode.3	8.2 (Berkeley) 4/28/95
31.\"
32.Dd March 12, 2022
33.Dt SETMODE 3
34.Os
35.Sh NAME
36.Nm getmode ,
37.Nm setmode
38.Nd modify mode bits
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In unistd.h
43.Ft void *
44.Fn setmode "const char *mode_str"
45.Ft mode_t
46.Fn getmode "const void *set" "mode_t mode"
47.Sh DESCRIPTION
48The
49.Fn setmode
50function accepts a string representation of a file mode change,
51compiles it to binary form, and returns an abstract representation
52that may be passed to
53.Fn getmode .
54The string may be a numeric (octal) or symbolic string of the form
55accepted by
56.Xr chmod 1 ,
57and may represent either an exact mode to set or a change to make to
58an existing mode.
59.Pp
60The
61.Fn getmode
62function adjusts the file permission bits given by
63.Fa mode
64according to the compiled change representation
65.Fa set ,
66and returns the adjusted mode.
67While only the permission bits are altered, other parts of the file
68mode, particularly the type, may be examined.
69.Pp
70Because some of the possible symbolic values are defined relative to
71the file creation mask,
72.Fn setmode
73may call
74.Xr umask 2 ,
75temporarily changing the mask.
76If this occurs, the file creation mask will be restored before
77.Fn setmode
78returns.
79If the calling program changes the value of its file creation mask
80after calling
81.Fn setmode ,
82.Fn setmode
83must be called again to recompile the mode string if
84.Fn getmode
85is to modify future file modes correctly.
86.Pp
87If the mode passed to
88.Fn setmode
89is invalid,
90.Fn setmode
91returns
92.Dv NULL .
93.Sh EXAMPLES
94The effects of the shell command
95.Ql "chmod a+x myscript.sh"
96can be duplicated as follows:
97.Bd -literal -offset indent
98const char *file = "myscript.sh";
99struct stat st;
100mode_t newmode;
101
102stat(file, &st);
103newmode = getmode(setmode("a+x"), st.st_mode);
104chmod(file, newmode);
105.Ed
106.Sh ERRORS
107The
108.Fn setmode
109function
110may fail and set
111.Va errno
112for any of the errors specified for the library routines
113.Xr reallocarr 3
114or
115.Xr strtol 3 .
116In addition,
117.Fn setmode
118may fail and set
119.Va errno
120to:
121.Bl -tag -width Er
122.It Bq Er EINVAL
123The
124.Fa mode
125argument does not represent a valid mode.
126.El
127.Sh SEE ALSO
128.Xr chmod 1 ,
129.Xr stat 2 ,
130.Xr umask 2 ,
131.Xr malloc 3
132.Sh HISTORY
133The
134.Fn getmode
135and
136.Fn setmode
137functions first appeared in
138.Bx 4.4 .
139.Sh BUGS
140Each call to
141.Nm setmode
142allocates a small amount of memory that there is no correct way to
143free.
144.Pp
145The type of
146.Fa set
147should really be some opaque struct type used only by these functions
148rather than
149.Ft void * .
150