Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-19-2010, 10:35 AM   #16
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671

look at setenv() for setting or creating an environment variable in a c program. There isn't a need to call a C program simply to set an environmental variable. You can assign a variable and export that variable in a bash script.

Last edited by jschiwal; 01-19-2010 at 11:55 PM.
Old 06-01-2010, 11:18 PM   #17
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 98
Blog Entries: 1

Rep: Reputation: 19
Originally Posted by carbonfiber View Post
What's your point? And why are you using fprintf?

Well this is an open question that can be taken at least two ways.

1) Why are you using a "fprintf" rather than an exit code?

Well an exit code can only contain the values of 0-255 for one thing
and since bash allows the assignment of stdout of a command, I choose the most general case which is to return a value in via stdout and hence a print. But please continue to read the next segment if this is the question that you were asking.

2) Why are you using a "fprintf" rather than a "printf"?

I always write C code where the "File Descriptor" (For C++ the "stream") is passed. This is because in Unix/Linux old-timers find code written that use the "File Descriptor/stream" is much more flexible than a function that passes in the file name.

For example you have a compressed file that is currently 2 Gigabytes of space. You know that it is just text. By passing the file descriptor, the file does not need to be de-compressed onto the hard drive and then read. The "popen" function can be used to get the field descriptor of the pipe that is de-compressing and read via the pipe. Remember that a pipe blocks the producer when the consumer is too slow, so it does not fill up the disk space. When a younger developer writes the function with a file name, the compressed file must be decompressed prior to reading it.

So I always used the fXXX i/o functions in my C code. I am not a lazy coder because the re-use of code well written can save much more time than using "printf" rather than "printf".

Does this help you understand why I used printf.
Old 06-02-2010, 05:54 PM   #18
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Originally Posted by jschiwal View Post
look at setenv() for setting or creating an environment variable in a c program. There isn't a need to call a C program simply to set an environmental variable. You can assign a variable and export that variable in a bash script.
You can use this method to pass data to a child process, but not to a parent process. A copy of an environment variable that is created by a shell will be visible to a C program launched by the shell. Any modification to to the environment variable by the C program will not be seen by the parent shell, because the C program sees only a copy of the one owned by the parent shell.

The possibilities for communicating between a parent and child process are described very well in

Beej's Guide to Unix Interprocess Communication

--- rod.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
return value from shell script to c code? khucinx Programming 1 05-13-2004 03:43 PM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 10:02 AM
return key on shell script chupacabra Programming 2 10-22-2002 12:11 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration