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