/* -*-c,save-*- */ /* * LUOPEN.C - Open library file * Robert Heller. Created: Sat Nov 8, 1986 13:08:49.68 * Last Mod: * * (c) Copyright 1986 by Robert Heller * All Rights Reserved * * */ #include /* STD I/O defs */ #include /* char type defs */ #include "ludef.h" /* ludefs */ #include "luvars.h" /* lu variables */ lu_opn(argcc,argvv) FAST int *argcc; FAST char ***argvv; { FAST int newp, status; FAST char *lname; LOCAL char temp[64]; LOCAL char plname[20]; if (lu_openp) { fprintf(stderr,"lu: library already open!\n"); while (*argcc > 0 && ***argvv != '-') { *argcc -= 1; *argvv += 1; } return; } if (*argcc > 0 && ***argvv != '-') { *argcc -= 1; lname = **argvv; *argvv += 1; strcpy(plname,lname); lname = strchr(plname,'.'); if (lname == NULL) strcat(plname,".LBR"); sprintf(temp,"lu: library file %s",plname); newp = FALSE; lu_fd = lopen(plname,OPEN_RW); if (lu_fd == FAILURE) { newp = TRUE; lu_fd = lcreat(plname,0777); } if (lu_fd == FAILURE) { perror(temp); return; } getname(lu_fd,lu_libr); if (newp) status = new_dire(); else status = old_dire(); if (status == FAILURE) { close(lu_fd); if (newp) { unlink(lu_libr); fprintf(stderr,"lu: error creating library %s\n",lu_libr); } else fprintf(stderr,"lu: error reading or bad format library %s\n", lu_libr); return; } lu_openp = TRUE; } } new_dire() { FAST numents,i,j,k; LOCAL char line[64]; FAST char *p; #ifdef CRCCHECK FAST unsigned short int crc; unsigned short int calcrc(); #endif char *gets(); printf("lu: new library %s opened. ",lu_libr); do { printf("Reserve how many entries? "); p = gets(line); if (p == NULL) { if (!isatty(fileno(stdin))) printf("\n"); return(FAILURE); } if (!isatty(fileno(stdin))) printf("%s\n",line); numents = atoi(line); } while (numents < 1 && numents >= MAXFILES); numents++; /* allow for directory */ /* adjust size to sector boundary */ numents += (SECTSIZE / sizeof(LUDIR)) - 1; k = numents % (SECTSIZE / sizeof(LUDIR)); numents -= k; lu_slots = numents; /* initialize directory entry */ lu_dire[0].lu_stat = ACTIVE; strncpy(lu_dire[0].lu_name," ",11); lu_dire[0].lu_off = 0; lu_dire[0].lu_len = ((numents * sizeof(LUDIR)) / SECTSIZE); lu_dire[0].lu_crc = 0; for (p = lu_dire[0].lu_fill, i=0; i<14; i++, p++) *p = '\0'; for (i = 1; i < numents; i++) lu_dire[i].lu_stat = UNUSED; #ifdef CRCCHECK crc = calcrc(0,&lu_dire[0],numents * sizeof(LUDIR)); lu_dire[0].lu_crc = crc; #else lu_dire[0].lu_crc = 0; #endif lu_modp = TRUE; printf("lu: new library file %s has %d entries, %d free\n",lu_libr,numents, numents-1); put_dire(); close(lu_fd); lu_fd = lopen(lu_libr,OPEN_RW); return(TRUE); } old_dire() { FAST numents,i,status,j; FAST char *p; #ifdef CRCCHECK FAST unsigned short int crc; unsigned short int calcrc(); #endif lseek(lu_fd,0L,0); status = read(lu_fd,&lu_dire[0],SECTSIZE); if (status != SECTSIZE) return(FAILURE); #ifdef CPM68K lu_dire[0].lu_off = swapw(lu_dire[0].lu_off); lu_dire[0].lu_len = swapw(lu_dire[0].lu_len); lu_dire[0].lu_crc = swapw(lu_dire[0].lu_crc); #endif if (lu_dire[0].lu_stat != 0 || strncmp(lu_dire[0].lu_name," ",11) != 0 || lu_dire[0].lu_off != 0 || lu_dire[0].lu_len == 0) return(FAILURE); numents = (lu_dire[0].lu_len * SECTSIZE) / sizeof(LUDIR); if (numents > MAXFILES) return(FAILURE); lseek(lu_fd,0L,0); i = SECTSIZE * lu_dire[0].lu_len; status = read(lu_fd,&lu_dire[0],i); if (status != i) return(FAILURE); #ifdef CPM68K for (i = 0; i < numents; i++) { lu_dire[i].lu_off = swapw(lu_dire[i].lu_off); lu_dire[i].lu_len = swapw(lu_dire[i].lu_len); lu_dire[i].lu_crc = swapw(lu_dire[i].lu_crc); } #endif #ifdef CRCCHECK crc = lu_dire[0].lu_crc; lu_dire[0].lu_crc = 0; if (crc != calcrc(0,&lu_dire[0],numents * sizeof(LUDIR))) return(FAILURE); lu_dire[0].lu_crc = crc; #else lu_dire[0].lu_crc = 0; #endif lu_modp = FALSE; j = 0; lu_slots = numents; for (i = 0; i < numents; i++) if (lu_dire[i].lu_stat == UNUSED) j++; printf("lu: old library file %s has %d entries, %d free\n",lu_libr,numents, j); return(SUCCESS); }