4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_futil_c
= "$Id$";
40 typedef struct t_pstack
{
42 struct t_pstack
*prev
;
45 static t_pstack
*pstack
=NULL
;
46 static bool bUnbuffered
=FALSE
;
53 void push_ps(FILE *fp
)
66 void ffclose(FILE *fp
)
81 while ((ps
->prev
!= NULL
) && (ps
->prev
->fp
!= fp
))
83 if (ps
->prev
->fp
== fp
) {
86 ps
->prev
=ps
->prev
->prev
;
100 void frewind(FILE *fp
)
107 fprintf(stderr
,"Cannot rewind compressed file!\n");
115 bool is_pipe(FILE *fp
)
130 static FILE *popen(char *nm
,char *mode
)
132 fatal_error(0,"Sorry no pipes...");
137 static int pclose(FILE *fp
)
139 fatal_error(0,"Sorry no pipes...");
146 FILE *uncompress(char *fn
,char *mode
)
151 sprintf(buf
,"uncompress -c < %s",fn
);
152 fprintf(stderr
,"Going to execute '%s'\n",buf
);
153 if ((fp
=popen(buf
,mode
)) == NULL
)
154 fatal_error(0,"Could not open %s",fn
);
160 FILE *gunzip(char *fn
,char *mode
)
165 sprintf(buf
,"gunzip -c < %s",fn
);
166 fprintf(stderr
,"Going to execute '%s'\n",buf
);
167 if ((fp
=popen(buf
,mode
)) == NULL
)
168 fatal_error(0,"Could not open %s",fn
);
174 bool fexist(char *fname
)
180 test
=fopen(fname
,"r");
197 if ((beof
=fread(data
,1,1,fp
))==1)
198 fseek(fp
,-1,SEEK_CUR
);
203 char *backup_fn(char *file
)
207 static char buf
[256];
209 for(i
=strlen(file
)-1; ((i
> 0) && (file
[i
] != '/')); i
--);
213 sprintf(buf
,"%s/#%s#",ptr
,ptr
+i
+1);
217 sprintf(buf
,"#%s#",file
);
222 FILE *ffopen(char *file
,char *mode
)
225 char buf
[256],*bf
,*bufsize
,*ptr
;
230 fprintf(stderr
,"Going to open %s on CPU %d with mode %s\n",
231 file
,gmx_cpu_id(),mode
);
233 if ((mode
[0]=='w') && fexist(file
)) {
235 if (rename(file
,bf
) == 0) {
236 fprintf(stderr
,"\nBack Off! I just backed up %s to %s\n",file
,bf
);
239 fprintf(stderr
,"Sorry, I couldn't backup %s to %s\n",file
,bf
);
245 if (fexist(buf
) || !bRead
) {
246 if ((ff
=fopen(buf
,mode
))==NULL
)
247 fatal_error(0,"Could not open %s",buf
);
249 /* Check whether we should be using buffering (default) or not
252 if (bUnbuffered
|| ((bufsize
=getenv("LOG_BUFS")) != NULL
)) {
253 /* Check whether to use completely unbuffered */
262 if (setvbuf(ff
,ptr
,_IOFBF
,bs
) != 0)
263 fatal_error(0,"Buffering File");
269 sprintf(buf
,"%s.Z",file
);
271 ff
=uncompress(buf
,mode
);
274 sprintf(buf
,"%s.gz",file
);
279 fatal_error(0,"%s does not exist",file
);
283 fprintf(stderr
,"Opened %s on CPU %d with mode %s\n",
284 file
,gmx_cpu_id(),mode
);
289 char *low_libfn(char *file
,bool bFatal
)
291 static char *libdir
="GMXLIB";
293 static char buf
[1024];
298 if ((lib
=getenv(libdir
)) == NULL
) {
300 fatal_error(0,"%s environment variable not set and file %s not found",
306 sprintf(buf
,"%s/%s",lib
,file
);
308 if (bFatal
&& !fexist(ret
))
309 fatal_error(0,"Library file %s found in current dir nor in libdir %s",
317 FILE *low_libopen(char *file
,bool bFatal
)
322 fn
=low_libfn(file
,bFatal
);
328 fprintf(stderr
,"Opening library file %s\n",fn
);
335 char *libfn(char *file
)
337 return low_libfn(file
,TRUE
);
340 FILE *libopen(char *file
)
342 return low_libopen(file
,TRUE
);