1.\" Copyright (c) 1992, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Casey Leedom of Lawrence Livermore National Laboratory. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)getcap.3 8.4 (Berkeley) 5/13/94 36.\" $FreeBSD$ 37.\" 38.Dd May 13, 1994 39.Dt GETCAP 3 40.Os 41.Sh NAME 42.Nm cgetent , 43.Nm cgetset , 44.Nm cgetmatch , 45.Nm cgetcap , 46.Nm cgetnum , 47.Nm cgetstr , 48.Nm cgetustr , 49.Nm cgetfirst , 50.Nm cgetnext , 51.Nm cgetclose 52.Nd capability database access routines 53.Sh SYNOPSIS 54.Fd #include <stdlib.h> 55.Ft int 56.Fn cgetent "char **buf" "char **db_array" "char *name" 57.Ft int 58.Fn cgetset "char *ent" 59.Ft int 60.Fn cgetmatch "char *buf" "char *name" 61.Ft char * 62.Fn cgetcap "char *buf" "char *cap" "int type" 63.Ft int 64.Fn cgetnum "char *buf" "char *cap" "long *num" 65.Ft int 66.Fn cgetstr "char *buf" "char *cap" "char **str" 67.Ft int 68.Fn cgetustr "char *buf" "char *cap" "char **str" 69.Ft int 70.Fn cgetfirst "char **buf" "char **db_array" 71.Ft int 72.Fn cgetnext "char **buf" "char **db_array" 73.Ft int 74.Fn cgetclose "void" 75.Sh DESCRIPTION 76.Fn Cgetent 77extracts the capability 78.Fa name 79from the database specified by the 80.Dv NULL 81terminated file array 82.Fa db_array 83and returns a pointer to a 84.Xr malloc Ns \&'d 85copy of it in 86.Fa buf . 87The 88.Fn cgetent 89function will first look for files ending in 90.Nm .db 91(see 92.Xr cap_mkdb 1) 93before accessing the ASCII file. 94.Fa Buf 95must be retained through all subsequent calls to 96.Fn cgetmatch , 97.Fn cgetcap , 98.Fn cgetnum , 99.Fn cgetstr , 100and 101.Fn cgetustr , 102but may then be 103.Xr free 3 Ns \&'d. 104On success 0 is returned, 1 if the returned 105record contains an unresolved 106.Nm tc 107expansion, 108\-1 if the requested record couldn't be found, 109\-2 if a system error was encountered (couldn't open/read a file, etc.) also 110setting 111.Va errno , 112and \-3 if a potential reference loop is detected (see 113.Ic tc= 114comments below). 115.Pp 116The 117.Fn cgetset 118function enables the addition of a character buffer containing a single capability 119record entry 120to the capability database. 121Conceptually, the entry is added as the first ``file'' in the database, and 122is therefore searched first on the call to 123.Fn cgetent . 124The entry is passed in 125.Fa ent . 126If 127.Fa ent 128is 129.Dv NULL , 130the current entry is removed from the database. 131A call to 132.Fn cgetset 133must precede the database traversal. It must be called before the 134.Fn cgetent 135call. If a sequential access is being performed (see below), it must be called 136before the first sequential access call ( 137.Fn cgetfirst 138or 139.Fn cgetnext 140), or be directly preceded by a 141.Fn cgetclose 142call. 143On success 0 is returned and \-1 on failure. 144.Pp 145The 146.Fn cgetmatch 147function will return 0 if 148.Fa name 149is one of the names of the capability record 150.Fa buf , 151\-1 if 152not. 153.Pp 154The 155.Fn cgetcap 156function searches the capability record 157.Fa buf 158for the capability 159.Fa cap 160with type 161.Fa type . 162A 163.Fa type 164is specified using any single character. If a colon (`:') is used, an 165untyped capability will be searched for (see below for explanation of 166types). A pointer to the value of 167.Fa cap 168in 169.Fa buf 170is returned on success, 171.Dv NULL 172if the requested capability couldn't be 173found. The end of the capability value is signaled by a `:' or 174.Tn ASCII 175.Dv NUL 176(see below for capability database syntax). 177.Pp 178The 179.Fn cgetnum 180function retrieves the value of the numeric capability 181.Fa cap 182from the capability record pointed to by 183.Fa buf . 184The numeric value is returned in the 185.Ft long 186pointed to by 187.Fa num . 1880 is returned on success, \-1 if the requested numeric capability couldn't 189be found. 190.Pp 191The 192.Fn cgetstr 193function retrieves the value of the string capability 194.Fa cap 195from the capability record pointed to by 196.Fa buf . 197A pointer to a decoded, 198.Dv NUL 199terminated, 200.Xr malloc Ns \&'d 201copy of the string is returned in the 202.Ft char * 203pointed to by 204.Fa str . 205The number of characters in the decoded string not including the trailing 206.Dv NUL 207is returned on success, \-1 if the requested string capability couldn't 208be found, \-2 if a system error was encountered (storage allocation 209failure). 210.Pp 211The 212.Fn cgetustr 213function is identical to 214.Fn cgetstr 215except that it does not expand special characters, but rather returns each 216character of the capability string literally. 217.Pp 218The 219.Fn cgetfirst 220and 221.Fn cgetnext 222functions comprise a function group that provides for sequential 223access of the 224.Dv NULL 225pointer terminated array of file names, 226.Fa db_array . 227The 228.Fn cgetfirst 229function returns the first record in the database and resets the access 230to the first record. 231The 232.Fn cgetnext 233function returns the next record in the database with respect to the 234record returned by the previous 235.Fn cgetfirst 236or 237.Fn cgetnext 238call. If there is no such previous call, the first record in the database is 239returned. 240Each record is returned in a 241.Xr malloc Ns \&'d 242copy pointed to by 243.Fa buf . 244.Ic Tc 245expansion is done (see 246.Ic tc= 247comments below). 248Upon completion of the database 0 is returned, 1 is returned upon successful 249return of record with possibly more remaining (we haven't reached the end of 250the database yet), 2 is returned if the record contains an unresolved 251.Nm tc 252expansion, \-1 is returned if an system error occurred, and \-2 253is returned if a potential reference loop is detected (see 254.Ic tc= 255comments below). 256Upon completion of database (0 return) the database is closed. 257.Pp 258The 259.Fn cgetclose 260function closes the sequential access and frees any memory and file descriptors 261being used. Note that it does not erase the buffer pushed by a call to 262.Fn cgetset . 263.Sh CAPABILITY DATABASE SYNTAX 264Capability databases are normally 265.Tn ASCII 266and may be edited with standard 267text editors. Blank lines and lines beginning with a `#' are comments 268and are ignored. Lines ending with a `\|\e' indicate that the next line 269is a continuation of the current line; the `\|\e' and following newline 270are ignored. Long lines are usually continued onto several physical 271lines by ending each line except the last with a `\|\e'. 272.Pp 273Capability databases consist of a series of records, one per logical 274line. Each record contains a variable number of `:'-separated fields 275(capabilities). Empty fields consisting entirely of white space 276characters (spaces and tabs) are ignored. 277.Pp 278The first capability of each record specifies its names, separated by `|' 279characters. These names are used to reference records in the database. 280By convention, the last name is usually a comment and is not intended as 281a lookup tag. For example, the 282.Em vt100 283record from the 284.Nm termcap 285database begins: 286.Pp 287.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:" 288.Pp 289giving four names that can be used to access the record. 290.Pp 291The remaining non-empty capabilities describe a set of (name, value) 292bindings, consisting of a names optionally followed by a typed values: 293.Bl -column "nameTvalue" 294.It name Ta "typeless [boolean] capability" 295.Em name No "is present [true]" 296.It name Ns Em \&T Ns value Ta capability 297.Pq Em name , \&T 298has value 299.Em value 300.It name@ Ta "no capability" Em name No exists 301.It name Ns Em T Ns \&@ Ta capability 302.Pq Em name , T 303does not exist 304.El 305.Pp 306Names consist of one or more characters. Names may contain any character 307except `:', but it's usually best to restrict them to the printable 308characters and avoid use of graphics like `#', `=', `%', `@', etc. Types 309are single characters used to separate capability names from their 310associated typed values. Types may be any character except a `:'. 311Typically, graphics like `#', `=', `%', etc. are used. Values may be any 312number of characters and may contain any character except `:'. 313.Sh CAPABILITY DATABASE SEMANTICS 314Capability records describe a set of (name, value) bindings. Names may 315have multiple values bound to them. Different values for a name are 316distinguished by their 317.Fa types . 318The 319.Fn cgetcap 320function will return a pointer to a value of a name given the capability 321name and the type of the value. 322.Pp 323The types `#' and `=' are conventionally used to denote numeric and 324string typed values, but no restriction on those types is enforced. The 325functions 326.Fn cgetnum 327and 328.Fn cgetstr 329can be used to implement the traditional syntax and semantics of `#' 330and `='. 331Typeless capabilities are typically used to denote boolean objects with 332presence or absence indicating truth and false values respectively. 333This interpretation is conveniently represented by: 334.Pp 335.Dl "(getcap(buf, name, ':') != NULL)" 336.Pp 337A special capability, 338.Ic tc= name , 339is used to indicate that the record specified by 340.Fa name 341should be substituted for the 342.Ic tc 343capability. 344.Ic Tc 345capabilities may interpolate records which also contain 346.Ic tc 347capabilities and more than one 348.Ic tc 349capability may be used in a record. A 350.Ic tc 351expansion scope (i.e., where the argument is searched for) contains the 352file in which the 353.Ic tc 354is declared and all subsequent files in the file array. 355.Pp 356When a database is searched for a capability record, the first matching 357record in the search is returned. When a record is scanned for a 358capability, the first matching capability is returned; the capability 359.Ic :nameT@: 360will hide any following definition of a value of type 361.Em T 362for 363.Fa name ; 364and the capability 365.Ic :name@: 366will prevent any following values of 367.Fa name 368from being seen. 369.Pp 370These features combined with 371.Ic tc 372capabilities can be used to generate variations of other databases and 373records by either adding new capabilities, overriding definitions with new 374definitions, or hiding following definitions via `@' capabilities. 375.Sh EXAMPLES 376.Bd -unfilled -offset indent 377example\||\|an example of binding multiple values to names:\e 378 :foo%bar:foo^blah:foo@:\e 379 :abc%xyz:abc^frap:abc$@:\e 380 :tc=more: 381.Ed 382.Pp 383The capability foo has two values bound to it (bar of type `%' and blah of 384type `^') and any other value bindings are hidden. The capability abc 385also has two values bound but only a value of type `$' is prevented from 386being defined in the capability record more. 387.Pp 388.Bd -unfilled -offset indent 389file1: 390 new\||\|new_record\||\|a modification of "old":\e 391 :fript=bar:who-cares@:tc=old:blah:tc=extensions: 392file2: 393 old\||\|old_record\||\|an old database record:\e 394 :fript=foo:who-cares:glork#200: 395.Ed 396.Pp 397The records are extracted by calling 398.Fn cgetent 399with file1 preceding file2. 400In the capability record new in file1, fript=bar overrides the definition 401of fript=foo interpolated from the capability record old in file2, 402who-cares@ prevents the definition of any who-cares definitions in old 403from being seen, glork#200 is inherited from old, and blah and anything 404defined by the record extensions is added to those definitions in old. 405Note that the position of the fript=bar and who-cares@ definitions before 406tc=old is important here. If they were after, the definitions in old 407would take precedence. 408.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS 409Two types are predefined by 410.Fn cgetnum 411and 412.Fn cgetstr : 413.Bl -column "nameXnumber" 414.Sm off 415.It Em name No \&# Em number Ta numeric 416capability 417.Em name 418has value 419.Em number 420.It Em name No = Em string Ta "string capability" 421.Em name 422has value 423.Em string 424.It Em name No \&#@ Ta "the numeric capability" 425.Em name 426does not exist 427.It Em name No \&=@ Ta "the string capability" 428.Em name 429does not exist 430.El 431.Pp 432Numeric capability values may be given in one of three numeric bases. 433If the number starts with either 434.Ql 0x 435or 436.Ql 0X 437it is interpreted as a hexadecimal number (both upper and lower case a-f 438may be used to denote the extended hexadecimal digits). 439Otherwise, if the number starts with a 440.Ql 0 441it is interpreted as an octal number. 442Otherwise the number is interpreted as a decimal number. 443.Pp 444String capability values may contain any character. Non-printable 445.Dv ASCII 446codes, new lines, and colons may be conveniently represented by the use 447of escape sequences: 448.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)" 449^X ('X' & 037) control-X 450\e\|b, \e\|B (ASCII 010) backspace 451\e\|t, \e\|T (ASCII 011) tab 452\e\|n, \e\|N (ASCII 012) line feed (newline) 453\e\|f, \e\|F (ASCII 014) form feed 454\e\|r, \e\|R (ASCII 015) carriage return 455\e\|e, \e\|E (ASCII 027) escape 456\e\|c, \e\|C (:) colon 457\e\|\e (\e\|) back slash 458\e\|^ (^) caret 459\e\|nnn (ASCII octal nnn) 460.El 461.Pp 462A `\|\e' may be followed by up to three octal digits directly specifies 463the numeric code for a character. The use of 464.Tn ASCII 465.Dv NUL Ns s , 466while easily 467encoded, causes all sorts of problems and must be used with care since 468.Dv NUL Ns s 469are typically used to denote the end of strings; many applications 470use `\e\|200' to represent a 471.Dv NUL . 472.Sh DIAGNOSTICS 473.Fn Cgetent , 474.Fn cgetset , 475.Fn cgetmatch , 476.Fn cgetnum , 477.Fn cgetstr , 478.Fn cgetustr , 479.Fn cgetfirst , 480and 481.Fn cgetnext 482return a value greater than or equal to 0 on success and a value less 483than 0 on failure. 484The 485.Fn cgetcap 486function returns a character pointer on success and a 487.Dv NULL 488on failure. 489.Pp 490The 491.Fn cgetent , 492and 493.Fn cgetseq 494functions may fail and set 495.Va errno 496for any of the errors specified for the library functions: 497.Xr fopen 3 , 498.Xr fclose 3 , 499.Xr open 2 , 500and 501.Xr close 2 . 502.Pp 503The 504.Fn cgetent , 505.Fn cgetset , 506.Fn cgetstr , 507and 508.Fn cgetustr 509may fail and set 510.Va errno 511as follows: 512.Bl -tag -width Er 513.It Bq Er ENOMEM 514No memory to allocate. 515.El 516.Sh SEE ALSO 517.Xr cap_mkdb 1 , 518.Xr malloc 3 519.Sh BUGS 520Colons (`:') can't be used in names, types, or values. 521.Pp 522There are no checks for 523.Ic tc= name 524loops in 525.Fn cgetent . 526.Pp 527The buffer added to the database by a call to 528.Fn cgetset 529is not unique to the database but is rather prepended to any database used. 530