/*! Set variable in the environment */ int ccp4_utils_setenv (char *str) { #if defined (sgi) || defined (sun) || defined (__hpux) || \ defined(_AIX) || defined (__OSF1__) || \ defined (__osf__) || defined (__FreeBSD__) || defined (linux) || \ defined (_WIN32) || defined __linux__ /* putenv is the POSIX.1, draft 3 proposed mechanism */ #if !(defined(__hpux) && defined(__HP_cc)) int putenv (); #endif char *param; if ( (param = (char *) ccp4_utils_malloc( (strlen(str)+1)*sizeof(char) )) == NULL) { ccp4_errno = CCP4_ERRNO(errno); return -1; } strcpy(param,str); return (putenv (param)); /* note the necessary lack of free() */ #else /* setenv is not POSIX, BSD might have to use `index' */ int setenv (); char *param1,*param2; if ( (param1 = (char *) ccp4_utils_malloc( (strlen(str)+1)*sizeof(char) )) == NULL) { ccp4_errno = CCP4_ERRNO(errno); return -1; } strcpy(param1,str); if ((param2 = (char *) strchr(param1, '=')) == NULL) { ccp4_errno = CCP4_ERRNO(errno); return -1; } *param2++ = '\0'; return (setenv (param1, param2, 1)); #endif }
/** Extracts the basename from a full file name. * Separators for directories and extensions are OS-specific. * @param filename full file name string. * @return pointer to basename */ char *ccp4_utils_basename(const char *filename) { int i, indx1=-1, length; char *basename; for ( i = strlen(filename)-1; i >= 0; i-- ) { if (is_sep(filename[i])) { indx1 = i; break; } } length = strlen(filename) - indx1; /* Search for extension separators must be performed backwards in case filename has multiple extension separators */ for ( i = strlen(filename)-1; i >= (indx1 < 0 ? 0 : indx1) ; i-- ) { if (filename[i] == EXT_SEPARATOR) { length = i - indx1; break; } } basename = ccp4_utils_malloc(length*sizeof(char)); strncpy(basename,filename+indx1+1,length-1); basename[length-1]='\0'; return basename; }
/*! Find the path of the SYMINFO file. We will try to locate the file in several places (in increasing order of significance): -# $SYMINFO environment variable -# ./syminfo.lib -# $prefix/share/gpp4/syminfo.lib -# $prefix/lib/syminfo.lib -# $CCP4/lib/data/syminfo.lib where $prefix is defined by the --prefix switch to the configure script. The function allocates memory for the pathname that must be free'd by the caller in order to avoid a memory leak. @return path to syminfo.lib file. */ char *gpp4_open_syminfo_file() { struct stat statbuf; char *str; char *fnam = (char *)ccp4_utils_malloc(512); if ((str = getenv("SYMINFO"))) { /* SYMINFO was defined, now check to see if file exists */ strncpy (fnam, str, 512); if( stat(fnam,&statbuf) == 0) { printf("\n Spacegroup information obtained from library file: \n"); printf(" Logical Name: SYMINFO Filename: %s\n\n", fnam); return fnam; } } printf("Environment variable SYMINFO not set ... guessing location of symmetry file.\n"); strncpy (fnam, "./syminfo.lib", 14); if( stat(fnam,&statbuf) == 0) { printf("\n Spacegroup information obtained from local file: %s\n", fnam); return fnam; } strncpy (fnam, GPP4_PREFIX, 512-24); strncat (fnam, "/share/gpp4/syminfo.lib", 24); if( stat(fnam,&statbuf) == 0) { printf("\n Spacegroup information obtained from system file: %s\n", fnam); return fnam; } strncpy (fnam, GPP4_PREFIX, 512-17); strncpy (fnam, "/lib/syminfo.lib", 17); if( stat(fnam,&statbuf) == 0) { printf("\n Spacegroup information obtained from system file: %s\n", fnam); return fnam; } /* Hmmm. Try one last time in the CCP4 installation */ if (!(str = getenv("CCP4"))) { printf("Environment variable CCP4 not set ... big trouble! \n"); free(fnam); return NULL; } strncpy(fnam, str, 512); strncat(fnam,"/lib/data/syminfo.lib", 22); if( stat(fnam,&statbuf) == 0) { printf("\n Spacegroup information obtained from CCP4 library: %s\n", fnam); return fnam; } /* We give up... */ free(fnam); return NULL; }
/** Creates a null-terminated C string from an input * string obtained from a Fortran call. Trailing blanks are * removed. If input string is blank then return string "\0". * Memory assigned by malloc, so can be freed. * @param str1 pointer to string * @param str1_len Fortran length of string */ char *ccp4_FtoCString(fpstr str1, int str1_len) { char *str2; size_t length = ccp4_utils_flength(FTN_STR(str1),str1_len); str2 = (char *) ccp4_utils_malloc((length+1)*sizeof(char)); if(length) strncpy(str2, FTN_STR(str1), length); str2[length] = '\0'; return str2; }
/** Extracts the pathname from a full file name. * Separators for directories and extensions are OS-specific. * @param filename full file name string. * @return pointer to pathname with trailing separator. */ char *ccp4_utils_pathname(const char *filename) { int i, indx1=-1, length; char *pathname; for ( i = strlen(filename)-1; i >= 0; i-- ) { if (is_sep(filename[i])) { indx1 = i; break; } } length = indx1+2; pathname = ccp4_utils_malloc(length*sizeof(char)); strncpy(pathname,filename,length-1); pathname[length-1]='\0'; return pathname; }
/** Extracts the extension from a full file name. * Separators for directories and extensions are OS-specific. * @param filename full file name string. * @return pointer to extension */ char *ccp4_utils_extension(const char *filename) { int i, indx1=-1, length=1; char *extension; for ( i = strlen(filename)-1; i >= 0; i-- ) { if (filename[i] == EXT_SEPARATOR) { indx1 = i; length = strlen(filename) - indx1; break; } else if (is_sep(filename[i])) { indx1 = i; length = 1; break; } } extension = ccp4_utils_malloc(length*sizeof(char)); strncpy(extension,filename+indx1+1,length-1); extension[length-1]='\0'; return extension; }
/** Joins a leading directory with a filename. * Separators for directories and extensions are OS-specific. * @param dir directory path. * @param file file name string. * @return pointer to joined directory-filename path. */ char *ccp4_utils_joinfilenames(const char *dir, const char *file) { char *join=NULL; int lendir,lenfile,lenjoin; lendir = strlen(dir); lenfile = strlen(file); lenjoin = lendir + lenfile + 2; join = (char *) ccp4_utils_malloc(sizeof(char)*lenjoin); if (!join) { return NULL; } strncpy(join,dir,lendir); join[lendir] = PATH_SEPARATOR; join[lendir+1] = '\0'; strncat(join,file,lenfile); join[lenjoin-1] = '\0'; return join; }