#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libant/EnvPrefix.c $ %D% SwRI" /* EnvPrefix -- prefix string with string from environment */ #include /* for FILE, etc. */ #include /* for malloc */ #include /* for string functions */ #include "ant.h" /* for GetEnv */ /* EnvDirPrefix -- prefix string with directory string from environment dir string MUST NOT end with '/' */ char * EnvDirPrefix(const char *envvar,char *str) { char *envstr; /* string value of environment variable */ char *newstr = NULL; /* same error convention as GetEnv */ envstr = GetEnv(envvar); if (envstr) { newstr = (char *) malloc(strlen(envstr)+strlen(str)+2); strcpy(newstr,envstr); strcat(newstr,"/"); strcat(newstr,str); } else { fprintf (stderr, "%s not found in environment!\n", envvar); } return newstr; } /* EnvFopen -- fopen file relative to dir found in environment */ FILE * EnvFopen(char *EnvName,char *filename,char *type) { char *dir; FILE *fp = NULL; char fullname[256]; char *path; static char *separators=":"; if (!(path=GetEnv(EnvName))) return NULL; /* try to open file somewhere on path */ dir = strtok(path,separators); for (; dir; dir = strtok(NULL,separators)) { strcpy(fullname,dir); strcat(fullname,"/"); strcat(fullname,filename); fp = fopen(fullname, type); if (fp) break; } return fp; } /* EnvPrefix -- prefix string with string from environment */ char * EnvPrefix(char *envvar,char *str) { char *envstr; /* string value of environment variable */ char *newstr; envstr = GetEnv(envvar); if (envstr) { newstr = (char *) malloc(strlen(envstr)+strlen(str)+1); strcpy(newstr,envstr); strcat(newstr,str); } else newstr = strdup(str); return newstr; }