00001
00007
00008
00009
00010
00011 #if !defined(WIN32) || defined(__CYGWIN__)
00012 #include <unistd.h>
00013 #endif
00014 #include <errno.h>
00015 #include <string.h>
00016 #include <sys/stat.h>
00017 #include <ctype.h>
00018
00019 #include "common.h"
00020 #include "InfoStream.h"
00021
00022 #include "charm++.h"
00023
00024 #ifdef WIN32
00025 #include <io.h>
00026 #define NOCOMPRESSED
00027 #endif
00028
00029 #ifdef USESTDIOSTUBS
00030
00031
00032
00033
00034 int write(int fd, const void* buf, int count)
00035 {
00036 return write(fd,(void*)buf,(size_t)count);
00037 }
00038
00039 int stat(const char* fn, struct stat* buf)
00040 {
00041 return stat((char*)fn, buf);
00042 }
00043
00044 #endif
00045
00046
00047
00048 char *NAMD_stringdup(const char *s) {
00049 char *rs;
00050
00051 if(!s)
00052 return NULL;
00053
00054 rs = new char[strlen(s) + 1];
00055 strcpy(rs,s);
00056
00057 return rs;
00058 }
00059
00060
00061
00062 void NAMD_quit(const char *err_msg)
00063
00064 {
00065 char *new_err_msg = new char[strlen(err_msg) + 20];
00066 sprintf(new_err_msg,"EXITING: %s\n",err_msg);
00067 CkPrintf(new_err_msg);
00068 fflush(stdout);
00069 CmiAbort(new_err_msg);
00070 delete [] new_err_msg;
00071 }
00072
00073
00074
00075 void NAMD_die(const char *err_msg)
00076
00077 {
00078 char *new_err_msg = new char[strlen(err_msg) + 20];
00079 sprintf(new_err_msg,"FATAL ERROR: %s\n",err_msg);
00080 CkPrintf(new_err_msg);
00081 fflush(stdout);
00082 CmiAbort(new_err_msg);
00083 delete [] new_err_msg;
00084 }
00085
00086
00087
00088 void NAMD_err(const char *err_msg)
00089
00090 {
00091 char *sys_err_msg = strerror(errno);
00092 if ( ! sys_err_msg ) sys_err_msg = "(unknown error)";
00093 char *new_err_msg = new char[strlen(err_msg) + 20 + strlen(sys_err_msg)];
00094 sprintf(new_err_msg,"FATAL ERROR: %s: %s\n",err_msg, sys_err_msg);
00095 CkPrintf(new_err_msg);
00096 fflush(stdout);
00097 CmiAbort(new_err_msg);
00098 delete [] new_err_msg;
00099 }
00100
00101
00102
00103 void NAMD_bug(const char *err_msg)
00104
00105 {
00106 const char *bug_msg =
00107 "FATAL ERROR: See http://www.ks.uiuc.edu/Research/namd/bugreport.html";
00108 char *new_err_msg = new char[strlen(err_msg) + 20 + strlen(bug_msg)];
00109 sprintf(new_err_msg,"FATAL ERROR: %s\n%s\n",err_msg,bug_msg);
00110 CkPrintf(new_err_msg);
00111 fflush(stdout);
00112 CmiAbort(new_err_msg);
00113 delete [] new_err_msg;
00114 }
00115
00116
00117 void NAMD_backup_file(const char *filename, const char *extension)
00118 {
00119 struct stat sbuf;
00120 if (stat(filename, &sbuf) == 0) {
00121 if ( ! extension ) extension = ".BAK";
00122 char *backup = new char[strlen(filename)+strlen(extension)+1];
00123 strcpy(backup, filename);
00124 strcat(backup, extension);
00125 #if defined(WIN32) && !defined(__CYGWIN__)
00126 remove(backup);
00127 #endif
00128 if ( rename(filename,backup) )
00129 {
00130 char *sys_err_msg = strerror(errno);
00131 if ( ! sys_err_msg ) sys_err_msg = "(unknown error)";
00132 iout << iERROR << "Error on renaming file " << filename
00133 << " to " << backup << ": " << sys_err_msg << "\n" << endi;
00134 fflush(stdout);
00135
00136
00137
00138 }
00139 delete [] backup;
00140 }
00141 }
00142
00143
00144 void NAMD_write(int fd, const char *buf, size_t count) {
00145 while ( count ) {
00146 #if defined(WIN32) && !defined(__CYGWIN__)
00147 long retval = _write(fd,buf,count);
00148 #else
00149 ssize_t retval = write(fd,buf,count);
00150 #endif
00151 if ( retval < 0 ) NAMD_die(strerror(errno));
00152 if ( retval > count ) NAMD_bug("extra bytes written in NAMD_write()");
00153 buf += retval;
00154 count -= retval;
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 FILE *Fopen (const char *filename, const char *mode)
00171 {
00172 struct stat buf;
00173
00174
00175 #if defined(NOCOMPRESSED)
00176 if (!stat(filename,&buf))
00177 {
00178 return(fopen(filename,mode));
00179 }
00180 #else
00181 if (!stat(filename,&buf))
00182 {
00183 if (!S_ISDIR(buf.st_mode))
00184 return(fopen(filename,mode));
00185 }
00186
00187 char *realfilename;
00188 char *command;
00189 FILE *fout;
00190 command = (char *)malloc(strlen(filename)+25);
00191
00192 sprintf(command,"zcat %s.Z",filename);
00193 realfilename = command+5;
00194 iout << "Command = " << command << "\n" << endi;
00195 iout << "Filename.Z = " << realfilename << "\n" << endi;
00196 if (!stat(realfilename,&buf))
00197 {
00198 if (!S_ISDIR(buf.st_mode))
00199 {
00200 fout = popen(command,mode);
00201
00202
00203 int C;
00204 do
00205 {
00206 C = fgetc(fout);
00207
00208 if (isalnum(C) || isspace(C))
00209 {
00210 ungetc(C,fout);
00211 C = -1;
00212 }
00213 } while(C != -1);
00214 free(command);
00215 return(fout);
00216 }
00217 }
00218
00219 sprintf(command,"gzip -d -c %s.gz",filename);
00220 realfilename = command+11;
00221 iout << "Command = " << command << "\n" << endi;
00222 iout << "Filename.gz = " << realfilename << "\n" << endi;
00223 if (!stat(realfilename,&buf))
00224 {
00225 if (!S_ISDIR(buf.st_mode))
00226 {
00227 fout = popen(command,mode);
00228
00229
00230 int C;
00231 do
00232 {
00233 C = fgetc(fout);
00234
00235 if (isalnum(C) || isspace(C))
00236 {
00237 ungetc(C,fout);
00238 C = -1;
00239 }
00240 } while(C != -1);
00241 free(command);
00242 return(fout);
00243 }
00244 }
00245 free(command);
00246 #endif
00247
00248 return(NULL);
00249 }
00250
00251
00252
00253
00254
00255 int Fclose (FILE *fout)
00256 {
00257 int rc = -1;
00258 #if !defined(NOCOMPRESSED)
00259 rc = pclose(fout);
00260 #endif
00261 if (rc == -1)
00262 {
00263 rc = fclose(fout);
00264 }
00265 return rc;
00266 }
00267
00268