Gigi Labs

Please follow Gigi Labs for the latest articles.
Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Friday, May 2, 2014

C# Security: Computing File Hashes

Hello again! :)

We're celebrating! :D Today, Programmer's Ranch turned one year old, and although I've turned most of my attention to an interesting spare-time project for the time being, I wanted to mark this occasion with a new article. And some cake.



Right, and today's article is about hashing. We've seen in "C# Security: Securing Passwords by Salting and Hashing" that a hash function transforms an input string into a totally different piece of data (a hash):


If you make even a slight change to the input, such as changing the first character from uppercase to lowercase, you get a totally different output:


Also, if you use a decent hash function (i.e. not MD5), it is normally not possible to get the input string from the hash.

In today's article, we're going to use hashes for something much simpler than securing passwords. We're going to hash the content of files, and then use that hash to check whether the file changed. Since I haven't been very impressed with SharpDevelop 5 Beta, I'm going to ditch it and use Visual Studio 2013 instead. You can use whatever you like - SharpDevelop, Visual Studio Express for Desktop, or maybe even MonoDevelop.

Create a new Console Application, and add the following at the top:

using System.Security.Cryptography;

This will allow you to use a variety of hash functions, which all derive from the HashAlgorithm class.

We'll also need a little helper function to convert our hashes from a byte array to a string, so that they may be displayed in hex in the command line. We'll use the following, which is a modified version of the Hash() method from "C# Security: Securing Passwords by Salting and Hashing":

        public static string ToHexString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
            sb.Append(b.ToString("x2").ToLower());

            return sb.ToString();
        }

Now, let's create a text file in the same folder as our .sln file and name it "test.txt", and put the following lyrics from the Eagles' "Hotel California" in it:

So I called up the Captain,
"Please bring me my wine"
He said, "We haven't had that spirit here since nineteen sixty nine"
And still those voices are calling from far away,
Wake you up in the middle of the night
Just to hear them say...

Let's read that file into memory. First, we need to add the following:

using System.IO;

We can now read the contents of the file into a string:

            string fileContents = File.ReadAllText(@"../../../test.txt");

...and quite easily compute the hash of those contents:

            using (HashAlgorithm hashAlgorithm = SHA256.Create())
            {
                byte[] plainText = Encoding.UTF8.GetBytes(fileContents);
                byte[] hash = hashAlgorithm.ComputeHash(plainText);
                Console.WriteLine(ToHexString(hash));
            }

            Console.ReadLine();

Note that I'm using SHA256 as the hash function this time - it's a lot more robust than MD5. If you check the documentation for the HashAlgorithm class, you can find a bunch of different hash algorithms you can use. As it is, we get the following output:


Now, let's see what happens if your little toddler manages to climb onto your keyboard and modify the file. Let's remove the first character in the file (the initial "S") - that might be within a toddler's ability - and save the file. When we rerun the program, the output is quite different:


And here we have already seen how hashing gives us a mean to verify a file's integrity, or in other words, check whether it has been tampered with. In fact, popular Linux distributions such as Ubuntu distribute MD5 hashes for the files they release, so that the people who can download them can check that they are really downloading the file they wanted, and not some weird video of goats yelling like humans:


So let's actually see this in action. After downloading an Ubuntu distribution, let's change the filename to that of the Ubuntu file we downloaded, and the hash algorithm to MD5:

            string fileContents = File.ReadAllText(@"../../../../ubuntu-14.04-desktop-amd64.iso");

            using (HashAlgorithm hashAlgorithm = MD5.Create())

Now, let's try to compute a hash of the Ubuntu file:


Oops! We tried to read a ~1GB file into memory, and that's a pretty stupid thing to do. Unless you've got a pretty awesome computer, you'll see the memory usage spike until you get an OutOfMemoryException, as above. And even if you do have a pretty awesome computer, you shouldn't load an entire massive file just to perform an operation on its contents.

In one of my first articles here, "C#: Working with Streams", I explained how you could read a file bit by bit (e.g. line by line) and work on those parts without having to have the entire file in memory at any one time. And quite conveniently, the hash algorithms have a variant of the ComputeHash() method that takes a stream as a parameter.

So let's change our code as follows:

        static void Main(string[] args)
        {
            using (FileStream fs = File.OpenRead(@"../../../../ubuntu-14.04-desktop-amd64.iso"))
            using (HashAlgorithm hashAlgorithm = MD5.Create())
            {
                byte[] hash = hashAlgorithm.ComputeHash(fs);
                Console.WriteLine(ToHexString(hash));
            }

Console.ReadLine();
        }

And let's run it:


There are a few things to note from the output:
  • It computes pretty quickly, despite the fact that it's going through a ~1GB file.
  • Memory levels remain at a pretty decent level (in fact the memory used by the program is negligible).
  • The output matches the first hash in the list of hashes on the Ubuntu webpage (in the background of the above screenshot).
Wonderful! :) In this first anniversary article, we revisited the concept of hashing, and learned the following:
  • There are several different hash algorithms provided by .NET that you can use, including MD5, SHA256, and others.
  • A hash gives you a way to verify whether a file has been tampered with.
  • Streaming provides the ability to process large files quickly and with very little memory overhead.

Thank you so much for reading, and please check back for more interesting articles here at Programmer's Ranch! :)

Sunday, March 16, 2014

C: Rock, Scissors, Paper (using random numbers)

Hello everyone, and welcome back to the Ranch! :)

In my last C article, "C: Calculating the Average of Several Numbers", we learned how to work with numbers in C. This included using integer variables, input/output of integers, and arithmetic operations.

Today, we're going to take this further and learn how to generate random numbers in C. We'll use this to write a little Rock, Scissors, Paper game.

Pseudo-random numbers in C


So let's start off with a pretty empty program:

#include <stdio.h>

int main(int argc, char ** argv)
{
    return 0;
}

We can generate a random integer using the rand() function, which is defined in stdlib.h. This gives us a random number between 0 and a constant integer called RAND_MAX. We can output the value of RAND_MAX as we would any other integer, although its value is not guaranteed to be the same across different computers:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
    int a = rand();
    int b = rand();
    int c = rand();

    printf("RAND_MAX is: %d\n", RAND_MAX);
    printf("a is: %d\n", a);
    printf("b is: %d\n", b);
    printf("c is: %d\n", c);

    return 0;
}

Let us now run this program a few times and see what happens:


On my computer, RAND_MAX is defined as 2147483647, so each time I call rand(), it will give me a random number between 0 and 2147483647.

Another thing you'll see from the output is that each time I run the program, I get the same values for a, b and c. That isn't quite random, is it?

The reason why this is happening is that these random numbers aren't really random.  They are actually generated from a mathematical sequence, that takes a very, very long time to begin repeating itself, but which isn't truly random. These are called pseudo-random numbers. Imagine this sequence:

4, 29, 55, 12, 4, 7, 3, 97, 84, ...

This sequence seems pretty random; there isn't any pattern in it. So each time we get a random number with rand(), we'll get a different number: first 4, then 29, then 55, etc.

Our problem is that each time we run the program, we're starting from the beginning of the sequence, and this gives us the same values. So what we want to do is start from a different position each time.

We do this by seeding the pseudo-random number generator using the srand() function. Let's just add this line at the beginning of main():

    srand(100);

Let's run the program again:


You'll notice the numbers have changed, since we're using a different seed. However, each time we run the program, the same numbers are repeated. In fact we are still making the same mistake as before: we are using the same seed, and starting the pseudo-random number sequence from the same point each time.

In order to use a different seed each time we run the program, we can use the current time as the seed. We can use the time() function, defined in time.h, to give us the current time. So let's include that header file at the top...

#include <time.h>

...and replace our call to srand() with this:

srand(time(NULL));

When we run the program, things work as we expect, and we get different pseudorandom numbers each time we run:


Scaling random numbers


Great. Now, the numbers we are generating are a little bit big. For example, for Rock, Scissors, Paper, we don't need a number between 0 and 2147483647; all we need is a number between 1 and 3 (both inclusive), to represent either rock, scissors, or paper. We can limit the range of a pseudorandom number by dividing it by the maximum we want, and extracting the remainder. This is done using the modulus (%) operator, which gives us the remainder of a division:

    int a = rand() % 10;
    int b = rand() % 10;
    int c = rand() % 10;

Let's say rand() gives us 55. If we divide 55 by 10, then the remainder can be at most 9. So what we're doing above is limiting each random number to the range 0 to 9 (both inclusive). We'll need to add 1 to the number by which we're dividing, in order to make it inclusive (i.e. between 0 and 10 in this case).

What if we need a minimum value that is not zero? In short, we declare a general-purpose function that returns random integers between two integers (both inclusive) like this:

// returns a random number between min and max, both inclusive)
int randomInt(int min, int max)
{
    return min + (rand() % (max - min + 1));
}

We can then use this function from within our main() like this, to give us random numbers between 1 and 10:

    int a = randomInt(1, 10);
    int b = randomInt(1, 10);
    int c = randomInt(1, 10);

And voilà:


Rock, Scissors, Paper in C


OK, so now that we know how to generate pseudorandom numbers, we can write out game of Rock, Scissors, Paper. In this game, two people independently choose one of rock, scissors or paper, and state their choice at the same time. The outcome is then as follows:

  • Rock beats scissors (breaks it).
  • Scissors beats paper (cuts it).
  • Paper beats rock (wraps it).

To start off, let's throw away all our code in main(), but keep our randomInt() function:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// returns a random number between min and max, both inclusive)
int randomInt(int min, int max)
{
    return min + (rand() % (max - min + 1));
}

int main(int argc, char ** argv)
{
    // code goes here

    return 0;
}

First, we add code to accept user input:

    int input = -1;

    while (input != 0)
    {
        printf("Choose...\n");
        printf("   1 for rock\n");
        printf("   2 for scissors\n");
        printf("   3 for paper\n");
        printf("or 0 to exit\n");

        scanf("%d", &input);
    }

The user will choose a number between 1 and 3 to represent rock, scissors, or paper respectively. A while loop repeats the game until the user enters 0 to exit the game. We have already used for loops in C: Calculating the Average of Several Numbers, but a while loop will simply keep executing the code within curly brackets as long as the condition within round brackets is true, in this case as long as input is not zero:


Once the human player has chosen, the computer player makes his choice at random:

       int computerChoice = randomInt(1, 3);

We can then display the computer's choice:

       switch(computerChoice)
       {
           case 1: printf("Computer chooses rock\n"); break;
           case 2: printf("Computer chooses scissors\n"); break;
           case 3: printf("Computer chooses paper\n"); break;
       }

A switch statement allows us to specify separate code to be executed depending on the value of a variable. In this case, if computerChoice is 1, we write that the computer has chosen rock, and similarly for the other choices. Note that each case in a switch statement should always end in a break statement; otherwise unexpected things may happen. :)

Finally, we can add the game logic to check who won:

       if (input == computerChoice)
       {
           printf("Game ends in a draw.\n");
       }
       else if (input == 1 && computerChoice == 2)
       {
          printf("Rock beats scissors! You win!\n");
       }
       else if (input == 2 && computerChoice == 3)
       {
          printf("Scissors beats paper! You win!\n");
       }
       else if (input == 3 && computerChoice == 1)
       {
          printf("Paper beats rock! You win!\n");
       }
       else if (computerChoice == 1 && input == 2)
       {
          printf("Rock beats scissors! Computer wins!\n");
       }
       else if (computerChoice == 2 && input == 3)
       {
          printf("Scissors beats paper! Computer wins!\n");
       }
       else if (computerChoice == 3 && input == 1)
       {
          printf("Paper beats rock! Computer wins!\n");
       }

Using a cascade of if statements, we compare the user input with the computer's random choice and see who won. The == operator allows us to compare two integers and determine whether they are equal. The && operator, on the other hand, is a boolean AND operator. If both conditions around the && operator are true, then the printf() statement within the curly brackets of that if statement is executed.

We can now actually play our game:


Summary


Great! :) We have a fully working Rock, Scissors, Paper game here. And in creating it, we learned about pseudo-random numbers, while loops, if statements, switch statements, and also a few operators including == (is equal to), != (is not equal to) and && (boolean AND). Below is the full code.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// returns a random number between min and max, both inclusive)
int randomInt(int min, int max)
{
    return min + (rand() % (max - min + 1));
}

int main(int argc, char ** argv)
{
    int input = -1;

    while (input != 0)
    {
        printf("Choose...\n");
        printf("   1 for rock\n");
        printf("   2 for scissors\n");
        printf("   3 for paper\n");
        printf("or 0 to exit\n");

        scanf("%d", &input);

       int computerChoice = randomInt(1, 3);

       switch(computerChoice)
       {
           case 1: printf("Computer chooses rock\n"); break;
           case 2: printf("Computer chooses scissors\n"); break;
           case 3: printf("Computer chooses paper\n"); break;
       }

       if (input == computerChoice)
       {
           printf("Game ends in a draw.\n");
       }
       else if (input == 1 && computerChoice == 2)
       {
          printf("Rock beats scissors! You win!\n");
       }
       else if (input == 2 && computerChoice == 3)
       {
          printf("Scissors beats paper! You win!\n");
       }
       else if (input == 3 && computerChoice == 1)
       {
          printf("Paper beats rock! You win!\n");
       }
       else if (computerChoice == 1 && input == 2)
       {
          printf("Rock beats scissors! Computer wins!\n");
       }
       else if (computerChoice == 2 && input == 3)
       {
          printf("Scissors beats paper! Computer wins!\n");
       }
       else if (computerChoice == 3 && input == 1)
       {
          printf("Paper beats rock! Computer wins!\n");
       }
    }

    return 0;
}

Saturday, November 9, 2013

C: Calculating the Average of Several Numbers

Hello everyone! :)

In my article "C: Hello World on Linux", we saw how to write a very simple C program, and how to compile and run it on Linux. In this article, we'll learn how to work with numbers in C, and also how to accept input and print output. I like to use Linux when writing or teaching C/C++, but you can just as well follow along if you're on Windows (e.g. by using Microsoft Visual C++). If you're going to use Linux, you can use your distribution's text editor, or else something like vi (check out my articles, "Linux: vi Essentials" and "Linux: Navigating Text with vi", if you feel you need a refresher on vi).

So, we'll begin with the following basic template:

#include <stdio.h>

int main(int argc, char ** argv)
{
    return 0;
}

Including the stdio.h header file gives us access to the input/output (I/O) functions that we'll need. The first of these is printf(), which we've already seen in my original article, "C: Hello World on Linux".

In our program, we're first going to ask the user how many numbers he's going to enter. Let's say he enters 5. In that case, we'll ask him for five numbers, and then add them up, and then compute the average.

Before the return statement, add the following:

    printf("How many numbers do you want to average?\n");

The \n translates to a newline.

We then declare a variable called count, of type int (integer), so that we can store how many numbers the user needs to enter:

    int count = 0;

Just a note... in C/C++ it is important to always initialise your variables with a value. If you don't, they will contain random garbage data, and using them before they're assigned might lead to unpredictable results.

Now, we can then use the scanf() function to accept input, as follows:

    scanf("%d", &count);

This looks pretty scary, doesn't it? The first parameter is a format string; it indicates what kind of data you're trying to accept. In this case, %d means we are expecting an integer. The second parameter indicates the variable where we'll store the input. By preceding the count variable with the & symbol, we're telling scanf() that the input should go to the location in memory where count resides. This might sound a bit weird until we talk about pointers and references (because it is), but just remember to include that & when accepting input, and you'll be fine.

Next, we need a few new variables:

    int i = 0;
    int number = 0;
    int total = 0;

We'll accept each number into the number variable, after which we'll add it to total. The i variable will be used to track how many numbers have been accepted, so that we know when to stop.

Next, we can write a for loop to repeatedly accept numbers as input, until we have reached the number of numbers we were expecting:

    for (i = 0; i < count; i++)
    {

    }

The code inside the curly brackets (which we will write shortly) will execute as many times as the count variable is set to. If count is 5, then it will execute 5 times. If you look at the first line of the for loop, we're starting with i set to zero, and we're telling it to continue executing as long as i is less than count. Each time the loop's code executes, i is increased by 1 (that's the i++ bit), until the i < count condition becomes true and the loop ends.

Sidenote: if you're coming from some other language, you might be wondering why I didn't declare i in the for loop. That's because some versions of C don't allow it (although it's fine in C++), so you may end up with an error like this:

error: ‘for’ loop initial declarations are only allowed in C99 mode

Let's add some code inside the for loop to accept numbers as input:

        printf("Enter number %d of %d: ", i, count);
        scanf("%d", &number);
        total = total + number;

In the first line, we can see the use of the %d (integer) placeholder within printf(). When you run the program, the placeholders are replaced with the other parameters, i.e. the first %d is replaced with the value of i, and the second one is replaced with the value of count.

In the second line, we're accepting a number as input, and storing it in the number variable (remember the &). Finally, in the third line, we add number to total. There's actually a more concise and efficient way of adding a value to a variable, which is the following:

        total += number;

After the end of the for loop, we can now calculate the average by dividing total by the number of items (count):

    float average = total / count;
    printf("Average: %f\n", average);

The float data type allows us to store numbers that are not integers (whole numbers), and which might have some decimal value. When using floats with printf(), we need to use the %f placeholder, instead of the %d that is used for integers.

Here's the code we have so far, with syntax highlighting courtesy of vim:


From the Linux terminal, the following command will compile your code to an executable called average:

gcc average.c -o average

...and the following command executes it:

./average

Let's test the program to see that it works correctly:


Hmmm, wait just a minute. 1 + 2 + 4 gives us 7, and divided by 3, that doesn't give us exactly 2 (it's actually 2.333333.....). The problem is in the following line of code:

    float average = total / count;

We're dividing an integer by another integer, and that gives us back an integer - losing any digits after the decimal point. To get this to work correctly, we can convert count to a float as follows:

    float average = total / (float) count;

This is called type casting: you put a data type in brackets before a variable to indicate that it should be interpreted as the specified data type. When we recompile and run the program, we can see that it now behaves as we originally expected:


Nice! :)

This article showed how to write a simple program - one that calculates the average of a set of numbers - in C. This simple program revealed several features of the C language, including input/output (via printf() and scanf()), placeholders for format strings (%d and %f), int and float variables, for loops, and type casting.

I hope you found this interesting. Stick around for more articles! :)

Wednesday, September 18, 2013

Linux: Navigating Text with vi

Howdy folks! :)

In the last article here, "Linux: vi Essentials", we looked at several of the vi text editor's basic commands. Today we'll take a look at a few more, and we'll focus on commands that allow you to quickly jump across portions of text. You will begin to notice patterns in how vi commands are structured, and this will allow you to harness its power and use it very efficiently.

Let's start off with some text that we can work with. First, open a Linux terminal and fire up vi with the following command:

vi test.txt

Enter insert mode using the i command, and press Shift+Insert to paste the following lyrics from Queen's song Friends Will Be Friends:

It's not easy love, but you've got friends you can trust,
Friends will be friends,
When you're in need of love they give you care and attention,
Friends will be friends,
When you're through with life and all hope is lost,
Hold out your hand cos friends will be friends right till the end.

Press ESC to go back into command mode. Move the cursor to the second line, and alternate between pressing e and b. You'll see that e takes you to the end of the word (the 's' in 'Friends'), and b takes you back to the beginning of the word (the 'F' in 'Friends'). Next, try pressing 'w' repeatedly - you'll notice you will move to the next word each time (first to the 'w' in 'will', then to the 'b' in 'be', and so on). This will continue to take you to the next word even if it is on another line.

If you've read "Linux: vi Essentials", you have already seen the 'w' command in use - as part of the 'cw' (change word) command. As a matter of fact, such commands are really made up of several parts. The first part is the command itself, such as c (change) or d (delete). The second part tells it until where it should work, so cw actually means "change from where the cursor is until the next word". You can achieve a similar effect with other commands - dw will delete characters until the next word.

The ^ command will take you to the beginning of the line the cursor is on, and the $ command will take you to the end of the line. Once again, you can combine these with other commands. c$ will change from the current cursor position until the end of the line. y^ will copy (for later pasting) from the current cursor position back to the beginning of the line.

These multi-character commands also have some other interesting patterns. If you re-type the command rather than specifying where it should end, it will affect the entire line you are on (no matter where in the line the cursor is) - we have already seen this with dd and yy. The same goes for cc. If you put a number between or before the command (as we have seen in the last article, d5d and 5dd both delete 5 lines), you affect a number of lines. If you type the command as a single uppercase character (e.g. D), you affect from the current cursor position until the end of the line - so C is actually equivalent to c$.

Other handy navigation commands allow you to run around the entire file or jump to specific line numbers. The gg command will take you to the beginning of the first line in the file, and the G command will take you to the beginning of the last line in the file. A handy way to clear an entire file is to first go to the beginning of the file (gg) and then delete until the end of the file (dG).

You can jump to a particular line number using :n, where n is the line number you want (e.g. :1 is equivalent to gg). The line number you're on is indicated near the bottom-right of the editor together with the column. If you want to show line numbers, you can use the special command :set nu, but the lines will take a portion of your editing space. Use :set nonu to turn off line numbers.


There are a bunch of navigation commands that you can check at the Vi Cheat Sheet if you really want to go deep. I think the ones I mentioned above are more than enough to get you rolling. I'll just mention one more thing you'll definitely find useful.

Use a forward slash (/) followed by a word to search for that word (e.g. /hand). vi is very powerful (I keep repeating that) and supports regular expressions as well as search and replace, but those are beyond the scope of this article. You can read about them at the Vi Cheat Sheet or elsewhere on the internet.

Nice! I hope this helps you in your day-to-day vi operations, and check back for more articles! :)

Update 2013.11.28: if you want to learn some of the more advanced things you can do with vi, the answer to this question is a really fantastic overview.

Sunday, September 15, 2013

Linux: vi Essentials

Hi people! :)

I've already briefly discussed vi in my article "C: Hello World on Linux". vi is a text editor, and a pretty versatile one at that. It takes a bit of time to learn because there are so many commands you can use, but once you learn it, you can do things really fast.

Learning to use a text editor like vi on Linux is important because you can use it for many things, from editing configuration files to writing code. Even better, make sure you have vim (vi improved) which brings syntax highlighting and better keyboard navigation (and probably more):


Now, there are many tutorials and references on vi, including this great cheat sheet. However, instead of teaching you every vi command, I'm going to go through the most important commands in a practical manner, in the usual spirit of Programmer's Ranch.

So let's start off by opening a file called test.txt:

vi test.txt

Note that the file does not actually exist until you save it. You can save it at any time using the :w command. You can save and quit using the :wq command. You can try to quit only with the :q command, but if there are pending changes, you'll get a warning (as below). In that case, you can quit and discard your changes with the q! command.



Basically, in vi you can be either in insert mode (in which you're actually writing stuff in the file) or in command mode (in which you write commands, such as those I just mentioned, and press ENTER to execute a variety of operations. You start off in command mode, and can switch to insert mode using various commands (which I'll mention in a minute). You can go back into command mode by pressing the ESC key.

To try out a bunch of commands, go into insert mode (press the 'i' key) and paste the following lyrics from The Eagles' song Lyin' Eyes:

City girls just seem to find out early
How to open doors with just a smile
A rich old man
And she won't have to worry
She'll dress up all in lace and go in style

You can move the cursor around the text using the arrow keys, whichever mode you're in. If you're in command mode, you can also use the h, j, k and l keys to move left, down, up or right (these are especially important if you have some old version of vi that doesn't support the arrow keys.

The basic keys to go into insertion mode are 'i' and 'a', meaning 'insert' and 'append' respectively. Try them both by moving your cursor over the 'r' in 'rich' while in command mode, and pressing the respective key. You'll find that 'i' goes into insert mode right before the 'r', while 'a' goes into insert mode starting right after it.

To copy and paste a line of text, go on that line and press yy. Then, move to another line and press p to paste the line of text right after it:



You can use dd to delete an entire line, or D to delete from the cursor until the end of the line. Try that by pressing dd while you are on the last line, and pressing D (Shift+d) while the cursor is at the beginning of the word "lace".

You can delete single characters by pressing 'x'. One of the great features about vi is that you can instruct it to repeat commands for a number of times. So, the command 5x will delete 5 characters. This also goes for other commands, for example you can delete 5 lines by using either d5d or 5dd.

The 'u' key will allow you to undo your last command - great if you accidentally deleted something. The '.' key, on the other hand, will allow you to repeat a text edit - but not in the way you're used to it. What it does is remember what your last command was, and reapply it at the cursor's current position. So if your last command was 'x', pressing '.' will delete the character at the cursor position.

At this point we've covered the very basics, and you can already get to work with these few commands. However, I'd like to add a few other commands that I've found particularly great when using vi on the job. Start over with a fresh copy of the lyrics at the beginning of this article.

Let's say we want to replace the word "lace" with the word "red". There are many ways we can do this (e.g. 4x on "lace", i for insert mode, and then type "red). A faster way is to hit cw (change word) while the cursor is on the beginning of "lace". That deletes "lace" and goes into insert mode automatically, so then you just have to type "red":



Another great command is 'o'. This will create a new line below the one your cursor is on, move the cursor to the beginning of it, and switch to insert mode. A variant is 'O' (Shift+o), which starts a new line directly above the one your cursor is on.

As an exercise in these extra commands, start off with this text:

select *
from yourtable
where 1=1

and find a quick way to change it to:

select *
from mytable
where 1=1
and 2=2

Great! :) I hope you find this useful, and don't forget to like the Programmer's Ranch Facebook page to stay up to date with articles as they are posted.

Wednesday, August 7, 2013

C: Hello World on Linux

Hello people!

Today I'm going to describe how you go about writing, compiling and executing C code using the Linux command line (also known as shell or terminal).

You will first need to make sure you have the tools necessary to compile C code. In particular you need gcc. You can get it by installing the build-essential package. On a Debian-based Linux distribution such as Ubuntu, you'd use the following command in the terminal:

sudo apt-get install build-essential

...and then, after entering the root password (if necessary), you proceed with the installation:


To actually write the C code, you can use the vi editor. Even better, you can make sure that you have the vim (vi improved) by using the following command:

sudo apt-get install vim

Then use the following command to edit a file called hello.c (it doesn't need to exist yet):

vi hello.c

This opens the vi editor. vi is very powerful but takes a little getting used to. You start off in command mode, and just about any letter you type has a particular meaning. If you press the 'I' key, you go into insert mode, and can type code to your heart's content. Do that, and enter the following code:

#include <stdio.h>

int main(int argc, char ** argv)
{
    printf("Hello world!\n");

    return 0;
}

Over here we're first including the stdio.h library, which allows us to do input/output (I/O) such as outputting text to the terminal. The actual code goes into the main() function, which returns an integer. You can ignore the argc and argv bits, and just use this as a template for your code, for the time being. We actually write something to the terminal using the printf() function. The only thing that might seem a little special here is the \n thing - it's an escape sequence that outputs a newline.


Once you're done typing the above, press ESC in vi to go back to command mode. Then type :wq and press enter - this saves the file (w) and quits (q).

Back in the terminal, type the following command to compile the code in hello.c:

gcc hello.c -o hello

The -o part means that the next argument (in this case "hello") is the name of the executable to be produced by gcc. On Linux, executables don't need to have extensions such as .exe, although it's perfectly fine to include them to make it easier to recognise them.


Finally, type ./hello and press ENTER to run the program. You should see "Hello world!" as the output, as above.

Note: just in case you can't run the program because of some permissions, try the following command:

chmod 777 hello

Great! This simple tutorial showed how to write C code using the vi editor, compile it using the gcc program, and execute it from within a Linux shell. Come back for more tutorials!

Wednesday, May 22, 2013

C#: Programming on Linux with MonoDevelop

Hello everyone! :)

C# is a language invented by Microsoft, and is normally used to create applications on Windows, using Visual Studio or, less commonly, SharpDevelop.

But as a matter of fact, it is also possible to write C# applications on Linux! There is a development environment similar to SharpDevelop, called MonoDevelop, which supports Linux development.

On a Debian-based system such as Kubuntu, you would open a terminal and install MonoDevelop using the following command:

sudo apt-get install monodevelop

If you have another flavour of Linux (e.g. based on Red Hat), you will need to use the appropriate package manager instead (e.g. rpm).

You can now launch MonoDevelop either by typing monodevelop in the terminal and pressing ENTER, or by finding it among your particular Linux distribution's applications (in the case of Kubuntu, via the K-menu).



From here, click Start New Solution... (see above).


Select "Console Project" from under C#. As with Visual Studio or SharpDevelop, you need to specify a name for the solution, and where you would like it to be created.


The next screen (above) is a bit weird. Just skip it by pressing the "OK" button.


You have now created your little console project, and MonoDevelop gives you sample code for a typical "Hello world" program. Press F5 to try and run it...


Well, that's not quite what we were expecting, right? We have a System.InvalidOperationException saying that "File name has not been set", but we have no clue how to fix it. Fortunately, though, someone has already bumped into this before, and the solution is simply to install xterm:

sudo apt-get install xterm

Once you install xterm, restart MonoDevelop. You can now debug your program to your heart's content:


Woohoo! :) This brief article showed you how to set up MonoDevelop on a Linux environment, and compile a basic application. You should now be able to go through just about any article at Programmer's Ranch so far using MonoDevelop if you like.

Note, however, that MonoDevelop can't do everything. For example, at the time of writing, it is simply not possible to get any WPF application working on Linux. However, MonoDevelop has some interesting alternatives such as Gtk# which I have yet to explore.

Happy coding, and stay tuned for more tutorials! :)

Tuesday, May 7, 2013

C#: Creating a grep-like tool using Files, Strings and Loops

Hello again! :)

Today we're going to learn to read files on the hard disk, and also some other interesting stuff that everybody obviously dreams of learning at some point in their lives, including loops and arrays.

Start a new SharpDevelop project, and right under the using System;, put in the following:

using System.IO;

What we're doing here is enabling the use of stuff related to input and output (I/O) in our program - in our case, files fall in that category.

When you learn C#, you don't just learn the language. There exist tons of functionality within this thing called the .NET Framework. System.IO is but one small part of this framework.

Great... two sentences of philosophy are enough. Let's get our hands dirty. Create a file called chat.txt anywhere you like, and put the following text in it:


Me: Hi!
John: Hello Daniel!
Me: So, uh... what's up?
John: Nothing much.
Me: Well, in my case, I ate loads of pastizzi yesterday, and I feel I might explode.
John: Great, you might take down the Luqa monument in doing so.
Me: Thanks, I'm flattered by your concern.
John: What are pastizzi anyway?
Me: The Maltese fast food. Very good, very unhealthy.


Now, drag that file into the Project panel on the right hand side of SharpDevelop. With the file selected in SharpDevelop, press F4 to bring up the Properties window, and set "Copy to Output Directory" to "Always". The next time you press F5, chat.txt will be copied to the same directory as the executable.



All we need to do to read a file is the following:

            String fileContents = File.ReadAllText("chat.txt");
            Console.WriteLine(fileContents);

We are reading the entire file into a String object and then writing it to the console window. This is what the result looks like:


That's easy. Let's do something a bit more advanced. Scrap the code you have so far, and instead, let's do this:

             String[] lines = File.ReadAllLines("chat.txt");

What we're doing here is reading each line into an array of Strings, which is what String[] means. The lines variable is not just a simple String; you can think of it as something like this:


An array is a sort of list, but it has a fixed size, and each item in the list is numbered, starting at zero. If you read yesterday's article, C# Basics: Working with Strings, this will sound familiar. In fact, a String is internally made up of an array of characters! We can access each item in this array just like how we accessed individual characters within a String:

             String firstLine = lines[0];

The example above gives you the first line, "Me: Hi!".

We can iterate over the array using a foreach statement:

            String[] lines = File.ReadAllLines("chat.txt");
       
            foreach (String line in lines)
            {
                Console.WriteLine(line);
            }
       
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);

This effectively gives us the same thing as the first example in this article, because we're going over each line in the lines array, storing it in a variable called line, and writing it to the console window:


Now, we can make this more interesting by writing only those lines that contain the word "pastizzi". The Contains() method, available to all Strings, allows us to check if a String contains another String. This is the complete code:

            String[] lines = File.ReadAllLines("chat.txt");
       
            foreach (String line in lines)
            {
                if (line.Contains("pastizzi"))
                {
                    Console.WriteLine(line);
                }
            }
       
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);

...and the output, as expected:


This simple functionality is useful when you want to find some particular text in a large file (searching for a needle in a haystack). In fact, if you've used Linux, you'll know that the grep tool does exactly that:


Excellent! After just a few articles, we're already able to create useful tools with a minimal amount of code. But don't go away... things are only going to get better!