UNIX env var parsing in C

G

g.kanaka.raju

Hi,

I wanted to write a function that gets the file path as an argument.
The file
path make take one of the two forms :

(i) Absolute file path e.g., /usr/local/file.log or
(ii)it may be $USRLOCAL/file.${EXT} where USRLOCAL=/usr/local and
$EXT=log

Could any one of you suggest a good function which takes a string with
env varaibles and returns plain string with the values of env variabels
sustituted.
Fox example,

Input : $USRLOCAL/file.${EXT} where USRLOCAL=/usr/local and $EXT=log
Output : /usr/local/file.log

Thanks in advance.

Regards,
Raju
 
P

Peter

Hi,

I wanted to write a function that gets the file path as an argument.
The file
path make take one of the two forms :

(i) Absolute file path e.g., /usr/local/file.log or
(ii)it may be $USRLOCAL/file.${EXT} where USRLOCAL=/usr/local and
$EXT=log

Could any one of you suggest a good function which takes a string with
env varaibles and returns plain string with the values of env variabels
sustituted.
Fox example,

Input : $USRLOCAL/file.${EXT} where USRLOCAL=/usr/local and $EXT=log
Output : /usr/local/file.log

Thanks in advance.

Regards,
Raju
#include <stdlib.h>
char *getenv(const char *name);
 
G

g.kanaka.raju

Peter said:
#include <stdlib.h>
char *getenv(const char *name);

I'm aware of getenv() which gives the value of an environment variable.
I'm looking for a funtion which takes an input string (which may have
'n' number of env variables) and outputs a string where all the env
variables got replaced with their values. I'm looking for a way to
parse it.

Regards,
Raju
 
A

ajm

Raju
I'm aware of getenv() which gives the value of an environment variable.
I'm looking for a funtion which takes an input string (which may have
'n' number of env variables) and outputs a string where all the env
variables got replaced with their values. I'm looking for a way to
parse it.

as I understand it you want something to resolve the vars embedded in
your string ? there is no function as such to do this but I think it is
relatively straightforward using the usual str* functions.

for example, you might tokenize the string (using "/" as the delimiter)
and resolve each token returned that begins with $ using gentenv. the
output string would be the concatenation of the (resolved) tokens.

the appropriate function here is strtok (which you need to invoke in a
loop
that checks for NULL returns).

does that make sense ?

hth,
ajm.
 
N

Nick Keighley

I'm aware of getenv() which gives the value of an environment variable.
I'm looking for a funtion which takes an input string (which may have
'n' number of env variables) and outputs a string where all the env
variables got replaced with their values. I'm looking for a way to
parse it.

there is no standard C function that does what you want. You're going
to have to parse it yourself. strtok() might give you a start in doing
this.
 
K

Keith Thompson

I wanted to write a function that gets the file path as an argument.
The file
path make take one of the two forms :

(i) Absolute file path e.g., /usr/local/file.log or
(ii)it may be $USRLOCAL/file.${EXT} where USRLOCAL=/usr/local and
$EXT=log

Could any one of you suggest a good function which takes a string with
env varaibles and returns plain string with the values of env variabels
sustituted.
Fox example,

Input : $USRLOCAL/file.${EXT} where USRLOCAL=/usr/local and $EXT=log
Output : /usr/local/file.log

There's no standard function to do this, though I'm sure it's been
implemented many times.

You'll have to define what you want to do in all the corner cases.
For example, you'll need to decide how to determine when you've
reached the end of the environment variable name. C's standard
getenv() function doesn't say anything about the form of the name; as
far as the standard is concerned, "USRLOCAL/" could be the name of an
environment variable. Your use of the "${EXT}" syntax probably
implies that you don't want '{' and '}' to appear in environment
variable names.

You'll also need to decide what to do if getenv() returns a null
pointer.
 
G

g.kanaka.raju

Keith said:
There's no standard function to do this, though I'm sure it's been
implemented many times.

You'll have to define what you want to do in all the corner cases.
For example, you'll need to decide how to determine when you've
reached the end of the environment variable name. C's standard
getenv() function doesn't say anything about the form of the name; as
far as the standard is concerned, "USRLOCAL/" could be the name of an
environment variable. Your use of the "${EXT}" syntax probably
implies that you don't want '{' and '}' to appear in environment
variable names.

You'll also need to decide what to do if getenv() returns a null
pointer.

Hi,

Thank you for all your suggestions. I'm little confused how to
determine the end of the environment variable. So, I just wanted to
know if any body of you have written a function to acheive the required
functionality.

Regards,
Raju
 
K

Keith Thompson

[snip]

Thank you for all your suggestions. I'm little confused how to
determine the end of the environment variable. So, I just wanted to
know if any body of you have written a function to acheive the required
functionality.

I don't know. Since your examples appear to be Unix-specific, you
might have better luck asking in comp.unix.programmer (even though the
solution might be implementable in pure standard C).
 
A

ajm

Raju

this should really not be that hard - for example if strtok (using '/'
to delimit) returns a string variable, token, then you know that one of
the following cases must arise:

(i) the whole token must be passed to getenv for resolution
(ii) part of the token must be resolved and the rest is literal
(iii) no resolution of the token is required

these cases are easy to identify:

(i) token[0]=='$' && token[1] != '{'
(ii) token[0]=='$' && token[1] == '{'
(iii) token[0] != '$'

only in the second case is a little more work required (i.e., strchr
search for '}' and pass this to getenv rather than the whole token).
If you find this sort of thing a little challenge then you will
probably benefit from learning by writing it yourself ;)

Keith's comment that this is really a UNIX post (rather than an ANSI C
one) is worth bearing in mind too.

hth

ajm.
 
B

bwegge

Raju

As ajm wrote this is probably not the right group, but here are my
thoughts anyway.

I wrote a function in c++, which among other replaced $<varname> with
environment variables. When it came to deciding which characters I
would allow in the name of env. var. I found a reference to something
called the "Portable Character Set". This set supposedly shows all the
characters allowed in a env var on a UNIX system. The c++ function I
wrote was for Windows, so I tried to see what I could get away with
here. Sadly the two don't match. For instance, windows allows regional
characters but not &, %. Both allow blanks which is a bit puzzling or
rather ambigious.

In the end I decided that the env var should look like an identifier,
not perfect but it was ok in this case. Anything other than a-z, A-z,
0-9 and _ would mark the end of a varname. If a string should follow
directly afterwards then the user would have to put in a $ to mark the
end. For example:

Input : $USRLOCAL$/file.$EXT where USRLOCAL=/usr/local and $EXT=log
Output : /usr/local/file.log

If you want a $ after the env var you would have to put $$, and two env
var following one another would have to look like $a$$b.

Hope this helps
bwegge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top