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