3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
42 #include <sys/types.h>
46 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
55 #include "gmx_fatal.h"
60 #include "thread_mpi.h"
63 /* Windows file stuff, only necessary for visual studio */
68 #define MAX_PATHBUF 4096
70 /* we keep a linked list of all files opened through pipes (i.e.
71 compressed or .gzipped files. This way we can distinguish between them
72 without having to change the semantics of reading from/writing to files)
74 typedef struct t_pstack
{
76 struct t_pstack
*prev
;
79 static t_pstack
*pstack
=NULL
;
80 static bool bUnbuffered
=FALSE
;
83 /* this linked list is an intrinsically globally shared object, so we have
84 to protect it with mutexes */
85 static tMPI_Thread_mutex_t pstack_mutex
=TMPI_THREAD_MUTEX_INITIALIZER
;
93 void push_ps(FILE *fp
)
98 tMPI_Thread_mutex_lock(&pstack_mutex
);
106 tMPI_Thread_mutex_unlock(&pstack_mutex
);
111 /* redefine fclose */
112 #define fclose fah_fclose
121 static FILE *popen(const char *nm
,const char *mode
)
123 gmx_impl("Sorry no pipes...");
128 static int pclose(FILE *fp
)
130 gmx_impl("Sorry no pipes...");
138 void ffclose(FILE *fp
)
142 tMPI_Thread_mutex_lock(&pstack_mutex
);
150 else if (ps
->fp
== fp
) {
157 while ((ps
->prev
!= NULL
) && (ps
->prev
->fp
!= fp
))
159 if (ps
->prev
->fp
== fp
) {
160 if (ps
->prev
->fp
!= NULL
)
161 pclose(ps
->prev
->fp
);
163 ps
->prev
=ps
->prev
->prev
;
172 tMPI_Thread_mutex_unlock(&pstack_mutex
);
180 void frewind(FILE *fp
)
184 tMPI_Thread_mutex_lock(&pstack_mutex
);
190 fprintf(stderr
,"Cannot rewind compressed file!\n");
192 tMPI_Thread_mutex_unlock(&pstack_mutex
);
200 tMPI_Thread_mutex_unlock(&pstack_mutex
);
204 bool is_pipe(FILE *fp
)
208 tMPI_Thread_mutex_lock(&pstack_mutex
);
215 tMPI_Thread_mutex_unlock(&pstack_mutex
);
222 tMPI_Thread_mutex_unlock(&pstack_mutex
);
228 static FILE *uncompress(const char *fn
,const char *mode
)
233 sprintf(buf
,"uncompress -c < %s",fn
);
234 fprintf(stderr
,"Going to execute '%s'\n",buf
);
235 if ((fp
=popen(buf
,mode
)) == NULL
)
242 static FILE *gunzip(const char *fn
,const char *mode
)
247 sprintf(buf
,"gunzip -c < %s",fn
);
248 fprintf(stderr
,"Going to execute '%s'\n",buf
);
249 if ((fp
=popen(buf
,mode
)) == NULL
)
256 bool gmx_fexist(const char *fname
)
262 test
=fopen(fname
,"r");
272 bool gmx_fexist_master(const char *fname
, t_commrec
*cr
)
278 bExist
= gmx_fexist(fname
);
282 gmx_bcast(sizeof(bExist
),&bExist
,cr
);
287 bool gmx_eof(FILE *fp
)
295 if ((beof
=fread(data
,1,1,fp
))==1)
296 fseek(fp
,-1,SEEK_CUR
);
301 char *backup_fn(const char *file
)
303 /* Use a reasonably low value for countmax; we might
304 * generate 4-5 files in each round, and we dont
305 * want to hit directory limits of 1024 or 2048 files.
312 smalloc(buf
, MAX_PATHBUF
);
314 for(i
=strlen(file
)-1; ((i
> 0) && (file
[i
] != '/')); i
--)
316 /* Must check whether i > 0, i.e. whether there is a directory
317 * in the file name. In that case we overwrite the / sign with
318 * a '\0' to end the directory string .
321 directory
= strdup(file
);
323 fn
= strdup(file
+i
+1);
326 directory
= strdup(".");
330 sprintf(buf
,"%s/#%s.%d#",directory
,fn
,count
);
332 } while ((count
< COUNTMAX
) && gmx_fexist(buf
));
334 /* Arbitrarily bail out */
335 if (count
== COUNTMAX
)
336 gmx_fatal(FARGS
,"Won't make more than %d backups of %s for you",
345 bool make_backup(const char * name
)
350 return FALSE
; /* skip making backups */
353 if(gmx_fexist(name
)) {
354 backup
= backup_fn(name
);
355 if(rename(name
, backup
) == 0) {
356 fprintf(stderr
, "\nBack Off! I just backed up %s to %s\n",
359 fprintf(stderr
, "Sorry couldn't backup %s to %s\n", name
, backup
);
368 FILE *ffopen(const char *file
,const char *mode
)
371 char buf
[256],*bf
,*bufsize
=0,*ptr
;
382 if (gmx_fexist(buf
) || !bRead
) {
383 if ((ff
=fopen(buf
,mode
))==NULL
)
386 /* Check whether we should be using buffering (default) or not
389 if (bUnbuffered
|| ((bufsize
=getenv("LOG_BUFS")) != NULL
)) {
390 /* Check whether to use completely unbuffered */
394 bs
=strtol(bufsize
, NULL
, 10);
399 if (setvbuf(ff
,ptr
,_IOFBF
,bs
) != 0)
400 gmx_file("Buffering File");
406 sprintf(buf
,"%s.Z",file
);
407 if (gmx_fexist(buf
)) {
408 ff
=uncompress(buf
,mode
);
411 sprintf(buf
,"%s.gz",file
);
412 if (gmx_fexist(buf
)) {
424 bool search_subdirs(const char *parent
, char *libdir
)
429 /* Search a few common subdirectory names for the gromacs library dir */
430 sprintf(libdir
,"%s%cshare%ctop%cgurgle.dat",parent
,
431 DIR_SEPARATOR
,DIR_SEPARATOR
,DIR_SEPARATOR
);
432 found
=gmx_fexist(libdir
);
434 sprintf(libdir
,"%s%cshare%cgromacs%ctop%cgurgle.dat",parent
,
435 DIR_SEPARATOR
,DIR_SEPARATOR
,
436 DIR_SEPARATOR
,DIR_SEPARATOR
);
437 found
=gmx_fexist(libdir
);
440 sprintf(libdir
,"%s%cshare%cgromacs-%s%ctop%cgurgle.dat",parent
,
441 DIR_SEPARATOR
,DIR_SEPARATOR
,VERSION
,
442 DIR_SEPARATOR
,DIR_SEPARATOR
);
443 found
=gmx_fexist(libdir
);
446 sprintf(libdir
,"%s%cshare%cgromacs%cgromacs-%s%ctop%cgurgle.dat",parent
,
447 DIR_SEPARATOR
,DIR_SEPARATOR
,DIR_SEPARATOR
,
448 VERSION
,DIR_SEPARATOR
,DIR_SEPARATOR
);
449 found
=gmx_fexist(libdir
);
452 /* Remove the gurgle.dat part from libdir if we found something */
454 ptr
=strrchr(libdir
,DIR_SEPARATOR
); /* slash or backslash always present, no check necessary */
461 /* Check if the program name begins with "/" on unix/cygwin, or
462 * with "\" or "X:\" on windows. If not, the program name
463 * is relative to the current directory.
465 static bool filename_is_absolute(char *name
)
467 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
468 return ((name
[0] == DIR_SEPARATOR
) || ((strlen(name
)>3) && strncmp(name
+1,":\\",2)));
470 return (name
[0] == DIR_SEPARATOR
);
474 bool get_libdir(char *libdir
)
478 char full_path
[MAX_PATHBUF
];
479 char test_file
[MAX_PATHBUF
];
480 char system_path
[MAX_PATHBUF
];
481 char *dir
,*ptr
,*s
,*pdum
;
485 /* First - detect binary name */
486 strncpy(bin_name
,Program(),512);
488 /* On windows & cygwin we need to add the .exe extension
489 * too, or we wont be able to detect that the file exists
491 #if (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || defined __CYGWIN__ || defined __CYGWIN32__)
492 if(strlen(bin_name
)<3 || strncasecmp(bin_name
+strlen(bin_name
)-4,".exe",4))
493 strcat(bin_name
,".exe");
496 /* Only do the smart search part if we got a real name */
497 if (NULL
!=bin_name
&& strncmp(bin_name
,"GROMACS",512)) {
499 if (!strchr(bin_name
,DIR_SEPARATOR
)) {
500 /* No slash or backslash in name means it must be in the path - search it! */
503 /* Add the local dir since it is not in the path on windows */
504 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
505 pdum
=_getcwd(system_path
,sizeof(system_path
)-1);
507 pdum
=getcwd(system_path
,sizeof(system_path
)-1);
509 strcat(system_path
,PATH_SEPARATOR
);
511 strcat(system_path
,s
);
514 while(!found
&& (dir
=gmx_strsep(&s
, PATH_SEPARATOR
)) != NULL
)
516 sprintf(full_path
,"%s%c%s",dir
,DIR_SEPARATOR
,bin_name
);
517 found
=gmx_fexist(full_path
);
521 } else if (!filename_is_absolute(bin_name
)) {
522 /* name contains directory separators, but
523 * it does not start at the root, i.e.
524 * name is relative to the current dir
526 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
527 pdum
=_getcwd(buf
,sizeof(buf
)-1);
529 pdum
=getcwd(buf
,sizeof(buf
)-1);
531 strncpy(full_path
,buf
,MAX_PATHBUF
);
532 strcat(full_path
,"/");
533 strcat(full_path
,bin_name
);
535 strncpy(full_path
,bin_name
,MAX_PATHBUF
);
538 /* Now we should have a full path and name in full_path,
539 * but on unix it might be a link, or a link to a link to a link..
541 #if (!defined WIN32 && !defined _WIN32 && !defined WIN64 && !defined _WIN64)
542 while( (i
=readlink(full_path
,buf
,sizeof(buf
)-1)) > 0 ) {
544 /* If it doesn't start with "/" it is relative */
545 if (buf
[0]!=DIR_SEPARATOR
) {
546 strncpy(strrchr(full_path
,DIR_SEPARATOR
)+1,buf
,MAX_PATHBUF
);
548 strncpy(full_path
,buf
,MAX_PATHBUF
);
552 /* Remove the executable name - it always contains at least one slash */
553 *(strrchr(full_path
,DIR_SEPARATOR
)+1)='\0';
554 /* Now we have the full path to the gromacs executable.
555 * Use it to find the library dir.
558 while(!found
&& ( (ptr
=strrchr(full_path
,DIR_SEPARATOR
)) != NULL
) ) {
560 found
=search_subdirs(full_path
,libdir
);
563 /* End of smart searching. If we didn't find it in our parent tree,
564 * or if the program name wasn't set, at least try some standard
565 * locations before giving up, in case we are running from e.g.
566 * a users home directory. This only works on unix or cygwin...
568 #if ((!defined WIN32 && !defined _WIN32 && !defined WIN64 && !defined _WIN64) || defined __CYGWIN__ || defined __CYGWIN32__)
570 found
=search_subdirs("/usr/local",libdir
);
572 found
=search_subdirs("/usr",libdir
);
574 found
=search_subdirs("/opt",libdir
);
580 char *low_libfn(const char *file
, bool bFatal
)
585 char libpath
[MAX_PATHBUF
];
586 bool env_is_set
=FALSE
;
587 char *s
,tmppath
[MAX_PATHBUF
];
590 /* GMXLIB can be a path now */
591 lib
=getenv("GMXLIB");
594 strncpy(libpath
,lib
,MAX_PATHBUF
);
596 else if (!get_libdir(libpath
))
597 strncpy(libpath
,GMXLIBDIR
,MAX_PATHBUF
);
599 if (gmx_fexist(file
))
606 strncpy(tmppath
,libpath
,MAX_PATHBUF
);
608 while(!found
&& (dir
=gmx_strsep(&s
, PATH_SEPARATOR
)) != NULL
)
610 sprintf(buf
,"%s%c%s",dir
,DIR_SEPARATOR
,file
);
611 found
=gmx_fexist(buf
);
613 if (bFatal
&& !found
)
616 gmx_fatal(FARGS
,"Library file %s not found in current dir nor in your GMXLIB path.\n",file
);
618 gmx_fatal(FARGS
,"Library file %s not found in current dir nor in default directories.\n"
619 "(You can set the directories to search with the GMXLIB path variable)",file
);
630 FILE *low_libopen(const char *file
,bool bFatal
)
635 fn
=low_libfn(file
,bFatal
);
641 fprintf(stderr
,"Opening library file %s\n",fn
);
649 char *libfn(const char *file
)
651 return low_libfn(file
,TRUE
);
654 FILE *libopen(const char *file
)
656 return low_libopen(file
,TRUE
);
659 void gmx_tmpnam(char *buf
)
663 if ((len
= strlen(buf
)) < 7)
664 gmx_fatal(FARGS
,"Buf passed to gmx_tmpnam must be at least 7 bytes long");
665 for(i
=len
-6; (i
<len
); i
++) {
668 /* mktemp is dangerous and we should use mkstemp instead, but
669 * since windows doesnt support it we have to separate the cases.
670 * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
672 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
679 gmx_fatal(FARGS
,"Invalid template %s for mkstemp",buf
);
682 gmx_fatal(FARGS
,"mkstemp created existing file",buf
);
685 gmx_fatal(FARGS
,"Permission denied for opening %s",buf
);
692 /* name in Buf should now be OK */
696 gmx_truncatefile(char *path
, off_t length
)
699 /* Microsoft visual studio does not have "truncate" */
701 LARGE_INTEGER win_length
;
703 win_length
.QuadPart
= length
;
705 fh
= CreateFile(path
,GENERIC_READ
| GENERIC_WRITE
,0,NULL
,
706 OPEN_EXISTING
,0,NULL
);
707 SetFilePointerEx(fh
,win_length
,NULL
,FILE_BEGIN
);
713 return truncate(path
,length
);