[TriLUG] Random numbers, backticks, regex in C?

Owen Berry oberry at trilug.org
Tue Mar 13 12:25:18 EDT 2007


On Tue, Mar 13, 2007 at 11:37:50AM -0400, Steve Litt wrote:
> Hi all,
> 
> I've had 13 years paid experience in C, every one of them using DOS/Windows, 
> PDP-11 or VAX. As a result, there are certain UNIX things I don't know how to 
> do in C, and was wondering if any of you could enlighten me.
> 
> How do I generate random numbers in C? Is there a library interface 
> to /dev/urandom? What library files and header files do I use?

Read the man pages for random or srandom. Otherwise you can open
/dev/urandom directly using fopen and reading using getc, or whatever
file access method you normally would use. AFAIK you can also contribute
back to the system entropy by writing to /dev/urandom.

> How do I do the equivalent of a Perl or Bash backtick operator in C? I need to 
> capture the stdout output of the called program. I could do it the way I did 
> in DOS -- have the spawned program write to a file, and then read the file, 
> but that kinda turns my stomach now that I use Linux. What library files and 
> header files do I use?

Read the man page on popen. If you want to capture stderr and stdout
seperately things get a little more complicated, and you need to use
fork and 2 pipes. Let me know if you need to do that and I can provide
some more details.

> Does the gcc bundled with Linux have libraries for Regex, and if so, where can 
> I see an example?

I haven't done this myself, but try 'man 0p regex.h'. If you're using
C++ the Boost libraries seem to be included with g++ these days.
http://www.boost.org/libs/regex/doc/index.html

Shout (or email) if you have more questions.

Owen




More information about the TriLUG mailing list