473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

UNIX env var parsing in C

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
9 2842

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

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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael Appelmans | last post by:
I'm a php novice and am developing a shopping cart application for a client who is hosted on a unix server. The hosting service requires that each php file have #!/usr/local/bin/php at the top....
8
by: Oli Schwarz | last post by:
Hello, how can I read out the uptime of a unix system in python (Linux and *BSD). I have not found a uptime-function in the Library. Regards Oli
4
by: rkoida | last post by:
Hello evryone I am a newbie to python. I have a makefile which i can compile in UNIX/LINUX, But i I am planning to write a python script which actually does what my MAKEFILE does. The make file...
9
by: ritchie | last post by:
Hello, Just wondering if anyone could help me with a basic C string-parsing question. I have an Integer date ie: YYYYMMDD. I can convert it into a string but I want to break it down to: Int...
3
by: Mitchell Vincent | last post by:
Would anyone happen to have any idea how to get a UNIX timestamp (AKA epoch, or number of seconds since Jan 1st 1970) into a VB.NET datetime type? I have a legacy application I'm trying to port and...
2
by: clintonG | last post by:
Does the documented property value UNIX mean the Request.Browser.Platform property determines requests are being made from a Linux machine? <%= Clinton Gallagher NET csgallagher AT...
7
by: programming | last post by:
Hi all, i have been having trouble with a login script that works on my windows machine, however when i upload it to the Unix server through VPN, the same script won't work! It won't parse...
2
by: frikk | last post by:
Hey everyone, (Sorry about the first message if it made it - i got cut off early). I have a homework problem for my Operating Systems class. I am supposed to use a command such as "ps -aux" and...
5
by: e_matthes | last post by:
Hello, I have a function which uses a regular expression to validate text input. Here's a short code sample testing the regex: <?php $dirty = "hello"; $clean = getCleanText($dirty, 0,50);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.