Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

common.C

Go to the documentation of this file.
00001 
00007 /*
00008    global functions as declared in common.h
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 // On the ASCI-Red, these functions have prototypes that don't match
00032 // the standard, so I'll override them
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 // make a duplicate of a string
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 // signal all nodes, it's time to quit
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 // signal all nodes, it's time to quit
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 // signal all nodes, it's time to quit
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 // signal all nodes, it's time to quit and it's our own damn fault
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 // move filename to filename.BAK
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     if ( remove(backup) ) if ( errno != ENOENT ) {
00127       char *sys_err_msg = strerror(errno);
00128       if ( ! sys_err_msg ) sys_err_msg = "(unknown error)";
00129       iout << iERROR << "Error on removing file "
00130         << backup << ": " << sys_err_msg << "\n" << endi;
00131       fflush(stdout);
00132     }
00133 #endif
00134     if ( rename(filename,backup) )
00135     {
00136       char *sys_err_msg = strerror(errno);
00137       if ( ! sys_err_msg ) sys_err_msg = "(unknown error)";
00138       iout << iERROR << "Error on renaming file " << filename
00139         << " to " << backup << ": " << sys_err_msg << "\n" << endi;
00140       fflush(stdout);
00141       // char errmsg[256];
00142       // sprintf(errmsg, "Error on renaming file %s to %s",filename,backup);
00143       // NAMD_err(errmsg);
00144     }
00145     delete [] backup;
00146   }
00147 }
00148 
00149 // same as write, only does error checking internally
00150 void NAMD_write(int fd, const char *buf, size_t count) {
00151   while ( count ) {
00152 #if defined(WIN32) && !defined(__CYGWIN__)
00153     long retval = _write(fd,buf,count);
00154 #else
00155     ssize_t retval = write(fd,buf,count);
00156 #endif
00157     if ( retval < 0 && errno == EINTR ) retval = 0;
00158     if ( retval < 0 ) NAMD_die(strerror(errno));
00159     if ( retval > count ) NAMD_bug("extra bytes written in NAMD_write()");
00160     buf += retval;
00161     count -= retval;
00162   }
00163 }
00164 
00165 
00166 /***************************************************************************
00167  Fopen(char *Filename, char *mode):  similar to fopen(filename,mode) except
00168  it checks for compressed file names too.
00169  For example:  Fopen("config");
00170    This will first look for the filename "config" (and return "r" file handle
00171    if it is found).
00172    Then it will look for "config.Z" (and run "zcat config.Z", returning
00173    a file handle to the uncompressed data if found).
00174    Then it will look for "config.gz" (and run "gzip -d -c config.gz", returning
00175    a file handle to the uncompressed data if found).
00176  ***************************************************************************/
00177 FILE *Fopen     (const char *filename, const char *mode)
00178 {
00179   struct stat buf;
00180   // check if basic filename exists (and not a directory)
00181 
00182 #if defined(NOCOMPRESSED)
00183   if (!stat(filename,&buf))
00184     {
00185       FILE *rval;
00186       while ( ! (rval = fopen(filename,mode)) ) {
00187         if ( errno != EINTR ) break;
00188       }
00189       return(rval);
00190     }
00191 #else
00192   if (!stat(filename,&buf))
00193     {
00194       if (!S_ISDIR(buf.st_mode)) {
00195         FILE *rval;
00196         while ( ! (rval = fopen(filename,mode)) ) {
00197           if ( errno != EINTR ) break;
00198         }
00199         return(rval);
00200       }
00201     }
00202   // check for a compressed file
00203   char *realfilename;
00204   char *command;
00205   FILE *fout;
00206   command = (char *)malloc(strlen(filename)+25);
00207   // check for .Z (unix compress)
00208   sprintf(command,"zcat %s.Z",filename);
00209   realfilename = command+5;
00210   iout << "Command = " << command << "\n" << endi;
00211   iout << "Filename.Z = " << realfilename << "\n" << endi;
00212   if (!stat(realfilename,&buf))
00213         {
00214         if (!S_ISDIR(buf.st_mode))
00215                 {
00216                 fout = popen(command,mode);
00217                 // on HP-UX, the first character(s) out of pipe may be
00218                 // garbage!  (Argh!)
00219                 int C;
00220                 do
00221                   {
00222                   C = fgetc(fout);
00223                   // iout << "C is " << C << "\n" << endi;
00224                   if (isalnum(C) || isspace(C))
00225                         {
00226                         ungetc(C,fout);
00227                         C = -1; // outta loop
00228                         }
00229                   } while(C != -1);
00230                 free(command);
00231                 return(fout);
00232                 }
00233         }
00234   // check for .gz (gzip)
00235   sprintf(command,"gzip -d -c %s.gz",filename);
00236   realfilename = command+11;
00237   iout << "Command = " << command << "\n" << endi;
00238   iout << "Filename.gz = " << realfilename << "\n" << endi;
00239   if (!stat(realfilename,&buf))
00240         {
00241         if (!S_ISDIR(buf.st_mode))
00242                 {
00243                 fout = popen(command,mode);
00244                 // on HP-UX, the first character(s) out of pipe may be
00245                 // garbage!  (Argh!)
00246                 int C;
00247                 do
00248                   {
00249                   C = fgetc(fout);
00250                   // iout << "C is " << C << "\n" << endi;
00251                   if (isalnum(C) || isspace(C))
00252                         {
00253                         ungetc(C,fout);
00254                         C = -1; // outta loop
00255                         }
00256                   } while(C != -1);
00257                 free(command);
00258                 return(fout);
00259                 }
00260         }
00261   free(command);
00262 #endif /* !defined(NOCOMPRESSED) */
00263 
00264   return(NULL);
00265 } /* Fopen() */
00266 
00267 /***************************************************************************
00268  Fclose(FILE *fout):  similar to fclose(fout) except it first checks if the
00269  file handle fout is a named pipe.
00270  ***************************************************************************/
00271 int     Fclose  (FILE *fout)
00272 {
00273   int rc = -1;
00274 #if !defined(NOCOMPRESSED)
00275   rc = pclose(fout);
00276 #endif
00277   if (rc == -1) // stream not associated with a popen()
00278     {
00279     rc = fclose(fout);
00280     }
00281   return rc;
00282 } /* Fclose() */
00283 
00284 

Generated on Sun Feb 12 04:07:53 2012 for NAMD by  doxygen 1.3.9.1