stdin = Standard Input (the information you give to a program while it's running). By default, this is connected to your keyboard.
stdout = Standard Output (the information you get from a program while it's running). By default, this is connected to the terminal you're using on-screen.
Typically, there's two ways of supplying a program with input. The first one is stdin. Here's an example of how stdin would be used by a program:
Code:
Please enter your username: (you type your username here and it goes into stdin)
Please enter your password: (again it goes into stdin)
There's one other way though of giving info to a program, and that's by giving it command Line arguments whereby you specify extra stuff at the command line before the program actually starts to run, an example would be:
Code:
iwconfig wlan0 essid MyNet key off
In this example, the program name is "iwconfig" and the command line arguments are "wlan0 essid MyNet key off".
Just to be clear from the very beginning: The arguments you supply at the command line do not go to stdin. stdin is a different thing altogether. stdin gets the input you give to a program after it has started running, for instance if the program asks you for your username and password, then the stuff you type in goes into stdin.
So if you're writing your own program that needs to take input from the user, you've got two options: Command line arguments or stdin. Of course you can do some fancy like write a GUI program, but we're gonna stick to the basics here.
Now on to output. You can write output to stdout, and by default it gets printed on the screen. In my original example above, the text "Please enter your username: " would get printed to the screen by sending the text to stdout.
Before I being with the following example, I have to be honest and say I've never had much of an interest in making big dictionary files for doing password cracking, but I do find it fun to write the programs that make the dictionary files (I think there's a diagnosis of Aspergers in there somewhere
).
Moving on...
The great thing about the Linux terminal is you can redirect stdin and stdout. The output of one program can become the input for another program.
For this example, I'm going to write two different programs to do with creating dictionary files. First of all though, I wanna start off with a small dictionary file as follows, here's "in.txt":
If we want this file to become the input to a program called "appnum", then we do the following at the terminal:
Code:
cat in.txt | ./appnum
This is referred to as "piping", we say that we're piping the output of the cat command into the appnum command (as if the text is going through a pipe between the two programs).
So now let's write a very small C program called "Append Numbers", its purpose is to append the numbers 0 through 9999 to every line of text it gets from stdin. This program does not take any command line arguments, and you know this because the "main" function has "void" as its parameter list. (If you want to take command line arguments you need to write the main function signature as follows:
int main(int argc, char **argv)).
So here's appnum.c:
Code:
#include <stdint.h> /* For types such as uint_fas8_t */
#include <stdio.h> /* printf */
#include <string.h> /* strlen */
void PrintCombos(char const *const str)
{
uint_fast16_t i = 0;
for (i = 0; i != 10000u; ++i)
{
printf("%s%u\n",str,i);
}
}
int main(void)
{
char line[62]; /* 59 for password from file
4 for the numbers we're gonna append,
1 for new line character,
1 for null terminator */
while ( fgets(line,sizeof line,stdin) )
{
uint_fast8_t const index_of_final_char = strlen(line) - 1;
/* If there's a new line at the end of the password, get rid of it */
if (line[index_of_final_char] == '\n')
{
line[index_of_final_char] = '\0';
}
PrintCombos(line);
}
return 0;
}
You can copy this code into a file called "appnum.c", and then compile it as follows:
Code:
gcc appnum.c -D NDEBUG -O3 -s -o appnum
You gotta remember, this program doesn't take any command line arguments, so you need to give it information via stdin after the program has started running. A handy way of supplying it with one line of text at the command line is as follows:
Code:
echo iliketowatch | ./appnum
Or you can shove a dictionary file into it as follows:
Code:
cat in.txt | ./appnum
As you can see if you run this, the output gets printed to the screen (because stdout gets sent to the screen by default). If you want it to go to a file instead, then do:
Code:
cat in.txt | ./appnum > out.txt
This is another form of piping, we use ">" to take the output of a program and create a text file from it.
You can use the Kate text viewer to check how the output file looks:
------------------------------------
To read more, scroll down to post #3 in this thread, I had to split it up coz I was having problems with posting.
----------------------------------------