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