Archive for 2008

VirtualBox: access Windows-host shared folders from Ubuntu-guest

Wednesday, April 9th, 2008
This is the scenario that you run Windows as your host operating system and Ubuntu in a VirtualBox, and that you want to access a specific Windows folder from Ubuntu.

First you have to make sure that have install Guest Additions. From the VirtualBox's menu go to Devices → Install Guest Additions... This will mount a virtual CD on your /media/cdrom. Normally this folder's window will show up. As root run the program VBoxLinuxAdditions.run. When the program completes reboot your VirtualBox.

With Guest Additions installed you may now go ahead and define the shared folder(s). From the VirtualBox's menu go to Devices → Shared Folders. A dialog will show up. In this dialog you can specify which folder from your Windows system you want to share with your Ubuntu. Press the button with the + symbol to add a new shared folder in the list. You will have to specify a Folder Name for each folder you add. Make sure you memorize that name because you will need it very soon.

When done with you shared folder(s) specification, you may now go ahead and actually mount these folders from Ubuntu. First you have to create a mounpoint, that is, a directory in your Ubuntu which will reflect the shared folder from Windows:

# sudo mkdir /media/windows-share

Of course you may choose an alternative path for your mountpoint. With your mountpoint created you can now mount the shared folder, like this:

# sudo mount -t vboxsf folder-name /media/windows-share

Where folder-name will be the name you assigned for this folder when you were adding it in the shared folders list.

You could use the /etc/init.d/rc.local script to execute these commands on startup to have the shared folders automatically mounted every time you start your Ubuntu VirtualBox.

JSP: how to read unicode from forms

Sunday, April 6th, 2008
Are you having encoding problems while trying to read multilingual data from a JSP form? That is because JSP by default is trying to interpret your input as ISO-8859-1. That's fine unless you have a form that sendsUTF-8.

The problem can be eliminated by simply adding the following line of before you do any calls to getParameter():

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
...
request.setCharacterEncoding("utf-8");
...
}


The Bic and Bac song (video + lyrics)

Wednesday, March 19th, 2008
This is the video of Bic & Bac from Les Mondes Engloutis, the best cartoon ever, together with the lyrics of the song (in french!) for you to sing aloud! I wish I knew what they mean, it's all french to me :)





C'est la gigue de Bic et Bac
Bac et Bic, c'est logebic
La petite gigue du flashbic
Tralala-lala-la !

Dans la peine ou le grand bourdon
Le Flashbic, nous dansons
Dans la grande gigue d'Arkadia
Venez avec moi !

Nous sommes deux Pangolins
Qui se carapatent
Nous sommes deux gros malins
Donnons-nous la main…

Vers Arkadia, marchons sous le vent la pluie
Tout là-bas, nous sortirons de la nuit
Et le Shagma, pour nous tous il revivra
Tourneboule et tourneboule la gigue de la vie

Pour Arkadia, nous danserons tous unis
Tout là-bas, quand brillera le Shagma
Et c'est ainsi que giguent tous nos amis
Dansez, chantez, chantez, dansez la gigue de la vie

C'est la guigne pour Bic et Bac
Bac et Bic c'est tragique
Dansons la gigue du Flashbic
Tralala-lala-la !

Nostalgique ou bien grognon
Le Flashbic, nous dansons
Dans la petite ronde du Shagma
Entrez avec moi !

Nous sommes deux Pangolins
Qui se décarcassent
N'ayons plus peur de rien
Donnons-nous la main…

Vers Arkadia, marchons sous le vent la pluie
Tout là-bas, nous sortirons de la nuit
Et le Shagma, pour nous tous il revivra
Tourneboule et tourneboule la gigue de la vie

Vers Arkadia, marchons dans les pains d'épis
Tout là-bas, on sauvera le Shagma
Et que la vie nous garde bien tous unis
Tourneboule et tourneboule la gigue de la vie

Pour Arkadia, nous danserons tous unis
Tout là-bas, quand brillera le Shagma
Et c'est ainsi que giguent tous nos amis
Dansez, chantez, chantez, dansez la gigue de la vie

scanf (and why you should avoid using it)

Thursday, February 7th, 2008
I believe scanf() is one of the most common pitfalls that most novice C programmers encounter. In our first steps with the C programming language we usually try to write some simple interactive programs, like a program that asks for our name ("What is your name?") and then sends some greetings in a more personalized fashion ("Hello, Giannis!"), or even some simple game.

In most books, students are encouraged to use the scanf() function as the standard method to read the user's input. And it really does a good job, as long as the input given by the user is what was expected by the author of the program. Or else, things may get a little tricky especially for the newcomer. Here is an example, a simple program that prompts the user to enter a number, and then it print out whether it was a even or an odd number. Here, our hypothetical programmer intended to apply some sort of defensive programming, and check whether the user's input was recognized as a number. The program will keep on asking until a valid number is entered:


#include <stdio.h>

int main()
{
int number, result;

do
{
printf("Give me a number: ");
result = scanf("%d", &number );
if (result < 1)
printf("You didn't type a number!\n");
}
while (result < 1);

if (number % 2 == 1)
printf("%d is odd.\n", number);
else
printf("%d is even.\n", number);
}



At first sight it looks fine. If we try to run it we'll get what we expected:


giannis@giannis-desktop:~$ ./a.out
Give me a number: 133
133 is odd.
giannis@giannis-desktop:~$ ./a.out
Give me a number: 44
44 is even.



It looks okay! But what happens if the user enters some invalid input? Let's find out:


Give me a number: abc
You didn't type a number!
Give me a number: You didn't type a number!
Give me a number: You didn't type a number!
Give me a number: You didn't type a number!
...



And it goes like this until we terminate the program with Ctrl-C. Not quite what expected, eh?

What did go wrong here? Well, let's take a step-by-step course. At first, the user is prompted to enter a number. Then scanf is called to read an integer number from standard input. The program blocks and waits for the user to type something and press Enter. After that, scanf reads the first character from standard input, which is 'a', which is not a digit. As a result, scanf puts back the character and bails out. The error message is displayed and we start all over again to ask the user for new input. However, when scanf is called on second time, it doesn't block at all, because the input buffer already contains the previously entered data. It tries once more to parse the user's input, and it fails once again, and the loop infinitely continues for ever.

It turns out that the problem here is that if some invalid input is entered, it never leaves the input buffer. What we need here is to find a solution, so that the user input is always taken off from the input buffer, either when it is valid or not. The "proper" way to achieve this, is to first read a whole line of input into a temporary buffer using fgets(), and then parse the buffer using sscanf(), a close-cousin of scanf(). This is how it will look like:


...
do
{
printf("Give me a number: ");
char st[1024];
fgets( st, sizeof(st), stdin );
result = sscanf(st, "%d", &number );
if (result < 1)
printf("You didn't type a number!\n");
}
while (result < 1);
...



And now let's try to test the new improved version of the program:


giannis@giannis-desktop:~$ ./a.out
Give me a number: rfdasf
You didn't type a number!
Give me a number: fasfdsfgsd
You didn't type a number!
Give me a number: 25
25 is odd.



Now we got somewhere, didn't we? Another, less elegant, way to solve the same problem would be to still use scanf() but also meticulously take care to remove the invalid line of data from the buffer. In C-terms, that would be:


...
do
{
printf("Give me a number: ");
result = scanf("%d", &number );
if (result < 1)
{
printf("You didn't type a number!\n");
char st[1024];
fgets( st, sizeof(st), stdin );
}
}
while (result < 1);
...



But as I said before, I personally find this version kind of ugly, because, we are using fgets() only to read and ignore a whole line from the input buffer, which is something we can avoid if we use sscanf().

I believe most C programmers have been puzzled at their first steps, and I hope that these little headaches will not discourage new programmers from continueing their efforts. For these efforts will be greatly rewarded!

X11 development files in Ubuntu

Wednesday, February 6th, 2008
If you tried to compile a X11 application in Ubuntu you may get error messages that several X11 related header files were missing (such as Xlib.h). The files you need are contained in the libx11-dev package. Type this command to install it:

sudo apt-get install libx11-dev

and then try again to build the application.

How to get rid of the "Share-to-Web Upload Folder"

Wednesday, January 30th, 2008
If you have a Hewlett Packard ScanJet you probably have been annoyed by this stupid HP Share-to-Web Upload Folder that has been settled in your Desktop. There seems to be no obvious way to remove it or delete it, and if you try to right-click on it the Windows Explorer hangs and you have to restart it.

Here is what you have to do to send it from where it came:

Go to Start → Settings → Control Panel → Add or Remove Programs. After the list is populated find and highlight the program named HP Photo and Imaging. Then push the Change button. The InstallShield Wizard will show up. Click Next and then select Repair. Click Next again. In the next step make sure that you DON'T check the Add a Shortcut to the Desktop checkbox. Finally click Install. By the end of the procedure you should see the evil icon missing from your Desktop.

install developer's man pages on ubuntu

Monday, January 28th, 2008
To install the valuable man pages for development in your Ubuntu box, simply do:

sudo apt-get install manpages-dev

This will install the man pages that "describe the Linux programming interface". They include the Linux system calls (section 2) and library functions (section 3).