tdbio_set_dbname( const char *new_dbname, int create, int *r_nofile)
{
    char *fname;
    static int initialized = 0;

    if( !initialized ) {
	atexit( cleanup );
	initialized = 1;
    }

    *r_nofile = 0;

    if(new_dbname==NULL)
      fname=make_filename(opt.homedir,"trustdb" EXTSEP_S GPGEXT_GPG, NULL);
    else if (*new_dbname != DIRSEP_C )
      {
	if (strchr(new_dbname, DIRSEP_C) )
	  fname = make_filename (new_dbname, NULL);
	else
	  fname = make_filename (opt.homedir, new_dbname, NULL);
      }
    else
      fname = xstrdup (new_dbname);

    if( access( fname, R_OK ) ) {
#ifdef HAVE_W32CE_SYSTEM
      /* We know how the cegcc implementation of access works ;-). */
      if (GetLastError () == ERROR_FILE_NOT_FOUND)
        gpg_err_set_errno (ENOENT);
      else
        gpg_err_set_errno (EIO);
#endif /*HAVE_W32CE_SYSTEM*/
	if( errno != ENOENT ) {
	    log_error( _("can't access '%s': %s\n"), fname, strerror(errno) );
	    xfree(fname);
	    return GPG_ERR_TRUSTDB;
	}
	if (!create)
          *r_nofile = 1;
        else {
	    FILE *fp;
	    TRUSTREC rec;
	    int rc;
	    char *p = strrchr( fname, DIRSEP_C );
	    mode_t oldmask;
            int save_slash;

#if HAVE_W32_SYSTEM
            {
              /* Windows may either have a slash or a backslash.  Take
                 care of it.  */
              char *pp = strrchr (fname, '/');
              if (!p || pp > p)
                p = pp;
            }
#endif /*HAVE_W32_SYSTEM*/
	    assert (p);
            save_slash = *p;
	    *p = 0;
	    if( access( fname, F_OK ) ) {
		try_make_homedir( fname );
                if (access (fname, F_OK ))
                  log_fatal (_("%s: directory does not exist!\n"), fname);
	    }
	    *p = save_slash;

	    xfree(db_name);
	    db_name = fname;
#ifdef __riscos__
	    if( !lockhandle )
              lockhandle = dotlock_create (db_name, 0);
	    if( !lockhandle )
		log_fatal( _("can't create lock for '%s'\n"), db_name );
            if( dotlock_make (lockhandle, -1) )
                log_fatal( _("can't lock '%s'\n"), db_name );
#endif /* __riscos__ */
	    oldmask=umask(077);
            if (is_secured_filename (fname)) {
                fp = NULL;
                gpg_err_set_errno (EPERM);
            }
            else
                fp =fopen( fname, "wb" );
	    umask(oldmask);
	    if( !fp )
		log_fatal (_("can't create '%s': %s\n"),
                           fname, strerror (errno));
	    fclose(fp);
	    db_fd = open( db_name, O_RDWR | MY_O_BINARY );
	    if( db_fd == -1 )
		log_fatal (_("can't open '%s': %s\n"),
                           db_name, strerror (errno));

#ifndef __riscos__
	    if( !lockhandle )
              lockhandle = dotlock_create (db_name, 0);
	    if( !lockhandle )
		log_fatal( _("can't create lock for '%s'\n"), db_name );
#endif /* !__riscos__ */

            rc = create_version_record ();
	    if( rc )
		log_fatal( _("%s: failed to create version record: %s"),
						   fname, gpg_strerror (rc));
	    /* and read again to check that we are okay */
	    if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
		log_fatal( _("%s: invalid trustdb created\n"), db_name );

	    if( !opt.quiet )
		log_info(_("%s: trustdb created\n"), db_name);

	    return 0;
	}
    }
    xfree(db_name);
    db_name = fname;
    return 0;
}