By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

UNIX env var parsing in C

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

g.***********@gmail.com wrote:
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);

Nov 15 '05 #2

P: n/a

Peter wrote:
g.***********@gmail.com wrote:
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);


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

Nov 15 '05 #3

P: n/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.

Nov 15 '05 #4

P: n/a
g.***********@gmail.com wrote:
Peter wrote:
g.***********@gmail.com wrote:
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
#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.


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.
--
Nick Keighely

My god it's full of stars!
Dave Bowman, on seeing HAL's source code

Nov 15 '05 #5

P: n/a
g.***********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6

P: n/a

Keith Thompson wrote:
g.***********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

Nov 15 '05 #7

P: n/a
g.***********@gmail.com writes:
Keith Thompson wrote:
g.***********@gmail.com writes:
> 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
[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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8

P: n/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.

Nov 15 '05 #9

P: n/a
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

Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.