Commit | Line | Data |
---|---|---|
463ee0b2 LW |
1 | #include "EXTERN.h" |
2 | #include "perl.h" | |
3 | #include "XSUB.h" | |
4 | ||
5 | #ifdef NULL | |
6 | #undef NULL | |
7 | #endif | |
8e07c86e AD |
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 LW |
31 | #include <fcntl.h> |
32 | ||
33 | typedef void* ODBM_File; | |
34 | ||
a0d0e21e LW |
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 LW |
40 | |
41 | static int dbmrefcnt; | |
42 | ||
85e6fe83 | 43 | #ifndef DBM_REPLACE |
463ee0b2 | 44 | #define DBM_REPLACE 0 |
85e6fe83 | 45 | #endif |
463ee0b2 LW |
46 | |
47 | MODULE = ODBM_File PACKAGE = ODBM_File PREFIX = odbm_ | |
48 | ||
49 | ODBM_File | |
a0d0e21e | 50 | odbm_TIEHASH(dbtype, filename, flags, mode) |
463ee0b2 LW |
51 | char * dbtype |
52 | char * filename | |
53 | int flags | |
54 | int mode | |
55 | CODE: | |
56 | { | |
46fc3d4c | 57 | char *tmpbuf; |
463ee0b2 LW |
58 | if (dbmrefcnt++) |
59 | croak("Old dbm can only open one database"); | |
46fc3d4c | 60 | New(0, tmpbuf, strlen(filename) + 5, char); |
61 | SAVEFREEPV(tmpbuf); | |
463ee0b2 LW |
62 | sprintf(tmpbuf,"%s.dir",filename); |
63 | if (stat(tmpbuf, &statbuf) < 0) { | |
64 | if (flags & O_CREAT) { | |
65 | if (mode < 0 || close(creat(tmpbuf,mode)) < 0) | |
66 | croak("ODBM_File: Can't create %s", filename); | |
67 | sprintf(tmpbuf,"%s.pag",filename); | |
68 | if (close(creat(tmpbuf,mode)) < 0) | |
69 | croak("ODBM_File: Can't create %s", filename); | |
70 | } | |
71 | else | |
72 | croak("ODBM_FILE: Can't open %s", filename); | |
73 | } | |
74 | RETVAL = (void*)(dbminit(filename) >= 0 ? &dbmrefcnt : 0); | |
75 | ST(0) = sv_mortalcopy(&sv_undef); | |
4e2a63a7 | 76 | sv_setptrobj(ST(0), RETVAL, dbtype); |
463ee0b2 LW |
77 | } |
78 | ||
79 | void | |
80 | DESTROY(db) | |
81 | ODBM_File db | |
82 | CODE: | |
83 | dbmrefcnt--; | |
84 | dbmclose(); | |
85 | ||
86 | datum | |
a0d0e21e | 87 | odbm_FETCH(db, key) |
463ee0b2 LW |
88 | ODBM_File db |
89 | datum key | |
90 | ||
91 | int | |
a0d0e21e | 92 | odbm_STORE(db, key, value, flags = DBM_REPLACE) |
463ee0b2 LW |
93 | ODBM_File db |
94 | datum key | |
95 | datum value | |
96 | int flags | |
a0d0e21e LW |
97 | CLEANUP: |
98 | if (RETVAL) { | |
99 | if (RETVAL < 0 && errno == EPERM) | |
100 | croak("No write permission to odbm file"); | |
748a9306 | 101 | croak("odbm store returned %d, errno %d, key \"%s\"", |
a0d0e21e LW |
102 | RETVAL,errno,key.dptr); |
103 | } | |
463ee0b2 LW |
104 | |
105 | int | |
a0d0e21e | 106 | odbm_DELETE(db, key) |
463ee0b2 LW |
107 | ODBM_File db |
108 | datum key | |
109 | ||
110 | datum | |
a0d0e21e | 111 | odbm_FIRSTKEY(db) |
463ee0b2 LW |
112 | ODBM_File db |
113 | ||
114 | datum | |
a0d0e21e | 115 | odbm_NEXTKEY(db, key) |
463ee0b2 LW |
116 | ODBM_File db |
117 | datum key | |
118 |