1.\" $NetBSD: setmode.3,v 1.21 2009/01/11 02:46:27 christos 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 January 4, 2009 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 an 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 58the existing mode. 59.Pp 60The 61.Fn getmode 62function 63adjusts the file permission bits given by 64.Fa mode 65according to the compiled change representation 66.Fa set , 67and returns the adjusted mode. 68While only the permission bits are altered, other parts of the file 69mode, particularly the type, may be examined. 70.Pp 71Because some of the possible symbolic values are defined relative to 72the file creation mask, 73.Fn setmode 74may call 75.Xr umask 2 , 76temporarily changing the mask. 77If this occurs, the file creation mask will be restored before 78.Fn setmode 79returns. 80If the calling program changes the value of its file creation mask 81after calling 82.Fn setmode , 83.Fn setmode 84must be called again to recompile the mode string if 85.Fn getmode 86is to modify future file modes correctly. 87.Pp 88If the mode passed to 89.Fn setmode 90is invalid, 91.Fn setmode 92returns 93.Dv NULL . 94.Sh EXAMPLES 95The effects of the shell command 96.Ql "chmod a+x myscript.sh" 97can be duplicated as follows: 98.Bd -literal -offset indent 99const char *file = "myscript.sh"; 100struct stat st; 101mode_t newmode; 102 103stat(file, \*[Am]st); 104newmode = getmode(setmode("a+x"), st.st_mode); 105chmod(file, newmode); 106.Ed 107.Sh ERRORS 108The 109.Fn setmode 110function 111may fail and set 112.Va errno 113for any of the errors specified for the library routines 114.Xr malloc 3 115or 116.Xr strtol 3 . 117In addition, 118.Fn setmode 119will fail and set 120.Va errno 121to: 122.Bl -tag -width Er 123.It Bq Er EINVAL 124The 125.Fa mode 126argument does not represent a valid mode. 127.El 128.Sh SEE ALSO 129.Xr chmod 1 , 130.Xr stat 2 , 131.Xr umask 2 , 132.Xr malloc 3 133.Sh HISTORY 134The 135.Fn getmode 136and 137.Fn setmode 138functions first appeared in 139.Bx 4.4 . 140.Sh BUGS 141Each call to 142.Nm setmode 143allocates a small amount of memory that there is no correct way to 144free. 145.Pp 146The type of 147.Fa set 148should really be some opaque struct type used only by these functions 149rather than 150.Ft void * . 151