typeof operator in c

January 30, 2010 at 3:20 AM (Uncategorized)

Few days back I came across typeof operator in c which is very handy to figure out type of operand at run-time. Here I have used it to write a swap macro which can work on int as well as char type argument.

md@Om:/media/disk/c_programming$ cat typeoff.c
#include<stdio.h>
#define swap(a,b) do{ typeof(a) tmp=a; a=b; b=tmp; }while(0)
int main()
{
int a=10, b=20;
char c=’b', d=’m';
swap(a,b);
swap(c,d);
printf(“a= %d \t b=%d”, a,b);
printf(“c= %c \t d=%c”, c,d);
return 0;
}
md@Om:/media/disk/c_programming$ 

md@Om:/media/disk/c_programming$ ./typeoff
a= 20    b=10c= m        d=b

Only thing I am not sure about is whether its cross-platform or not. If you come to know about it do let me know as well.

Permalink Leave a Comment

Small bash script to get dictionary meaning for a word from unix command line

January 26, 2010 at 7:09 PM (Uncategorized)

I have written a small script (not well written as of now) which will fetch you dictionary meaning for a given word. You can run script as given below:

md@Om:~$ ./getMeaningFor  time 8
times To adjust so that a force is applied or an action occurs at the desired time: To adjust to keep accurate time. To record the speed or duration of: To set or maintain the tempo, speed, or duration of: To set the time for (an event or occasion).Used to indicate the number of instances by which something is multiplied or divided: You must change with the times.

md@Om:~$

Script code is given below:

md@Om:~$ cat getMeaningFor

#!/usr/bin/env sh

LOGFILE=”tmp$$”
Count=8

usage() {
echo “${0} <string:WordToSearchMeaningFor> <integer:NoOfMeaningsYouWantUsToDisplay>”
exit 0
}

cleanup() {
if [ -f "$LOGFILE" ] ; then
rm “$LOGFILE”
fi
}

if [ -n "$2" ] ; then
[ "$2" -lt 9 ] && Count=$2
fi

trap “cleanup” 1 2 15
[ $# -ne 2 ] && usage
wget -q “http://dictionary.reference.com/browse/${1}” -O “$LOGFILE”
flg=`cat ${LOGFILE} | wc -l`
[ $flg -eq 0 ] && echo “Please check your Internet connectivity…”
ToPrint=`sed -n ‘/<ol type=/p’ ${LOGFILE} | awk -F’–BOF_DEF–>’ ‘{print $2}’ | awk -F’<li><p>’ ‘{ print }’ | tr ‘[<>]‘ ‘\n’| sort | uniq | tail -$Count`
echo $ToPrint
cleanup

You can  paste above code in a file, make it executable &amp; run it. If you get time to parse output more perfectly do let me know too…

Permalink Leave a Comment

some awesome macros from linux kernel code

July 4, 2008 at 11:30 AM (Uncategorized) ()

In linux kernel you will find macros implemented for easy use of circular doubly linked list. One of them is list_entry. I m going to explain functionality of list_entry macro. Basically list_entry returns base address of any structure by taking input as any of its member field.

e.g. you have following structure.

typedef struct student_str1 {
int rollno;
char name[20];
char foo;
char bar;
}str1;

str1 obj;

now if we pass here &obj.foo to list_entry macro, it should return us base address of struct. If you do not understand usage scenario  of list_entry macro, then please browse linux kernel source code.  Here you will find only implementation details of it.

Now coming back to point, to get base address of structure we’ll have to first find out offset of corresponding member in struct & it can be achieved as follows :

&( ((str1 *) (0)) ->foo )

After this, you will have to find out total bytes used by all elements declared above foo member. You can get this by

(char *)  ( &obj.foo)

Now to get base address of structure you can subtract offset of foo from total bytes used by remaining members in the structure ( declared before foo ).

(char *)  ( &obj.foo)  -    ( &( (str1 *) (0)) -> foo)

And in last you can get base address of structure str1  by

(str1 *)   ( (char *)  ( &obj.foo)  -    ( &( (str1 *) (0))-> foo) )

I know I explain things very badly. So for better understanding play with following code.

#include<stdio.h>

typedef struct student_str1 {
int rollno;
char name[20];
char foo;
char bar;
}str1;

main() {

str1 obj1;

printf ( ” \n address_rollno = %p \t offset_rollno = %u “, &obj1.rollno ,  (unsigned ) &(( (str1 *) (0))->rollno) );
printf ( ” \n address_foo = %p \t offset_foo = %u “, &obj1.foo , (unsigned ) &(( (str1 *)(0) )->foo ) );
printf ( ” \n address_bar = %p \t offset_bar %u” , &obj1.bar , (unsigned)  &(((str1 *) (0))->bar ) ) ;

printf ( ” \n computing base address of structure by getting address of one of its members… \n\n ” ) ;

printf(“\nbase address of obj1 = % p “, (str1 *) ( (char *)(&obj1.foo) – (unsigned) &(((str1 *) (0))->foo) )  );

return 0 ;
}
I have verified above program with gcc.

Permalink Leave a Comment

IO redirection – using proc

March 14, 2008 at 6:06 AM (Uncategorized) ()

In /proc, there are subdirectories for each process running on the system, having name based on the PID number of the process. In each of these directories, there is a fd/ subdirectory that contains files that represent the file descriptors the process currently has open. These files are actually symlinks that point to the actual device, socket, or other file the process currently has open and mapped to that file descriptor. If you have a program that can read input from a file but not from standard input, or that can write to a file but not to standard output, you may be able to cheat by taking advantage of these special files:
/proc/self/fd/0 is standard input of the current process
/proc/self/fd/1 is standard output of the current process
/proc/self/fd/2 is standard error of the current process

Permalink Leave a Comment