src/standard.c
char *env_expand(char *in)
{
	char *txt_beg;
	char *out;
	char *txt_end;
	char *var_beg;
	char *var_end;
	char *value;
	char *next;
	int out_len;
	int val_len;

	if (!in)
		return in;

	value = out = NULL;
	out_len = 0;

	txt_beg = in;
	do {
		/* look for next '$' sign in <in> */
		for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++);

		if (!*txt_end && !out) /* end and no expansion performed */
			return in;

		val_len = 0;
		next = txt_end;
		if (*txt_end == '$') {
			char save;

			var_beg = txt_end + 1;
			if (*var_beg == '{')
				var_beg++;

			var_end = var_beg;
			while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') {
				var_end++;
			}

			next = var_end;
			if (*var_end == '}' && (var_beg > txt_end + 1))
				next++;

			/* get value of the variable name at this location */
			save = *var_end;
			*var_end = '\0';
			value = getenv(var_beg);
			*var_end = save;
			val_len = value ? strlen(value) : 0;
		}

		out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
		if (txt_end > txt_beg) {
			memcpy(out + out_len, txt_beg, txt_end - txt_beg);
			out_len += txt_end - txt_beg;
		}
		if (val_len) {
			memcpy(out + out_len, value, val_len);
			out_len += val_len;
		}
		out[out_len] = 0;
		txt_beg = next;
	} while (*txt_beg);

	/* here we know that <out> was allocated and that we don't need <in> anymore */
	free(in);
	return out;
}
/* Convert occurrences of environment variables in the input string to their
 * corresponding value. A variable is identified as a series of alphanumeric
 * characters or underscores following a '$' sign. The <in> string must be
 * free()able. NULL returns NULL. The resulting string might be reallocated if
 * some expansion is made. Variable names may also be enclosed into braces if
 * needed (eg: to concatenate alphanum characters).
 */
Convert occurrences of environment variables in the input string to their
corresponding value. A variable is identified as a series of alphanumeric
characters or underscores following a '$' sign. The  string must be
free()able. NULL returns NULL. The resulting string might be reallocated if
some expansion is made. Variable names may also be enclosed into braces if
needed .
Convert occurrences of environment variables in the input string to their corresponding value . A variable is identified as a series of alphanumeric characters or underscores following a ' $ ' sign . The string must be free () able . NULL returns NULL . The resulting string might be reallocated if some expansion is made . Variable names may also be enclosed into braces if needed .
char *env_expand(char *in)
{
	char *txt_beg;
	char *out;
	char *txt_end;
	char *var_beg;
	char *var_end;
	char *value;
	char *next;
	int out_len;
	int val_len;
	if (!in)
		return in;
	value = out = NULL;
	out_len = 0;
	txt_beg = in;
	do {
		for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++);
		if (!*txt_end && !out) 
			return in;
		val_len = 0;
		next = txt_end;
		if (*txt_end == '$') {
			char save;
			var_beg = txt_end + 1;
			if (*var_beg == '{')
				var_beg++;
			var_end = var_beg;
			while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') {
				var_end++;
			}
			next = var_end;
			if (*var_end == '}' && (var_beg > txt_end + 1))
				next++;
			save = *var_end;
			*var_end = '\0';
			value = getenv(var_beg);
			*var_end = save;
			val_len = value ? strlen(value) : 0;
		}
		out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
		if (txt_end > txt_beg) {
			memcpy(out + out_len, txt_beg, txt_end - txt_beg);
			out_len += txt_end - txt_beg;
		}
		if (val_len) {
			memcpy(out + out_len, value, val_len);
			out_len += val_len;
		}
		out[out_len] = 0;
		txt_beg = next;
	} while (*txt_beg);
	free(in);
	return out;
}
char * env_expand ( char * in ) { char * txt_beg ; char * out ; char * txt_end ; char * var_beg ; char * var_end ; char * value ; char * next ; int out_len ; int val_len ; if ( ! in ) return in ; value = out = NULL ; out_len = 0 ; txt_beg = in ; do { for ( txt_end = txt_beg ; * txt_end && * txt_end != ' ' ; txt_end ++ ) ; if ( ! * txt_end && ! out ) return in ; val_len = 0 ; next = txt_end ; if ( * txt_end == ' ' ) { char save ; var_beg = txt_end + 1 ; if ( * var_beg == ' ' ) var_beg ++ ; var_end = var_beg ; while ( isalnum ( ( int ) ( unsigned char ) * var_end ) || * var_end == ' ' ) { var_end ++ ; } next = var_end ; if ( * var_end == ' ' && ( var_beg > txt_end + 1 ) ) next ++ ; save = * var_end ; * var_end = ' \0 ' ; value = getenv ( var_beg ) ; * var_end = save ; val_len = value ? strlen ( value ) : 0 ; } out = realloc ( out , out_len + ( txt_end - txt_beg ) + val_len + 1 ) ; if ( txt_end > txt_beg ) { memcpy ( out + out_len , txt_beg , txt_end - txt_beg ) ; out_len += txt_end - txt_beg ; } if ( val_len ) { memcpy ( out + out_len , value , val_len ) ; out_len += val_len ; } out [ out_len ] = 0 ; txt_beg = next ; } while ( * txt_beg ) ; free ( in ) ; return out ; }
Convert occurrences of environment variables in the input string to their
 corresponding value.
Convert occurrences of environment variables in the input string to their corresponding value .
/* look for next '$' sign in <in> */ /* end and no expansion performed */ /* get value of the variable name at this location */ /* here we know that <out> was allocated and that we don't need <in> anymore */