- int i = 0;
- mc->nmolecules = 1;
- mc->molecules = (molecule *) calloc (sizeof (molecule), mc->nmolecules);
- parse_pdb_file (&mc->molecules[i], molecule_str);
- generate_molecule_formula (&mc->molecules[i]);
- insert_vertical_whitespace ((char *) mc->molecules[i].label);
+ /* The -molecule option can point to a .pdb file, or to
+ a directory of them.
+ */
+ struct stat st;
+ int nfiles = 0;
+ int list_size = 0;
+ char **files = 0;
+
+ 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);
+ exit (1);
+ }
+ }
+ 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);
+ }