From 6a7eb1f6ade5f6bcb315deaa6453702e35d8ac7d Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Mon, 9 Mar 2009 20:27:21 -1000 Subject: [PATCH] Sync getcap() with FreeBSD: * Expand contractions and fix sentence breaks. * In cgetmatch(3), return -1 if a NULL or 0-length name parameter is used. This prevents lpd from hanging at boot with certain (legal) printcap entries. * Sprinkle some const. --- include/stdlib.h | 14 +++--- lib/libc/gen/getcap.3 | 120 +++++++++++++++++++++++++++++--------------------- lib/libc/gen/getcap.c | 54 +++++++++++++---------- 3 files changed, 108 insertions(+), 80 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index dee562fbab..6845f23bb7 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -229,16 +229,16 @@ __uint32_t arc4random_uniform(__uint32_t); char *getbsize(int *, long *); /* getcap(3) functions */ -char *cgetcap(char *, char *, int); +char *cgetcap(char *, const char *, int); int cgetclose(void); -int cgetent(char **, char **, char *); +int cgetent(char **, char **, const char *); int cgetfirst(char **, char **); -int cgetmatch(char *, char *); +int cgetmatch(const char *, const char *); int cgetnext(char **, char **); -int cgetnum(char *, char *, long *); -int cgetset(char *); -int cgetstr(char *, char *, char **); -int cgetustr(char *, char *, char **); +int cgetnum(char *, const char *, long *); +int cgetset(const char *); +int cgetstr(char *, const char *, char **); +int cgetustr(char *, const char *, char **); int daemon(int, int); char *devname(dev_t, mode_t); diff --git a/lib/libc/gen/getcap.3 b/lib/libc/gen/getcap.3 index 93f3b0cf4c..b1b49a55ac 100644 --- a/lib/libc/gen/getcap.3 +++ b/lib/libc/gen/getcap.3 @@ -12,10 +12,6 @@ .\" 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. @@ -33,10 +29,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)getcap.3 8.4 (Berkeley) 5/13/94 -.\" $FreeBSD: src/lib/libc/gen/getcap.3,v 1.12.2.7 2003/03/13 18:05:37 trhodes Exp $ +.\" $FreeBSD: src/lib/libc/gen/getcap.3,v 1.30 2007/02/11 18:14:49 maxim Exp $ .\" $DragonFly: src/lib/libc/gen/getcap.3,v 1.4 2008/05/02 02:05:03 swildner Exp $ .\" -.Dd May 13, 1994 +.Dd March 22, 2002 .Dt GETCAP 3 .Os .Sh NAME @@ -56,19 +52,19 @@ .Sh SYNOPSIS .In stdlib.h .Ft int -.Fn cgetent "char **buf" "char **db_array" "char *name" +.Fn cgetent "char **buf" "char **db_array" "const char *name" .Ft int -.Fn cgetset "char *ent" +.Fn cgetset "const char *ent" .Ft int -.Fn cgetmatch "char *buf" "char *name" +.Fn cgetmatch "const char *buf" "const char *name" .Ft char * -.Fn cgetcap "char *buf" "char *cap" "int type" +.Fn cgetcap "char *buf" "const char *cap" "int type" .Ft int -.Fn cgetnum "char *buf" "char *cap" "long *num" +.Fn cgetnum "char *buf" "const char *cap" "long *num" .Ft int -.Fn cgetstr "char *buf" "char *cap" "char **str" +.Fn cgetstr "char *buf" "const char *cap" "char **str" .Ft int -.Fn cgetustr "char *buf" "char *cap" "char **str" +.Fn cgetustr "char *buf" "const char *cap" "char **str" .Ft int .Fn cgetfirst "char **buf" "char **db_array" .Ft int @@ -111,8 +107,8 @@ 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 was encountered (couldn't open/read a file, etc.) also +\-1 if the requested record could not be found, +\-2 if a system error was encountered (could not open/read a file, etc.) also setting .Va errno , and \-3 if a potential reference loop is detected (see @@ -136,9 +132,11 @@ is the current entry is removed from the database. A call to .Fn cgetset -must precede the database traversal. It must be called before the +must precede the database traversal. +It must be called before the .Fn cgetent -call. If a sequential access is being performed (see below), it must be called +call. +If a sequential access is being performed (see below), it must be called before the first sequential access call .Fn ( cgetfirst or @@ -167,16 +165,19 @@ with type .Fa type . A .Fa type -is specified using any single character. If a colon (`:') is used, an +is specified using any single character. +If a colon (`:') is used, an untyped capability will be searched for (see below for explanation of -types). A pointer to the value of +types). +A pointer to the value of .Fa cap in .Fa buf is returned on success, .Dv NULL -if the requested capability couldn't be -found. The end of the capability value is signaled by a `:' or +if the requested capability could not be +found. +The end of the capability value is signaled by a `:' or .Tn ASCII .Dv NUL (see below for capability database syntax). @@ -191,7 +192,7 @@ The numeric value is returned in the .Ft long pointed to by .Fa num . -0 is returned on success, \-1 if the requested numeric capability couldn't +0 is returned on success, \-1 if the requested numeric capability could not be found. .Pp The @@ -210,7 +211,7 @@ pointed to by .Fa str . The number of characters in the decoded string not including the trailing .Dv NUL -is returned on success, \-1 if the requested string capability couldn't +is returned on success, \-1 if the requested string capability could not be found, \-2 if a system error was encountered (storage allocation failure). .Pp @@ -241,7 +242,8 @@ 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 +call. +If there is no such previous call, the first record in the database is returned. Each record is returned in a .Xr malloc 3 Ns \&'d @@ -251,8 +253,8 @@ copy pointed to by 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 +Upon completion of the database 0 is returned, 1 is returned upon successful +return of record with possibly more remaining (we have not reached the end of the database yet), 2 is returned if the record contains an unresolved .Ic tc expansion, \-1 is returned if a system error occurred, and \-2 @@ -264,27 +266,35 @@ Upon completion of database (0 return) the database is closed. The .Fn cgetclose function closes the sequential access and frees any memory and file descriptors -being used. Note that it does not erase the buffer pushed by a call to +being used. +Note that it does not erase the buffer pushed by a call to .Fn cgetset . .Sh CAPABILITY DATABASE SYNTAX Capability databases are normally .Tn ASCII and may be edited with standard -text editors. Blank lines and lines beginning with a `#' are comments -and are ignored. Lines ending with a `\|\e' indicate that the next line +text editors. +Blank lines and lines beginning with a `#' are comments +and are ignored. +Lines ending with a `\|\e' indicate that the next line is a continuation of the current line; the `\|\e' and following newline -are ignored. Long lines are usually continued onto several physical +are ignored. +Long lines are usually continued onto several physical lines by ending each line except the last with a `\|\e'. .Pp Capability databases consist of a series of records, one per logical -line. Each record contains a variable number of `:'-separated fields -(capabilities). Empty fields consisting entirely of white space +line. +Each record contains a variable number of `:'-separated fields +(capabilities). +Empty fields consisting entirely of white space characters (spaces and tabs) are ignored. .Pp The first capability of each record specifies its names, separated by `|' -characters. These names are used to reference records in the database. +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 +a lookup tag. +For example, the .Em vt100 record from the .Xr termcap 5 @@ -309,16 +319,21 @@ has value does not exist .El .Pp -Names consist of one or more characters. Names may contain any character -except `:', but it's usually best to restrict them to the printable -characters and avoid use of graphics like `#', `=', `%', `@', etc. Types +Names consist of one or more characters. +Names may contain any character +except `:', but it is usually best to restrict them to the printable +characters and avoid use of graphics like `#', `=', `%', `@', etc. +Types are single characters used to separate capability names from their -associated typed values. Types may be any character except a `:'. -Typically, graphics like `#', `=', `%', etc. are used. Values may be any +associated typed values. +Types may be any character except a `:'. +Typically, graphics like `#', `=', `%', etc.\& are used. +Values may be any number of characters and may contain any character except `:'. .Sh 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 +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 . The @@ -327,7 +342,8 @@ function will return a pointer to a value of a name given the capability name and the type of the value. .Pp The types `#' and `=' are conventionally used to denote numeric and -string typed values, but no restriction on those types is enforced. The +string typed values, but no restriction on those types is enforced. +The functions .Fn cgetnum and @@ -352,7 +368,8 @@ capabilities may interpolate records which also contain .Ic tc capabilities and more than one .Ic tc -capability may be used in a record. A +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 @@ -360,7 +377,8 @@ file in which the 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 +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 @@ -387,7 +405,8 @@ example\||\|an example of binding multiple values to names:\e .Ed .Pp The capability foo has two values bound to it (bar of type `%' and blah of -type `^') and any other value bindings are hidden. The capability abc +type `^') and any other value bindings are hidden. +The capability abc also has two values bound but only a value of type `$' is prevented from being defined in the capability record more. .Bd -unfilled -offset indent @@ -408,7 +427,8 @@ 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 +tc=old is important here. +If they were after, the definitions in old would take precedence. .Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS Two types are predefined by @@ -453,7 +473,8 @@ Otherwise, if the number starts with a 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 +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: @@ -472,7 +493,8 @@ of escape sequences: .El .Pp A `\|\e' may be followed by up to three octal digits directly specifies -the numeric code for a character. The use of +the numeric code for a character. +The use of .Tn ASCII .Dv NUL Ns s , while easily @@ -532,7 +554,7 @@ No memory to allocate. .Xr cap_mkdb 1 , .Xr malloc 3 .Sh BUGS -Colons (`:') can't be used in names, types, or values. +Colons (`:') cannot be used in names, types, or values. .Pp There are no checks for .Ic tc Ns = Ns Ic name diff --git a/lib/libc/gen/getcap.c b/lib/libc/gen/getcap.c index 1a57080cb1..19fc805021 100644 --- a/lib/libc/gen/getcap.c +++ b/lib/libc/gen/getcap.c @@ -13,10 +13,6 @@ * 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. @@ -33,10 +29,9 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libc/gen/getcap.c,v 1.11.2.2 2001/01/15 06:48:09 gad Exp $ - * $DragonFly: src/lib/libc/gen/getcap.c,v 1.7 2005/11/19 22:32:53 swildner Exp $ - * * @(#)getcap.c 8.3 (Berkeley) 3/25/94 + * $FreeBSD: src/lib/libc/gen/getcap.c,v 1.20 2007/01/09 00:27:53 imp Exp $ + * $DragonFly: src/lib/libc/gen/getcap.c,v 1.7 2005/11/19 22:32:53 swildner Exp $ */ #include "namespace.h" @@ -68,9 +63,9 @@ static size_t topreclen; /* toprec length */ static char *toprec; /* Additional record specified by cgetset() */ static int gottoprec; /* Flag indicating retrieval of toprecord */ -static int cdbget (DB *, char **, char *); -static int getent (char **, u_int *, char **, int, char *, int, char *); -static int nfcmp (char *, char *); +static int cdbget(DB *, char **, const char *); +static int getent(char **, u_int *, char **, int, const char *, int, char *); +static int nfcmp(char *, char *); /* * Cgetset() allows the addition of a user specified buffer to be added @@ -78,7 +73,7 @@ static int nfcmp (char *, char *); * virtual database. 0 is returned on success, -1 on failure. */ int -cgetset(char *ent) +cgetset(const char *ent) { if (ent == NULL) { if (toprec) @@ -110,9 +105,10 @@ cgetset(char *ent) * return NULL. */ char * -cgetcap(char *buf, char *cap, int type) +cgetcap(char *buf, const char *cap, int type) { - char *bp, *cp; + char *bp; + const char *cp; bp = buf; for (;;) { @@ -160,7 +156,7 @@ cgetcap(char *buf, char *cap, int type) * reference loop is detected. */ int -cgetent(char **buf, char **db_array, char *name) +cgetent(char **buf, char **db_array, const char *name) { u_int dummy; @@ -186,8 +182,8 @@ cgetent(char **buf, char **db_array, char *name) * MAX_RECURSION. */ static int -getent(char **cap, u_int *len, char **db_array, int fd, char *name, int depth, - char *nfield) +getent(char **cap, u_int *len, char **db_array, int fd, const char *name, + int depth, char *nfield) { DB *capdbp; char *r_end, *rp, **db_p; @@ -534,19 +530,25 @@ tc_exp: { } static int -cdbget(DB *capdbp, char **bp, char *name) +cdbget(DB *capdbp, char **bp, const char *name) { DBT key, data; + char *namebuf; - key.data = name; - key.size = strlen(name); + namebuf = strdup(name); + if (namebuf == NULL) + return (-2); + key.data = namebuf; + key.size = strlen(namebuf); for (;;) { /* Get the reference. */ switch(capdbp->get(capdbp, &key, &data, 0)) { case -1: + free(namebuf); return (-2); case 1: + free(namebuf); return (-1); } @@ -559,6 +561,7 @@ cdbget(DB *capdbp, char **bp, char *name) } *bp = (char *)data.data + 1; + free(namebuf); return (((char *)(data.data))[0] == TCERR ? 1 : 0); } @@ -567,9 +570,12 @@ cdbget(DB *capdbp, char **bp, char *name) * record buf, -1 if not. */ int -cgetmatch(char *buf, char *name) +cgetmatch(const char *buf, const char *name) { - char *np, *bp; + const char *np, *bp; + + if (name == NULL || *name == '\0') + return -1; /* * Start search at beginning of record. @@ -777,7 +783,7 @@ cgetnext(char **bp, char **db_array) * allocation failure). */ int -cgetstr(char *buf, char *cap, char **str) +cgetstr(char *buf, const char *cap, char **str) { u_int m_room; char *bp, *mp; @@ -904,7 +910,7 @@ cgetstr(char *buf, char *cap, char **str) * error was encountered (storage allocation failure). */ int -cgetustr(char *buf, char *cap, char **str) +cgetustr(char *buf, const char *cap, char **str) { u_int m_room; char *bp, *mp; @@ -972,7 +978,7 @@ cgetustr(char *buf, char *cap, char **str) * numeric capability couldn't be found. */ int -cgetnum(char *buf, char *cap, long *num) +cgetnum(char *buf, const char *cap, long *num) { long n; int base, digit; -- 2.11.4.GIT