Posted: . At: 9:57 PM. This was 6 years ago. Post ID: 12600
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Very useful C program. Print a random fortune.


Here is one of my programs. I wrote this ages ago and it uses the standard ASCII character set. This will print a random fortune by running the fortune app to get a fortune and this selects a category to print from.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
const char* x[] = {
	"limerick", "fortunes2", "linuxcookie",
	"freebsd-tips", "osfortune", "startrek",
	"zippy", "debian-hints"
};
const size_t FORTUNES = sizeof(x)/sizeof(*x) - 1;
int cool(int Size) {
	int k;
	k = 0;
	srand((unsigned)time(NULL));
	k = rand() % Size;
	return k;
}
int main()
{
	execlp("/usr/games/fortune", x[cool(FORTUNES)], "-l", NULL, NULL);
	return 0;
}

Compile it thusly.

4.4 Sat Oct 20 jason@Yog-Sothoth 0: $ gcc -Wall fortune.c -o fortune

Then run the executable to view a random fortune in your terminal. I know fortune is random anyway, but there is nothing wrong with writing a program to do this. It is an interesting programming exercise to use execlp or execve properly. They are very useful functions in a C program. More so than using system().

4.4 Sat Oct 20 jason@Yog-Sothoth 0: $ ./fortune 
"The stars are made of the same atoms as the earth."  I usually pick one small
topic like this to give a lecture on.  Poets say science takes away from the
beauty of the stars -- mere gobs of gas atoms.  Nothing is "mere."  I too can
see the stars on a desert night, and feel them.  But do I see less or more?
The vastness of the heavens stretches my imagination -- stuck on this carousel
my little eye can catch one-million-year-old light.  A vast pattern -- of which
I am a part -- perhaps my stuff was belched from some forgotten star, as one
is belching there.  Or see them with the greater eye of Palomar, rushing all
apart from some common starting point when they were perhaps all together.
What is the pattern, or the meaning, or the *why?*  It does not do harm to the
mystery to know a little about it.  For far more marvelous is the truth than
any artists of the past imagined!  Why do the poets of the present not speak
of it?  What men are poets who can speak of Jupiter if he were like a man, but
if he is an immense spinning sphere of methane and ammonia must be silent?
		-- Richard P. Feynman (1918-1988)

Another example.

4.4 Sat Oct 20 jason@Yog-Sothoth 0: $ ./fortune 
"I distrust a close-mouthed man.  He generally picks the wrong time to talk
and says the wrong things.  Talking's something you can't do judiciously,
unless you keep in practice.  Now, sir, we'll talk if you like.	I'll tell
you right out, I'm a man who likes talking to a man who likes to talk."
		-- Sidney Greenstreet, _The Maltese Falcon_

This line.

execlp("/usr/games/fortune", x[cool(FORTUNES)], "-l", NULL, NULL);

Is much better than using system(). This allows custom parameters to a program the C utility is calling.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.