1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
39 #include "gmx_header_config.h"
44 #include <sys/types.h>
54 #ifdef GMX_NATIVE_WINDOWS
63 #include "gmx_fatal.h"
69 #include "thread_mpi.h"
72 /* Windows file stuff, only necessary for visual studio */
77 /* we keep a linked list of all files opened through pipes (i.e.
78 compressed or .gzipped files. This way we can distinguish between them
79 without having to change the semantics of reading from/writing to files)
81 typedef struct t_pstack {
83 struct t_pstack *prev;
86 static t_pstack *pstack=NULL;
87 static gmx_bool bUnbuffered=FALSE;
90 /* this linked list is an intrinsically globally shared object, so we have
91 to protect it with mutexes */
92 static tMPI_Thread_mutex_t pstack_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
100 void push_ps(FILE *fp)
104 #ifdef GMX_THREAD_MPI
105 tMPI_Thread_mutex_lock(&pstack_mutex);
112 #ifdef GMX_THREAD_MPI
113 tMPI_Thread_mutex_unlock(&pstack_mutex);
118 /* don't use pipes!*/
119 #define popen fah_fopen
120 #define pclose fah_fclose
130 static FILE *popen(const char *nm,const char *mode)
132 gmx_impl("Sorry no pipes...");
137 static int pclose(FILE *fp)
139 gmx_impl("Sorry no pipes...");
146 int ffclose(FILE *fp)
153 #ifdef GMX_THREAD_MPI
154 tMPI_Thread_mutex_lock(&pstack_mutex);
162 else if (ps->fp == fp) {
169 while ((ps->prev != NULL) && (ps->prev->fp != fp))
171 if ((ps->prev != NULL) && ps->prev->fp == fp) {
172 if (ps->prev->fp != NULL)
173 ret = pclose(ps->prev->fp);
175 ps->prev=ps->prev->prev;
183 #ifdef GMX_THREAD_MPI
184 tMPI_Thread_mutex_unlock(&pstack_mutex);
195 void frewind(FILE *fp)
198 #ifdef GMX_THREAD_MPI
199 tMPI_Thread_mutex_lock(&pstack_mutex);
205 fprintf(stderr,"Cannot rewind compressed file!\n");
206 #ifdef GMX_THREAD_MPI
207 tMPI_Thread_mutex_unlock(&pstack_mutex);
214 #ifdef GMX_THREAD_MPI
215 tMPI_Thread_mutex_unlock(&pstack_mutex);
219 int gmx_fseek(FILE *stream, gmx_off_t offset, int whence)
222 return fseeko(stream, offset, whence);
224 #ifdef HAVE__FSEEKI64
225 return _fseeki64(stream, offset, whence);
227 return fseek(stream, offset, whence);
232 gmx_off_t gmx_ftell(FILE *stream)
235 return ftello(stream);
237 #ifdef HAVE__FSEEKI64
238 return _ftelli64(stream);
240 return ftell(stream);
246 gmx_bool is_pipe(FILE *fp)
249 #ifdef GMX_THREAD_MPI
250 tMPI_Thread_mutex_lock(&pstack_mutex);
256 #ifdef GMX_THREAD_MPI
257 tMPI_Thread_mutex_unlock(&pstack_mutex);
263 #ifdef GMX_THREAD_MPI
264 tMPI_Thread_mutex_unlock(&pstack_mutex);
270 static FILE *uncompress(const char *fn,const char *mode)
275 sprintf(buf,"uncompress -c < %s",fn);
276 fprintf(stderr,"Going to execute '%s'\n",buf);
277 if ((fp=popen(buf,mode)) == NULL)
284 static FILE *gunzip(const char *fn,const char *mode)
289 sprintf(buf,"gunzip -c < %s",fn);
290 fprintf(stderr,"Going to execute '%s'\n",buf);
291 if ((fp=popen(buf,mode)) == NULL)
298 gmx_bool gmx_fexist(const char *fname)
304 test=fopen(fname,"r");
306 /*Windows doesn't allow fopen of directory - so we need to check this seperately */
307 #ifdef GMX_NATIVE_WINDOWS
308 DWORD attr = GetFileAttributes(fname);
309 return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY);
319 static gmx_bool gmx_is_file(const char *fname)
325 test=fopen(fname,"r");
333 /*Windows doesn't allow fopen of directory - so we don't need to check this seperately */
334 #if (!((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__))
338 status = stat (fname, &st_buf);
339 if (status != 0 || !S_ISREG(st_buf.st_mode))
350 gmx_bool gmx_fexist_master(const char *fname, t_commrec *cr)
356 bExist = gmx_fexist(fname);
360 gmx_bcast(sizeof(bExist),&bExist,cr);
365 gmx_bool gmx_eof(FILE *fp)
373 if ((beof=fread(data,1,1,fp))==1)
374 gmx_fseek(fp,-1,SEEK_CUR);
379 static char *backup_fn(const char *file,int count_max)
381 /* Use a reasonably low value for countmax; we might
382 * generate 4-5 files in each round, and we dont
383 * want to hit directory limits of 1024 or 2048 files.
392 count_max = COUNTMAX;
395 smalloc(buf, GMX_PATH_MAX);
397 for(i=strlen(file)-1; ((i > 0) && (file[i] != DIR_SEPARATOR)); i--)
399 /* Must check whether i > 0, i.e. whether there is a directory
400 * in the file name. In that case we overwrite the / sign with
401 * a '\0' to end the directory string .
404 directory = gmx_strdup(file);
406 fn = gmx_strdup(file+i+1);
409 directory = gmx_strdup(".");
410 fn = gmx_strdup(file);
413 sprintf(buf,"%s/#%s.%d#",directory,fn,count);
415 } while ((count <= count_max) && gmx_fexist(buf));
417 /* Arbitrarily bail out */
418 if (count > count_max)
419 gmx_fatal(FARGS,"Won't make more than %d backups of %s for you.\n"
420 "The env.var. GMX_MAXBACKUP controls this maximum, -1 disables backups.",
429 gmx_bool make_backup(const char * name)
436 return FALSE; /* skip making backups */
439 if (gmx_fexist(name))
441 env = getenv("GMX_MAXBACKUP");
445 sscanf(env,"%d",&count_max);
448 /* Do not make backups and possibly overwrite old files */
454 /* Use the default maximum */
457 backup = backup_fn(name,count_max);
458 if(rename(name, backup) == 0) {
459 fprintf(stderr, "\nBack Off! I just backed up %s to %s\n",
462 fprintf(stderr, "Sorry couldn't backup %s to %s\n", name, backup);
471 FILE *ffopen(const char *file,const char *mode)
474 return fopen(file,mode);
477 char buf[256],*bf,*bufsize=0,*ptr;
491 bRead= (mode[0]=='r'&&mode[1]!='+');
493 if (!bRead || gmx_fexist(buf)) {
494 if ((ff=fopen(buf,mode))==NULL)
497 /* Check whether we should be using buffering (default) or not
500 if (bUnbuffered || ((bufsize=getenv("LOG_BUFS")) != NULL)) {
501 /* Check whether to use completely unbuffered */
505 bs=strtol(bufsize, NULL, 10);
510 if (setvbuf(ff,ptr,_IOFBF,bs) != 0)
511 gmx_file("Buffering File");
517 sprintf(buf,"%s.Z",file);
518 if (gmx_fexist(buf)) {
519 ff=uncompress(buf,mode);
522 sprintf(buf,"%s.gz",file);
523 if (gmx_fexist(buf)) {
534 /* Our own implementation of dirent-like functionality to scan directories. */
539 #elif (defined GMX_NATIVE_WINDOWS)
540 intptr_t windows_handle;
541 struct _finddata_t finddata;
550 gmx_directory_open(gmx_directory_t *p_gmxdir,const char *dirname)
552 struct gmx_directory * gmxdir;
560 if( (gmxdir->dirent_handle = opendir(dirname)) != NULL)
570 #elif (defined GMX_NATIVE_WINDOWS)
572 if(dirname!=NULL && strlen(dirname)>0)
578 len = strlen(dirname);
581 strncpy(tmpname,dirname,len+1);
583 /* Remove possible trailing directory separator */
584 if(tmpname[len]=='/' || tmpname[len]=='\\')
590 strcat(tmpname,"/*");
593 if( (gmxdir->windows_handle=_findfirst(tmpname,&gmxdir->finddata))>0L)
617 "Source compiled without POSIX dirent or windows support - cannot scan directories.\n"
618 "In the very unlikely event this is not a compile-time mistake you could consider\n"
619 "implementing support for your platform in futil.c, but contact the developers\n"
620 "to make sure it's really necessary!\n");
628 gmx_directory_nextfile(gmx_directory_t gmxdir,char *name,int maxlength_name)
634 struct dirent * direntp_large;
638 if(gmxdir!=NULL && gmxdir->dirent_handle!=NULL)
640 /* On some platforms no space is present for d_name in dirent.
641 * Since d_name is guaranteed to be the last entry, allocating
642 * extra space for dirent will allow more size for d_name.
643 * GMX_MAX_PATH should always be >= the max possible d_name.
645 smalloc(direntp_large, sizeof(*direntp_large) + GMX_PATH_MAX);
646 rc = readdir_r(gmxdir->dirent_handle,direntp_large,&p);
650 strncpy(name,direntp_large->d_name,maxlength_name);
657 sfree(direntp_large);
665 #elif (defined GMX_NATIVE_WINDOWS)
669 if(gmxdir->windows_handle<=0)
675 else if(gmxdir->first==1)
677 strncpy(name,gmxdir->finddata.name,maxlength_name);
683 if(_findnext(gmxdir->windows_handle,&gmxdir->finddata)==0)
685 strncpy(name,gmxdir->finddata.name,maxlength_name);
698 "Source compiled without POSIX dirent or windows support - cannot scan directories.\n");
706 gmx_directory_close(gmx_directory_t gmxdir)
710 rc = (gmxdir != NULL) ? closedir(gmxdir->dirent_handle) : EINVAL;
711 #elif (defined GMX_NATIVE_WINDOWS)
712 rc = (gmxdir != NULL) ? _findclose(gmxdir->windows_handle) : EINVAL;
715 "Source compiled without POSIX dirent or windows support - cannot scan directories.\n");
726 gmx_bool search_subdirs(const char *parent, char *libdir)
731 /* Search a few common subdirectory names for the gromacs library dir */
732 sprintf(libdir,"%s%cshare%ctop%cgurgle.dat",parent,
733 DIR_SEPARATOR,DIR_SEPARATOR,DIR_SEPARATOR);
734 found=gmx_fexist(libdir);
736 sprintf(libdir,"%s%cshare%cgromacs%ctop%cgurgle.dat",parent,
737 DIR_SEPARATOR,DIR_SEPARATOR,
738 DIR_SEPARATOR,DIR_SEPARATOR);
739 found=gmx_fexist(libdir);
742 sprintf(libdir,"%s%cshare%cgromacs-%s%ctop%cgurgle.dat",parent,
743 DIR_SEPARATOR,DIR_SEPARATOR,VERSION,
744 DIR_SEPARATOR,DIR_SEPARATOR);
745 found=gmx_fexist(libdir);
748 sprintf(libdir,"%s%cshare%cgromacs%cgromacs-%s%ctop%cgurgle.dat",parent,
749 DIR_SEPARATOR,DIR_SEPARATOR,DIR_SEPARATOR,
750 VERSION,DIR_SEPARATOR,DIR_SEPARATOR);
751 found=gmx_fexist(libdir);
754 /* Remove the gurgle.dat part from libdir if we found something */
756 ptr=strrchr(libdir,DIR_SEPARATOR); /* slash or backslash always present, no check necessary */
763 /* Check if the program name begins with "/" on unix/cygwin, or
764 * with "\" or "X:\" on windows. If not, the program name
765 * is relative to the current directory.
767 static gmx_bool filename_is_absolute(char *name)
769 #ifdef GMX_NATIVE_WINDOWS
770 return ((name[0] == DIR_SEPARATOR) || ((strlen(name)>3) && strncmp(name+1,":\\",2)) == 0);
772 return (name[0] == DIR_SEPARATOR);
776 gmx_bool get_libdir(char *libdir)
778 #define GMX_BINNAME_MAX 512
779 char bin_name[GMX_BINNAME_MAX];
780 char buf[GMX_BINNAME_MAX];
781 char full_path[GMX_PATH_MAX+GMX_BINNAME_MAX];
782 char system_path[GMX_PATH_MAX];
783 char *dir,*ptr,*s,*pdum;
784 gmx_bool found=FALSE;
787 if (Program() != NULL)
790 /* First - detect binary name */
791 if (strlen(Program()) >= GMX_BINNAME_MAX)
793 gmx_fatal(FARGS,"The name of the binary is longer than the allowed buffer size (%d):\n'%s'",GMX_BINNAME_MAX,Program());
795 strncpy(bin_name,Program(),GMX_BINNAME_MAX-1);
797 /* On windows & cygwin we need to add the .exe extension
798 * too, or we wont be able to detect that the file exists
800 #if (defined GMX_NATIVE_WINDOWS || defined GMX_CYGWIN)
801 if(strlen(bin_name)<3 || gmx_strncasecmp(bin_name+strlen(bin_name)-4,".exe",4))
802 strcat(bin_name,".exe");
805 /* Only do the smart search part if we got a real name */
806 if (NULL!=bin_name && strncmp(bin_name,"GROMACS",GMX_BINNAME_MAX)) {
808 if (!strchr(bin_name,DIR_SEPARATOR)) {
809 /* No slash or backslash in name means it must be in the path - search it! */
810 /* Add the local dir since it is not in the path on windows */
811 #ifdef GMX_NATIVE_WINDOWS
812 pdum=_getcwd(system_path,sizeof(system_path)-1);
814 pdum=getcwd(system_path,sizeof(system_path)-1);
816 sprintf(full_path,"%s%c%s",system_path,DIR_SEPARATOR,bin_name);
817 found = gmx_is_file(full_path);
818 if (!found && (s=getenv("PATH")) != NULL)
822 dupped=gmx_strdup(s);
824 while(!found && (dir=gmx_strsep(&s, PATH_SEPARATOR)) != NULL)
826 sprintf(full_path,"%s%c%s",dir,DIR_SEPARATOR,bin_name);
827 found = gmx_is_file(full_path);
835 } else if (!filename_is_absolute(bin_name)) {
836 /* name contains directory separators, but
837 * it does not start at the root, i.e.
838 * name is relative to the current dir
840 #ifdef GMX_NATIVE_WINDOWS
841 pdum=_getcwd(buf,sizeof(buf)-1);
843 pdum=getcwd(buf,sizeof(buf)-1);
845 sprintf(full_path,"%s%c%s",buf,DIR_SEPARATOR,bin_name);
847 strncpy(full_path,bin_name,GMX_PATH_MAX);
850 /* Now we should have a full path and name in full_path,
851 * but on unix it might be a link, or a link to a link to a link..
853 #ifndef GMX_NATIVE_WINDOWS
854 while( (i=readlink(full_path,buf,sizeof(buf)-1)) > 0 ) {
856 /* If it doesn't start with "/" it is relative */
857 if (buf[0]!=DIR_SEPARATOR) {
858 strncpy(strrchr(full_path,DIR_SEPARATOR)+1,buf,GMX_PATH_MAX);
860 strncpy(full_path,buf,GMX_PATH_MAX);
864 /* Remove the executable name - it always contains at least one slash */
865 *(strrchr(full_path,DIR_SEPARATOR)+1)='\0';
866 /* Now we have the full path to the gromacs executable.
867 * Use it to find the library dir.
870 while(!found && ( (ptr=strrchr(full_path,DIR_SEPARATOR)) != NULL ) ) {
872 found=search_subdirs(full_path,libdir);
876 /* End of smart searching. If we didn't find it in our parent tree,
877 * or if the program name wasn't set, at least try some standard
878 * locations before giving up, in case we are running from e.g.
879 * a users home directory. This only works on unix or cygwin...
881 #ifndef GMX_NATIVE_WINDOWS
883 found=search_subdirs("/usr/local",libdir);
885 found=search_subdirs("/usr",libdir);
887 found=search_subdirs("/opt",libdir);
893 char *low_gmxlibfn(const char *file, gmx_bool bAddCWD, gmx_bool bFatal)
898 char libpath[GMX_PATH_MAX];
899 gmx_bool env_is_set=FALSE;
900 char *s,tmppath[GMX_PATH_MAX];
902 /* GMXLIB can be a path now */
903 lib=getenv("GMXLIB");
907 strncpy(libpath,lib,GMX_PATH_MAX);
909 else if (!get_libdir(libpath))
911 strncpy(libpath,GMXLIBDIR,GMX_PATH_MAX);
915 if (bAddCWD && gmx_fexist(file))
917 ret = gmx_strdup(file);
921 strncpy(tmppath,libpath,GMX_PATH_MAX);
923 while(ret == NULL && (dir=gmx_strsep(&s, PATH_SEPARATOR)) != NULL )
925 sprintf(buf,"%s%c%s",dir,DIR_SEPARATOR,file);
928 ret = gmx_strdup(buf);
931 if (ret == NULL && bFatal)
936 "Library file %s not found %sin your GMXLIB path.",
937 file, bAddCWD ? "in current dir nor " : "");
942 "Library file %s not found %sin default directories.\n"
943 "(You can set the directories to search with the GMXLIB path variable)",
944 file, bAddCWD ? "in current dir nor " : "");
956 FILE *low_libopen(const char *file,gmx_bool bFatal)
961 fn=low_gmxlibfn(file,TRUE,bFatal);
967 fprintf(debug,"Opening library file %s\n",fn);
975 char *gmxlibfn(const char *file)
977 return low_gmxlibfn(file,TRUE,TRUE);
980 FILE *libopen(const char *file)
982 return low_libopen(file,TRUE);
985 void gmx_tmpnam(char *buf)
989 if ((len = strlen(buf)) < 7)
990 gmx_fatal(FARGS,"Buf passed to gmx_tmpnam must be at least 7 bytes long");
991 for(i=len-6; (i<len); i++) {
994 /* mktemp is dangerous and we should use mkstemp instead, but
995 * since windows doesnt support it we have to separate the cases.
996 * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
998 #ifdef GMX_NATIVE_WINDOWS
1005 gmx_fatal(FARGS,"Invalid template %s for mkstemp",buf);
1008 gmx_fatal(FARGS,"mkstemp created existing file",buf);
1011 gmx_fatal(FARGS,"Permission denied for opening %s",buf);
1018 /* name in Buf should now be OK */
1021 int gmx_truncatefile(char *path, gmx_off_t length)
1024 /* Microsoft visual studio does not have "truncate" */
1026 LARGE_INTEGER win_length;
1028 win_length.QuadPart = length;
1030 fh = CreateFile(path,GENERIC_READ | GENERIC_WRITE,0,NULL,
1031 OPEN_EXISTING,0,NULL);
1032 SetFilePointerEx(fh,win_length,NULL,FILE_BEGIN);
1038 return truncate(path,length);
1043 int gmx_file_rename(const char *oldname, const char *newname)
1045 #ifndef GMX_NATIVE_WINDOWS
1046 /* under unix, rename() is atomic (at least, it should be). */
1047 return rename(oldname, newname);
1049 if (MoveFileEx(oldname, newname,
1050 MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH))
1057 int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty)
1059 /* the full copy buffer size: */
1060 #define FILECOPY_BUFSIZE (1<<16)
1065 snew(buf, FILECOPY_BUFSIZE);
1067 in=fopen(oldname, "rb");
1071 /* If we don't copy when empty, we postpone opening the file
1072 until we're actually ready to write. */
1075 out=fopen(newname, "wb");
1084 nread=fread(buf, sizeof(char), FILECOPY_BUFSIZE, in);
1090 /* so this is where we open when copy_if_empty is false:
1091 here we know we read something. */
1092 out=fopen(newname, "wb");
1096 ret=fwrite(buf, sizeof(char), nread, out);
1116 #undef FILECOPY_BUFSIZE
1120 int gmx_fsync(FILE *fp)
1125 /* the fahcore defines its own os-independent fsync */
1127 #else /* GMX_FAHCORE */
1131 /* get the file number */
1132 #if defined(HAVE_FILENO)
1134 #elif defined(HAVE__FILENO)
1138 /* do the actual fsync */
1141 #if (defined(HAVE_FSYNC))
1143 #elif (defined(HAVE__COMMIT))
1148 #endif /* GMX_FAHCORE */
1150 /* We check for these error codes this way because POSIX requires them
1151 to be defined, and using anything other than macros is unlikely: */
1153 /* we don't want to report an error just because fsync() caught a signal.
1154 For our purposes, we can just ignore this. */
1155 if (rc && errno==EINTR)
1159 /* we don't want to report an error just because we tried to fsync()
1160 stdout, a socket or a pipe. */
1161 if (rc && errno==EINVAL)