summaryrefslogtreecommitdiff
path: root/ext/sdbm/_sdbm.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-10-27 15:11:06 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-10-27 15:11:06 +0000
commitf8751046b8b0a37b6257efcbb2f5aa082346719d (patch)
tree597752c8ce0b67e7c4f2a8a6a3f6430c70a01ac7 /ext/sdbm/_sdbm.c
parentc1de5660711505d9f381d562596b3b06407766e1 (diff)
* ext/sdbm/_sdbm.c (sdbm_prep): refactored for less nesting.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33538 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/sdbm/_sdbm.c')
-rw-r--r--ext/sdbm/_sdbm.c38
1 files changed, 21 insertions, 17 deletions
diff --git a/ext/sdbm/_sdbm.c b/ext/sdbm/_sdbm.c
index 553190fe9c..64cda29437 100644
--- a/ext/sdbm/_sdbm.c
+++ b/ext/sdbm/_sdbm.c
@@ -187,6 +187,8 @@ sdbm_prep(char *dirname, char *pagname, int flags, int mode)
if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
return errno = ENOMEM, (DBM *) NULL;
+ db->pagf = -1;
+ db->dirf = -1;
db->flags = 0;
db->hmask = 0;
db->blkptr = 0;
@@ -205,31 +207,33 @@ sdbm_prep(char *dirname, char *pagname, int flags, int mode)
* If we fail anywhere, undo everything, return NULL.
*/
flags |= O_BINARY;
- if ((db->pagf = open(pagname, flags, mode)) > -1) {
- if ((db->dirf = open(dirname, flags, mode)) > -1) {
+
+ if ((db->pagf = open(pagname, flags, mode)) == -1) goto err;
+ if ((db->dirf = open(dirname, flags, mode)) == -1) goto err;
/*
* need the dirfile size to establish max bit number.
*/
- if (fstat(db->dirf, &dstat) == 0) {
+ if (fstat(db->dirf, &dstat) == -1) goto err;
/*
* zero size: either a fresh database, or one with a single,
* unsplit data page: dirpage is all zeros.
*/
- db->dirbno = (!dstat.st_size) ? 0 : -1;
- db->pagbno = -1;
- db->maxbno = dstat.st_size * (long) BYTESIZ;
+ db->dirbno = (!dstat.st_size) ? 0 : -1;
+ db->pagbno = -1;
+ db->maxbno = dstat.st_size * (long) BYTESIZ;
- (void) memset(db->pagbuf, 0, PBLKSIZ);
- (void) memset(db->dirbuf, 0, DBLKSIZ);
- /*
- * success
- */
- return db;
- }
- (void) close(db->dirf);
- }
- (void) close(db->pagf);
- }
+ (void) memset(db->pagbuf, 0, PBLKSIZ);
+ (void) memset(db->dirbuf, 0, DBLKSIZ);
+/*
+ * success
+ */
+ return db;
+
+ err:
+ if (db->pagf != -1)
+ (void) close(db->pagf);
+ if (db->dirf != -1)
+ (void) close(db->dirf);
free((char *) db);
return (DBM *) NULL;
}