473,652 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2859

g.***********@g mail.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.***********@g mail.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.***********@g mail.com wrote:
Peter wrote:
g.***********@g mail.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.***********@g mail.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_Keit h) 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.***********@g mail.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_Keit h) 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.***********@g mail.com writes:
Keith Thompson wrote:
g.***********@g mail.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.progr ammer (even though the
solution might be implementable in pure standard C).

--
Keith Thompson (The_Other_Keit h) 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
3076
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. When I test these pages on my developer Windows php installation I get errors because Windows does not recognize the bang command so it is passed on as content to the php server. Any suggestions on hiding the first line from windows while still...
8
5259
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
4057
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 is #Makefile and some scripts to give output #numbers #Change till sign #END
9
2816
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 DD, int MM, int YYYY. Is this possible?
3
8109
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 all the dates and times are stored in the UNIX format.. Thanks! -- - Mitchell Vincent
2
3585
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 metromilwaukee.com URL http://clintongallagher.metromilwaukee.com/ MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h
7
2063
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 member.txt properly i think. The password and usernames i am using are at the bottom of this post. Each time i go to login on the unix server, it clears the username and password field. I have been attempting to solve the problem, but have been...
2
1589
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 return "N number of users, M number of processes". How am I to go about doing this? I am not very familiar with string parsing in c++, is this the only way? I am talking about getting the output from "ps -aux" and parsing it to retrieve the...
5
1769
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); print "<br>dirty: $dirty<br>clean: $clean";
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.