Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / fileio / filenm.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team,
6  * Copyright (c) 2013, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "filenm.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "futil.h"
48 #include "gmx_fatal.h"
49 #include "macros.h"
50 #include "smalloc.h"
51 #include "string2.h"
52 #include "types/simple.h"
53 #include "types/commrec.h"
54
55 /* XDR should be available on all platforms now,
56  * but we keep the possibility of turning it off...
57  */
58 #define USE_XDR
59
60 /* Use bitflag ... */
61 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
62 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
63 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
64 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
65 #define DO_SET(fn) (fn.flag = (fn.flag |  ffSET))
66
67 enum
68 {
69     eftASC, eftBIN, eftXDR, eftGEN, eftNR
70 };
71
72 /* To support multiple file types with one general (eg TRX) we have
73  * these arrays.
74  */
75 static const int trxs[] =
76 {
77 #ifdef USE_XDR
78     efXTC, efTRR, efCPT,
79 #endif
80     efTRJ, efGRO, efG96, efPDB, efG87
81 };
82 #define NTRXS asize(trxs)
83
84 static const int tros[] =
85 {
86 #ifdef USE_XDR
87     efXTC, efTRR,
88 #endif
89     efTRJ, efGRO, efG96, efPDB, efG87
90 };
91 #define NTROS asize(tros)
92
93 static const int trns[] =
94 {
95 #ifdef USE_XDR
96     efTRR, efCPT,
97 #endif
98     efTRJ
99 };
100 #define NTRNS asize(trns)
101
102 static const int stos[] =
103 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
104 #define NSTOS asize(stos)
105
106 static const int stxs[] =
107 {
108     efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
109 #ifdef USE_XDR
110     efTPR,
111 #endif
112     efTPB, efTPA
113 };
114 #define NSTXS asize(stxs)
115
116 static const int tpxs[] =
117 {
118 #ifdef USE_XDR
119     efTPR,
120 #endif
121     efTPB, efTPA
122 };
123 #define NTPXS asize(tpxs)
124
125 static const int tpss[] =
126 {
127 #ifdef USE_XDR
128     efTPR,
129 #endif
130     efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
131 };
132 #define NTPSS asize(tpss)
133
134 typedef struct
135 {
136     int         ftype;
137     const char *ext;
138     const char *defnm;
139     const char *defopt;
140     const char *descr;
141     int         ntps;
142     const int  *tps;
143 } t_deffile;
144
145 /* this array should correspond to the enum in filenm.h */
146 static const t_deffile
147     deffile[efNR] =
148 {
149     { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
150     { eftGEN, ".???", "traj", "-f",
151       "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
152     { eftGEN, ".???", "trajout", "-f",
153       "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
154     { eftGEN, ".???", "traj", NULL,
155       "Full precision trajectory: trr trj cpt", NTRNS, trns },
156     { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
157     { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
158     { eftXDR, ".xtc", "traj", NULL,
159       "Compressed trajectory (portable xdr format)" },
160     { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
161     { eftXDR, ".edr", "ener",   NULL, "Energy file"},
162     { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
163       NSTXS, stxs },
164     { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
165       NSTOS, stos },
166     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
167     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
168     { eftASC, ".pdb", "eiwit",  "-f", "Protein data bank file"},
169     { eftASC, ".brk", "eiwit",  "-f", "Brookhaven data bank file"},
170     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
171     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
172     { eftASC, ".pqr", "state",  "-o", "Coordinate file for MEAD"},
173     { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
174     { eftXDR, ".cpt", "state",  "-cp", "Checkpoint file"},
175     { eftASC, ".log", "run",    "-l", "Log file"},
176     { eftASC, ".xvg", "graph",  "-o", "xvgr/xmgr file"},
177     { eftASC, ".out", "hello",  "-o", "Generic output file"},
178     { eftASC, ".ndx", "index",  "-n", "Index file", },
179     { eftASC, ".top", "topol",  "-p", "Topology file"},
180     { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
181     { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
182       NTPXS, tpxs },
183     { eftGEN, ".???", "topol", "-s",
184       "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
185     { eftXDR, ".tpr", "topol",  "-s", "Portable xdr run input file"},
186     { eftASC, ".tpa", "topol",  "-s", "Ascii run input file"},
187     { eftBIN, ".tpb", "topol",  "-s", "Binary run input file"},
188     { eftASC, ".tex", "doc",    "-o", "LaTeX file"},
189     { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
190     { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
191     { eftASC, ".hdb", "polar",  NULL, "Hydrogen data base"},
192     { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
193     { eftASC, ".dlg", "user",   NULL, "Dialog Box data for ngmx"},
194     { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
195     { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
196     { eftASC, ".mat", "ss",     NULL, "Matrix Data file"},
197     { eftASC, ".m2p", "ps",     NULL, "Input file for mat2ps"},
198     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
199     { eftASC, ".edi", "sam",    NULL, "ED sampling input"},
200     { eftASC, ".cub", "pot",  NULL, "Gaussian cube file" },
201     { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
202     { eftASC, "", "rundir", NULL, "Run directory" }
203 };
204
205 #define NZEXT 2
206 static const char *z_ext[NZEXT] =
207 { ".gz", ".Z" };
208
209 const char *ftp2ext(int ftp)
210 {
211     if ((0 <= ftp) && (ftp < efNR))
212     {
213         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
214     }
215     else
216     {
217         return "unknown";
218     }
219 }
220
221 const char *ftp2ext_generic(int ftp)
222 {
223     if ((0 <= ftp) && (ftp < efNR))
224     {
225         switch (ftp)
226         {
227             case efTRX:
228                 return "trx";
229             case efTRN:
230                 return "trn";
231             case efSTO:
232                 return "sto";
233             case efSTX:
234                 return "stx";
235             case efTPX:
236                 return "tpx";
237             case efTPS:
238                 return "tps";
239             default:
240                 return ftp2ext(ftp);
241         }
242     }
243     else
244     {
245         return "unknown";
246     }
247 }
248
249 const char *ftp2ext_with_dot(int ftp)
250 {
251     if ((0 <= ftp) && (ftp < efNR))
252     {
253         return deffile[ftp].ext;
254     }
255     else
256     {
257         return "unknown";
258     }
259 }
260
261 int ftp2generic_count(int ftp)
262 {
263     if ((0 <= ftp) && (ftp < efNR))
264     {
265         return deffile[ftp].ntps;
266     }
267     else
268     {
269         return 0;
270     }
271 }
272
273 const int *ftp2generic_list(int ftp)
274 {
275     if ((0 <= ftp) && (ftp < efNR))
276     {
277         return deffile[ftp].tps;
278     }
279     else
280     {
281         return 0;
282     }
283 }
284
285 const char *ftp2desc(int ftp)
286 {
287     if ((0 <= ftp) && (ftp < efNR))
288     {
289         return deffile[ftp].descr;
290     }
291     else
292     {
293         return "unknown filetype";
294     }
295 }
296
297 const char *ftp2ftype(int ftp)
298 {
299     if ((ftp >= 0) && (ftp < efNR))
300     {
301         switch (deffile[ftp].ftype)
302         {
303             case eftASC:
304                 return "ASCII";
305             case eftBIN:
306                 return "Binary";
307             case eftXDR:
308                 return "XDR portable";
309             case eftGEN:
310                 return "";
311             default:
312                 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
313                 break;
314         }
315     }
316     return "unknown";
317 }
318
319 const char *ftp2defnm(int ftp)
320 {
321     if ((0 <= ftp) && (ftp < efNR))
322     {
323         return deffile[ftp].defnm;
324     }
325     else
326     {
327         return NULL;
328     }
329 }
330
331 static void check_opts(int nf, t_filenm fnm[])
332 {
333     int              i;
334     const t_deffile *df;
335
336     for (i = 0; (i < nf); i++)
337     {
338         df = &(deffile[fnm[i].ftp]);
339         if (fnm[i].opt == NULL)
340         {
341             if (df->defopt == NULL)
342             {
343                 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
344                           deffile[fnm[i].ftp].ext, fnm[i].ftp);
345             }
346             else
347             {
348                 fnm[i].opt = df->defopt;
349             }
350         }
351     }
352 }
353
354 int fn2ftp(const char *fn)
355 {
356     int         i, len;
357     const char *feptr;
358     const char *eptr;
359
360     if (!fn)
361     {
362         return efNR;
363     }
364
365     len = strlen(fn);
366     if ((len >= 4) && (fn[len - 4] == '.'))
367     {
368         feptr = &(fn[len - 4]);
369     }
370     else
371     {
372         return efNR;
373     }
374
375     for (i = 0; (i < efNR); i++)
376     {
377         if ((eptr = deffile[i].ext) != NULL)
378         {
379             if (gmx_strcasecmp(feptr, eptr) == 0)
380             {
381                 break;
382             }
383         }
384     }
385
386     return i;
387 }
388
389 static void set_extension(char *buf, int ftp)
390 {
391     int              len, extlen;
392     const t_deffile *df;
393
394     /* check if extension is already at end of filename */
395     df     = &(deffile[ftp]);
396     len    = strlen(buf);
397     extlen = strlen(df->ext);
398     if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
399     {
400         strcat(buf, df->ext);
401     }
402 }
403
404 static void add_filenm(t_filenm *fnm, const char *filenm)
405 {
406     srenew(fnm->fns, fnm->nfiles+1);
407     fnm->fns[fnm->nfiles] = strdup(filenm);
408     fnm->nfiles++;
409 }
410
411 static void set_grpfnm(t_filenm *fnm, const char *name, const char *deffnm)
412 {
413     char       buf[256], buf2[256];
414     int        i, type;
415     gmx_bool   bValidExt;
416     int        nopts;
417     const int *ftps;
418
419     nopts = deffile[fnm->ftp].ntps;
420     ftps  = deffile[fnm->ftp].tps;
421     if ((nopts == 0) || (ftps == NULL))
422     {
423         gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
424     }
425
426     bValidExt = FALSE;
427     if (name && deffnm == NULL)
428     {
429         strcpy(buf, name);
430         /* First check whether we have a valid filename already */
431         type = fn2ftp(name);
432         if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
433         {
434             /*if file exist don't add an extension for trajectory reading*/
435             bValidExt = gmx_fexist(name);
436         }
437         for (i = 0; (i < nopts) && !bValidExt; i++)
438         {
439             if (type == ftps[i])
440             {
441                 bValidExt = TRUE;
442             }
443         }
444     }
445     else if (deffnm != NULL)
446     {
447         strcpy(buf, deffnm);
448     }
449     else
450     {
451         /* No name given, set the default name */
452         strcpy(buf, ftp2defnm(fnm->ftp));
453     }
454
455     if (!bValidExt && (fnm->flag & ffREAD))
456     {
457         /* for input-files only: search for filenames in the directory */
458         for (i = 0; (i < nopts) && !bValidExt; i++)
459         {
460             type = ftps[i];
461             strcpy(buf2, buf);
462             set_extension(buf2, type);
463             if (gmx_fexist(buf2))
464             {
465                 bValidExt = TRUE;
466                 strcpy(buf, buf2);
467             }
468         }
469     }
470
471     if (!bValidExt)
472     {
473         /* Use the first extension type */
474         set_extension(buf, ftps[0]);
475     }
476
477     add_filenm(fnm, buf);
478 }
479
480 static void set_filenm(t_filenm *fnm, const char *name, const char *deffnm,
481                        gmx_bool bReadNode)
482 {
483     /* Set the default filename, extension and option for those fields that
484      * are not already set. An extension is added if not present, if fn = NULL
485      * or empty, the default filename is given.
486      */
487     char buf[256];
488     int  i, len, extlen;
489
490     if ((fnm->flag & ffREAD) && !bReadNode)
491     {
492         return;
493     }
494
495     if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
496     {
497         gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
498     }
499
500     if (name)
501     {
502         strcpy(buf, name);
503     }
504     if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
505     {
506         /* check if filename ends in .gz or .Z, if so remove that: */
507         len = strlen(name);
508         for (i = 0; i < NZEXT; i++)
509         {
510             extlen = strlen(z_ext[i]);
511             if (len > extlen)
512             {
513                 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
514                 {
515                     buf[len-extlen] = '\0';
516                     break;
517                 }
518             }
519         }
520     }
521
522     if (deffile[fnm->ftp].ntps)
523     {
524         set_grpfnm(fnm, name ? buf : NULL, deffnm);
525     }
526     else
527     {
528         if (name == NULL || deffnm != NULL)
529         {
530             if (deffnm != NULL)
531             {
532                 strcpy(buf, deffnm);
533             }
534             else
535             {
536                 strcpy(buf, ftp2defnm(fnm->ftp));
537             }
538         }
539         set_extension(buf, fnm->ftp);
540
541         add_filenm(fnm, buf);
542     }
543 }
544
545 static void set_filenms(int nf, t_filenm fnm[], const char *deffnm, gmx_bool bReadNode)
546 {
547     int i;
548
549     for (i = 0; (i < nf); i++)
550     {
551         if (!IS_SET(fnm[i]))
552         {
553             set_filenm(&(fnm[i]), fnm[i].fn, deffnm, bReadNode);
554         }
555     }
556 }
557
558 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
559                      const char *deffnm, gmx_bool bKeep, gmx_bool bReadNode)
560 {
561     int       i, j;
562     gmx_bool *bRemove;
563
564     check_opts(nf, fnm);
565
566     for (i = 0; (i < nf); i++)
567     {
568         UN_SET(fnm[i]);
569     }
570
571     if (*argc > 1)
572     {
573         snew(bRemove, (*argc)+1);
574         i = 1;
575         do
576         {
577             for (j = 0; (j < nf); j++)
578             {
579                 if (strcmp(argv[i], fnm[j].opt) == 0)
580                 {
581                     DO_SET(fnm[j]);
582                     bRemove[i] = TRUE;
583                     i++;
584                     /* check if we are out of arguments for this option */
585                     if ((i >= *argc) || (argv[i][0] == '-'))
586                     {
587                         set_filenm(&fnm[j], fnm[j].fn, deffnm, bReadNode);
588                     }
589                     /* sweep up all file arguments for this option */
590                     while ((i < *argc) && (argv[i][0] != '-'))
591                     {
592                         set_filenm(&fnm[j], argv[i], NULL, bReadNode);
593                         bRemove[i] = TRUE;
594                         i++;
595                         /* only repeat for 'multiple' file options: */
596                         if (!IS_MULT(fnm[j]))
597                         {
598                             break;
599                         }
600                     }
601
602                     break; /* jump out of 'j' loop */
603                 }
604             }
605             /* No file found corresponding to option argv[i] */
606             if (j == nf)
607             {
608                 i++;
609             }
610         }
611         while (i < *argc);
612
613         if (!bKeep)
614         {
615             /* Remove used entries */
616             for (i = j = 0; (i <= *argc); i++)
617             {
618                 if (!bRemove[i])
619                 {
620                     argv[j++] = argv[i];
621                 }
622             }
623             (*argc) = j - 1;
624         }
625         sfree(bRemove);
626     }
627
628     set_filenms(nf, fnm, deffnm, bReadNode);
629
630 }
631
632 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
633 {
634     int i;
635
636     for (i = 0; (i < nfile); i++)
637     {
638         if (strcmp(opt, fnm[i].opt) == 0)
639         {
640             return fnm[i].fns[0];
641         }
642     }
643
644     fprintf(stderr, "No option %s\n", opt);
645
646     return NULL;
647 }
648
649 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
650                           t_commrec *cr)
651 {
652     return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
653 }
654
655 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
656 {
657     int i;
658
659     for (i = 0; (i < nfile); i++)
660     {
661         if (strcmp(opt, fnm[i].opt) == 0)
662         {
663             *fns = fnm[i].fns;
664             return fnm[i].nfiles;
665         }
666     }
667
668     fprintf(stderr, "No option %s\n", opt);
669     return 0;
670 }
671
672 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
673 {
674     int i;
675
676     for (i = 0; (i < nfile); i++)
677     {
678         if (ftp == fnm[i].ftp)
679         {
680             return fnm[i].fns[0];
681         }
682     }
683
684     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
685     return NULL;
686 }
687
688 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
689 {
690     int i;
691
692     for (i = 0; (i < nfile); i++)
693     {
694         if (ftp == fnm[i].ftp)
695         {
696             *fns = fnm[i].fns;
697             return fnm[i].nfiles;
698         }
699     }
700
701     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
702     return 0;
703 }
704
705 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
706 {
707     int i;
708
709     for (i = 0; (i < nfile); i++)
710     {
711         if (ftp == fnm[i].ftp)
712         {
713             return (gmx_bool) IS_SET(fnm[i]);
714         }
715     }
716
717     fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
718
719     return FALSE;
720 }
721
722 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
723 {
724     int i;
725
726     for (i = 0; (i < nfile); i++)
727     {
728         if (strcmp(opt, fnm[i].opt) == 0)
729         {
730             return (gmx_bool) IS_SET(fnm[i]);
731         }
732     }
733
734     fprintf(stderr, "No option %s\n", opt);
735
736     return FALSE;
737 }
738
739 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
740 {
741     int i;
742
743     for (i = 0; (i < nfile); i++)
744     {
745         if (strcmp(opt, fnm[i].opt) == 0)
746         {
747             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
748             {
749                 return NULL;
750             }
751             else
752             {
753                 return fnm[i].fns[0];
754             }
755         }
756     }
757     fprintf(stderr, "No option %s\n", opt);
758     return NULL;
759 }
760
761 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
762 {
763     int i;
764
765     for (i = 0; (i < nfile); i++)
766     {
767         if (ftp == fnm[i].ftp)
768         {
769             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
770             {
771                 return NULL;
772             }
773             else
774             {
775                 return fnm[i].fns[0];
776             }
777         }
778     }
779     fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
780     return NULL;
781 }
782
783 gmx_bool is_optional(const t_filenm *fnm)
784 {
785     return ((fnm->flag & ffOPT) == ffOPT);
786 }
787
788 gmx_bool is_output(const t_filenm *fnm)
789 {
790     return ((fnm->flag & ffWRITE) == ffWRITE);
791 }
792
793 gmx_bool is_set(const t_filenm *fnm)
794 {
795     return ((fnm->flag & ffSET) == ffSET);
796 }
797
798 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
799 {
800     int   i, j, pos;
801     char  buf[STRLEN], newname[STRLEN];
802     char *extpos;
803
804     for (i = 0; i < nfile; i++)
805     {
806         if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
807         {
808             /* We never use multiple _outputs_, but we might as well check
809                for it, just in case... */
810             for (j = 0; j < fnm[i].nfiles; j++)
811             {
812                 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
813                 extpos  = strrchr(buf, '.');
814                 *extpos = '\0';
815                 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
816                 free(fnm[i].fns[j]);
817                 fnm[i].fns[j] = strdup(newname);
818             }
819         }
820     }
821     return 0;
822 }
823
824 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
825 {
826     int       i, j;
827     t_filenm *ret;
828
829     snew(ret, nf);
830     for (i = 0; i < nf; i++)
831     {
832         ret[i] = tfn[i]; /* just directly copy all non-string fields */
833         if (tfn[i].opt)
834         {
835             ret[i].opt = strdup(tfn[i].opt);
836         }
837         else
838         {
839             ret[i].opt = NULL;
840         }
841
842         if (tfn[i].fn)
843         {
844             ret[i].fn = strdup(tfn[i].fn);
845         }
846         else
847         {
848             ret[i].fn = NULL;
849         }
850
851         if (tfn[i].nfiles > 0)
852         {
853             snew(ret[i].fns, tfn[i].nfiles);
854             for (j = 0; j < tfn[i].nfiles; j++)
855             {
856                 ret[i].fns[j] = strdup(tfn[i].fns[j]);
857             }
858         }
859     }
860     return ret;
861 }
862
863 void done_filenms(int nf, t_filenm fnm[])
864 {
865     int i, j;
866
867     for (i = 0; i < nf; ++i)
868     {
869         for (j = 0; j < fnm[i].nfiles; ++j)
870         {
871             sfree(fnm[i].fns[j]);
872         }
873         sfree(fnm[i].fns);
874         fnm[i].fns = NULL;
875     }
876 }