1: /* $Header$ */
2:
3: /*
4: * Author: Peter J. Nicklin
5: */
6:
7: /*
8: * openpdb() opens a database named by name located in a directory with
9: * name path and associates a database stream with it. Returns a pointer
10: * which identifies the database stream in subsequent operations. A null
11: * pointer is returned if the database cannot be accessed or is already
12: * open for writing or appending. When writing or appending, a temporary
13: * file with name name_temp is created in the same directory as the database.
14: */
15: #include <stdio.h>
16: #include "null.h"
17: #include "path.h"
18: #include "pdb.h"
19: #include "system.h"
20: #include "yesno.h"
21:
22: extern int errno;
23: extern int sys_nerr;
24: extern char *sys_errlist[];
25:
26: char PDBERR[PDBERRSIZE]; /* database error message buffer */
27:
28: PDB *
29: openpdb(name, path, mode)
30: char *name; /* database name */
31: char *path; /* directory pathname to database */
32: register char *mode; /* mode of access */
33: {
34: char *malloc(); /* memory allocator */
35: char *pathcat(); /* pathname concatenation */
36: char *sprintf(); /* print output to string */
37: char *strcat(); /* string concatenation */
38: char *strcpy(); /* string copy */
39: char *strncpy(); /* string copy n characters */
40: char tname[15]; /* temporary database name */
41: FILE *fopen(); /* open file */
42: PDB *pdbp; /* database stream */
43:
44: if ((pdbp = (PDB *) malloc(sizeof(PDB))) == NULL)
45: {
46: sprintf(PDBERR, "out of memory");
47: return(NULL);
48: }
49: pathcat(pdbp->path, path, name);
50: if (mode[0]=='r' && mode[1]=='\0')
51: {
52: if ((pdbp->fp = fopen(pdbp->path, mode)) == NULL)
53: {
54: if (errno < sys_nerr)
55: {
56: sprintf(PDBERR, "%s: %s", pdbp->path,
57: sys_errlist[errno]);
58: }
59: else {
60: sprintf(PDBERR, "can't open %s", pdbp->path);
61: }
62: free((char *) pdbp);
63: return(NULL);
64: }
65: }
66: else if (mode[0]=='w' || mode[0]=='a' || (mode[0]=='r' && mode[1]=='w'))
67: {
68: strncpy(tname, name, 8);
69: tname[8] = '\0';
70: strcat(tname, "_temp");
71: pathcat(pdbp->tpath, path, tname);
72: if (FILEXIST(pdbp->tpath))
73: {
74: sprintf(PDBERR,"%s temporarily unavailable",pdbp->path);
75: free((char *) pdbp);
76: return(NULL);
77: }
78: if ((pdbp->fp = fopen(pdbp->path, mode)) == NULL)
79: {
80: if (errno < sys_nerr)
81: {
82: sprintf(PDBERR, "%s: %s", pdbp->path,
83: sys_errlist[errno]);
84: }
85: else {
86: sprintf(PDBERR, "can't open %s", pdbp->path);
87: }
88: free((char *) pdbp);
89: return(NULL);
90: }
91: if ((pdbp->tfp = fopen(pdbp->tpath, "w")) == NULL)
92: {
93: if (errno < sys_nerr)
94: {
95: sprintf(PDBERR, "%s: %s", pdbp->path,
96: sys_errlist[errno]);
97: }
98: else {
99: sprintf(PDBERR, "can't open %s", pdbp->path);
100: }
101: fclose(pdbp->fp);
102: free((char *) pdbp);
103: return(NULL);
104: }
105: if (mode[0]=='w' || mode[0]=='a')
106: fclose(pdbp->tfp);
107: }
108: else {
109: sprintf(PDBERR, "bad mode %s opening %s", mode, pdbp->path);
110: free((char *) pdbp);
111: return(NULL);
112: }
113: pdbp->flag &= ~(_PACCESS|_PSTAT);
114: switch (*mode)
115: {
116: case 'r':
117: pdbp->flag |= _PREAD;
118: if (mode[1] != 'w')
119: break;
120: case 'w':
121: pdbp->flag |= _PWRITE;
122: break;
123: case 'a':
124: pdbp->flag |= _PAPPEND | _PEOF;
125: break;
126: }
127: strcpy(pdbp->root, path);
128: *pdbp->perr = '\0';
129: return(pdbp);
130: }
Defined functions
Defined variables