Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | 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 access(PATH,MODE) _access(PATH,00)
00027 #define NOCOMPRESSED
00028 #endif
00029 
00030 #ifdef USESTDIOSTUBS
00031 
00032 // On the ASCI-Red, these functions have prototypes that don't match
00033 // the standard, so I'll override them
00034 
00035 int write(int fd, const void* buf, int count) 
00036 { 
00037   return write(fd,(void*)buf,(size_t)count);
00038 }
00039 
00040 int stat(const char* fn, struct stat* buf)
00041 {
00042   return stat((char*)fn, buf);
00043 }
00044 
00045 #endif
00046 
00047 
00048 // make a duplicate of a string
00049 char *NAMD_stringdup(const char *s) {
00050   char *rs;
00051 
00052   if(!s)
00053     return NULL;
00054 
00055   rs = new char[strlen(s) + 1];
00056   strcpy(rs,s);
00057 
00058   return rs;
00059 }
00060 
00061 
00062 // signal all nodes, it's time to quit
00063 void NAMD_quit(const char *err_msg)
00064 
00065 {
00066    char *new_err_msg = new char[strlen(err_msg) + 20];
00067    sprintf(new_err_msg,"EXITING: %s\n",err_msg);
00068    CkPrintf(new_err_msg);
00069    fflush(stdout);
00070    CmiAbort(new_err_msg);
00071    delete [] new_err_msg;
00072 }
00073 
00074  
00075 // signal all nodes, it's time to quit
00076 void NAMD_die(const char *err_msg)
00077 
00078 {
00079    char *new_err_msg = new char[strlen(err_msg) + 20];
00080    sprintf(new_err_msg,"FATAL ERROR: %s\n",err_msg);
00081    CkPrintf(new_err_msg);
00082    fflush(stdout);
00083    CmiAbort(new_err_msg);
00084    delete [] new_err_msg;
00085 }
00086 
00087 
00088 // signal all nodes, it's time to quit
00089 void NAMD_err(const char *err_msg)
00090 
00091 {
00092    char *sys_err_msg = strerror(errno);
00093    if ( ! sys_err_msg ) sys_err_msg = "(unknown error)";
00094    char *new_err_msg = new char[strlen(err_msg) + 20 + strlen(sys_err_msg)];
00095    sprintf(new_err_msg,"FATAL ERROR: %s: %s\n",err_msg, sys_err_msg);
00096    CkPrintf(new_err_msg);
00097    fflush(stdout);
00098    CmiAbort(new_err_msg);
00099    delete [] new_err_msg;
00100 }
00101 
00102 
00103 // signal all nodes, it's time to quit and it's our own damn fault
00104 void NAMD_bug(const char *err_msg)
00105 
00106 {
00107    const char *bug_msg = 
00108      "FATAL ERROR: See http://www.ks.uiuc.edu/Research/namd/bugreport.html";
00109    char *new_err_msg = new char[strlen(err_msg) + 20 + strlen(bug_msg)];
00110    sprintf(new_err_msg,"FATAL ERROR: %s\n%s\n",err_msg,bug_msg);
00111    CkPrintf(new_err_msg);
00112    fflush(stdout);
00113    CmiAbort(new_err_msg);
00114    delete [] new_err_msg;
00115 }
00116 
00117 // move filename to filename.BAK
00118 void NAMD_backup_file(const char *filename, const char *extension)
00119 {
00120   if (access(filename, F_OK) == 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 Thu May 23 04:07:14 2013 for NAMD by  doxygen 1.3.9.1