/*===exphome===([o]i)==================================================
* if SIn is not NULL then
* if SIn starts with '~'
* then expands $HOME, prepends it to the rest of SIn, and
* stores result in SOut or, if SOut==NULL, in a new
* allocated string and returns it
* else if SOut!=NULL
* then copies SIn into SOut and returns SOut
* else returns duplicated SIn
* else returns NULL
=*===================================================================*/
char *exphome(char *SOut, char *SIn)
{char *Rt= NULL;
char *envhome= NULL;
if(SIn)
if(*SIn=='~' && (envhome=getenv("HOME")))
{Rt= malloc(strlen(SIn)+strlen(envhome)+1);
strcpy(Rt, envhome); strcat(Rt, SIn+1);
if(SOut) {strcpy(SOut, Rt); free(Rt); Rt= SOut;}
}
else if(SOut) {strcpy(SOut, SIn); Rt= SOut;}
else Rt= strdup(SIn);
return Rt;
} /*exphome*/
接着
fopen(exphome(NULL, yourfile), ...)