This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
AIX patch (including Configure support for {sched,pthread}_yield,
[perl5.git] / ext / DB_File / DB_File.xs
index 93cf44a..b6e8a03 100644 (file)
@@ -3,8 +3,8 @@
  DB_File.xs -- Perl 5 interface to Berkeley DB 
 
  written by Paul Marquess (pmarquess@bfsec.bt.co.uk)
- last modified 12th Mar 1997
- version 1.12
+ last modified 8th Oct 1997
+ version 1.16
 
  All comments/suggestions/problems are welcome
 
                in-memory files when db 1.86 is used.
        1.11 -  No change to DB_File.xs
        1.12 -  No change to DB_File.xs
+       1.13 -  Tidied up a few casts.
+       1.14 -  Made it illegal to tie an associative array to a RECNO
+               database and an ordinary array to a HASH or BTREE database.
+       1.15 -  Patch from Gisle Aas <gisle@aas.no> to suppress "use of 
+               undefined value" warning with db_get and db_seq.
+       1.16 -  Minor additions to DB_File.xs to support multithreaded perl.
 
 */
 
 #include "perl.h"
 #include "XSUB.h"
 
+/* Being the Berkeley DB we prefer the <sys/cdefs.h> (which will be
+ * shortly #included by the <db.h>) __attribute__ to the possibly
+ * already defined __attribute__, for example by GNUC or by Perl. */
+#undef __attribute__
+
 #include <db.h>
+/* #ifdef DB_VERSION_MAJOR */
+/* #include <db_185.h> */
+/* #endif */
 
 #include <fcntl.h> 
 
@@ -84,7 +98,7 @@ typedef DB_File_type * DB_File ;
 typedef DBT DBTKEY ;
 
 
-/* #define TRACE    */
+/* #define TRACE */
 
 #define db_DESTROY(db)                  ((db->dbp)->close)(db->dbp)
 #define db_DELETE(db, key, flags)       ((db->dbp)->del)(db->dbp, &key, flags)
@@ -283,7 +297,7 @@ RECNOINFO * recno ;
     printf ("  cachesize = %d\n", recno->cachesize) ;
     printf ("  psize     = %d\n", recno->psize) ;
     printf ("  lorder    = %d\n", recno->lorder) ;
-    printf ("  reclen    = %d\n", recno->reclen) ;
+    printf ("  reclen    = %lu\n", (unsigned long)recno->reclen) ;
     printf ("  bval      = %d 0x%x\n", recno->bval, recno->bval) ;
     printf ("  bfname    = %d [%s]\n", recno->bfname, recno->bfname) ;
 }
@@ -351,7 +365,8 @@ I32      value ;
 }
 
 static DB_File
-ParseOpenInfo(name, flags, mode, sv)
+ParseOpenInfo(isHASH, name, flags, mode, sv)
+int    isHASH ;
 char * name ;
 int    flags ;
 int    mode ;
@@ -386,6 +401,10 @@ SV *   sv ;
 
         if (sv_isa(sv, "DB_File::HASHINFO"))
         {
+
+           if (!isHASH)
+               croak("DB_File can only tie an associative array to a DB_HASH database") ;
+
             RETVAL->type = DB_HASH ;
             openinfo = (void*)info ;
   
@@ -418,6 +437,9 @@ SV *   sv ;
         }
         else if (sv_isa(sv, "DB_File::BTREEINFO"))
         {
+           if (!isHASH)
+               croak("DB_File can only tie an associative array to a DB_BTREE database");
+
             RETVAL->type = DB_BTREE ;
             openinfo = (void*)info ;
    
@@ -462,23 +484,26 @@ SV *   sv ;
         }
         else if (sv_isa(sv, "DB_File::RECNOINFO"))
         {
+           if (isHASH)
+               croak("DB_File can only tie an array to a DB_RECNO database");
+
             RETVAL->type = DB_RECNO ;
             openinfo = (void *)info ;
 
             svp = hv_fetch(action, "flags", 5, FALSE);
-            info->recno.flags = (u_long) svp ? SvIV(*svp) : 0;
+            info->recno.flags = (u_long) (svp ? SvIV(*svp) : 0);
          
             svp = hv_fetch(action, "cachesize", 9, FALSE);
-            info->recno.cachesize = (u_int) svp ? SvIV(*svp) : 0;
+            info->recno.cachesize = (u_int) (svp ? SvIV(*svp) : 0);
          
             svp = hv_fetch(action, "psize", 5, FALSE);
-            info->recno.psize = (int) svp ? SvIV(*svp) : 0;
+            info->recno.psize = (u_int) (svp ? SvIV(*svp) : 0);
          
             svp = hv_fetch(action, "lorder", 6, FALSE);
-            info->recno.lorder = (int) svp ? SvIV(*svp) : 0;
+            info->recno.lorder = (int) (svp ? SvIV(*svp) : 0);
          
             svp = hv_fetch(action, "reclen", 6, FALSE);
-            info->recno.reclen = (size_t) svp ? SvIV(*svp) : 0;
+            info->recno.reclen = (size_t) (svp ? SvIV(*svp) : 0);
          
            svp = hv_fetch(action, "bval", 4, FALSE);
             if (svp && SvOK(*svp))
@@ -499,7 +524,7 @@ SV *   sv ;
             svp = hv_fetch(action, "bfname", 6, FALSE); 
             if (svp && SvOK(*svp)) {
                char * ptr = SvPV(*svp,na) ;
-                info->recno.bfname = (char*) na ? ptr : NULL ;
+                info->recno.bfname = (char*) (na ? ptr : NULL) ;
            }
            else
                info->recno.bfname = NULL ;
@@ -779,7 +804,8 @@ constant(name,arg)
 
 
 DB_File
-db_DoTie_(dbtype, name=undef, flags=O_CREAT|O_RDWR, mode=0666, type=DB_HASH)
+db_DoTie_(isHASH, dbtype, name=undef, flags=O_CREAT|O_RDWR, mode=0666, type=DB_HASH)
+       int             isHASH
        char *          dbtype
        int             flags
        int             mode
@@ -788,13 +814,13 @@ db_DoTie_(dbtype, name=undef, flags=O_CREAT|O_RDWR, mode=0666, type=DB_HASH)
            char *      name = (char *) NULL ; 
            SV *        sv = (SV *) NULL ; 
 
-           if (items >= 2 && SvOK(ST(1))) 
-               name = (char*) SvPV(ST(1), na) ; 
+           if (items >= 3 && SvOK(ST(2))) 
+               name = (char*) SvPV(ST(2), na) ; 
 
-            if (items == 5)
-               sv = ST(4) ;
+            if (items == 6)
+               sv = ST(5) ;
 
-           RETVAL = ParseOpenInfo(name, flags, mode, sv) ;
+           RETVAL = ParseOpenInfo(isHASH, name, flags, mode, sv) ;
            if (RETVAL->dbp == NULL)
                RETVAL = NULL ;
        }
@@ -879,7 +905,7 @@ db_FIRSTKEY(db)
            ST(0) = sv_newmortal();
            if (RETVAL == 0)
            {
-               if (Db->type != DB_RECNO)
+               if (db->type != DB_RECNO)
                    sv_setpvn(ST(0), key.data, key.size);
                else
                    sv_setiv(ST(0), (I32)*(I32*)key.data - 1);
@@ -900,7 +926,7 @@ db_NEXTKEY(db, key)
            ST(0) = sv_newmortal();
            if (RETVAL == 0)
            {
-               if (Db->type != DB_RECNO)
+               if (db->type != DB_RECNO)
                    sv_setpvn(ST(0), key.data, key.size);
                else
                    sv_setiv(ST(0), (I32)*(I32*)key.data - 1);
@@ -1047,7 +1073,7 @@ int
 db_get(db, key, value, flags=0)
        DB_File         db
        DBTKEY          key
-       DBT             value
+       DBT             value = NO_INIT
        u_int           flags
        INIT:
          CurrentDB = db ;
@@ -1083,7 +1109,7 @@ int
 db_seq(db, key, value, flags)
        DB_File         db
        DBTKEY          key 
-       DBT             value
+       DBT             value = NO_INIT
        u_int           flags
        INIT:
          CurrentDB = db ;