Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 20.8 Source Code 757


60 /*
61 * Internal functions.
62 */
63 static DB *_db_alloc(int);
64 static void _db_dodelete(DB *);
65 static int _db_find_and_lock(DB *, const char *, int);
66 static int _db_findfree(DB *, int, int);
67 static void _db_free(DB *);
68 static DBHASH _db_hash(DB *, const char *);
69 static char *_db_readdat(DB *);
70 static off_t _db_readidx(DB *, off_t);
71 static off_t _db_readptr(DB *, off_t);
72 static void _db_writedat(DB *, const char *, off_t, int);
73 static void _db_writeidx(DB *, const char *, off_t, int, off_t);
74 static void _db_writeptr(DB *, off_t, off_t);

75 /*
76 * Open or create a database. Same arguments as open(2).
77 */
78 DBHANDLE
79 db_open(const char *pathname, int oflag, ...)
80 {
81 DB *db;
82 int len, mode;
83 size_t i;
84 char asciiptr[PTR_SZ + 1],
85 hash[(NHASH_DEF + 1) * PTR_SZ + 2];
86 /* +2 for newline and null */
87 struct stat statbuff;

88 /*
89 * Allocate a DB structure, and the buffers it needs.
90 */
91 len=strlen(pathname);
92 if ((db = _db_alloc(len)) == NULL)
93 err_dump("db_open: _db_alloc error for DB");

[60 – 74] We have chosen to name all the user-callable (public) functions starting with
db_and all the internal (private) functions starting with_db_.The public
functions weredeclared in the library’s header file,apue_db.h.Wedeclare
the internal functions asstaticso they arevisible only to functions residing
in the same file (the file containing the library implementation).
[75 – 93] Thedb_openfunction has the same arguments asopen( 2 ).Ifthe caller wants
to create the database files, the optional thirdargument specifies the file
permissions. Thedb_openfunction opens the index file and the data file,
initializing the index file, if necessary.The function starts by calling
_db_allocto allocate and initialize aDBstructure.
Free download pdf