1: /* $Header$ */ 2: 3: /* 4: * Author: Peter J. Nicklin 5: */ 6: 7: /* 8: * mustopenpdb() opens a database in the manner of openpdb(). However, if the 9: * database cannot be accessed, exit(1) is called. 10: */ 11: #include <stdio.h> 12: #include "null.h" 13: #include "path.h" 14: #include "pdb.h" 15: #include "system.h" 16: #include "yesno.h" 17: 18: PDB * 19: mustopenpdb(name, path, mode) 20: char *name; /* database name */ 21: char *path; /* directory pathname to database */ 22: register char *mode; /* mode of access */ 23: { 24: char *malloc(); /* memory allocator */ 25: char *pathcat(); /* pathname concatenation */ 26: char *strcat(); /* string concatenation */ 27: char *strcpy(); /* string copy */ 28: char *strncpy(); /* string copy n characters */ 29: char tname[15]; /* temporary database name */ 30: FILE *fopen(); /* open file */ 31: PDB *pdbp; /* database stream */ 32: 33: if ((pdbp = (PDB *) malloc(sizeof(PDB))) == NULL) 34: fatal("out of memory"); 35: pathcat(pdbp->path, path, name); 36: if (mode[0]=='r' && mode[1]=='\0') 37: { 38: if ((pdbp->fp = fopen(pdbp->path, mode)) == NULL) 39: { 40: pperror(pdbp->path); 41: exit(1); 42: } 43: } 44: else if (mode[0]=='w' || mode[0]=='a' || (mode[0]=='r' && mode[1]=='w')) 45: { 46: strncpy(tname, name, 8); 47: tname[8] = '\0'; 48: strcat(tname, "_temp"); 49: pathcat(pdbp->tpath, path, tname); 50: if (FILEXIST(pdbp->tpath)) 51: fatal("%s temporarily unavailable", pdbp->path); 52: if ((pdbp->fp = fopen(pdbp->path, mode)) == NULL) 53: { 54: pperror(pdbp->path); 55: exit(1); 56: } 57: if ((pdbp->tfp = fopen(pdbp->tpath, "w")) == NULL) 58: { 59: pperror(pdbp->path); 60: exit(1); 61: } 62: if (mode[0]=='w' || mode[0]=='a') 63: fclose(pdbp->tfp); 64: } 65: else 66: fatal("bad mode %s opening %s", mode, pdbp->path); 67: pdbp->flag &= ~(_PACCESS|_PSTAT); 68: switch (*mode) 69: { 70: case 'r': 71: pdbp->flag |= _PREAD; 72: if (mode[1] != 'w') 73: break; 74: case 'w': 75: pdbp->flag |= _PWRITE; 76: break; 77: case 'a': 78: pdbp->flag |= _PAPPEND | _PEOF; 79: break; 80: } 81: strcpy(pdbp->root, path); 82: return(pdbp); 83: }