[BACK]Return to getcap.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/lib/libc/gen/getcap.c between version 1.29 and 1.46

version 1.29, 1999/03/29 09:27:29 version 1.46, 2006/05/29 21:55:41
Line 15 
Line 15 
  * 2. Redistributions in binary form must reproduce the above copyright   * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the   *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.   *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software   * 3. Neither the name of the University nor the names of its contributors
  *    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   *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.   *    without specific prior written permission.
  *   *
Line 36 
Line 32 
  * SUCH DAMAGE.   * SUCH DAMAGE.
  */   */
   
   #if HAVE_NBTOOL_CONFIG_H
   #include "nbtool_config.h"
   #endif
   
 #include <sys/cdefs.h>  #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)  #if defined(LIBC_SCCS) && !defined(lint)
 #if 0  #if 0
Line 45  __RCSID("$NetBSD$");
Line 45  __RCSID("$NetBSD$");
 #endif  #endif
 #endif /* LIBC_SCCS and not lint */  #endif /* LIBC_SCCS and not lint */
   
   #ifndef SMALL
 #include "namespace.h"  #include "namespace.h"
   #endif
 #include <sys/types.h>  #include <sys/types.h>
   #include <sys/param.h>
   
   #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
   #ifndef SMALL
 #include <db.h>  #include <db.h>
   #endif
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <limits.h>  #include <limits.h>
Line 58  __RCSID("$NetBSD$");
Line 65  __RCSID("$NetBSD$");
 #include <unistd.h>  #include <unistd.h>
   
 #ifdef __weak_alias  #ifdef __weak_alias
 __weak_alias(cgetcap,_cgetcap);  __weak_alias(cgetcap,_cgetcap)
 __weak_alias(cgetclose,_cgetclose);  __weak_alias(cgetclose,_cgetclose)
 __weak_alias(cgetent,_cgetent);  __weak_alias(cgetent,_cgetent)
 __weak_alias(cgetfirst,_cgetfirst);  __weak_alias(cgetfirst,_cgetfirst)
 __weak_alias(cgetmatch,_cgetmatch);  __weak_alias(cgetmatch,_cgetmatch)
 __weak_alias(cgetnext,_cgetnext);  __weak_alias(cgetnext,_cgetnext)
 __weak_alias(cgetnum,_cgetnum);  __weak_alias(cgetnum,_cgetnum)
 __weak_alias(cgetset,_cgetset);  __weak_alias(cgetset,_cgetset)
 __weak_alias(cgetstr,_cgetstr);  __weak_alias(cgetstr,_cgetstr)
 __weak_alias(cgetustr,_cgetustr);  __weak_alias(cgetustr,_cgetustr)
 #endif  #endif
   
 #define BFRAG           1024  #define BFRAG           1024
Line 84  static size_t  topreclen; /* toprec leng
Line 91  static size_t  topreclen; /* toprec leng
 static char     *toprec;        /* Additional record specified by cgetset() */  static char     *toprec;        /* Additional record specified by cgetset() */
 static int       gottoprec;     /* Flag indicating retrieval of toprecord */  static int       gottoprec;     /* Flag indicating retrieval of toprecord */
   
 static int      cdbget __P((DB *, char **, const char *));  #ifndef SMALL
 static int      getent __P((char **, size_t *, char **, int, const char *, int, char *));  static int      cdbget(DB *, char **, const char *);
 static int      nfcmp __P((char *, char *));  #endif
   static int      getent(char **, size_t *, const char * const *, int,
       const char *, int, char *);
   static int      nfcmp(char *, char *);
   
 /*  /*
  * Cgetset() allows the addition of a user specified buffer to be added   * Cgetset() allows the addition of a user specified buffer to be added
Line 94  static int nfcmp __P((char *, char *));
Line 104  static int nfcmp __P((char *, char *));
  * virtual database. 0 is returned on success, -1 on failure.   * virtual database. 0 is returned on success, -1 on failure.
  */   */
 int  int
 cgetset(ent)  cgetset(const char *ent)
         const char *ent;  
 {  {
         const char *source, *check;          const char *source, *check;
         char *dest;          char *dest;
Line 156  cgetcap(buf, cap, type)
Line 165  cgetcap(buf, cap, type)
         char *bp;          char *bp;
         const char *cp;          const char *cp;
   
           _DIAGASSERT(buf != NULL);
           _DIAGASSERT(cap != NULL);
   
         bp = buf;          bp = buf;
         for (;;) {          for (;;) {
                 /*                  /*
Line 201  cgetcap(buf, cap, type)
Line 213  cgetcap(buf, cap, type)
  * encountered (couldn't open/read a file, etc.), and -3 if a potential   * encountered (couldn't open/read a file, etc.), and -3 if a potential
  * reference loop is detected.   * reference loop is detected.
  */   */
   /* coverity[+alloc : arg-*0] */
 int  int
 cgetent(buf, db_array, name)  cgetent(char **buf, const char * const *db_array, const char *name)
         char **buf, **db_array;  
         const char *name;  
 {  {
         size_t dummy;          size_t dummy;
   
           _DIAGASSERT(buf != NULL);
           _DIAGASSERT(db_array != NULL);
           _DIAGASSERT(name != NULL);
   
         return (getent(buf, &dummy, db_array, -1, name, 0, NULL));          return (getent(buf, &dummy, db_array, -1, name, 0, NULL));
 }  }
   
Line 229  cgetent(buf, db_array, name)
Line 244  cgetent(buf, db_array, name)
  *        names interpolated, a name can't be found, or depth exceeds   *        names interpolated, a name can't be found, or depth exceeds
  *        MAX_RECURSION.   *        MAX_RECURSION.
  */   */
   /* coverity[+alloc : arg-*0] */
 static int  static int
 getent(cap, len, db_array, fd, name, depth, nfield)  getent(char **cap, size_t *len, const char * const *db_array, int fd,
         char **cap, **db_array, *nfield;      const char *name, int depth, char *nfield)
         const char *name;  
         size_t *len;  
         int fd, depth;  
 {  {
   #ifndef SMALL
         DB *capdbp;          DB *capdbp;
         char *r_end, *rp = NULL, **db_p;        /* pacify gcc */          char pbuf[MAXPATHLEN];
         int myfd = 0, eof, foundit, retval;          char *cbuf;
           int retval;
         size_t clen;          size_t clen;
         char *record, *cbuf;  #endif
           char *record, *newrecord;
           char *r_end, *rp = NULL;        /* pacify gcc */
           const char * const *db_p;
           int myfd = 0, eof, foundit;
         int tc_not_resolved;          int tc_not_resolved;
         char pbuf[_POSIX_PATH_MAX];  
   
           _DIAGASSERT(cap != NULL);
           _DIAGASSERT(len != NULL);
           _DIAGASSERT(db_array != NULL);
           /* fd may be -1 */
           _DIAGASSERT(name != NULL);
           /* nfield may be NULL */
   
         /*          /*
          * Return with ``loop detected'' error if we've recursed more than           * Return with ``loop detected'' error if we've recursed more than
          * MAX_RECURSION times.           * MAX_RECURSION times.
Line 288  getent(cap, len, db_array, fd, name, dep
Line 313  getent(cap, len, db_array, fd, name, dep
                 if (fd >= 0) {                  if (fd >= 0) {
                         (void)lseek(fd, (off_t)0, SEEK_SET);                          (void)lseek(fd, (off_t)0, SEEK_SET);
                 } else {                  } else {
   #ifndef SMALL
                         (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p);                          (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p);
                         if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0))                          if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0))
                              != NULL) {                               != NULL) {
Line 300  getent(cap, len, db_array, fd, name, dep
Line 326  getent(cap, len, db_array, fd, name, dep
                                 }                                  }
                                 /* save the data; close frees it */                                  /* save the data; close frees it */
                                 clen = strlen(record);                                  clen = strlen(record);
                                 cbuf = malloc(clen + 1);                                  if ((cbuf = malloc(clen + 1)) == NULL) {
                                           (void)capdbp->close(capdbp);
                                           errno = ENOMEM;
                                           return (-2);
                                   }
                                 memmove(cbuf, record, clen + 1);                                  memmove(cbuf, record, clen + 1);
                                 if (capdbp->close(capdbp) < 0) {                                  if (capdbp->close(capdbp) < 0) {
                                           int serrno = errno;
   
                                         free(cbuf);                                          free(cbuf);
                                           errno = serrno;
                                         return (-2);                                          return (-2);
                                 }                                  }
                                 *len = clen;                                  *len = clen;
                                 *cap = cbuf;                                  *cap = cbuf;
                                 return (retval);                                  return (retval);
                         } else {                          } else
   #endif
                           {
                                 fd = open(*db_p, O_RDONLY, 0);                                  fd = open(*db_p, O_RDONLY, 0);
                                 if (fd < 0) {                                  if (fd < 0) {
                                         /* No error on unfound file. */                                          /* No error on unfound file. */
Line 355  getent(cap, len, db_array, fd, name, dep
Line 390  getent(cap, len, db_array, fd, name, dep
                                                 if (myfd)                                                  if (myfd)
                                                         (void)close(fd);                                                          (void)close(fd);
                                                 if (n < 0) {                                                  if (n < 0) {
                                                           int serrno = errno;
   
                                                         free(record);                                                          free(record);
                                                           errno = serrno;
                                                         return (-2);                                                          return (-2);
                                                 } else {                                                  } else {
                                                         fd = -1;                                                          fd = -1;
Line 413  getent(cap, len, db_array, fd, name, dep
Line 451  getent(cap, len, db_array, fd, name, dep
   
                                         pos = rp - record;                                          pos = rp - record;
                                         newsize = r_end - record + BFRAG;                                          newsize = r_end - record + BFRAG;
                                         record = realloc(record, newsize);                                          newrecord = realloc(record, newsize);
                                         if (record == NULL) {                                          if (newrecord == NULL) {
                                                 errno = ENOMEM;                                                  free(record);
                                                 if (myfd)                                                  if (myfd)
                                                         (void)close(fd);                                                          (void)close(fd);
                                                   errno = ENOMEM;
                                                 return (-2);                                                  return (-2);
                                         }                                          }
                                           record = newrecord;
                                         r_end = record + newsize;                                          r_end = record + newsize;
                                         rp = record + pos;                                          rp = record + pos;
                                 }                                  }
Line 555  tc_exp: {
Line 595  tc_exp: {
                                 newsize = r_end - record + diff + BFRAG;                                  newsize = r_end - record + diff + BFRAG;
                                 tcpos = tcstart - record;                                  tcpos = tcstart - record;
                                 tcposend = tcend - record;                                  tcposend = tcend - record;
                                 record = realloc(record, newsize);                                  newrecord = realloc(record, newsize);
                                 if (record == NULL) {                                  if (newrecord == NULL) {
                                         errno = ENOMEM;                                          free(record);
                                         if (myfd)                                          if (myfd)
                                                 (void)close(fd);                                                  (void)close(fd);
                                         free(icap);                                          free(icap);
                                           errno = ENOMEM;
                                         return (-2);                                          return (-2);
                                 }                                  }
                                   record = newrecord;
                                 r_end = record + newsize;                                  r_end = record + newsize;
                                 rp = record + pos;                                  rp = record + pos;
                                 tcstart = record + tcpos;                                  tcstart = record + tcpos;
Line 593  tc_exp: {
Line 635  tc_exp: {
         if (myfd)          if (myfd)
                 (void)close(fd);                  (void)close(fd);
         *len = rp - record - 1; /* don't count NUL */          *len = rp - record - 1; /* don't count NUL */
         if (r_end > rp)          if (r_end > rp) {
                 if ((record =                  if ((newrecord =
                      realloc(record, (size_t)(rp - record))) == NULL) {                       realloc(record, (size_t)(rp - record))) == NULL) {
                           free(record);
                         errno = ENOMEM;                          errno = ENOMEM;
                         return (-2);                          return (-2);
                 }                  }
                   record = newrecord;
           }
   
         *cap = record;          *cap = record;
         if (tc_not_resolved)          if (tc_not_resolved)
Line 606  tc_exp: {
Line 651  tc_exp: {
         return (0);          return (0);
 }  }
   
   #ifndef SMALL
 static int  static int
 cdbget(capdbp, bp, name)  cdbget(DB *capdbp, char **bp, const char *name)
         DB *capdbp;  
         char **bp;  
         const char *name;  
 {  {
         DBT key;          DBT key;
         DBT data;          DBT data;
   
         /* LINTED key is not modified */          _DIAGASSERT(capdbp != NULL);
         key.data = (char *)name;          _DIAGASSERT(bp != NULL);
           _DIAGASSERT(name != NULL);
   
           key.data = __UNCONST(name);
         key.size = strlen(name);          key.size = strlen(name);
   
         for (;;) {          for (;;) {
Line 639  cdbget(capdbp, bp, name)
Line 685  cdbget(capdbp, bp, name)
         *bp = (char *)data.data + 1;          *bp = (char *)data.data + 1;
         return (((char *)(data.data))[0] == TCERR ? 1 : 0);          return (((char *)(data.data))[0] == TCERR ? 1 : 0);
 }  }
   #endif
   
 /*  /*
  * Cgetmatch will return 0 if name is one of the names of the capability   * Cgetmatch will return 0 if name is one of the names of the capability
  * record buf, -1 if not.   * record buf, -1 if not.
  */   */
 int  int
 cgetmatch(buf, name)  cgetmatch(const char *buf, const char *name)
         const char *buf, *name;  
 {  {
         const char *np, *bp;          const char *np, *bp;
   
           _DIAGASSERT(buf != NULL);
           _DIAGASSERT(name != NULL);
   
         /*          /*
          * Start search at beginning of record.           * Start search at beginning of record.
          */           */
Line 672  cgetmatch(buf, name)
Line 721  cgetmatch(buf, name)
                 /*                  /*
                  * Match failed, skip to next name in record.                   * Match failed, skip to next name in record.
                  */                   */
                 bp--;   /* a '|' or ':' may have stopped the match */                  if (bp > buf)
                           bp--;   /* a '|' or ':' may have stopped the match */
                   else
                           return (-1);
                 for (;;)                  for (;;)
                         if (*bp == '\0' || *bp == ':')                          if (*bp == '\0' || *bp == ':')
                                 return (-1);    /* match failed totally */                                  return (-1);    /* match failed totally */
Line 683  cgetmatch(buf, name)
Line 735  cgetmatch(buf, name)
 }  }
   
 int  int
 cgetfirst(buf, db_array)  cgetfirst(char **buf, const char * const *db_array)
         char **buf, **db_array;  
 {  {
   
           _DIAGASSERT(buf != NULL);
           _DIAGASSERT(db_array != NULL);
   
         (void)cgetclose();          (void)cgetclose();
         return (cgetnext(buf, db_array));          return (cgetnext(buf, db_array));
 }  }
   
 static FILE *pfp;  static FILE *pfp;
 static int slash;  static int slash;
 static char **dbp;  static const char * const *dbp;
   
 int  int
 cgetclose()  cgetclose(void)
 {  {
         if (pfp != NULL) {          if (pfp != NULL) {
                 (void)fclose(pfp);                  (void)fclose(pfp);
Line 712  cgetclose()
Line 767  cgetclose()
  * specified by db_array.  It returns 0 upon completion of the database, 1   * specified by db_array.  It returns 0 upon completion of the database, 1
  * upon returning an entry with more remaining, and -1 if an error occurs.   * upon returning an entry with more remaining, and -1 if an error occurs.
  */   */
   /* coverity[+alloc : arg-*0] */
 int  int
 cgetnext(bp, db_array)  cgetnext(char **bp, const char * const *db_array)
         char **bp;  
         char **db_array;  
 {  {
         size_t len;          size_t len = 0;
         int status, done;          int status, done;
         char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];          char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];
         size_t dummy;          size_t dummy;
   
           _DIAGASSERT(bp != NULL);
           _DIAGASSERT(db_array != NULL);
   
         if (dbp == NULL)          if (dbp == NULL)
                 dbp = db_array;                  dbp = db_array;
   
Line 735  cgetnext(bp, db_array)
Line 792  cgetnext(bp, db_array)
                         line = toprec;                          line = toprec;
                 } else {                  } else {
                         line = fgetln(pfp, &len);                          line = fgetln(pfp, &len);
                         if (line == NULL && pfp) {                          if (line == NULL) {
                                   if (pfp == NULL)
                                           return -1;
                                 if (ferror(pfp)) {                                  if (ferror(pfp)) {
                                         (void)cgetclose();                                          (void)cgetclose();
                                         return (-1);                                          return (-1);
Line 776  cgetnext(bp, db_array)
Line 835  cgetnext(bp, db_array)
                 /*                  /*
                  * Line points to a name line.                   * Line points to a name line.
                  */                   */
                   if (len > sizeof(nbuf))
                           return -1;
                 done = 0;                  done = 0;
                 np = nbuf;                  np = nbuf;
                 for (;;) {                  for (;;) {
Line 807  cgetnext(bp, db_array)
Line 868  cgetnext(bp, db_array)
                                         line[len - 1] = '\0';                                          line[len - 1] = '\0';
                         }                          }
                 }                  }
                   if (len > sizeof(buf))
                           return -1;
                 rp = buf;                  rp = buf;
                 for(cp = nbuf; *cp != '\0'; cp++)                  for(cp = nbuf; *cp != '\0'; cp++)
                         if (*cp == '|' || *cp == ':')                          if (*cp == '|' || *cp == ':')
Line 842  cgetnext(bp, db_array)
Line 905  cgetnext(bp, db_array)
  * allocation failure).   * allocation failure).
  */   */
 int  int
 cgetstr(buf, cap, str)  cgetstr(char *buf, const char *cap, char **str)
         char *buf;  
         const char *cap;  
         char **str;  
 {  {
         u_int m_room;          u_int m_room;
         const char *bp;          const char *bp;
         char *mp;          char *mp;
         int len;          int len;
         char *mem;          char *mem, *newmem;
   
           _DIAGASSERT(buf != NULL);
           _DIAGASSERT(cap != NULL);
           _DIAGASSERT(str != NULL);
   
         /*          /*
          * Find string capability cap           * Find string capability cap
Line 938  cgetstr(buf, cap, str)
Line 1002  cgetstr(buf, cap, str)
                 if (m_room == 0) {                  if (m_room == 0) {
                         size_t size = mp - mem;                          size_t size = mp - mem;
   
                         if ((mem = realloc(mem, size + SFRAG)) == NULL)                          if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
                                   free(mem);
                                 return (-2);                                  return (-2);
                           }
                           mem = newmem;
                         m_room = SFRAG;                          m_room = SFRAG;
                         mp = mem + size;                          mp = mem + size;
                 }                  }
Line 951  cgetstr(buf, cap, str)
Line 1018  cgetstr(buf, cap, str)
         /*          /*
          * Give back any extra memory and return value and success.           * Give back any extra memory and return value and success.
          */           */
         if (m_room != 0)          if (m_room != 0) {
                 if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL)                  if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
                           free(mem);
                         return (-2);                          return (-2);
                   }
                   mem = newmem;
           }
         *str = mem;          *str = mem;
         return (len);          return (len);
 }  }
Line 969  cgetstr(buf, cap, str)
Line 1040  cgetstr(buf, cap, str)
  * error was encountered (storage allocation failure).   * error was encountered (storage allocation failure).
  */   */
 int  int
 cgetustr(buf, cap, str)  cgetustr(char *buf, const char *cap, char **str)
         char *buf;  
         const char *cap;  
         char **str;  
 {  {
         u_int m_room;          u_int m_room;
         const char *bp;          const char *bp;
         char *mp;          char *mp;
         int len;          int len;
         char *mem;          char *mem, *newmem;
   
           _DIAGASSERT(buf != NULL);
           _DIAGASSERT(cap != NULL);
           _DIAGASSERT(str != NULL);
   
         /*          /*
          * Find string capability cap           * Find string capability cap
Line 1014  cgetustr(buf, cap, str)
Line 1086  cgetustr(buf, cap, str)
                 if (m_room == 0) {                  if (m_room == 0) {
                         size_t size = mp - mem;                          size_t size = mp - mem;
   
                         if ((mem = realloc(mem, size + SFRAG)) == NULL)                          if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
                                   free(mem);
                                 return (-2);                                  return (-2);
                           }
                           mem = newmem;
                         m_room = SFRAG;                          m_room = SFRAG;
                         mp = mem + size;                          mp = mem + size;
                 }                  }
Line 1027  cgetustr(buf, cap, str)
Line 1102  cgetustr(buf, cap, str)
         /*          /*
          * Give back any extra memory and return value and success.           * Give back any extra memory and return value and success.
          */           */
         if (m_room != 0)          if (m_room != 0) {
                 if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL)                  if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
                           free(mem);
                         return (-2);                          return (-2);
                   }
                   mem = newmem;
           }
         *str = mem;          *str = mem;
         return (len);          return (len);
 }  }
Line 1041  cgetustr(buf, cap, str)
Line 1120  cgetustr(buf, cap, str)
  * numeric capability couldn't be found.   * numeric capability couldn't be found.
  */   */
 int  int
 cgetnum(buf, cap, num)  cgetnum(char *buf, const char *cap, long *num)
         char *buf;  
         const char *cap;  
         long *num;  
 {  {
         long n;          long n;
         int base, digit;          int base, digit;
         const char *bp;          const char *bp;
   
           _DIAGASSERT(buf != NULL);
           _DIAGASSERT(cap != NULL);
           _DIAGASSERT(num != NULL);
   
         /*          /*
          * Find numeric capability cap           * Find numeric capability cap
          */           */
Line 1106  cgetnum(buf, cap, num)
Line 1186  cgetnum(buf, cap, num)
  * Compare name field of record.   * Compare name field of record.
  */   */
 static int  static int
 nfcmp(nf, rec)  nfcmp(char *nf, char *rec)
         char *nf, *rec;  
 {  {
         char *cp, tmp;          char *cp, tmp;
         int ret;          int ret;
   
           _DIAGASSERT(nf != NULL);
           _DIAGASSERT(rec != NULL);
   
         for (cp = rec; *cp != ':'; cp++)          for (cp = rec; *cp != ':'; cp++)
                 ;                  ;
   

Legend:
Removed from v.1.29  
changed lines
  Added in v.1.46

CVSweb <webmaster@jp.NetBSD.org>