strvars(3pub)

strvars3pub

Expand variables in string

Usage

#include <publib.h>
int strvars(char **res, const char *str, char *(*expand)(const char *));

Overview

strvars will replaces references to variables in the string str with the values of the variables. A reference to a variable is of the form $(foo) or $x (where x is a single character, but not $). A dollar sign is expressed in the string as $$, and will be converted to $ in the output. Memory for the expanded string is allocated dynamically, and *res is set to point to it.

The values of the variables are fetched using the function expand. 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.

Return value

strvars will return 0 if successful, or NULL if an error occurred (malformed input string, result too big, or unknown variable).

Examples

To replace references to environment variables, one would the following.

#include <publib.h>
#include <stdlib.h>
char line[1024];
char *res;
if (strvars(&res, line, getenv) == NULL)
    errormsg(1, 0, "strvars failed");
printf("res = <%s>\n", res);

Releases

16 releases since Debian 1.2 (Rex); the text changed 5 times.

  1. 1.2–1.3
  2. 2.0–2.2
  3. 3.0
  4. 3.1–6.0
  5. 7–8
  6. 9–13

Author

Lars Wirzenius ([email protected])

From publib-dev 0.40-4 in Debian 13 (Trixie). Parsed by carta from the page's man source.