engintola.com/tech-blog
integer overflow detection
Tuesday, December 25, 2012 18:10:25 +0200

GCC option -ftrapv provides limited support for detecting integer exceptions at runtime. this option "generates traps for signed overflow on addition, subtraction, multiplication operations"

there's also a dedicated tool called Integer Overflow Checker (IOC) here that you can compile with your code to catch overflow errors. In their website, IOC reports to have more error checks than ftrapv and it definitely produces much more useful error messages.

integers #3
Tuesday, December 25, 2012 17:41:57 +0200

what is the value of z ?

int x = -1;
unsigned int y = INT_MAX;
int z = x/y;

hint: two's complement
integers #2
Tuesday, December 25, 2012 17:28:19 +0200

below code segment fails for x = INT_MIN; can you explain why?

if( x < 0 ) x = -x;

hint: two's complement
integers
Tuesday, December 25, 2012 17:28:19 +0200

floating point arithmetic is not the only source for head-scratchers... integers can cause elusive bugs as well... can you say the below code is safe for all b != 0 ?

int a, b;
int c = a / b; // is this safe for all b != 0 ? if not why ?

hint: overflow
line spacing in html/css
Tuesday, November 27, 2012 21:40:00 +0200

you can set the spacing between the lines in an html construct by setting
line-height: 2em;
remove non-existing local branch references for a remote
Tuesday, November 27, 2012 21:15:49 +0200

to tidy up the local branch references and getting rid of old branches that no longer exists, call this:
git fetch -p remote_name
setting up a remote git repository
Tuesday, November 27, 2012 17:52:32 +0200

this script is used to set up a remote git repository on a server and add a 'remote' to the local .git/config file pointing to the server repository. use it script like:
git-server-setup.sh daisy
don't forget to edit the default parameters of the script like server address and ssh port number or user name once you download it. call it inside a git-enabled directory.

git-server-setup.sh
installing bitstream vera fonts (ubuntu 9.10)
Tuesday, March 30, 2010 15:31:59 +0200

how to install new fonts to an ubuntu machine. ripped off from here
  1. download the 'bitstream vera sans mono' from www.gnome.org/fonts
  2. put them in ~/.fonts
  3. scan the font directories on the system and build font cache files
  4. fc-cache -f
  5. check if ok
  6. fc-list | grep Bitstream
    cd ~/.fonts
  7. for every font file found create X11 XLFD-name and add to fonts.scale
  8. mkfontscale
  9. read the font files in the directory, convert names to lower case and add names to file "fonts.dir" in the directory
  10. mkfontdir
    xset fp+ ~/.fonts // add this dir to the FONT path
    xset -q // can check with
  11. now to add dir to the fontpath permanently (e.g. after reboot) : add it to /etc/X11/xorg.conf as:
  12. Section "Files"
    FontPath "/home/USERNAME/.fonts"
    EndSection
postfix errors
Sunday, June 7, 2009 15:10:55 +0200

there are different reasons for getting a relayhost error in your postfix setup. I document my problem/solution here because I couldn't find a similar problem like mine while googling around (maybe I'm doing something funny).

here's my problem: I have an ubuntu 9.04 + fetchmail + postfix setup where my mails are relayed through my gmail account. postfix works at the office but my mails are deferred when I'm at home. fetchmail has no problem. the error messages in /var/log/mail.log are like:
warning: relayhost configuration problem
unable to look up host smtp.gmail.com: Name or service not known
so, it seems my problem is related with my dns setup. apparently, postfix has its own 'resolv.conf' file (anybody knows why?) under /var/spool/postfix/etc/. (this might be at a different place for other distros). I have my own scripts to manage my network and when I'm at home, my scripts only update /etc/resolv.conf with the correct dns server address and then postfix cannot resolve the gmail server because it reads the old settings from /var/spool/postfix/etc/resolv.conf. so, by adding for example 192.168.1.1 for my wireless router I got rid of this problem.

by the way, the deferred mails are stored under /var/spool/postfix/deferred/ if you don't know this. you can read the mails under this folder using
postcat /var/spool/postfix/deferred/whatever
postfix, by default, waits for 5 days to tell you if a mail cannot be sent - which is a very very long time for my taste. you can change this to 15 minutes by adding 'maximal_queue_lifetime=15m' to your main.cf postfix setup file or by just running
postconf -e maximal_queue_lifetime=15m
defining an emacs macro*
Monday, June 1, 2009 13:15:31 +0200

If you find yourself doing some repetetive editing chore within emacs, such as converting all the /*...*/ coments to //-style comments in some C or C++ program, you may find it helpful to define a 'keyboard macro'. Start with
'contol-X ('
then do some sequence of keystrokes such as:
  1. use 'control-S /*' to find the next opening comment delimiter
  2. delete the *, insert a / to convert that /* to //
  3. use 'control-S */' to find the closing comment delimiter
  4. delete it
and then end with
'control-X )'
to end the storage of this sequence. Thereafter, the command 'control-X E' will repeat the sequence, and 'Esc 237 control-X E' will attempt to repeat it 237 times, but will give up when anything goes wrong such as failing to find another opening comment delimiter. If you really like your keyboard macro and want to keep it until you finally terminate emacs, you can always do 'Esc X name-last-kbd-macro' and give it a name such as 'modernise-comment', and then do 'Esc X modernise-comment' when you want to re-use it.
(*taken from here)
execute a command whenever
Monday, March 23, 2009 23:37:18 +0100

you can schedule a command to run at a specific time:
echo 'wget some-url' | at 23:39
capturing the desktop using
Monday, March 23, 2009 23:33:04 +0100

you can capture your desktop:
ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg
watch terminal star wars
Monday, March 23, 2009 23:05:05 +0100

ok, this is not strictly technical knowledge but it is very cool. just go ahead and watch the star wars: a new hope in ascii.
telnet towel.blinkenlights.nl
merging pdfs
Monday, March 16, 2009 0:30:21 +0100

merge multiple pdfs into a single one.
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=out.pdf -dBATCH in1.pdf in2.pdf

go ahead and try doing 'ctrl-r' in command line and start typing, your bash history is searched for whatever you are typing. press enter whenever you came accross to the command you're looking for. if there're more commands that fit, try doing 'ctrl-r' to cycle through them.
sleeping screen
Wednesday, January 14, 2009 17:21:55 +0100

screen goes to sleep if you Ctrl-s and wakes up with Ctrl-q
emacs function call from command-line
Monday, July 7, 2008 17:56:40 +0200

starting emacs with calling a lisp-function can be done with:
emacs filename -eval '(goto-line 5)'
actually, you can go to a specific line with:
emacs +5 filename
but this is just an example about how to use 'eval'
lookfor
Monday, July 7, 2008 18:05:14 +0200

this is a small script that recursively looks for a 'key' string inside files ending with a specific extention like '.cpp' in a directory tree. it is also possible to skip certain directories with an 'ignore_list'. i use this script to maintain my libraries: when i change a function prototype in a library, it is necessary to change all of the places it's used. so, here comes the 'lookfor.sh'

it can either output in summary mode displaying only files that contain the 'key' with number_of_occurances and a list of line numbers for the occurances:
$ lookfor.sh . invert .h true
./include/kutility/calibration.h 2 256 287
./include/kutility/corecv.h 2 78 88
or you can run it with verbose mode in which you get a grep-like more readable response
$ lookfor.sh . invert .h false
----------------------------------------------------------------------
./include/kutility/calibration.h
----------------------------------------------------------------------
256: invert( k, 3, k_inv );
287: invert_3x3( kr, r_trans_k_inv );

----------------------------------------------------------------------
./include/kutility/corecv.h
----------------------------------------------------------------------
78: invert(H0, 3, H0inv);
88: invert(H,3,Hinv);

lookfor.sh //Monday, July 7, 2008 19:14:57 +0200
gnus-namazu
Monday, July 7, 2008 16:08:38 +0200

namazu is a full-text search engine. you can use it to index your emails so that you can make keyword-based queries. it is quite easy to integrate to gnus. here's how you do it:
  1. install namazu first
  2. # debian-based systems:
    sudo apt-get install namazu namazu2-index-tools
    # if you're not a debian user, go to the namazu website and follow the installation procedure from there.
  3. add these lines to your .gnus file
  4. (require 'gnus-namazu)
    (gnus-namazu-insinuate)
    (setq gnus-namazu-make-index-command "mknmz")
    (setq gnus-namazu-make-index-arguments
    '("--all" "--mailnews" "--deny=^.*[^0-9].*$" "--exclude=(spam|duplicates)"))

    ;; namazu index file is located in
    (setq gnus-namazu-index-directories
    (list (expand-file-name "~/Mail/namazu")))

    ;; update indices every 10 hours
    (setq gnus-namazu-index-update-interval 36000)
  5. to create the index file, call
  6. M-x gnus-namazu-create-index
    this process can take for a while depending on the number of your messages.
  7. after indexing is finished, to search for a keyword in your emails, use
  8. 'C-c C-n' or 'M-x gnus-namazu-search'
matlab: saving in eps
Tuesday, June 17, 2008 15:54:33 +0200

if you're not shy of using matlab, you can use this small script to convert files to eps.
function convert2eps(file_name)
im=imread(file_name);
h = figure;
imshow(im);
saveas( h, strcat(file_name,'.eps'), 'psc2');
close(h);
matlab figure borders
Monday, June 16, 2008 22:01:35 +0200

you can get rid of the gray border in matlab images by using
imshow(im, 'Border', 'tight');
powertop
Sunday, March 23, 2008 23:57:59 +0100

powertop is a linux utility which helps to find programs that are inefficient in a power usage point-of-view while the computer is idle. it basically reports the number of wakeups for the cpu. an unoptimized system may have 1000 wakeups/second whereas an optimized one may have 3 per second. I managed to get my system as low as to 11 wakeups/second ( a generic kernel + disable usb + disabled wifi ) check it out yourself: http://www.lesswatts.org/projects/powertop/
live tail
Tuesday, March 4, 2008 1:04:29 +0100

with this command you can see the updates in a file in real time. this is quite handy to monitor log files.
tail -f /var/log/syslog
openmp - number of threads
Monday, February 25, 2008 14:23:51 +0100

openmp is a library for parallel programming. it is very easy to convert existing code to operate in parallel with a few preprocessor definitions without the need to write new code (generally). by default, it uses all the cpus available but you can set this number with the OMP_NUM_THREADS environment variable.
debugging with emacs
Monday, February 11, 2008 10:00:57 +0100

emacs has a nice mode, gdb-mode, you can use for debugging. you can activate it with "M-x gdb". you can also enable some useful buffers along with gdb to make debugging easier: mainly local variables, stack, breakpoint list... these buffers are enabled using "M-x gdb-many-window". (or enable by default in your .emacs file:
(setq gdb-many-windows t)
running shell commands within a c++ code
Tuesday, January 1, 2008 22:27:33 +0100

you can run shell commands within a c++ code like this:
#include <stdlib.h>
system("shellscript.sh");
starting gnus offline
Monday, December 31, 2007 2:03:12 +0100

when gnus starts up, by default it tries to connect to your news server. however, if you are not connected to the net, you'll then have to wait till timeout. needless to say this could be tiresome. to start gnus in offline mode, a.k.a in unplugged mode, you need to add
(setq gnus-plugged nil)
to your .gnus file.
after starting up, you can plug/unplug using J j
ibm thinkpad t60 wifi + debian etch
Wednesday, December 26, 2007 16:12:10 +0100

to install your ipw3945 driver: add
deb http://ftp.debian.org/debian/ etch main contrib non-free
to your /etc/apt/sources.list
and then
sudo -s
apt-get update
apt-get install firmware-ipw3945 ipw3945d ipw3945-source module-assistant
m-a prepare && m-a a-i ipw3945
modprobe ipw3945
automatic un-tar
Sunday, December 9, 2007 19:24:42 +0100

here is a small bash script you can use to decompress compressed files automatically according to their extensions. it supports tar, tar.gz, tar.bz2, bz2, tbz, tbz2, zip, rar, Z, z. it's fairly easy to modify if you examine the script yourself.

untar.sh //Sunday, December 9, 2007 19:30:37 +0100
dwm
Sunday, December 9, 2007 1:23:05 +0100

dwm is a great tiled window manager where you can partition your desktop and traverse between the windows using keyboard shortcuts very efficiently. here are some screenshots. it is written entirely in c and around 2000 lines of code and hence very easy to play with. for example, here is my patch to have an alt-tab like function that cycles through the active windows. just include this c file to your config.h and add your keyboard shortcuts.

alttab.c (for dwm-4.7) // Sunday, December 9, 2007 1:37:06 +0100
hdd wear and tear on some linux systems
Friday, December 7, 2007 14:17:12 +0100

it seems that acpi causes an unusual number of load/unload cycles on some harddrives when the laptop-mode ( power-save ) is enabled on battery power on some laptops running linux. the bug report about the matter is here

i have a thinkpad t60 with ubuntu 7.10 and i noticed i have this problem. disabling laptop mode resolved the issue for me: you should have ENABLE_LAPTOP_MODE=false in your /etc/default/acpi-support or you can manually disable it by

sudo hdparm -B 255 /dev/sda
you can check if you have this problem by
sudo /usr/sbin/smartctl -a /dev/sda | grep Load_Cycle_Count
(you need smartmontools for this)
the last number is yout load cycle and it should increase around 15 per hour. if it's faster than this, you have the problem...
webcam with mplayer
Monday, December 3, 2007 22:59:14 +0100

you can access your webcam with this command:
mplayer tv:// -tv driver=v4l:width=640:height=480:device=/dev/video0
note: your device could be /dev/video
ibm t60 microphone
Thursday, November 8, 2007 20:42:38 +0100

i had a problem with my microphone sound in my ibm thinkpad t60 with a kubuntu gutsy 7.10 installation. the mic was feeding the sound to my speaker but when i tried to use it with skype or tried to record my sound, it didn't work. here is the solution i found from ubuntuforums.org:

  1. do 'alsamixer -Va' and set the microphone to capture and unmute it.
  2. get out of there, then go ahead and do 'alsamixer -c 0 -V capture'
  3. make sure it's set to record from mic, make sure that both channels from mic are set to capture
  4. press space bar on the capture to enable capturing
  5. go all the way to the right to the ADC / DAC settings. set ADC to capture by pressing the space bar
  6. now test if it works by recording your voice by 'arecord test.wav' --speak-- Ctrl-C, then play it with 'aplay test.wav'
  7. sometimes it stil does not work. then, to generate /etc/asound.names files, run:
  8. sudo alsactl names
    sudo alsactl store
creating an x GB file in linux
Wednesday, November 7, 2007 11:58:48 +0100

to create a file of any size you can use 'dd'. ie. this generates a file of 2GB in size:
dd if=/dev/zero of=outfile bs=1024 count=2097152
here, bs is the block size in bytes and count is the number of blocks ( 2097152 = 2 * 1024 * 1024 = 2 Mega)
multiple .emacs files
Monday, October 29, 2007 1:15:14 +0100

i have two different .emacs files where in one of them i have a very simple configuration while the other one is quite heavy. i use the light-weight version when i want emacs to open faster. you can load different .emacs files using the command:
emacs -q -l ~/.emacs-alt
if you're in a networked environment and working on someone elses computer, you can load your own .emacs file using the quite nifty command
emacs -u username
yeahconsole
Monday, October 29, 2007 0:01:31 +0100

yeahconsole is a quake like terminal emulator (meaning, your terminal slides down from the top). current version supports urxvt and xterm but it is quite easy to hack to include your favorite terminal to it. i made some changes in the code:
  1. to have a y-offset so that yeahconsole doesn't paint over my status bar, which resides on the top of my screen
  2. to be able to pass xterm options from the Xresources file like the font size and enabling anti-aliasing...
yeahconsole-0.3.4
yoffset+xterm_param_passing.diff ( my patch enabling yOffset and xterm option-passing; )
convert
Saturday, October 27, 2007 21:22:12 +0200

one of the most useful tools of imagemagick is *convert*. you can use it to convert multiple files to a variety of image formats by applying various transformations like cropping, resizing, blurring, resampling, rotating and much more... i use this bash script to covert a bunch of files to lossless jpeg.

convert2jpg.sh
mencoder
Saturday, October 27, 2007 21:04:54 +0200

by using this command you can make avi movies from jpg files. it is easy to understand each option: fps -> frames per second; vcodec -> video codec and bitrate is bitrate. i do not use default mpeg codec since it inserts the stupid divx logo to the movie.
mencoder "mf://*.jpg" -mf fps=15 -o o.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800
some possible codecs are ljpeg (lossless jpeg), h263, h263p, mpeg4, ffv1 (ffmpeg lossless). by the way you should give jpg images to the mencoder. otherwise it does not work.
upload/download speed
Thursday, October 25, 2007 3:12:56 +0200

this small script prints the download and upload speed of the interface (eth0, eth1... ). the result is like +14k/s -32k/s where + is the download speed and - is the upload speed. here you go

traffic-speed.sh

Updated: Thursday, July 24, 2014 10:29:21 +0200