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