1: /* copen.c 1.8 85/03/24 */ 2: 3: #include <stdio.h> 4: #include "cpmio.h" 5: #include "cpmfio.h" 6: 7: /* 8: * Open cp/m file with the given file name and extension, return 9: * file pointer. A null pointer is returned if the file could not 10: * be opened. mode tells whether the file is to be read, written 11: * or both. 12: */ 13: 14: C_FILE * 15: c_open(name, ext, mode) 16: char *name, *ext; 17: { 18: 19: register int i, index, scnt; 20: register C_FILE *fptr; 21: char *malloc(), *fixname(); 22: 23: if ((index = searchdir(name, ext)) == -1) { 24: fprintf(stderr, "file not found: %s\n", fixname(name, ext)); 25: return (NULL); 26: } 27: /* test for legal mode */ 28: if (!(mode & RW)) { 29: fprintf(stderr, "open: illegal mode - %d\n", mode); 30: return (NULL); 31: } 32: #ifdef DEBUG 33: printf("directory index: %d\n", index); 34: #endif 35: for ((i = 0, fptr=c_iob); i < C_NFILE; i++,fptr++) 36: if (!(fptr->c_flag)) 37: break; 38: if (i == C_NFILE) { 39: fprintf(stderr, "too many open files\n"); 40: return (NULL); 41: } 42: 43: /* 44: * Free file descriptor slot found, initialize field, allocate 45: * memory and read first block. 46: */ 47: if ((fptr->c_buf = malloc(blksiz)) == NULL) { 48: printf("c_open: no memory!\n"); 49: return (NULL); 50: } 51: fptr->c_extno = 0; 52: fptr->c_base = fptr->c_buf; 53: fptr->c_flag = mode; 54: fptr->c_blk = 0; 55: fptr->c_ext = index; 56: fptr->c_dirp = dirbuf+index; 57: fptr->c_seccnt = 0xff &(dirbuf+index)->blkcnt; 58: scnt = (fptr->c_seccnt > blksiz/seclth) ? blksiz/seclth:fptr->c_seccnt; 59: #ifdef DEBUG 60: printf("c_open: scnt=%d\n",scnt); 61: #endif 62: if (getblock(0xff &(int)fptr->c_dirp->pointers[0], fptr->c_buf, scnt) 63: == EOF) 64: return (NULL); 65: fptr->c_cnt = seclth*scnt; 66: fptr->c_seccnt -= scnt; 67: return (fptr); 68: }