int CStest2 (bool verbose,bool crypt) { short protectSave; char uniqueSave; int st; int cnt; int my_crypt; csFILE *dtStrm; struct tst_lst_ *tp; char last_name [48]; __ALIGNMENT__1 /* Required by some Sun compilers. */ struct cs_Dtdef_ dt_def; protectSave = cs_Protect; uniqueSave = cs_Unique; printf ("Testing Datum Dictionary functions.\n"); /* We do the same thing now with datum definitions. */ tp = dt_tst; while (tp->name [0] != '\0') { cs_Protect = protectSave; cs_Unique = uniqueSave; if (verbose) { printf ("Fetching %s datum definition.",tp->name); } tp->dt_ptr = CS_dtdef (tp->name); if (tp->dt_ptr == NULL) { (void)printf ("\nCS_dtdef failed on %s. [%d:%d]\n", tp->name, cs_Error, cs_Errno); return (1); } if (verbose) printf (" OK.\n"); if (CS_stricmp (tp->dt_ptr->group,"tESt")) { if (verbose) { printf ("Updating %s datum definition, protection off.",tp->name); } cs_Protect = -1; st = CS_dtupd (tp->dt_ptr,crypt); if (st != 1) { (void)printf ("\nCS_dtupd failed on %s at line %d. [%d:%d]\n",tp->name,(__LINE__-3),cs_Error,cs_Errno); return (1); } if (verbose) printf (" OK.\n"); if (verbose) { printf ("Deleting %s datum definition.",tp->name); } st = CS_dtdel (tp->dt_ptr); if (st != 0) { (void)printf ("CS_dtdel failed on %s at line %d. [%d:%d]\n",tp->name,(__LINE__-3),cs_Error,cs_Errno); return (1); } if (verbose) printf (" OK.\n"); } else { if (verbose) { printf ("Updating %s datum definition, protection on.",tp->name); } cs_Protect = 10000; st = CS_dtupd (tp->dt_ptr,crypt); if (st != 1) { (void)printf ("\nCS_dtupd failed on %s at line %d. [%d:%d]\n",tp->name,(__LINE__-3),cs_Error,cs_Errno); return (1); } if (verbose) printf (" OK.\n"); if (verbose) { printf ("Deleting %s datum definition.",tp->name); } st = CS_dtdel (tp->dt_ptr); if (st != 0) { (void)printf ("CS_dtdel failed on %s at line %d. [%d:%d]\n",tp->name,(__LINE__-3),cs_Error,cs_Errno); return (1); } if (verbose) printf (" OK.\n"); } tp++; } tp = dt_tst; while (tp->name [0] != '\0') { cs_Protect = protectSave; cs_Unique = uniqueSave; if (verbose) { printf ("Putting %s datum definition back in.",tp->name); } cs_Unique = '\0'; st = CS_dtupd (tp->dt_ptr,crypt); if (st != 0) { (void)printf ("\nCS_dtupd (2) failed on %s. [%d:%d]\n", tp->name, cs_Error, cs_Errno); return (1); } if (verbose) printf (" OK.\n"); CS_free (tp->dt_ptr); tp++; } if (verbose) { printf ("Checking the order of the Datum Dictionary.\n"); } cnt = 0; dtStrm = CS_dtopn (_STRM_BINRD); st = CS_dtrd (dtStrm,&dt_def,&my_crypt); CS_stncp (last_name,dt_def.key_nm,sizeof (last_name)); while (CS_dtrd (dtStrm,&dt_def,&my_crypt)) { cnt += 1; if (verbose && (cnt % 4) == 0) putchar ('.'); st = CS_stricmp (last_name,dt_def.key_nm); if (st > 0) { printf ("\nDatum Dictionary no longer sorted!!!\n"); CS_fclose (dtStrm); return (1); } else if (st == 0) { printf ("\nDuplicate entries in Datum Dictionary!!!\n"); CS_fclose (dtStrm); return (1); } } if (verbose) printf (" OK.\n"); CS_dtDictCls (dtStrm); return (0); }
int EXP_LVL3 CS_dtupd (struct cs_Dtdef_ *dtdef,int crypt) { extern char csErrnam []; extern short cs_Protect; extern char cs_Unique; short cs_time; int st; int flag; int dummy; csFILE *strm; long fpos; char *cp; __ALIGNMENT__1 /* For some versions of Sun compiler. */ struct cs_Dtdef_ my_dtdef; /* Capture the current time. For our purposes here, time is the number of days since (approx) January 1, 1990. If this record does get written, the protect field will indicate that it has changed. */ cs_time = (short)((CS_time ((cs_Time_ *)0) - 630720000L) / 86400L); if (dtdef->protect >= 0) { dtdef->protect = cs_time; } /* Prepare for a possible error. */ strm = NULL; /* Adjust the name and make sure it is all upper case. By convention, datum names are case insensitive. */ st = CS_nampp (dtdef->key_nm); if (st != 0) goto error; /* Open up the Datum Dictionary and verify its magic number. */ strm = CS_dtopn (_STRM_BINUP); if (strm == NULL) { goto error; } /* See if we have a datum with this name already. */ flag = CS_bins (strm,(long)sizeof (cs_magic_t),0L,sizeof (*dtdef), (char *)dtdef,(CMPFUNC_CAST)CS_dtcmp); if (flag < 0) goto error; if (flag) { /* Here when the datum already exists. See if we are allowed to change this definition. */ if (cs_Protect >= 0) { /* Distribution protection is enabled. */ fpos = CS_ftell (strm); if (fpos < 0L) { CS_erpt (cs_IOERR); goto error; } st = CS_dtrd (strm,&my_dtdef,&dummy); if (st == 0) CS_erpt (cs_INV_FILE); if (st <= 0) { goto error; } if (my_dtdef.protect == 1) { CS_stncp (csErrnam,dtdef->key_nm,MAXPATH); CS_erpt (cs_DT_PROT); goto error; } if (cs_Protect > 0 && my_dtdef.protect > 0) { if (my_dtdef.protect < (cs_time - cs_Protect)) /*lint !e644 */ { CS_stncp (csErrnam,dtdef->key_nm,MAXPATH); CS_erpt (cs_DT_UPROT); goto error; } } st = CS_fseek (strm,fpos,SEEK_SET); if (st < 0L) { CS_erpt (cs_IOERR); goto error; } } /* If we're still here, it's OK to update this definition. */ if (CS_dtwr (strm,dtdef,crypt)) { goto error; } } else { /* Here if the datum definition doesn't exist. We have to add it. If cs_Unique is not zero, we require that a cs_Unique character be present in the key name before we'll allow it to be written. */ if (cs_Unique != '\0') { cp = strchr (dtdef->key_nm,cs_Unique); if (cp == NULL) { csErrnam [0] = cs_Unique; csErrnam [1] = '\0'; CS_erpt (cs_UNIQUE); goto error; } } /* Now we can add it. Write to the end of the file, and then sort the file. */ st = CS_fseek (strm,0L,SEEK_END); if (st != 0) { CS_erpt (cs_IOERR); goto error; } if (CS_dtwr (strm,dtdef,crypt)) { goto error; } /* Sort the file into proper order, thereby moving the new datum to its proper place in the dictionary. */ st = CS_fseek (strm,(long)sizeof (cs_magic_t),SEEK_SET); if (st != 0) { CS_erpt (cs_IOERR); goto error; } st = CS_ips (strm,sizeof (*dtdef),0L,(CMPFUNC_CAST)CS_dtcmp); if (st < 0) goto error; } /* The Datum Dictionary has been updated. */ CS_dtDictCls (strm); return (flag); error: if (strm != NULL) CS_dtDictCls (strm); return (-1); }
struct cs_Dtdef_ * EXP_LVL3 CS_dtdef (Const char *dat_nam) { extern char csErrnam []; extern double cs_DelMax; /* 5,000.0 */ extern double cs_RotMax; /* 15.0 */ extern double cs_SclMax; /* 200.0 */ int st; int flag; int crypt; csFILE *strm; struct cs_Dtdef_ *dtptr; __ALIGNMENT__1 /* For some versions of Sun compiler. */ struct cs_Dtdef_ dtdef; /* Prepare for an error condition. */ dtptr = NULL; strm = NULL; /* Give the application first shot at satisfying this request. */ if (CS_usrDtDefPtr != NULL) { st = (*CS_usrDtDefPtr)(&dtdef,dat_nam); if (st < 0) return NULL; if (st == 0) { dtptr = (struct cs_Dtdef_ *)CS_malc (sizeof (struct cs_Dtdef_)); if (dtptr == NULL) { CS_erpt (cs_NO_MEM); goto error; } memmove (dtptr,&dtdef,sizeof (*dtptr)); return dtptr; } } /* Verify the name is OK. */ CS_stncp (dtdef.key_nm,dat_nam,sizeof (dtdef.key_nm)); st = CS_nampp (dtdef.key_nm); if (st != 0) goto error; /* Mark this name as unencrypted so that the comaprison function will work. */ dtdef.fill [0] = '\0'; /* Open the Datum Dictionary and test its magic number. */ strm = CS_dtopn (_STRM_BINRD); if (strm == NULL) goto error; /* Search for the requested datum. */ flag = CS_bins (strm,(long)sizeof (cs_magic_t),0L,sizeof (dtdef),&dtdef,(CMPFUNC_CAST)CS_dtcmp); if (flag < 0) goto error; /* Tell the user if we didn't find the requested datum. */ if (!flag) { CS_stncp (csErrnam,dat_nam,MAXPATH); CS_erpt (cs_DT_NOT_FND); goto error; } else { /* The datum exists, malloc some memory for it. */ dtptr = (struct cs_Dtdef_ *)CS_malc (sizeof (*dtptr)); if (dtptr == NULL) { CS_erpt (cs_NO_MEM); goto error; } /* Read it in. */ if (!CS_dtrd (strm,dtptr,&crypt)) { goto error; } } /* We don't need the datum dictionary anymore. */ if (!cs_DtStrmFlg) { CS_dtDictCls (strm); } strm = NULL; /* Verify that the values are not completely bogus. */ if (fabs (dtptr->delta_X) > cs_DelMax || fabs (dtptr->delta_Y) > cs_DelMax || fabs (dtptr->delta_Z) > cs_DelMax || fabs (dtptr->rot_X) > cs_RotMax || fabs (dtptr->rot_Y) > cs_RotMax || fabs (dtptr->rot_Z) > cs_RotMax || fabs (dtptr->bwscale) > cs_SclMax) { CS_stncp (csErrnam,dat_nam,MAXPATH); CS_erpt (cs_DTDEF_INV); goto error; } /* Return the initialized datum structure to the user. */ return (dtptr); error: if (strm != NULL) CS_dtDictCls (strm); if (dtptr != NULL) { CS_free (dtptr); dtptr = NULL; } return (dtptr); }
int EXP_LVL3 CS_dtdel (struct cs_Dtdef_ *dtdef) { extern char csErrnam []; extern char cs_Dir []; extern short cs_Protect; short cs_time; int st; csFILE *old_strm; csFILE *new_strm; int rd_st; int crypt; size_t wr_cnt; cs_magic_t magic; struct cs_Dtdef_ *my_ptr; char tmp_nam [MAXPATH]; __ALIGNMENT__1 /* For some versions of Sun compiler. */ struct cs_Dtdef_ cpy_buf; /* Capture the current time. */ cs_time = (short)((CS_time ((cs_Time_ *)0) - 630720000L) / 86400L); /* Prepare for an error. */ new_strm = NULL; old_strm = NULL; my_ptr = NULL; tmp_nam [0] = '\0'; /* Adjust the name and make sure it is all upper case. By convention, datum names are case insensitive. */ st = CS_nampp (dtdef->key_nm); if (st != 0) goto error; /* Get a pointer to the existing definition. If it doesn't exist, we're all done. */ my_ptr = CS_dtdef (dtdef->key_nm); if (my_ptr == NULL) { goto error; } /* See if this definition is protected. If so, we have to leave it alone. If cs_Protect < 0, there is no protection. */ if (cs_Protect >= 0) { if (my_ptr->protect == 1) { CS_stncp (csErrnam,my_ptr->key_nm,MAXPATH); CS_erpt (cs_DT_PROT); goto error; } if (cs_Protect > 0) { /* Here if user definition protection is enabled. */ if (my_ptr->protect < (cs_time - cs_Protect)) { CS_stncp (csErrnam,my_ptr->key_nm,MAXPATH); CS_erpt (cs_DT_UPROT); goto error; } } } CS_free (my_ptr); my_ptr = NULL; /* Make sure the entry that we have been provided is marked as unencrypted so that the comparison function will work. */ dtdef->fill [0] = '\0'; /* Open up the datum dictionary file and verify its magic number. */ old_strm = CS_dtopn (_STRM_BINRD); if (old_strm == NULL) { goto error; } /* Create a temporary file for the new dictionary. */ st = CS_tmpfn (tmp_nam); if (st != 0) { goto error; } new_strm = CS_fopen (tmp_nam,_STRM_BINWR); if (new_strm == NULL) { CS_erpt (cs_TMP_CRT); goto error; } /* Copy the file, skipping the entry to be deleted. First we must deal with the magic number. */ magic = cs_DTDEF_MAGIC; CS_bswap (&magic,"l"); wr_cnt = CS_fwrite ((char *)&magic,1,sizeof (magic),new_strm); if (wr_cnt != sizeof (magic)) { if (CS_ferror (new_strm)) CS_erpt (cs_IOERR); else CS_erpt (cs_DISK_FULL); goto error; } /* Now we copy the file. If the existing record was encrypted, we encrypt the record which we write. */ while ((rd_st = CS_dtrd (old_strm,&cpy_buf,&crypt)) > 0) { if (CS_dtcmp (&cpy_buf,dtdef) != 0) { if (CS_dtwr (new_strm,&cpy_buf,crypt)) { goto error; } } } if (rd_st != 0) { /* The copy loop terminated due to an error. */ goto error; } /* Close up, remove the old dictionary and rename the new dictionary. */ CS_fclose (new_strm); new_strm = NULL; CS_dtDictCls (old_strm); old_strm = NULL; st = CS_remove (cs_Dir); if (st != 0) { strcpy (csErrnam,cs_Dir); CS_erpt (cs_UNLINK); goto error; } st = CS_rename (tmp_nam,cs_Dir); if (st != 0) goto error; /* We're done. */ return (0); error: if (new_strm != NULL) { /* tmp_nam can never be uninitialized if new_fd >= 0 */ CS_fclose (new_strm); CS_remove (tmp_nam); /*lint !e534 !e645 */ } if (old_strm != NULL) CS_dtDictCls (old_strm); if (my_ptr != NULL) CS_free (my_ptr); return (-1); }