.\" part of publib .\" "@(#)publib-strutil:$Id: strvars.3,v 1.2 1995/06/17 10:47:23 wirzeniu Exp $" .\" .TH STRVARS 3 "C Programmer's Manual" Publib "C Programmer's Manual" .SH NAME strvars \- expand variables in string .SH SYNOPSIS .nf #include int \fBstrvars\fR(char **\fIres\fR, const char *\fIstr\fR, char *(*\fIexpand\fR)(const char *)); .SH DESCRIPTION \fIstrvars\fR will replaces references to variables in the string \fIstr\fR with the values of the variables. A reference to a variable is of the form \fI$(foo)\fR or \fI$x\fR (where \fIx\fR is a single character, but not \fI$\fR). A dollar sign is expressed in the string as \fI$$\fR, and will be converted to \fI$\fR in the output. Memory for the expanded string is allocated dynamically, and \fI*res\fR is set to point to it. .PP The values of the variables are fetched using the function \fIexpand\fR. It is given the name of the variable as its argument, and must return a pointer to the value, or NULL if that variable doesn't exist. .SH "RETURN VALUE" \fIstrvars\fR will return 0 if successful, or NULL if an error occured (malformed input string, result too big, or unknown variable). .SH EXAMPLE To replace references to environment variables, one would the following. .sp 1 .nf .in +5 #include #include char line[1024]; char *res; if (strvars(&res, line, getenv) == NULL) errormsg(1, 0, "strvars failed"); printf("res = <%s>\\n", res); .in -5 .SH "SEE ALSO" publib(3), getenv(3) .SH AUTHOR Lars Wirzenius (lars.wirzenius@helsinki.fi)