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