curses library for gcc

November 15, 2008

For all those who type ‘sl’ instead of ‘ls’ often in the command window: Get the program ‘sl’

sudo apt-get install sl

From its man page:

NAME
sl – display animations aimed to correct users who accidentally enter sl instead of ls.

BUGS
It rarely shows contents of current directory.

When going through the source code for sl, I saw the use of the curses library. Its new implementation ncurses describes itself as “CRT screen handling and optimization package.”

The library is very useful. All the old Turbo C cursor functions, mouse functions, getch() and much more is implemented in this. [1]

[1] Avoid common mistakes: a) Read up on the function refresh(). b) When compiling in gcc, use the option -lcurses option.

Advertisement

graphics.h in gcc (plus kbhit() and getch() too!)

November 7, 2008

After migrating completely to Ubuntu, one of the things I didn’t appreciate was that my old graphics.h programs (which I spent many a long hour on) didn’t work here, for gcc did not have support for those primitive graphics.

I found today (from another blog) that there exists a library to do the trick. To quote the specific library’s man page, “libgraph –  library providing the TurboC graphics API (a.k.a graphics.h) in Linux using SDL.” Exactly what I was looking for!

I reproduce (almost exactly) the content of that blog post:

1) Install the following in Synaptic Package Manager

libsdl-image1.2
libsdl-image1.2-dev
guile-1.8
guile-1.8-dev
libsdl1.2debian-arts
libartsc0-dev
libaudiofile-dev
libesd0-dev
libdirectfb-dev
libdirectfb-extra
libfreetype6-dev
libxext-dev
x11proto-xext-dev
libfreetype6(upgrade)
libaa1
libaa1-dev
libslang2-dev
libasound2
libasound-dev

Actually, only the first 4 need be checked for installation, the rest get included as dependencies. But just in case, confirm that all of the above are getting installed.

The above process removes some packages, but that’s fine. Unfortunately for some reason, many other important packages of mine got removed, but things are ok (till now).

2)  Download and install libgraph-1.0.1 using:

./configure
sudo make
sudo make install

3) Include graphics.h in your C file. Initialize the graphics system by:

int gd=DETECT, gm=VGAMAX;
initgraph(&gd, &gm, NULL);

4) Compile your program using g++ with the argument -lgraph

5) The error “./a.out: error while loading shared libraries: libgraph.so.1: cannot open shared object file: No such file or directory” can be solved by

sudo cp /usr/local/lib/libgraph.* /usr/lib

Interestingly, the function getch() (which is very useful to a novice programmer) is also a part of this graphics.h. When porting my old programs to gcc, I used to comment out the getch() (which, frankly, I used to overuse). So one more problem solved!

I also needed the functionality of kbhit() (another useful function for the novice Turbo C programmer). Google is truly great, I found this.

Edit: For a better implementation of getch(), read about the ncurses library here.


Installing apache2 server

October 14, 2008

I finally got an apache2 server working on my laptop, after hours of (almost pointless) editing of configuration files, server restarts, reading Ubuntu forums[1] and googling. It was lack of all three, information, insight and luck that made this what-should-have-been simple process drawn out and painful.

1) Install apache2

sudo apt-get install apache2 apache<version>-common apache2-mpm-worker

The version I’m using is 2.2. To not bother about version numbers and dependencies, install apache2 using Synaptic Package Manager

2) Open your browser and type

http://localhost/

It should display the page index.html located in /var/www/, with a message “It works!” Proceed only this step is successful.

3) Create a new user, say, myserver

sudo adduser myserver

This step is necessary because you have to give apache2 access to folders /home/, /home/user/ and a folder called /home/user/public_html/. It is prudent to use a dummy user like myserver instead of yourself.

4) Create a folder called public_html/ in /home/myserver/

sudo mkdir /home/myserver/public_html/

Create your web page and put it in this folder under the name index.html (apache2 searches for an .html file with this name). If you want to host a directory, simply put the directory here.

5) Give apache access permissions to this folder hierarchy

sudo chmod -R 775 /home/myserver/

To make editing of the .html files easier (so that you don’t need to use sudo everytime) change the myuser/ hierarchy in your group, before running the above sudo chmod command. (I’m not sure how safe this is, though)

sudo chgrp -R your_group_name /home/myserver/
sudo chmod -R 775 /home/myserver/

6) Enable user directory module in apache

sudo a2enmod userdir

Now the files userdir.conf and userdir.load will be created in /etc/apache2/mods-enabled/ folder. Some websites suggest creating a link in the mods-enabled/ folder to the same files kept in /etc/apache2/mods-available/ folder (instead of using a2enmod) but I read that this method could be less safe.

7) Open the file /etc/apache2/httpd.conf and add the following line to it

ServerName localhost

Later on, ‘localhost’ can be replaced with the actual name of your site.

8) Restart the server

sudo /etc/init.d/apache2 restart
sudo /etc/init.d/apache2 force-reload

(I don’t know if this is a bug, but sometimes, just restarting or force-reloading doesn’t cause the changes made to be registered)

9) Open your browser and test the server with:

http://localhost/~myserver/

It displays either the /home/myserver/public_html/index.html file, if it exists, or displays the public_html/ folder hierarchy.[2]

10) Next steps include port forwarding in your router, changing listening port (if required), registering in a DNS site like dyndns.com, fiddling around with a few more settings and you have a web site! This, for later, in this post.

[1] This was done on Ubuntu 8.04 LTS Hardy Heron

[2] In case its still not working, go to the folder /etc/apache2/sites-available/, make a backup of the file default. Open this file with a text editor with root permissions.

sudo cp default default.OLD
sudo gedit default &

The default directory where apache searches for your web page is in /var/www/. We change this default directory to /home/myserver/public_html/ at two places, as highlighted below:

NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost

DocumentRoot /home/myserver/public_html/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/myserver/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

Now restart the server as in step 8), open your browser and test the server with:

http://localhost/