Ejemplo n.º 1
0
int hdbm_insert(hdbm db, datum key, datum value)
/*
  Insert a new entry into the table without looking
  for duplicates.  The datums (key and value) are duplicated.
  
  Return 1 on sucess, 0 on failure.
  
  */
{
    entry *top = NewEntry(key, value);
    
    call_stats.inserts++;
    
    if (!top)
	return 0;
    
    if (!hdbm_check(db)) {
	fprintf(stderr, "hdbm_insert: invalid database handle %d\n", db);
	return 0;
    }
    
    if (hash_tables[db].file) {
	/* If using a file write key/pair to file and free memory for value */
	
	int status = write_file_entry(db, top);
	datum_free(top->value); top->value.dptr = 0;
	if (!status) return 0;
    }
    
    put_in_bin(db, top);
    
    return 1;
}
Ejemplo n.º 2
0
static int hdbm_load(hdbm db, FILE *file)
/*
  Check that the file is indeed a data base and load data in
  */
{
    char header[32];
    file_entry fe;
    long rec_ptr;
    rewind(file);
    if ((hdbm_fread(header, sizeof(cookie), (size_t) 1, file) != 1) ||
	(strncmp(header, cookie, strlen(cookie)) != 0)) {
	(void) fprintf(stderr, "hdbm_load: cookie missing ... not a database\n");
	return 0;
    }

    /* Now simply keep reading until we hit end of file */
    
    rec_ptr = ftell(file);
    while (!hdbm_fseek(file, rec_ptr, SEEK_SET) &&
	   (hdbm_fread((char *) &fe, sizeof(fe), (size_t) 1, file) == 1)) {

	datum key, value;
	entry *e;

	if (fe.active) {
	    if (!datum_fread(file, rec_ptr+sizeof(fe), fe.key_size, &key)) {
		(void) fprintf(stderr, 
			       "hdbm_load: header present but key missing?\n");
		return 0;
	    }
	    
	    value.dptr = 0; value.dsize = fe.val_size;
	    
	    if (!(e = NewEntry(key, value)))
		return 0;
	    
	    datum_free(key); datum_free(value);
	    
	    e->rec_ptr = rec_ptr;
	    e->val_ptr = rec_ptr + ((long) sizeof(fe)) + fe.key_size;
	    
	    put_in_bin(db, e);
	}
	
	rec_ptr = rec_ptr + ((long) sizeof(fe)) + fe.key_size + fe.val_size;
	
    }
    
    /* Now check that we really are at EOF */
    
    (void) hdbm_fseek(file, 0L, SEEK_END);
    
    if (rec_ptr != ftell(file)) {
	fprintf(stderr, "hdbm_load: inconsistent end to file %ld != %ld\n",
		rec_ptr, ftell(file));
    }
    
    return TRUE;
}
Ejemplo n.º 3
0
int		auto_file(char *str, int *index, t_bin *bin, int star)
{
  glob_t	pglob;
  char		*tmp;
  int		i;
  int		ret;

  if ((tmp = get_word(str, index, star)) == NULL)
    return (-1);
  ret = glob(tmp, GLOB_ERR | GLOB_TILDE | GLOB_MARK, NULL, &pglob);
  if (ret == GLOB_NOSPACE)
    return (my_put_error("Glob failed\n"));
  if (ret == GLOB_NOMATCH || ret == GLOB_ABORTED)
    return (0);
  free(tmp);
  i = 0;
  while (i < (int)pglob.gl_pathc)
    {
      put_in_bin(bin, pglob.gl_pathv[i]);
      i = i + 1;
    }
  return (0);
}