avoid negative return value from Win32::GetTickCount()
[p5sagit/p5-mst-13.2.git] / ext / ODBM_File / ODBM_File.xs
CommitLineData
463ee0b2 1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5#ifdef NULL
e5c9fcd0 6#undef NULL /* XXX Why? */
463ee0b2 7#endif
8e07c86e 8#ifdef I_DBM
9# include <dbm.h>
10#else
11# ifdef I_RPCSVC_DBM
12# include <rpcsvc/dbm.h>
13# endif
14#endif
463ee0b2 15
1639c7b3 16#ifdef DBM_BUG_DUPLICATE_FREE
17/*
18 * DBM on at least Ultrix and HPUX call dbmclose() from dbminit(),
19 * resulting in duplicate free() because dbmclose() does *not*
20 * check if it has already been called for this DBM.
21 * If some malloc/free calls have been done between dbmclose() and
22 * the next dbminit(), the memory might be used for something else when
23 * it is freed.
24 * Verified to work on ultrix4.3. Probably will work on HP/UX.
25 * Set DBM_BUG_DUPLICATE_FREE in the extension hint file.
26 */
27/* Close the previous dbm, and fail to open a new dbm */
28#define dbmclose() ((void) dbminit("/tmp/x/y/z/z/y"))
29#endif
30
463ee0b2 31#include <fcntl.h>
32
33typedef void* ODBM_File;
34
a0d0e21e 35#define odbm_FETCH(db,key) fetch(key)
36#define odbm_STORE(db,key,value,flags) store(key,value)
37#define odbm_DELETE(db,key) delete(key)
38#define odbm_FIRSTKEY(db) firstkey()
39#define odbm_NEXTKEY(db,key) nextkey(key)
463ee0b2 40
41static int dbmrefcnt;
42
85e6fe83 43#ifndef DBM_REPLACE
463ee0b2 44#define DBM_REPLACE 0
85e6fe83 45#endif
463ee0b2 46
47MODULE = ODBM_File PACKAGE = ODBM_File PREFIX = odbm_
48
e5c9fcd0 49#ifndef NULL
50# define NULL 0
51#endif
52
463ee0b2 53ODBM_File
a0d0e21e 54odbm_TIEHASH(dbtype, filename, flags, mode)
463ee0b2 55 char * dbtype
56 char * filename
57 int flags
58 int mode
59 CODE:
60 {
46fc3d4c 61 char *tmpbuf;
463ee0b2 62 if (dbmrefcnt++)
63 croak("Old dbm can only open one database");
46fc3d4c 64 New(0, tmpbuf, strlen(filename) + 5, char);
65 SAVEFREEPV(tmpbuf);
463ee0b2 66 sprintf(tmpbuf,"%s.dir",filename);
3280af22 67 if (stat(tmpbuf, &PL_statbuf) < 0) {
463ee0b2 68 if (flags & O_CREAT) {
69 if (mode < 0 || close(creat(tmpbuf,mode)) < 0)
70 croak("ODBM_File: Can't create %s", filename);
71 sprintf(tmpbuf,"%s.pag",filename);
72 if (close(creat(tmpbuf,mode)) < 0)
73 croak("ODBM_File: Can't create %s", filename);
74 }
75 else
76 croak("ODBM_FILE: Can't open %s", filename);
77 }
78 RETVAL = (void*)(dbminit(filename) >= 0 ? &dbmrefcnt : 0);
6b88bc9c 79 ST(0) = sv_mortalcopy(&PL_sv_undef);
4e2a63a7 80 sv_setptrobj(ST(0), RETVAL, dbtype);
463ee0b2 81 }
82
83void
84DESTROY(db)
85 ODBM_File db
86 CODE:
87 dbmrefcnt--;
88 dbmclose();
89
90datum
a0d0e21e 91odbm_FETCH(db, key)
463ee0b2 92 ODBM_File db
93 datum key
94
95int
a0d0e21e 96odbm_STORE(db, key, value, flags = DBM_REPLACE)
463ee0b2 97 ODBM_File db
98 datum key
99 datum value
100 int flags
a0d0e21e 101 CLEANUP:
102 if (RETVAL) {
103 if (RETVAL < 0 && errno == EPERM)
104 croak("No write permission to odbm file");
748a9306 105 croak("odbm store returned %d, errno %d, key \"%s\"",
a0d0e21e 106 RETVAL,errno,key.dptr);
107 }
463ee0b2 108
109int
a0d0e21e 110odbm_DELETE(db, key)
463ee0b2 111 ODBM_File db
112 datum key
113
114datum
a0d0e21e 115odbm_FIRSTKEY(db)
463ee0b2 116 ODBM_File db
117
118datum
a0d0e21e 119odbm_NEXTKEY(db, key)
463ee0b2 120 ODBM_File db
121 datum key
122