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