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