#include #include #include #include #include #include #include #include "rtcdef.h" #ifdef _SKY #include #include #include #else #include "mathlib_simulate.h" #endif /*********************************************************************** * rtc_util.c -- * * Utility routines ... * * History: * 25-May-2000 TRG New functions: get_nreset_ and set_nreset_. * 22-May-2000 TRG Moved rtcc_init to its own source file, because * references to nstx_pcs_gen forced inclusion of a lot * of Fortran-related stuff if called from an otherwise * pure C program. * 06-Jan-2000 TRG rewrite_cd_: new. * 25-Oct-1999 TRG rtcc_init: new. * 10-Feb-1999 TRG Create. * *************************************************************************/ /*======================================================== * External variables ... *=======================================================*/ /*======================================================= * Static variables: *======================================================*/ static struct rtcHostComm *rm; /* "reserved memory" pointer */ /*======================================================== * Function prototypes ... *=======================================================*/ char *getwd( char *path ); /* should be in unistd.h, but is not */ /**************************************************************** * circuitName: ****************************************************************/ char *circuitName( int icircuit ) { static char *circuits[] = { "OH","PF1aU","PF1aL","PF1b","PF2U" ,"PF2L","PF3U","PF3L","PF5","CHI","TF","PF4" }; return((icircuit>=0 && icircuit<12) ? circuits[icircuit] : "unknown"); } /***************************************************************** * nonblanklen_: * Fortran-callable: return length of trimmed string *****************************************************************/ int nonblanklen_( /* Return: string length, no trailing blanks */ char *string /* address of string */ ,int maxLen /* max length of wdir string */ ) { char *p,*p2,*pMax; pMax = string + maxLen; p2 = 0; for (p=string ; *p && p current working directory */ ,int maxLen /* max length of wdir string */ ) { char wd[MAXPATHLEN]; getcwd(wd,sizeof(wd)); strncpy(wdir,wd,maxLen); return; } /****************************************************************** * chdir_: * Fortran versions of C functions ******************************************************************/ void chdir_( char *path /* path name */ ,int pathLen /* path length */ ) { char newdir[MAXPATHLEN]; if (pathLen >= MAXPATHLEN) { perror("from chdir_"); exit(-1); } strncpy(newdir,path,pathLen); newdir[pathLen] = '\0'; chdir(newdir); return; } /***************************************************************** * ftn_sleep_: * Fortran-callable version of C's sleep() *****************************************************************/ void FORTRAN_SUBROUTINE(ftn_sleep)( int *nsec ) { sleep(*nsec); return; } /***************************************************************** * connectToHost: *****************************************************************/ #ifdef _SKY void *connectToHost() /* _SKY version */ { int boltPid; boltPid = bolt_getpid(); pcsmsg(0,"Bolt pid = %04X",boltPid); /*======================================================= * connect to Sky reserved memory, shared with host ... *======================================================*/ rm = (void *)get_share_mem_addr(); if (rm == 0) { printf ("==> ERROR -- Can't get memory address. Type\n"); printf ("==> setenv BOLTuserdata 1\n"); printf ("==> to allocate shared memory.\n\n"); host_kill(0,BOLT_EXIT); exit(0); } memset(rm,0,512); /* initialize some of rm to zero */ printf("==> NOTIFY host ...\n"); host_kill(0,NOTIFY); /* wake-up host */ printf("==> ... OK\n"); return(rm); } #else void *connectToHost() /* Non-_SKY version */ { void *rm; static struct rtcHostComm rtcHostComm; rm = &rtcHostComm; return(rm); } #endif void *connecttohost_() /* really a FORTRAN_FUNCTION, but ... */ { return(connectToHost()); } /**************************************************************** * rewrite_cd_: * Rewrite older version of c.d file ... ****************************************************************/ int FORTRAN_FUNCTION(rewrite_cd)( int *iversion_cd /* version number: see above */ ,char *path /* path to c.d's directory */ ,int npath /* num chars in "path" */ ) { char cmd[256]; char cdFilename[128]; char newFilename[128]; struct tm *t; time_t bintim; if (npath >= sizeof(cdFilename)) exit(pcsmsg(0,"rewrite_cd_: OOPS")); strncpy(cdFilename,path,npath); strcpy(cdFilename+npath,"/c.d"); /*------------------------------------------------------ * Name of new c.d file is c.d_yymmdd_new *-----------------------------------------------------*/ time(&bintim); t = localtime(&bintim); sprintf(newFilename,"%s_%02d%02d%02d_new",cdFilename, t->tm_year%100,t->tm_mon+1,t->tm_mday); sprintf(cmd,"/bin/test -f %s",newFilename); if (!system(cmd)) { printf("\n"); sprintf(cmd,"/bin/rm %s",newFilename); printf("--> %s\n",cmd); if (system(cmd)) exit(pcsmsg(0,"rewrite_cd: error executing command\t\t%s",cmd)); } sprintf(cmd,"/home/ppcc/gibney/psc/bin/rewrite_cd_file <%s >%s", cdFilename,newFilename); printf("--> %s\n",cmd); if (system(cmd)) exit(pcsmsg(0,"rewrite_cd: *EXIT* failed on cmd:\n\t%s\n",cmd)); /*-------------------------------------------------------- * On success, rename old file to c.d_yymmdd, * rename new file to c.d *-------------------------------------------------------*/ sprintf(cmd,"/bin/mv %s %s_%02d%02d%02d",cdFilename, cdFilename,t->tm_year%100,t->tm_mon+1,t->tm_mday); printf("--> %s\n",cmd); if (system(cmd)) exit(pcsmsg(0,"rewrite_cd: *EXIT* failed on cmd:\n\t%s\n",cmd)); sprintf(cmd,"/bin/mv %s %s",newFilename,cdFilename); printf("--> %s\n",cmd); if (system(cmd)) exit(pcsmsg(0,"rewrite_cd: *EXIT* failed on cmd:\n\t%s\n",cmd)); return(1); } /**************************************************************** * get_nreset_: * set_nreset_: ****************************************************************/ int FORTRAN_FUNCTION(get_nreset)() { return(rm ? rm->nreset : 0); } int FORTRAN_FUNCTION(set_nreset)( int newval ) { int k; if (!rm) k = 0; else { k = rm->nreset; rm->nreset = newval; } return(k); }