+ int i;
+
+ mc->nmolecules = 0;
+# ifdef LOAD_FILES
+ if (molecule_str && *molecule_str &&
+ strcmp(molecule_str, "(default)")) /* try external PDB files */
+ {
+ /* The -molecule option can point to a .pdb file, or to
+ a directory of them.
+ */
+ int wire = MI_IS_WIREFRAME(mi);
+ struct stat st;
+ int nfiles = 0;
+ int list_size = 0;
+ char **files = 0;
+ int molecule_ctr;
+
+ if (!stat (molecule_str, &st) &&
+ S_ISDIR (st.st_mode))
+ {
+ char buf [255];
+ DIR *pdb_dir;
+ struct dirent *dentry;
+
+ pdb_dir = opendir (molecule_str);
+ if (! pdb_dir)
+ {
+ sprintf (buf, "%.100s: %.100s", progname, molecule_str);
+ perror (buf);
+ exit (1);
+ }
+
+ if (verbose_p)
+ fprintf (stderr, "%s: directory %s\n", progname, molecule_str);
+
+ nfiles = 0;
+ list_size = 100;
+ files = (char **) calloc (sizeof(*files), list_size);
+
+ while ((dentry = readdir (pdb_dir)))
+ {
+ int L = strlen (dentry->d_name);
+ if (L > 4 && !strcasecmp (dentry->d_name + L - 4, ".pdb"))
+ {
+ char *fn;
+ if (nfiles >= list_size-1)
+ {
+ list_size = (list_size + 10) * 1.2;
+ files = (char **)
+ realloc (files, list_size * sizeof(*files));
+ if (!files)
+ {
+ OOM:
+ fprintf (stderr, "%s: out of memory (%d files)\n",
+ progname, nfiles);
+ exit (1);
+ }
+ }
+
+ fn = (char *) malloc (strlen (molecule_str) + L + 10);
+ if (!fn) goto OOM;
+ strcpy (fn, molecule_str);
+ if (fn[strlen(fn)-1] != '/') strcat (fn, "/");
+ strcat (fn, dentry->d_name);
+ files[nfiles++] = fn;
+ if (verbose_p)
+ fprintf (stderr, "%s: file %s\n", progname, fn);
+ }
+ }
+ closedir (pdb_dir);
+
+ if (nfiles == 0)
+ fprintf (stderr, "%s: no .pdb files in directory %s\n",
+ progname, molecule_str);
+ }
+ else
+ {
+ files = (char **) malloc (sizeof (*files));
+ nfiles = 1;
+ files[0] = strdup (molecule_str);
+ if (verbose_p)
+ fprintf (stderr, "%s: file %s\n", progname, molecule_str);
+ }
+
+ mc->nmolecules = nfiles;
+ mc->molecules = (molecule *) calloc (sizeof (molecule), mc->nmolecules);
+ molecule_ctr = 0;
+ for (i = 0; i < mc->nmolecules; i++)
+ {
+ if (verbose_p)
+ fprintf (stderr, "%s: reading %s\n", progname, files[i]);
+ if (!parse_pdb_file (&mc->molecules[molecule_ctr], files[i]))
+ {
+ if ((wire || !do_atoms) &&
+ !do_labels &&
+ mc->molecules[molecule_ctr].nbonds == 0)
+ {
+ /* If we're not drawing atoms (e.g., wireframe mode), and
+ there is no bond info, then make sure labels are turned
+ on, or we'll be looking at a black screen... */
+ fprintf (stderr, "%s: %s: no bonds: turning -label on.\n",
+ progname, files[i]);
+ do_labels = 1;
+ }
+ free (files[i]);
+ files[i] = 0;
+ molecule_ctr++;
+ }
+ }
+
+ free (files);
+ files = 0;
+ mc->nmolecules = molecule_ctr;
+ }
+# endif /* LOAD_FILES */