My Tech notes: November 2008

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Tuesday, November 25, 2008

Best Buy black friday ads 2009 slick deals Released

Check out information on black friday ads,
Best Buy black friday ads 2009 slick deals Released target, best buy black friday ads, walmart black friday ads, best buy deals

Best Buy From black friday ads Home Page
Target Black Friday ad listing for 2008 at along with the Black Friday, and online coupons.

Best Buy Black Friday 2008 From Sears Circuit City and Wal-Mart

1. How to start making out of best deals and best buy?
2. Why slickDeals is favorite?
They have created a Excel spreadsheet to get idea and quick overview of items and prices. No need to scan through tons of color ad pages.
and Kmart, Target, Meijer, and a few others will be opening at 6:00am.
Check out some of best deals for 2009 black friday.
Best Buy
Blu-ray from Circuit City titles for $8.99, $11.99, and $17.99.
Blu-rays, DVDs, and CDs black friday ads, walmart black friday ads, target black friday ads, shopko black friday ads, sears black friday ads
 blog it

SATA disk is Not Detecting While Linux bootup

Did you experience below kind of error anytime with SATA disk,

PCI: enabling device 0000:00:01.0 (0140 -> 0143)
ata1: SATA max UDMA/100 cmd 0xC2852080 ctl 0xC285208A bmdma 0xC2852000 irq 22
ata2: SATA max UDMA/100 cmd 0xC28520C0 ctl 0xC28520CA bmdma 0xC2852008 irq 22
ata1 is slow to respond, please be patient

after waiting you get
ata1: no device found (phy stat 00000000)
scsi0 : sata_sil

and at last
ata2 failed to respond (30 secs)

And you know how simple answer is for this.
Yes only thing is knowing is the key.

The answer lies in power source connector; SATA disks are very sensitive to power source, so if you see such errors coming while bootup and your SATA disk is not getting detected then press the power connector, And your are saved. This is my experience after struggling for an hour with changing SATA disk and changing SMPS, changing target board, but nothing worked; finally press hold on power connector to SATA disk and done. bbuuuurrr..

Sunday, November 23, 2008

what does /dev/null 2>&1 command do?

what does /dev/null 2>&1 command do ?
What does > /dev/null 2>&1″ mean?
I was running some CGI for disk formatting and during this process some warning messages were coming on screen; even though I was duplicating standard out file pointer to some temporary file pointer. It was not redirecting standard error messages to this temporary file. Searched on google, but was not able to hit google with right keyword. Got this answer of using /dev/null 2>&1.
Here is explanation,

In Linux There are three standard file descriptors, Standard In (STDIN, file descriptor 0), Standard Output (STDOUT, file descriptor 1) and Standard Error (STDERR, file descriptor 2). /dev/null is the null device, which is like "write only memory". > will write to the specified file (overwriting its contents) and >> will append to the specified file.

> /dev/null 2>&1 redirect both output and error to /dev/null
> /file 2>&1 redirect both output and error to /file

> redirects output to /dev/null
2>&1 redirect error to where > is redirected
So error also gets redirected to /dev/null

“> /dev/null” will redirect STDOUT to a BLACK HOLE

“2 > &1” will redirect STDERR to STDOUT which in turn is redirected to a BLACK HOLE

Friday, November 21, 2008

Use of CUT Grep AWK Linux Command Combinations

Today I learn about power usage of grep cut and awk command;
My requirement was to know on which physical USB port which device is connected,
I can get this information from /proc/bus/usb/devices; which tells me about on which port which device is connected; my udev script will automatically mount the USB device with combination of Manufacturer name and serial number;
Here is the steps how I reached to simple solution instead of coding hundred lines of code.

To search for the SerialNumber in proc file i used
$ grep 'SerialNumber' /proc/bus/usb/devices

was giving me output in "S: SerialNumber=LDVTCGP6" format, I don't need that S: so used pipe;
$ grep 'SerialNumber' /proc/bus/usb/devices | awk '{print $2}'

This removes initial "S:" The output is combination of SerialNumber=LDVTCGP6 lines;
I need to separate each strings with individual values; use cut command from linux.
$ grep 'SerialNumber' /proc/bus/usb/devices | awk '{print $2}' | cut -d= -f2

This gives me "LDVTCGP6" as serial number; I need only for two ports so use tail command.
$ grep 'SerialNumber' /proc/bus/usb/devices | awk '{print $2}' | cut -d= -f2 | tail -2

This gives me only last two values; redirect this to some file like
$ grep 'SerialNumber' /proc/bus/usb/devices | awk '{print $2}' | cut -d= -f2 | tail -2 > /var/usb_port

This file contains now serialnumbers of usb device connected now for same way append values for port in same file;
$ grep 'Port' /proc/bus/usb/devices | awk '{print $5}' | cut -d= -f2 | tail -2 >> /var/usb_port

This way I got serialnumbers and physical ports connected values in one file now I can open this file and manipulate to meet my requirements.
Life is easy with cut,grep and awk commands in linux.

Some more examples to work out.
To print only the second and third lines of a file:
head -3 some.file | tail -2

Cut Command
Divide a file into several parts (columns)
cut [-b] [-c] [-f] list [-n] [-d delim] [-s] [file]
You have a file test.txt which has colon(:) seperated data


If you want to print first set of data from each row, you can use cut command as follow:

cut -d":" -f1 test.txt

Thursday, November 20, 2008

It's a Bigger World than Java and C++

We hear a lot about Java and C++, but that doesn't mean they're the only languages developers are using.
Tokeneer was developed for the U.S. National Security Agency (NSA), an outfit known for keeping things secret. Which makes it even more surprising that not only did the NSA acknowledge Tokeneer's existence, but they released it as open source software.
In a nutshell, Tokeneer is a proof-of-concept of what's called "high-assurance software engineering." Secure software, in other words. Software you can trust. Software that must work correctly or else the consequences could be calamitous. And software that's written in Ada—yes, Ada—and developed using Praxis High Integrity Systems ( Correctness-by-Construction (CbyC) methodology, the SPARK Ada language (, and AdaCore's GNAT Pro environment ( The project demonstrates how to meet or exceed all those things that are necessary to achieve high assurance, such as Evaluation Assurance Level 5 in the Common Criteria (whatever that is). All in all, Tokeneer was created in just 260-person days, and implemented in about 10,000 lines of code. Originally a subset of the Ada language, SPARK Ada is designed in such a way that all SPARK programs are legal Ada programs.
No less than Tony Hoare, Fellow of the Royal Society of Microsoft Research, says that "the Tokeneer project is a milestone in the transfer of program verification technology into industrial application. Publication of the full documents for the project has provided unprecedented experimental material for yet further development of the technology by pure academic research. It will serve as a touchstone to chart and measure progress of the basic science of programming, on which the technology is based." Tokeneer is aimed at both the industrial and academic communities, forming an base for research in program verification and as a high-level teaching aid for educators. You can download the entire Tokeneer project, including requirements, security target, specifications, designs, source code, and proofs at
Jonathan Erickson
Read more on:
IntellaSys ( ,,

Wednesday, November 19, 2008

Refresh HTML Page Every 5 Seconds Usages META tag

How to Refresh HTML/CGI page every 5 seconds?
Today I was searching for page refresh after a certain activity is completed. I was running some task in say CGI 1; which uses fork() to execute another task; once this task is done CGI 1 should get refreshed or should show me the updated status which is being updated by new task.
I tried lot of other combination like calling refresh function from new task; with window.opener.refresh_fucntion(); and a lot.
Finally got the solution using meta tag of html.

Meta HTML tag,The element provides meta-information about your page
And the answer to question is just include before head element this tag;
Refresh page every 5 seconds:

Refresh page every 5 seconds:

<meta http-equiv="refresh" content="5" />
and my job is done.
Thanks to meta tag.

Tuesday, November 18, 2008

C Programming Caution while using execlp function

What is C Programming Caution while using execlp function?

programming caution while using execl function series. execlp function series most of the time are used with fork() system call;
If the requirement is there to kill the process started by execl function, then one caution need to take care, as execl functions will replace complete child process image with the new process image, This means new process started is having new pid and not as same as child pid.
In order to kill the process we need to call getpid function in calling function and store this pid in temp file and while killing this new process use
kill(pid) system call.

Example usage assuming in pid is stored in /var/childpid.

fptr = fopen("/var/childpid","r");
if(fptr != NULL)
printf("Can't open childpid file");
child_pid[strlen(child_pid)-1] = '\0';

sprintf(cmd,"kill %s >/var/done",child_pid);

Monday, November 17, 2008

How to Check Directory Size In Linux Also lists file size

How to check directory size in linux?
du is the answer.
du - estimate file space usage
Summarize disk usage of each FILE, recursively for directories.
useful options are
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
-s, --summarize
display only a total for each argument
To check the directory size use $ du -hs
this will give current working directory size if specified gives specific directory size ie
$du -hs /home/bhagwat/test/dir
gives size of /dir.

while df is also there but it gives disk space not directory space
df - report filesystem disk space usage
df displays the amount of disk space
available on the filesystem containing each file name argument. If no file name is given,
the space available on all currently mounted filesystems is shown.

Sunday, November 16, 2008

Use of Execlp Functions C program Explained

Below code listing illustrates usage of execl function series, and talks about how to redirect the output from these functions.
Here is trick to redirect the output from execlp command series
In other words implematiation of "ls -l >/var/test.log"
first open file using "open" system call


int open(const char *pathname, int flags);

then duplicate this file pointer with stdio file pointers
stdio file pointer are 0 for stdin
1 for stdout and 2 for stderr

use of dup2

dup and dup2 create a copy of the file descriptor oldfd.
int dup2(int oldfd, int newfd);

here is c example implementation in function;

int spawn(char* cmd)
pid_t child_pid;
int fptr,fptr2;
char tmp_devfs[20];
int child_status=-1,ret=-1;

// printf("executing cmd=%s\n",cmd);
/*create new process */
child_pid = fork();
if (child_pid != 0){
/* This is the parent process. */
ret = child_pid;
/*Here is trick to direct or redirect the output to execlp*/
/* The execl function returns only if an error occurs. */
fprintf (stderr, "an error occurred in execl sfdisk\n.");
exit -1;
//printf("done with cmd=%s child ret=%d\n",cmd,ret);

return ret;

The child process starts executing in another shell replacing the current process image or child;
parent process continues to execute.

if one has to wait for child to finish one can make use of wait() functions;

if (WIFEXITED (child_status))
ret = WEXITSTATUS (child_status);
ret = -1;

Add this code before return statement.
In a simillar way if one needs to read input from file while using exece series function open that file in read mode and duplicate the file descriptor with stdin file descriptor.
ie simply change


Related questions:
use the execlp command in my C program to run the PS command and send the output into a file specified in the Argument.
Man pages for fork, open, exec and wait function.
man page for open
The fork system call creates a new child process.
The exec functions (there are more than one) are a family of functions that execute some program within the current process space.
The two versions of exec that we will be using are execlp and execvp. The difference is in the way they accept arguments, however both of them are typically called the same way.
The wait function is in and it’s a special function that allows a parent process to wait for its child processes to exit.

Thursday, November 13, 2008

Why We Need to Call a Pig a Pig - The politics of language ..

We know Orwell for his novels, but it's the way he saw the politics of language that makes him relevant.
clipped from

Why We Need to Call a Pig a Pig (With Or Without Lipstick)

We know Orwell for his novels, but it's the way he saw the politics of language that makes him relevant.

clipped from

Though many of Orwell's essays describe single incidents, his concerns are political, in the largest sense: the way human dignity is corrupted by false phrases. He was less interested in what motivates people to act without integrity than in the words they use to camouflage and perpetuate their dishonesty: for Orwell, bad language and bad politics were one and the same. Yet for all his penury and despair, his faith in the power of clear, strong language can only be read as optimistic.

Orwell's ideas have been bastardized and simplified over time, so that "Big Brother," the totalitarian, state-run citizen-control mechanism of "1984," is now the name of a reality-TV show that bears little resemblance to the book, except for the fact that contestants are watched by cameras
 blog it

Stupid Products

clipped from

Inflatable Toast


The Goatee Saver


Taco Propers


Sci Fi Themed Condoms


Handsets for cell phones


Cassette Tape Dispenser


Day-Glo Camouflage


Zaky Infant Pillow


Two-da-loo Tandom Toilet




Hair Guard for Noodle Eating


DVD Rewinder


Noodle Cooling Fan


Umbrella Shoes

 blog it

Wednesday, November 12, 2008

Get Mounted File System Type C function

How to get Mounted file system type using C program?
Here is trick to get mounted file system type or say this trick can be applied to get variable from linux; This is simple combination of "grep" and "awk" commands. check out below; this function takes mounted point name like where you have mounted the disk example
/dev/sda1 is being mounted at /mnt/test so the input argument to this function is test and the output should get is /dev/sda1 filesytem type.
If you type mount command you will get the file type

on my system with one pen drive; it shows
dev/sda1 on /mnt/JetFlash type ext3 (rw)
input to this function is JetFlash and I will get return as ext3 which is file system type.
Description          : returns mounted FS type
char *get_fs_type(char *mount_point_name)
char temp[20];
char cmd[100],*ret_p;
FILE *fptr;

sprintf(cmd,"/sbin/fdisk -l | grep %s | awk '{print $6,$7}' > /var/mpt",mount_point_name);

fptr = fopen("/var/mpt","r");
ret_p = (char*)malloc(strlen(cmd) + 1);
if(ret_p != NULL)

return ret_p;

one word of caution
this function uses fgets to read from file; the returned output contains an appended new line character; so before using this returned output in other commands one must remove new line char using simple trick like;
temp_p[strlen(temp_p)-l] = '\0'

Tuesday, November 11, 2008

Scratchbox Cross-compiling Made Easy

Often Getting problems with corss compiling here is the perfect solution, read on.
Scratchbox is a configuration and compilation environment for building Linux software and entire Linux distributions.
The basic idea of Scratchbox is to offer developers an environment that works and looks like the target environment before the target environment is available.
The Scratchbox is an environment that you log into like you would log into some machine. However, the Scratchbox tools can be used either inside or outside of Scratchbox download
untar all tar ball using tar -xzf command
then run

’’ asks you some questions, just use defaults. After this we need to add user for
Scratchbox. User must be some user that you have in your system.

Cross-compiling outside Scratchbox
$/scratchbox/compilers/arm-gcc-3.3.4-glibc-2.3.2/bin/arm-linux-gcc -Wall -o arm-hello hello.c

Cross-compiling in Scratchbox
Scratchbox is a chrooted cross-compilation environment. This means one has to log into the Scratchbox like one would log into a some real machine
You can log into the Scratchbox system with a command: ’/scratchbox/login’. You should get some welcome messages and your prompt should change to something like
’[sbox-HOST: ~] > ’..

The Scratchbox prompt ’[sbox-HOST: ~] > ’ tells you that you are in Scratchbox environment and that you are using target called HOST. In Scratchbox you can have multiple targets which means that you can compile programs for many different architectures and settings.
then create a target for our cross-compilation needs.

[sbox-HOST: ~] > sb-conf setup MYTARGET -c arm-gcc-3.3.4-glibc-2.3.2
-t qemu-arm
[sbox-HOST: ~] > sb-conf select MYTARGET
[sbox-MYTARGET: ~] >

Scratchbox comes with ARM emulator QEMU.

[sbox-MYTARGET: ~] > gcc -Wall -o sb-arm-hello hello.c
as we are inside the Scratchbox environment we can also run the program although it is an ARM
binary and we are on X86 machine. This was because we have the emulator:
[sbox-MYTARGET: ~] > ./sb-arm-hello
Hello World!

Scratchbox website (
Installing Scratchbox (

Monday, November 10, 2008

fdisk Compilation for ARM-Target board Errors

Today Trying my luck with compiling fdisk utility;

Source code at:

Current fdisk 1.0 is July 7 2007 370K source .tar.gz

./configure --disable-cfdisk --host=arm-linux --build=i686 --disable-largefile
./configure says to install GNU/Parted before compiling.
It needs parted/parted.h. Latest version is 1.8.7 of May 2007.
1.4GB source code tar.gz.
./configure --host=arm-linux --build=i686
configure: error: GNU Parted requires libuuid - a part of the e2fsprogs package (but
GNU Parted requires libuuid - a part of the e2fsprogs package (but
sometimes distributed separately in uuid-devel or similar)
This can probably be found on your distribution's CD or FTP site or at:
Note: if you are using pre compiled packages you will also need the development
package as well (which may be called e2fsprogs-devel or something similar).
If you compile e2fsprogs yourself then you need to do 'make install' and
'make install-libs'.

I got the e2fsprogs tools, still same error.
Well while doing this I discovered about an easy way of cross-compilation ie Scratchbox;
will post more about Scratchbox after I install and test it.

Thursday, November 6, 2008

Install NTFS read Write Support ntfsmount a big Bang

Today's leanings are
1. How to create NTFS partition from FAT32 filesystem?
Need to have windows XP or NT to run this command.
Created NTFS partition using command from cmd prompt; -

2. Major task was to enable NTFS Read/write support;
Started with enabling NTFS_RW in Linux kernel, its help says;
This enables the partial, but safe, write support in the NTFS driver.
The only supported operation is overwriting existing files, without changing the file length. No file or directory creation, deletion or renaming is possible. Note only non-resident files can be written to so you may find that some very small files (<500 href=""> user level driver(requires FUSE) and kernel mode driver.

Started with kernel mode driver from ntfs-3g which uses mkedev function ref to our arm-tool chain in file /tools/arm-tools-3.3.1/tools.Linux/hard_fp/armv5-linux/sys-include/sys/stat.h which is not there in my arm-linux toolchain. bangg...

Now back to last option ie compile user level read-write module from
with option
./configure --host=arm-linux --target=arm-linux --build=i686 --enable-ntfsmount PKG_CONFIG=/home/bhagwat/test_softwares/fuse-2.7.4/fuse.pc

and I am lost again with below error
checking pkg-config is at least version 0.9.0... ./configure: line 20202: /home/bhagwat/test_softwares/fuse-2.7.4/fuse.pc: Permission denied
checking for special C compiler options needed for large files... no
checking for _FILE_OFFSET_BITS value needed for large files... 64
checking for LIBNTFS_GNOMEVFS... configure: WARNING: Linux-NTFS Gnome VFS module requires glib-2.0 and gnome-vfs-module-2.0 libraries.
checking for FUSE... configure: error: ntfsmount requires FUSE version >= 2.6.1.

Anyone have any IDEA.

Tuesday, November 4, 2008

UDEV Simple Rules For Events In Linux

How to execute external program upon certain events occurred in linux?
This is quite wide requirement if you are running embedded linux on target board. Well The answer could be varied as question is more general. Lets limit the scope of question to USB devices.
Running external programs upon certain events to be specific want to run a program on USB device connection and on USB device removal.
Well I am going to do it with UDEV,
For the beginners/novices about UDEV and howto write UDEV rules the howto is pretty nice and easy to apply. udev plus tux, from the udev page
One can do it with signals also, but I like this way. To start with,
I have written simple udev rules file and kept in /etc/udev/rules.d/90-local.rules (off course you can give any name but with ext .rules and start with some no like 90 here); My udev rules are in 50-udev.rules files which defines the way to mount on USB connection and unmount on USB device disconnect.
I can't believe myself that with simple two lines rule placed in 90-local.rules file my job is done;
Here is 90-local.rules file



These two line says on connection of disconnection run the local file ie kept in /usr/sbin/.
You can write whatever you want in this exe; I am creating a default share on USB detection and making it to available for others to see the USB contents. On device removal I am deleting the shares.

The functionality introduced here allows you to run a program after the device node is put in place. This program can act on the device, however it must not run for any extended period of time, because udev is effectively paused while these programs are running. One workaround for this limitation is to make sure your program immediately detaches itself.

Here is an example rule which demonstrates the use of the RUN list assignment:

KERNEL=="sdb", RUN+="/usr/bin/my_program"
One observation if you enable printf inside your my_program you will not get the output on consol. Don't get panic, rather read the UDEV howto.

Monday, November 3, 2008

Palindrome Number Checking for Int C program Functions

Write a c program to know if given number is palindrome or not?
Well first try to digg out what is palindrome?
A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction. Palindrome example is TENT on either direction characters are same.
The most familiar palindromes, in English at least, are character-by-character: the written characters read the same backwards as forwards. Palindromes may consist of a single word (civic, level, racecar, rotator, Malayalam), or a phrase or sentence ("Was it a rat I saw?", "Wasilla: All I saw", "Mr. Owl ate my metal worm", "Sit on a potato pan, Otis", "Neil, a trap! Sid is part alien!", "Go hang a salami I'm a lasagna hog.", "Satan, oscillate my metallic sonatas", "I roamed under it as a tired nude Maori"). Punctuation, case and spacing are usually ignored, although some (such as "Rats live on no evil star") include the spacing.
That was about palindrome strings or char.
What is importance of palindrome number?

A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285. They are studied in recreational mathematics where palindromic numbers with special properties are sought. A palindromic prime is a palindromic number that is a prime number.

Now time for C-code;
Its very easy for string or words to know if it is palindrome or not; like

#define size 26
int main(void)
char strsrc[size];
char strtmp[size];
printf("\n Enter String:= "); gets(strsrc);
printf("\n Entered string \"%s\" ispalindrome",strsrc);
printf("\n Entered string \"%s\" is not palindrome",strsrc);
return 0;

here is simple program for int palindrome numbers

int main()
int i,j,k=0 ;

i = 122223;
while( i>0 )
j = i % 10;
i = i/10;
k = k*10 + j ;
return 0;

One can customize this for checking given int is numbers are palindrome are not.