Tuesday, March 30, 2010

Advanced PHP - part 2

We will learn about the access modifiers available to PHP classes. Like any other programming languge , we have public ( acessible to all ) , private ( acessible to methods inside the class ) and protected ( classes derived from it can access ..besides methods inside ).



<?php
class Test {
////////////////////////////////////////
// Private variables are only accessible to
// methods inside the class
private $d1;
////////////////////////////////////////
// Public variables are available for anyone
//
public $d2;
////////////////////////////////////////
// Protected variables are available to
// all the methods inside the class and
// and classes inherited from it..
protected $d3;

public function Dump () {
echo $this->d3;
echo "\n";
$this->d2=400;
echo $this->d2;
echo "\n";
$this->d1=200;
echo $this->d1;
}

};

////////////////////////////////////
//
// Test2 inherits from Test..it can access
// protected members of the base...
class Test2 extends Test
{
public function Set( $val ) {
$this->d3 = $val;
}
};


$test_inst = new Test2();
$test_inst->Set(10);
$test_inst->Dump();


?>


[sandhya@localhost newoop]$ php -f second2.cpp
10
400
200[sandhya@localhost newoop]$

Advanced PHP - Part 1

We will start with Object Oriented Programming Techniques in PHP. The Web Programming is mostly a procedural activity and OOP is only supported in PHP from the version 5 only.

The name of the file is simple.php

<?php
/////////////////////////////////////////
// A Simple Class in PHP
//
class SimpleClass {

private $data;
////////////////////////////////////
//
// A Setter function
public function SetData( $pdata ) {
$this->data = $pdata;

}
/////////////////////////////////////
// This method spits the private data
//
public function EchoString() {
echo $this->data;
}
}
///////////////////////////////////////
// Create an instance of class .. Set the data
// Spit it..
$test_obj = new SimpleClass();
$test_obj->SetData("Hello World\n\n");
$test_obj->EchoString();

?>


You can execute the script using the following command:

php -f simple.php

To access an instance variable , we need to prefix with this-> in PHP.

PHP basics - part 6

In the previous two parts , we learned about _GET and _POST. They are called Super Global Arrays in PHP parlance. There is _REQUEST super global array which will have name/value pairs from HTTP Get,
HTTP Post and HTTP Query String.

Create a file post_sample.php and paste the content

<form action="name_age_process2.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>




Create the post handler file name_age_process2.php

<HTML>
<BODY>
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.
</BODY>
</HTML>


copy the files to the web folder ( /var/www/html/five/ )

Restart Apache and Access the contents as

http://127.0.0.1/five/post_sample.php

PHP basics - part 5

In this part , we will learn about processing HTTP POST using PHP.

Create a file post_sample.php and paste the content

<form action="name_age_process.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>




Create the post handler file name_age_process.php

<form action="name_age_process.php" method="post">
<HTML>
<BODY>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</BODY>
</HTML>


copy the files to the web folder ( /var/www/html/five/ )

Restart Apache and Access the contents as

http://127.0.0.1/five/post_sample.php

Monday, March 29, 2010

PHP basics - Part 4

We will see how one can process request using GET in this Part.

// mainfile.php
<HTML>
<BODY>
<FORM ACTION="getprocessor.php" METHOD=GET>
Enter your name ?:
<INPUT TYPE=TEXT NAME="personname">
<BR><BR>
<INPUT TYPE=SUBMIT>
</FORM>
<BODY>
</HTML>


Now create the getprocessor.php

//getprocessor.php
<HTML>
<BODY>
<?php
echo("The name is ".$_GET['personname']);
?>
</BODY>
</HTML>


copy to the web folder

http://127.0.0.1/mainfile.php

PHP basics - Part 3

Let us embedd some php code into an HTML file as given below

<HTML>
<TITLE> Hello World in Php </TITLE>
<BODY>
<BR> <?php echo "Hello World" ?> </BR>
</BODY>
</HTML>



Save the file as helloworld.php


[root@localhost php]# php -f helloworld.php
<HTML>
<TITLE> Hello World in Php </TITLE>
<BODY>
<BR> Hello World </BR>
</BODY>
</HTML>
[root@localhost php]# cp helloworld.php /var/www/html/helloworld.php
[root@localhost php]#


you can execute the code as a web page by giving http://localhost/helloworld.php

PHP basics - Part 2

There is a popular misconception that PHP is only a web scripting language. The PHP language can be used for desktop applications as well.

<?php
phpinfo();
?>



at my shell , i can execute it as follows ( the file name is phpinfo.php )


php -f phpinfo.php

PHP basics - Part 1

When I started Programming , I was a chavunistic C/C++ supporter. I even vowed that I won't be programming using any other programming language.

After four years ,when I saw a huge CAD/CAM software written using Visual Basic ( 3 million lines of Code ) and Visual C/C++ ( 1 million lines of code ) , I began to look at "toy" languages seriously. Later , the accidental "discovery" of Perl Programming Language and an article written by John Ousterheart ( of TCL fame ) convinced me about the power of scripting languages.

( using Perl , i converted a C/C++ Program worth 4000+ lines of code to just 531 lines of code with additional functionality. Read Ousterhort's article Scripting: Higher-Level Programming for the 21st Century March 1998 (vol. 31 no. 3) pp. 23-30 )



I resisted Java for a while , but forced to change my mind when i discovered Java 3D API's Scene Graph System. The same story repeated with C# and VB.net ( In one of the company I worked , VB.net was the implementation language ). My first C# Project was an interpreter for a Spread Sheet Rules Evaluation Engine ( SREE). The flexibility of the language and the underlying platform helped me to finish the project in under two weeks. Most of the code which I wrote
for my first C# project is the kernel of the SLANG4.net Interpreter.


Ten years after discovering it , I am throwing the towel at the PHP Programming Language.

I assume that skill set in PHP and a content management system like Drupal will give me good insight into the business aspect of the Web.


I am using Fedora 10 to learn PHP. Consult a local guru to setup Apache , MySQL and PHP to start
working with PHP development System.

To test whether PHP development system is installed correctly , key into the following program

<?php
phpinfo();
?>



I have named the above file as phpinfo.php. I copied to the /var/www/html folder ( default web app directory of Apache on my installation ) and keyed in the following url.

http://127.0.0.1/phpinfo.php

if the setup is correct , you should get lot of information regarding your php development system.

C/C++ Programming under Linux - Part 36

In this part , we learn about POSIX mutex. Mutex stands for Mutual Exclusion. Only one thread will be able to enter a region guarded by a mutex ( co-operating threads should look for same variable).

/////////////////////////////////////////
// thread_third.cpp
// A Simple POSIX thread program to demonstrate
// mutex for guarding modification to shared
// variables.
// g++ -o thread_third.exe thread_third.cpp -lpthread
//
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
/////////////////////////////////////////
//
// some global variables;
//
//
int Counter=0;
pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;

/////////////////////////////////////////
//
// Thread Entry Point
//
void *PrintFunction( void *temp )
{

pthread_mutex_lock( &m1 ); //guard area

Counter++; // we can safely modify variable now

pthread_mutex_unlock(&m1); //remove guard

int s = *((int *)temp) ;
printf("Hello World from Worker Thread %d\n",s);
return 0;
}

///////////////////////////////////
//
// User Entry Point for the Application
//
int main( int argc , char **argv )
{

////////////////////
//
// Thread Handle
//
pthread_t th;
pthread_t th1;
/////////////////////////////
//
// Create two Threads and start the execution
int param = 100;
int re_val = pthread_create( &th, NULL, PrintFunction,
(void *)&param // Thread parameter
);

int param1=200;
int re_val1 = pthread_create( &th1, NULL, PrintFunction,
(void *)&param1 // Thread parameter
);

printf("Hello World from the main Thread\n");
/////////////////////////////////
//
// Wait for Worker threads to finish

pthread_join( th, NULL);
pthread_join(th1,NULL);
return 0;


}

C/C++ Programming under Linux - Part 35

In this part , we will learn how to pass parameters to a thread function. we need to make only a slight modification to the earlier program to achieve this.

/////////////////////////////////////////
// thread_second.cpp
// A Simple POSIX thread program to demonstrate
// passing of parameters to the thread entry point
//
// g++ -o thread_second.exe thread_second.cpp -lpthread
//
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

/////////////////////////////////////////
//
// Thread Entry Point
//
void *PrintFunction( void *temp )
{
int s = *((int *)temp) ;
printf("Hello World from Worker Thread %d\n",s);
return 0;
}

///////////////////////////////////
//
// User Entry Point for the Application
//
int main( int argc , char **argv )
{

////////////////////
//
// Thread Handle
//
pthread_t th;

/////////////////////////////
//
// Create a Thread and start the execution
int param = 100;
int re_val = pthread_create( &th, NULL, PrintFunction,
(void *)&param // Thread parameter
);

printf("Hello World from the main Thread\n");
/////////////////////////////////
//
// Wait for Worker thread to finish

pthread_join( th, NULL);
return 0;


}


[sandhya@localhost thirtyfour]$ g++ -o thread_second.exe thread_second.cpp -lpthread
[sandhya@localhost thirtyfour]$ ./thread_second.exe
Hello World from the main Thread
Hello World from Worker Thread 100
[sandhya@localhost thirtyfour]$

C/C++ Programming under Linux - Part 34

Today , we will learn about POSIX Threads. When a Program starts , it has got a single thread ( path ) of execution. In GNU Linux , we can create additional threads ( paths ) which can run asynchronously with the main thread.

Read about topics like MultiThreading , POSIX threads on the Wiki to know more about this. The program given below is a multi threaded version of hello world program.


/////////////////////////////////////////
//
// A Simple POSIX thread program
//
// g++ -o thread_first.exe thread_first.cpp -lpthread
//
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

/////////////////////////////////////////
//
// Thread Entry Point
//
void *PrintFunction( void *temp )
{
printf("Hello World from Worker Thread\n");
return 0;
}

///////////////////////////////////
//
// User Entry Point for the Application
//
int main( int argc , char **argv )
{

////////////////////
//
// Thread Handle
//
pthread_t th;

/////////////////////////////
//
// Create a Thread and start the execution
int re_val = pthread_create( &th, NULL, PrintFunction, 0);

printf("Hello World from the main Thread\n");
/////////////////////////////////
//
// Wait for Worker thread to finish

pthread_join( th, NULL);
return 0;


}


[sandhya@localhost thirtyfour]$ g++ -o thread_first.exe thread_first.cpp -lpthread[sandhya@localhost thirtyfour]$ ./thread_first.exe
Hello World from the main Thread
Hello World from Worker Thread
[sandhya@localhost thirtyfour]$

Sunday, March 28, 2010

BarCamp 8 - A Personal Perspective

I started around 4.00 am for the BCK8 event held @ MACFAST Thiruvalla on the 28th March 2010. I boarded a Bus to Ernakulam and reached the KSRTC Bus station around 4.45 am. Fearing that I will be reaching the venue even before the gate keeper , I was evaluating options between boarding an Ordinary Bus (to Kottayam ) or Super Fast (to Trivandrum). After ten minutes of pondering (!!), I decided to board the super fast bus as i can reach Thiruvalla without changing buses.


I reached Thiruvalla around 7.20 AM and when i asked about the MACFAST college , none of them have a clue about the college. After a while , I asked about the Mar Athanesius College and it clicked. At a Petrol Pump , i was joined by an amazing trio (three cousins ) from Kozhikode to the venue.


They introduced themselves and I was clueless about how to start conversation with those "kids" ( who were in 9th , 10th and 11th ) . Bolt from the blue , one of them asked about my blog id and I told them about it. This helped to start the conversation.

Out of curiosity , I just asked them about their Blog , if at all any. For
the next thirty minutes I ended up quizzing them about their blogs,background, parental relationship ,family background , their peers and i got convinced that a new era of DOGs ( Digitally Optimized Geeks ) have arrived. Between them , they are making $600 every month from the Google Adsense. I am forced to confess that I was envious at their achievement and that prompted me to quiz them a lot. we took breakfast before our journey to MacFast!.

After an hour of walk ( thanks to some "excellent" human navigational aids ) we reached the venue. We were greeted by Aravind Jose. While we were chit chatting , he told about his new venture "Kottayam Pattanam".com . It was amazing to hear how boys even before getting out of the college , have started to make a living for themselves using Web as a medium.

Then Binny , Ranjith Avarachan and Deepak arrived after having their
breakfast. We started exchanging pleasentaries and discussed about Perl , PHP,
Blogs , Social Media etc.



Ranjith,Binny and three amazing
cousins . They are talking to me.








We suspected that turn out will be low this time as there were only around 15 people when the clock ticked 9.30 am. Slowly,people began to flock to the venue. Each of them bringing their own "tale" of reasons being late.

A) Statistical Machine Translation Basics by Deepak P.
-----------------------------------------------------------------------------












His presentation was one of the best session at this BarCamp. Even though the topic was bit abstract ( when it comes to modelling ) , he with the help of Tables , a Print out and verbal explanations could give the gist of Machine Translation of natural language using Statistical ( Probabilistic | Stochastic ) models.

He discussed about the Statistical and non Statistical (Parsing ) machine translation techniques.

When we go for Machine Translation using non statistical techniques, one needs to understand the sentential structure of the source and destination language and juxtapose constructs to have equivalent meaning.

Eg:-

( English => Subject Verb Object => Rama Killed Ravana )
( Malayalam => Subject Object Verb => Raman Ravanane Konnu )


Unfortunately, Statistical Machine translation involves Probability Computations, Statistical Modelling , Bayes Theorom and all sorts of
esoteric statistical techniques and explaining the issues without the help of mathematical machinery is only possible to a certain extent. He tried to simplify the stuff to the extent possible and had he attempted to simplify it further , it would have become 'simpler'. ( Remember Einstien's "dictum"=>"Keept it simple , do not make it simpler" )

B) The HTML5 Bandwagon by Shwetank Dixit
---------------------------------------------------------------












He had come for the BCK5 and it has been close to one year after and we were expecting a good technical explanation about HTML 5 ( which he did a wonderful job last time around ),Browser support for HTML 5 standard etc.

Rather than discussing HTML 5 ( which he did mention towards the end of the presentation) he talked about Mobile Web. Based on the audience poll, he talked about the Market Trends as opposed to Technical internals.

The Talk was informative about the Embedded Device Vendor Penetration across several countries and being a good presenter he is , audience really enjoyed it. ( Even though he used the Opera Mini Browser Traffic statistics , he says it is a general indicative trend for the whole of the 'mobile web')

Towards the end he discussed about HTML 5 and SVG a bit. All the people who were expecting a follow up presentation of what he did the last time, got bit disappointed (myself included).

Couple of us asked about Browser Business models and he gave us deep insight into the stuff. ( we discussed this at the corridor )

C) Spara-Energy Conservation Product By Deepa ---------------------------------------------------------------------











The Next session was taken by a famous TV anchor. She has got remarkable presentation skills.Unfortunately for her , people asked some deep technical questions and she was forced to put down her hands. Her colleagues ( it was a promotional undertaking kind of stuff) answered questions from the audience. Most BarCamp attendees are averse to commercial promotions at the event and people were irritated a bit about it.

She talked about a device ( named as "SPARA" after a swedish word ) which can save power in your computer infrastructure. She highlighted the machine intelligence feature of the device. The Talk was good in one way as some people appreciated some thing original happening out of Kerala.

The Company name is Artin Dynamics and they are planning to OEM license the stuff.

D) Web Penetration in Kerala - Status Report & Forecast by Aravind Jose T.
-----------------------------------------------------------------------------------------------------------











Rather than giving a verbose talk , he created a short video and in under five
minutes he convinced all of us about the emerging web scenario in the context of Kerala. He mentioned about the lack of Business Transactional Web sites
centred towards the need of Kerala. This is an opportunity for some people to cash in.The case study of KottayamPatanam.com was interesting one.

E) An adhoc presentation by Ranjit K. Avarachan
---------------------------------------------------------------------












He mentioned about community based charity activities under taken by
a group of social media enthusiasts which he is also a part. Unfortunately,
I missed most his presentation.


F) Y Perl? by Alan Haggai Alavi
--------------------------------------------












The Session was good for a person who appreciates the power of Perl ( for that matter, anyone who has fiddled with Perl for a long time ) and he discussed about stuff like Perl EcoSystem , Perl versions,the origin of Perl etc.There was some mention about Parrot Virtual Machine as well. He also discussed about a Web framework (which i am not able to recollect ) in Perl. The talk did mention the war which perl lost out to php. ( According to him,perl is making a comeback )

He did not show any samples. ( The best way to convince the power of perl is to write some text processing stuff ) The Author has contributed some perl modules to the Comprehensive Perl Archive Network ( CPAN )


G) An Adhoc Presentation by Fahad Methar
--------------------------------------------------------------
He talked about an initiative started by him to treat a day of month ( 24th of every month - being his birth day ) not to do things one consider as "not so good". Being an HR professional (one's profession certainly colors the vision ) , he finds people are sinners ,liars and we need a day off from it.

There was a suggestion that why can't it be the first day of the month. My question was "Why undertake an activity well done by the Kerala Govt?" ( you cannot get liquor on the first day of the month out in the open...!)

He is very enthusiastic about the concept and there is good support for this group on the web. The "Ego" filled techies will find it bit hard to digest his vision.


H) An Adhoc talk by Father Abraham
----------------------------------------------------












He is the vision behind the MACFAST college and he talked about the need to bridge the gap between the Industry and the Academy. He has taken some concrete steps in this direction. He has talked to some companies in technopark and a financial services provider to start their offices in the campus. He names the concept "Earn While Learn".

I) Entrepreneurship by Adv. Ivin Gancius
---------------------------------------------------------












He really rocked !!!

First, He talked about how an individual can start his own business as Sole Proprietership and legal compliance issues associated with it. It was informative to know that if your turn over does not cross 40 lkh INR , you need not audit the account using a Chartered Accountant.

He talked about Limited Liability Partnership , Taxation issues, Private Limited Companies ,Trade Mark related issues to just name a few.

What i appreciated was his remarkable knowledge about the IT Eco System. Being a guy who has observed IT industry closely, he could communicate with the audience really well.

J) INTERNET SECURITY by RENI YOHANNAN
---------------------------------------------------------------
He talked about the Taxonomy of Security Attacks and for a newbie it was good. Little did he realize that the audience was full of people like him who keenly follow the attacks and exploits out on the internet.

He ended up irritating lot of guys in the audience. He did not understand the barcamp spirit of participatory discussions. When one of the guy mentioned about "RainBow" table attack on MD5 , he quickly brushed aside that ,Violating the Barcamp ethics of allowing another to speak if he is a better Subject Matter Expert on a point which needs clarification.

K) Cultural Invasion Vs Social Evolution by Kenney Jacob
---------------------------------------------------------------------------------












Kenney's presentation always triggers a debate and i think he has mastered the art of making people immerse in his talks by actively debating about each point. His theme was Marital Instituitions

He talked about Muslim Marriage and it's Polygamic connotations, Nair Polyandric System in Southern Kerala , Namboodiri Marriages , Devadasi system etc. After a heated discussion he told that monogamy is a jewish concept and it has been "imposed" on all of us. The point he was trying to put across was we cannot judge marital system just because it looks awkward at this point of time.

His slides ended up by asking "Will Co-habitation be the future of marriage ?".

I) Social Media Recruitment by Ajith Sam John (AJ)

---------------------------------------------------------------------------------
This was a peep into the future of recruitment of talent for projects. Anyone
who has seen the presentation , will understand the value of Linked In,FaceBook profiles in building a career. I think the guy has got some
vision and watch out for him.


I) Tata Jagriti Yatra by Niju Mohan
-------------------------------------------------












He discussed about Tata Jagriti Yatra and showed lot of footages of the event. He also discussed about the case study of Dabbawallahs(Mumbai).


J)WordPress-ing It by Arun Basil Lal
---------------------------------------------------












He talked about WordPress installation specifics, in detail. He also showed various features of the System. Being a energetic presenter , he did justice to his job.

K) eNGO Product Launch by Binny V A
------------------------------------------------------
This was the last presentation at the event and he talked about Make A Difference NGO who is the first client of eNGO. if you are an NGO , you can put up your ad there.














All together , it was a good barcamp event. I think a bunch of new kids have arrived at the scene who considers internet as a media and acceptance based on number (of visitors to your side ) will be the winners of the future.

Saturday, March 27, 2010

C/C++ Programming under Linux - Part 33

Welcome to the world of Xlib Programming. Xlib is a C library which acts as a thin wrapper over X Protocol. All GUI toolkits ultimately make a X call to implement their functionality. GTK+ wraps X through GDK library.

#include <stdio.h>   
#include <unistd.h>
#include <stdlib.h>
#include <X11/Xlib.h>
////////////////////////////////////////
//
// A Simple Xlib Program ( helloxlib.cpp )
//
// g++ -o helloxlib.exe helloxlib.cpp -lX11
// ./helloxlib.exe
//
//
int main(int argc , char **argv )
{
//------------ Open the Display
Display *dpy = XOpenDisplay(0);

if ( dpy == 0 )
{
fprintf(stderr,"Failed to Open Display \n");
exit(0);
}

//-------------- Create the Window by dispatching a call
//-------------- to X server
//-------------- consult Xlib manual for the meaning of
//-------------- each parameter
Window w = XCreateWindow(dpy,
DefaultRootWindow(dpy),
0, 0,
200, 100, 0,
CopyFromParent, CopyFromParent,
CopyFromParent,
0, 0);
XMapWindow(dpy, w);
XFlush(dpy);

//----------- Wait for Ten seconds to close
sleep(10);
}



In a way , writing this program was a nostalgic event. I still vividly remember the year 1995 where i encountered X through SCO unix.

[sandhya@localhost curses]$ g++ -o helloxlib.exe helloxlib.cpp -lX11
[sandhya@localhost curses]$ ./helloxlib.exe

C/C++ Programming under Linux - Part 32

We will get into the wonderful world of GUI programming in this Part. We are going to use gtk+ library to write our first GUI program.

Pls. consult documentation of Gtk+ and other sources to understand more about this exciting library.

///////////////////////////////////////
//
//
// A Hello world Program in GTK+
//
// g++ -o gtk_first.exe `gyk-config --cflags --libs` helloworld.cpp
// ./gtk_first.exe
//
#include <gtk/gtk.h>

void hello()
{
g_print("Hello World\n");
gtk_main_quit();

}

int main( int argc , char **argv )
{
// Controls are called GtkWidget
// in Gtk paralance
GtkWidget *window , *button;

//-------- init gtk library
gtk_init(&argc,&argv);


//--------- create a top level window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

//---------- create a button
button = gtk_button_new_with_label ("Hello World");

//------- connect clicked method to hello handler
gtk_signal_connect( GTK_OBJECT(button),"clicked",
GTK_SIGNAL_FUNC( hello ),NULL);

//-- add button to window and show both
gtk_container_add(GTK_CONTAINER(window),button);
gtk_widget_show(button);
gtk_widget_show(window);

//------- get into message loop
gtk_main();
return 0;
}


Here is how i compiled the program and ran it.


[sandhya@localhost gtk]$ g++ -o gtk_first.exe `gtk-config --cflags --libs` helloworld.cpp
[sandhya@localhost gtk]$ ./gtk_first.exe
Hello World
[sandhya@localhost gtk]$

I am using Fedora 10 , in some linux distributions there is chance for variations.

Friday, March 26, 2010

C/C++ Programming under Linux - Part 31

In this part , we will learn about Named Pipes. Named Pipe is one of the inter process communication mechanism available in Linux. Microsoft SQL server and client talks to each other using Named Pipes.

The Following Program spits to the console whatever recieved through the named pipe.

/////////////////////////////////////////
//
// Program to demonstrate Named Pipes
//
// g++ -o np_server.exe echo_server.cpp
// ./np_server.exe & ( Run in the back ground )
//

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main( int argc , char ** )
{
//-------------- if named pipe does not exist
//-------------- create one
if ( access("named_pipe",F_OK ) == -1 )
{
int stat = mkfifo("named_pipe",0777);

if ( stat != 0 )
{
fprintf(stdout,"Failed to create named pipe\n");
exit(-1);
}

}

//----------- open the fifo queue in read only mode ...
int pipe_handle = open("named_pipe",O_RDONLY );

if ( pipe_handle == -1 )
{
fprintf(stdout,"Failed to open the named pipe\n");
exit(-2);
}

int num_read;
char buffer[128];
memset(buffer,0,128);

while (( num_read = read( pipe_handle,buffer,127)) != -1 )
{
//------ nothing has been read from the client
if ( num_read == 0 ) {
sleep(5);
}



if ( strcmp(buffer,"QUIT\n") == 0 )
exit(0);

if ( buffer[0] == 0 )
continue;
printf("%s \n",buffer);
memset(buffer,0,128);
}

close(pipe_handle);

}



From the shell , we can access named pipes by giving it's name...

[sandhya@localhost pipe]$ ls -l named_pipe

prw-rw-r-- 1 sandhya sandhya 0 2010-03-27 13:24 named_pipe

[sandhya@localhost pipe]$


After compiling the program , let us start the server.

[sandhya@localhost pipe]$ g++ -o np_server.exe echo_server.cpp
[sandhya@localhost pipe]$ ./np_server.exe &
[1] 3366

Now send some data to the named pipe from the shell

[sandhya@localhost pipe]$ echo "Hello World" > named_pipe
Hello World

[sandhya@localhost pipe]$ echo "Hello World2" > named_pipe
[sandhya@localhost pipe]$ Hello World2

Now send the Quit message

[sandhya@localhost pipe]$ echo "QUIT" > named_pipe
[sandhya@localhost pipe]$
[1]+ Done ./np_server.exe
[sandhya@localhost pipe]$

C/C++ Programming under Linux - Part 30

In Part 26 , we learned about the signal API under Linux. In this part , we will learn about POSIX compliant signal API.

/////////////////////////////////////
//
// POSIX Signaling API demo
//
// g++ sigaction.cpp
// ./a.out
//
//
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

////////////////
//
// CTRL-C handler
//
void SigIntHandler( int sig , siginfo_t *siginfo , void *ign )
{
printf("Pressed CTRL-C to exit \n");
exit(0);
}


int main()
{
struct sigaction action;
memset(&action,0,sizeof(action));
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = SigIntHandler;
sigaction(SIGINT,&action,0);


while ( 1 )
;


}


[sandhya@localhost cross]$ g++ sigaction.cpp
[sandhya@localhost cross]$ ./a.out
^CPressed CTRL-C to exit
[sandhya@localhost cross]$

C/C++ Programming under Linux - Part 29

In this part , we will see how we can use memory mapped files. Memory Mapped Files are a handy mechanism by which we can treat a file as a memory based array. Whatever we write to the mapped array will go to the file.

One nice example is open a file , map it as a array of characters and use C/C++ string functions to search a file. Microsoft Windows uses Memory mapped files for loading it's executables.

The following program writes values from [0-19] to a file.
The file is once again opened and mapped as an array of int.

We double the value in the array. ( *2 )
When we dump the contents of the file, we will see values in the range [0-38]
/////////////////////
// A Program to demonstrate Memory Mapped files
//
// g++ -o mmaptest.exe mmaptest.cpp
// ./mmaptest.exe
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>

//////////////////////////////
//
// This Function Writes values to a binary file.
// This function will write values from 0-19
// Total file size will be 80 bytes
// 20*sizeof(int)
//
void PopulateValues()
{
int fd = open("TEST.bin",O_RDWR | O_CREAT );

if ( fd == -1 )
{
fprintf(stdout,"Failed to open TEST.bin\n");
exit(0);
}

int val;

for( val = 0; val < 20; ++val )
write(fd,&val,4 );

close(fd);
}

//////////////////////////////
//
//
//
int main()
{
PopulateValues();

int fd;
if ( ( fd = open("TEST.bin",O_RDWR) ) == -1 )
{
fprintf(stdout,"Failed to Open TEST.bin");
exit(-1);
}

////////////////////////////////////////////
// Map the entire contents of a file to a pointer...
//
int *mapped_values = (int *)
mmap(0,
20*sizeof(int),
PROT_READ | PROT_WRITE,
MAP_SHARED,fd,0 );

/////////////////////////////////////////////
//
// Now we can treat the whole file as an array of integer
//
int val;
for(val=0; val<20; ++val )
mapped_values[val] *= 2; // double the value...

msync((void *)mapped_values,20*sizeof(int),MS_ASYNC);
munmap((void *)mapped_values,20*sizeof(int));

//////////////////////////////////////////////////////
// Now reset the file pointer and dump the values...
//
lseek(fd,0,0);

for( int i=0; i<20; ++i )
{
read(fd,&val,4);
printf("%d\n",val);
}
close(fd);

}


Here is the dump of my terminal window...

[sandhya@localhost cross]$ g++ -o mmaptest.exe mmaptest.cpp
[sandhya@localhost cross]$ ./mmaptest.exe
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
[sandhya@localhost cross]$

C/C++ Programming under Linux - Part 28

In the previous part , i did show how to dump the environment variable. There was a flaw in the program. I changed the status of the global variable by incrementing the pointer variable.

///////////////////////
//
// A Program to Dump Environment Variable
// g++ dumpenv.cpp
// ./a.out
//
//
#include <stdio.h>

//------------ Global Environment variable
extern char **environ;

//-------- Cycle through each element and dump it.
int main()
{
//------- declare a new pointer
//------- to point to global variable
char **envp = environ;

while ( *envp != 0 )
puts(*envp++);

//--- The global variable environment is still valid
//----here
}

C/C++ Programming under Linux - Part 27

In Part 3 , i talked about emiting Environment Variables. I used the third parameter of the main function to dump the environment variables.

int main( int argc , char **argv , char **envp );


In every Linux C/C++ Program , there is a global variable by the name
environ which keeps track of the environment variable.

To access it , the following declaration should be there in your program.

extern char **environ;

The following program demonstrates how we can use this variable.

////////////////////////
//
// A Program to Dump Environment Variable
// g++ dumpenv.cpp
// ./a.out
//
//
#include <stdio.h>

//------------ Global Environment variable
extern char **environ;

//-------- Cycle through each element and dump it.
int main()
{
while ( *environ != 0 )
puts(*environ++);

}

C/C++ Programming under Linux - Part 26

In this part, we will learn about signal handling in Linux. While a program is running , we can use CTRL-C to terminate the program. We will make it mandatory that a user has to press CTRL-C three times to terminate this program.

///////////////////
//
// Press CTRL-C three times to terminate this program
//
// g++ -o Signal.exe Signal.cpp
//
// ./Signal.exe

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

void interrupt_handler( int cause )
{
static int test = 0;

test++;

#if 0 // make this 1 to compile #if part
if ( test == 3 )
exit(0);
else
printf("Press CTRL-C %d times\n",
3-test );
#else

if ( test == 2 ) {
//--------- Reset to the defualt behaviour
signal(SIGINT,SIG_DFL);
}
else
printf("Press CTRL-C %d times\n", 3-test );
#endif

}


int main( int argc , char **argv )
{
signal( SIGINT , interrupt_handler);
while (1)
{
//----------- an infinite loop to demonstrate signal
//------------ handling
}
return 0;
}


C/C++ Programming under Linux - Part 25

In this previous part , we changed (to) the directory to get information in each directory. This is dangerous , if you are running as a root and program terminated prematurely.

In this part , we will modify the program to avoid this pitfall.

/////////////////////////////////
//
// This Program demonstrates Recursive Directory
// Traversal
//
// g++ -o Directory2.exe Directory2.cpp
// ./Directory2.exe
//
//
//

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>

//////////////////////
//
// This Routine does not change directories to traverse the
// directory for it's entries.
//

void TraverseDirectory( char *DirName , int level )
{
DIR *dir = 0;
struct stat status;
struct dirent *direntry;
char TempDir[8192];

strcpy(TempDir,DirName);
if (( dir = opendir(TempDir) ) == 0 ) {
fprintf(stdout,"Failed to Get into %s\n",DirName);
return;
}

while ( ( direntry = readdir(dir) ) != 0 )
{
//------------- Copy the Directory name to Temp Dir
//------------- Add the entry (can be directory or file )
strcpy(TempDir,DirName);
strcat(TempDir,direntry->d_name);

//--------- We need to give the fully qualified
//--------- path name to retrieve the status
lstat(TempDir,&status);


if ( S_ISDIR( status.st_mode ) )
{
if ( direntry->d_name[0] == '.')
continue;
fprintf(stdout,"%*s%s/\n",level ,
"", direntry->d_name );
//--------- TempDir , contains fully qualified file name
TraverseDirectory(TempDir,level + 4 );
}

else {
fprintf(stdout,"%*s%s/\n",level ,
"",direntry->d_name );

}
}
closedir(dir);
return;
}

int main( int argc , char **argv )
{
char directory_buffer[8192];
strcpy(directory_buffer,"/usr/lib/");
TraverseDirectory(directory_buffer,0);

}


C/C++ Programming under Linux - Part 24

In this part , we will extend the program we wrote in the step 23 to support sub directory processing. This is a well known algorithm to most programmers.

/////////////////////////////////
//
// This Program demonstrates Recursive Directory
// Traversal
//
// g++ -o Directory.exe Directory.cpp
// ./Directory.exe
//
//
//

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>

////////////////////////////
// A Recursive Directory Traversal Routine
//
//

void TraverseDirectory( char *DirName , int level )
{
DIR *dir = 0;
struct stat status;
struct dirent *direntry;

//---------------- open the directory
if (( dir = opendir(DirName) ) == 0 ) {
fprintf(stdout,"Failed to Get into %s\n",
DirName);
return;
}

//---------- change the directory
chdir(DirName);

//------------ Try to read the contents of the
// current directory
while ( ( direntry = readdir(dir) ) != 0 )
{
lstat(direntry->d_name,&status);

if ( S_ISDIR( status.st_mode ) )
{
if ( direntry->d_name[0] == '.')
continue;
fprintf(stdout,"%*s%s/\n",level ,"",
direntry->d_name );

//---------- Recurse to Process the Sub directory
TraverseDirectory(direntry->d_name,level + 4 );
}

else {
fprintf(stdout,"%*s%s/\n",level ,
"",direntry->d_name );

}

}
//------------ move to the previous directory
chdir("..");
//------------ close the directory
closedir(dir);
return;
}


int main( int argc , char **argv )
{
char directory_buffer[8192];
strcpy(directory_buffer,"/usr/lib/");
TraverseDirectory(directory_buffer,0);

}


C/C++ Programming under Linux - Part 23

In this Part , we will start fiddling with Directory Access functions in Linux.
The opendir/readdir/closedir/ model is very similar to open/read/close programming model for accessing files. The only difference is addition of dir suffix with every call.


The Algorithm for dumping the contents of a Directory is as follows

a) Open Dir ( opendir )
b) As long as there is entry , Read an entry ( readdir )
c) Check the Directory Bit , if true , it is a directory
d) Otherwise , print file name , loop back to [b]
e) When finished , Close the Directory. (closedir)

The following program given below puts the algorithm into practice...

/////////////////////////////////
//
// This Program demonstrates simple Directory
// Traversal
//
// g++ -o SimpleDir.exe SimpleDir.cpp
// ./SimpleDir.exe
//
//
//

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>

/////////////////////////////////////
//
// Emits the content of Directory ...
//

void DumpDirectory( char *DirName )
{

DIR *dir = 0;
struct stat status;
struct dirent *direntry;
char TempDir[8192];

if (( dir = opendir(DirName) ) == 0 ) {
fprintf(stdout,"Failed to Get into %s\n",DirName);
return;
}

//----------------- Read one entry at a Time
//----------------- Check the status ,
//------------ if Directory give appropriate message
while ( ( direntry = readdir(dir) ) != 0 )
{
strcpy(TempDir,DirName);
strcat(TempDir,direntry->d_name);
lstat(TempDir,&status);

//----------- Check the Directory Bit
if ( S_ISDIR( status.st_mode ) )
{
//---------- ignore . , ..
if ( direntry->d_name[0] == '.')
continue;

fprintf(stdout,"Directory @ %s\n",
direntry->d_name );

}

else {
fprintf(stdout,"File @ %s\n",
direntry->d_name );

}
}
//------------- Finished Read the Directory...
closedir(dir);
return;
}

////////////////////////////////
//
// Driver Program
//

int main( int argc , char **argv )
{
DumpDirectory("/usr/lib/");

}

// Eof SimpleDir.cpp


Thursday, March 25, 2010

C/C++ Programming under Linux - Part 22

In this part , we will enhance the program which we wrote in the part 21. We will be using waitpid ( wait-process-id call ) to query the exit status of the child program.

////////////////////////////
//
// A Demonstrate Forking of a new process....
// The Program waits for the status of the child process to continue
//
// g++ -o forktest2.exe forktest2.cpp
// ./forktest2.exe
//
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>


int main( int argc , char **argv )
{
//------------ Every Process has a Process id
printf("PARENT Process ID = %d\n",getpid() );

int child_pd;
int ps_status;

// Here we make the fork system call to create a child process

if ( ( child_pd = fork() ) == 0 ) {

//---------- We check the return value of Fork call
//--------- if it is zero , this part is child process
printf("CHILD Process ID = %d\n",getpid() );
exit(3);
}
// continue with the parent process
// if the waitpid call succeded , we will get the
// child process id as return value

if ( waitpid(child_pd,&ps_status,0) != child_pd )
{
// if there is no error , we should not reach here
printf("Error In execution of Child Process\n");

}

if ( WIFEXITED(ps_status) ) {
printf("The child process terminated properly with status %d\n",
WEXITSTATUS(ps_status));
}

printf("PARENT Process ID = %d\n",getpid() );

}




Here is the dump of my terminal window


[sandhya@localhost procs]$ g++ -o forktest2.exe forktest2.cpp
[sandhya@localhost procs]$ ./forktest2.exe
PARENT Process ID = 3273
CHILD Process ID = 3274
The child process terminated properly with status 3
PARENT Process ID = 3273
[sandhya@localhost procs]$

C/C++ Programming under Linux - Part 21

In this part , we will learn about Forking a child process. This was the way multi tasking was achieved at one point of time in Unix. Now a days , people use Pthreads to achieve the same objective.

insert a call to fork system call

int main()
{
//------------ do some logic

if ( fork() == 0 )
{
// This Part is child process
Exit once we finished the action
}

//---------- continue with the parent process logic

}



Here is a almost useless program which demonstrates the idea .

////////////////////////////
//
// A Simple Program to Demonstrate Forking of a new process....
//
// g++ -o forktest.exe forktest.cpp
// ./forktest.exe
//
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

int main( int argc , char **argv )
{
//------------ Every Process has a Process id
printf("PARENT Process ID = %d\n",getpid() );

int child_pd;

// Here we make the fork system call to create a child process

if ( ( child_pd = fork() ) == 0 ) {

//---------- We check the return value of Fork call
//--------- if it is zero , this part is child process
printf("CHILD Process ID = %d\n",getpid() );
exit(0);
}

// continue with the parent process
// sleep for 3 seconds
sleep(3);
printf("PARENT Process ID = %d\n",getpid() );

}


Tuesday, March 23, 2010

A Timeless Book

As i am revisiting Unix/Linux Programming , from my shelf , I took a worn out copy of the book "Design of the Unix Operating System" by Maurice J Bach. Even after 25 years since it's publication , most of what he discusses is relevant to this day amazes me about the robustness of Unix/Linux Programming Model.

The Chapter on System Calls , System Calss for Files , Unix Domain Sockets , Pipes are worth a read even if we you are not planning to program Unix/Linux. Another advantage of the book is it's short programs ( at least 100 of them ) to demonstrate the concept discussed.


I am also reading "The Art of Unix Programming" by Eric S Raymond to get more inspiration.

Saturday, March 20, 2010

C/C++ Programming under Linux - Part 20

In this Part , we will learn about Conditional Compilation , Bit Wise And to write a program to determine whether a value is even or not.

///////////////////////////////////
//
// g++ -o evenodd EvenOrOdd.cpp
// ./evenodd &lt;number>
//
//
#include <stdio.h>
#include <stdlib.h>


#if 1
// Make this Zero for alternate version to get compiled
//
// Check whether 0th bit (Mask is 1 ) is set
// for an Oddd number it will be set
//
bool IsOdd( long s ) {
return ( s&1 ) ? true : false;
}

#else
//
// if it is Odd , Modulus 2 should give 1
//
bool IsOdd( long s ) {
return ( s%2 == 1 ) ? true : false;
}
#endif

bool IsEven( long s ) {
return !IsOdd(s);
}



int main( int argc , char ** argv )
{
if ( argc != 2 ) {
fprintf(stdout,"Error in # of Arguments\n");
return -1;
}

long val = atol(argv[1]);

if ( IsEven( val ) )
printf("Even\n");
else
printf("Odd\n");
}


Thursday, March 18, 2010

What is dy/dx ?

Last week , I purchased a book titled "what is d /dx ?" written by a professor of Physics from TKM college of Engineering,Kollam. The Book published in Malayalam is an entertaining read and I suspect that any one who bothers to sift through one hundred odd pages (of the book) will understand the rationale of Differential Calculus ( I do not know,whether the same professor has published a book On Integral calculus in this spirit ) in a clear manner.

Having studied ( and applied ) Mathematics through English language all along my life , i found the book to be a amusing read. When you hear certain terms in your native language , you can "connect" better to the idea a term depicts.

The Publisher is Phasor Books. The cost of Book is Rs. 30 ( THIRTY ONLY ! ). Highly recommended to all Mallus around the world. I got my copy from M/S Modern Book House, Gandhari Amman Kovil Road,Trivandrum.

Uff... i forgot to Mention the author B. Premlet.

C/C++ Programming under Linux - Part 19

In this part , we will learn about open/read/write/close model of FILE I/O. These are system calls ( interrupt 80 calls ) and are exposed to the programmer as C callable subroutines.


//
// sysfile.cpp
//
// A program to copy file to another using Linux System Calls
// These are conveniently packaged as C called routine by glibc
//
// g++ -o sysfile sysfile.cpp
// ./sysfile sysfile.cpp sysfile.out
//

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main( int argc , char **argv )
{

if ( argc != 3 ) {
fprintf( stdout,"usage: sysfile <srcfile> <destfile> \n");
return -1;
}

int inp_fd; // input file descriptor
int out_fd; // output file descriptor


// use Linux System calls to open file...
// O_RDONLY => Read only access
if ( ( inp_fd = open(argv[1],O_RDONLY) ) == -1 )
{
fprintf(stdout,"Failed to Open src file\n");
return -2;
}

//
// This file should not exist when the call is made
if ( ( out_fd = open(argv[2],O_WRONLY | O_CREAT | O_EXCL) ) == -1 )
{
close(inp_fd);
fprintf(stdout,"Failed to Open desitnation file\n");
return -2;
}


char bfr[8192];
int num = -1;

while ( ( num = read(inp_fd,bfr,8192) ) == 8192 )
{
write(out_fd,bfr,8192);
}

if ( num > 0 )
write(out_fd,bfr,num);

close(inp_fd);
close(out_fd);

}



Consult the documentation on these functions and also learn about the system calls.

C/C++ Programming under Linux - Part 18

In this part , we learn about Binary I/O through C run time library calls. The Buffered I/O calls fread and fwrite is used to write and read from binary files.

// Program To Demonstrate Binary I/O using C run time library function
//
// fopen/fread/fwrite/fclose family of functions
//
// g++ -o mycp mycp.cpp
//
// ./mycp mycp.cpp mycp.tmp
//
#include <stdio.h>

int main( int argc , char **argv )
{
if ( argc != 3 ) {
fprintf( stdout,"usage: mycp <srcfile> <destfile> \n");
return -1;
}

FILE *inp , *outp;

inp = outp = 0;


if ( ( inp = fopen(argv[1],"rb") ) == 0 )
{
fprintf(stdout,"Failed to Open src file\n");
return -2;
}

if ( ( outp = fopen(argv[2],"wb") ) == 0 )
{
fclose(inp);
fprintf(stdout,"Failed to Open desitnation file\n");
return -2;
}


char bfr[8192];
int num = -1;

while ( ( num = fread(bfr,1,8192,inp) ) == 8192 )
{
fwrite(bfr,1,8192,outp);
}

if ( num > 0 )
fwrite(bfr,1,num,outp);

fclose(inp);
fclose(outp);


}


Wednesday, March 17, 2010

C/C++ Programming under Linux - Part 17

This Part is a small step into the wonderful world of TCP/IP network programming. The Berkeley Socket API is the standard API for programming TCP/IP networks. Even the WinSock Library is based on BSD socket API.

In TCP/IP communication , IP address will uniquely identify a machine. The IP + port # uniquely identifies a service. The service can be SMTP,POP3,HTTP,NNTP,Oracle,SQL server to name a few.

The example program connects to the google server to retrieve the index page on the server.

//
// txtfiledump.cpp
// Demonstrates C standard I/O which is portable
// across platforms
//
// g++ -o txtfiledump txtfiledump.cpp
//
// ./txtfiledump txtfiledump.cpp
//
#include <stdio.h>

int main( int argc , char **argv )
{
//----------- the program expects one argument
//----------- if there are more or less..it is considered as error

if ( argc != 2 ) {
fprintf(stdout,"Usage: txtfiledump &lt;filename> \n");
return -1;
}

//--------- argv[1] contains the first argument
//----------argv[0] contains the executable file name

FILE *fp = fopen(argv[1],"rt");

//--------- Failed to Open the file
if ( fp == 0 )
{
fprintf(stdout,"Error Opening File %s\n",argv[1] );
return -2;
}

char bfr[4096];

//----------- While not the end of file
//----------- take one line at a time and spit
while ( !feof(fp) )
{
fgets(bfr,4096,fp);
fprintf(stdout,"%s",bfr);
}

//--------- close the file ...
fclose(fp);
}




The Socket API treats Sockets like files and we can use read/write file I/O for socket I/O. In other words , Sockets are files.

C/C++ Programming under Linux - Part 16

In this part we will learn about C standard file I/O to dump the contents of a text file... ( binary file I/O will be covered in a future post )

This method is portable across platforms ( Win32 and Linux for sure )

//
// txtfiledump.cpp
// Demonstrates C standard I/O which is portable
// across platforms
//
// g++ -o txtfiledump txtfiledump.cpp
//
// ./txtfiledump txtfiledump.cpp
//
#include <stdio.h>

int main( int argc , char **argv )
{
//----------- the program expects one argument
//----------- if there are more or less..it is considered as error

if ( argc != 2 ) {
fprintf(stdout,"Usage: txtfiledump &lt;filename> \n");
return -1;
}

//--------- argv[1] contains the first argument
//----------argv[0] contains the executable file name

FILE *fp = fopen(argv[1],"rt");

//--------- Failed to Open the file
if ( fp == 0 )
{
fprintf(stdout,"Error Opening File %s\n",argv[1] );
return -2;
}

char bfr[4096];

//----------- While not the end of file
//----------- take one line at a time and spit
while ( !feof(fp) )
{
fgets(bfr,4096,fp);
fprintf(stdout,"%s",bfr);
}

//--------- close the file ...
fclose(fp);
}


Tuesday, March 16, 2010

C/C++ Programming under Linux - Part 15

In this part , we will learn how we can use pipes to sort a list of names. We are going to use sort command to achieve our objective.

// psp.cpp
//
// g++ -o psp.exe psp.cpp
// ./psp.exe
//

#include <stdio.h>
#include <unistd.h>

int main( int argc , char **argv )
{
FILE *fp = 0;

//---------- open a pipe for writing
if ( ( fp = popen("sort","w")) == 0 )
return -1;

//------------ write to the pipe
fprintf(fp,"Ziyad\n");
fprintf(fp,"Anand\n");
fprintf(fp,"Arjun\n");
fprintf(fp,"Ram\n");
fprintf(fp,"Hooper\n");

pclose(fp); // when you close the pipe sorting will happen..

return 0;

}

// EOF psp.cpp






[sandhya@localhost pai]$ g++ -o psp.exe psp.cpp
[sandhya@localhost pai]$ ./psp.exe
Anand
Arjun
Hooper
Ram
Ziyad
[sandhya@localhost pai]$
[sandhya@localhost pai]$

C/C++ Programming under Linux - Part 14

This part will use system function from stdlib.h to invoke a system command (Remember the Linux maxim .... Every Command is a Program and Every Program is a Command )

// syscall.cpp
//
//
// g++ -o test.exe syscall.cpp
//
// ./test.exe
//

#include <stdio.h>
#include <stdlib.h>

int main( int argc , char **argv )
{
int rc = system("ls -l");
return rc;
}

// eof syscall.cpp



g++ -o test.exe syscall.cpp

./test.exe

C/C++ Programming under Linux - Part 13

This part will show how one can redirect the output of a system command
to a program using Pipes. In the program given below , we use popen function to open a pipe for read access. All the program does is go through the pipe stream one line at a time and emit the line at the console.

// test.cpp
//
//
//g++ -o test.exe test.cpp
//./test.exe
//

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
FILE *fp=0;

//--------- open a pipe to the ls command
//--------- second parameter (r) indicates read

if ( ( fp = popen("ls -l","r") ) == 0 )
return -1;

char bfr[8192];

//--------- Read line at a time and spit to the console

while ( !feof(fp) )
{
fgets(bfr,8192,fp);
printf(bfr);
}

//-----------close the file
fclose(fp);
}




g++ -o test.exe test.cpp
./test.exe

C/C++ Programming under Linux - Part 12

This time i will introduce a handy utility called ObjDump. using this i will demonstrate a technique called Self Modifying Code. This material is bit advanced. I think any one with some familiarity of x86 Assembly language can master it.

Let us get into the action

//add.cpp
//This function add two integers ( to keep things simple )
//
//
//g++ -c -o add.o add.cpp // -c compile only
//

int Add( int a , int b ) { return a + b; }

// Eof add.cpp



Compile the source code to an object file . ( Do not link it !)

g++ -c -o Add.o Add.cpp // -c compile only

Here is the log

[sandhya@localhost new]$ cat add.cpp
int Add( int a , int b ) {
return a+b;
}

[sandhya@localhost new]$ g++ -c -o add.o add.cpp
[sandhya@localhost new]$ ls -l add.o
-rw-rw-r-- 1 sandhya sandhya 690 2010-03-16 23:05 add.o
[sandhya@localhost new]$

//--------------- Invoke ObjDump utility with -d -S switch

[sandhya@localhost new]$ objdump -d -S add.o

add.o: file format elf32-i386


Disassembly of section .text:

00000000 &lt;_Z3Addii>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 55 0c mov 0xc(%ebp),%edx
6: 8b 45 08 mov 0x8(%ebp),%eax
9: 01 d0 add %edx,%eax
b: 5d pop %ebp
c: c3 ret
[sandhya@localhost new]$



///----------------- Convert the Object Code into an array

char addfunc[] = "\x55\x89\xe5\x8b\x55\x0c\x8b\x45\x08\x01\xd0\x5d\xc3\x90";




using Memory Mapped File , we can execute the code


///
/// objtest.cpp

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>

/******************
Generated using ObjDump utility

0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 55 0c mov 0xc(%ebp),%edx
6: 8b 45 08 mov 0x8(%ebp),%eax
9: 01 d0 add %edx,%eax
b: 5d pop %ebp
c: c3 ret
********************************************************/


// --- The Above code snippet was converted to a string ....

char addfunc[] = "\x55\x89\xe5\x8b\x55\x0c\x8b\x45\x08\x01\xd0\x5d\xc3\x90";



int main( int argc , char **argv )
{

// Allocate memory from Heap

char *Code = (char *)malloc(100);

// Compute the Page #
unsigned page = (unsigned)&Code & ~( getpagesize() - 1 );


//-------------- Set the Executable Bit

if( mprotect( (char*)page, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC ) )
{
perror( "mprotect failed" );
exit( errno );
}

//--------------- Copy the Code from addfunc to Code
memcpy(Code,addfunc,sizeof(addfunc));

// Cast the Code into a Function Pointer
int (*AddFunc)(int , int ) = (int (*)(int,int))((void *)Code);

// invoke the method
int retval = AddFunc(4,5);

printf("%d\n",retval);

// ------------ Remove the executable bit

if( mprotect( (char*)page, getpagesize(), PROT_READ | PROT_WRITE ) )
{
perror( "mprotect failed" );
exit( errno );
}

//----------Free the heap allocated memory

free(Code);


}

/// EOF objtest.cpp





[sandhya@localhost new]$ g++ objtest.cpp
[sandhya@localhost new]$ ./a.out
9
[sandhya@localhost new]$


We have executed the code in the array. Experiment with this , you will be good in Array and Pointer manipulation. ( This code will work only on x86 32 machines )

C/C++ Programming under Linux - Part 11

Let us write a Program to print the executable file name

// exename.cpp 

#include <stdio.h>

int main( int argc , char **argv )
{

printf(argv[0]);
printf("\n");
return 0;
}

// Eof exename.cpp


Compile this using g++

g++ -o exename.exe exename.cpp

./exename.exe


Here is the log of my shell window

[sandhya@localhost new]$ g++ -o exename.exe exename.cpp
[sandhya@localhost new]$ ./exename.exe
./exename.exe
[sandhya@localhost new]$ cp exename.exe exe2.exe
[sandhya@localhost new]$ ./exe2.exe
./exe2.exe
[sandhya@localhost new]$

C/C++ Programming under Linux - Part 10

In the part 9 , we learned about file copying with the support of I/O redirection Operators

This time , we will write a filter which converts a file to upper case.

With Linux Operating System's ability to string together commands , this can become very powerful.

//
// fupper2.cpp
//
#include <stdio.h>
#include <ctype.h>

int main()
{

int c;

while ( ( c = getchar() ) != EOF )
putchar(toupper(c));

return 0;

}


// here is the log of shell
[sandhya@localhost new]$ cp fcopy2.cpp fupper2.cpp
[sandhya@localhost new]$ gedit fupper2.cpp
[sandhya@localhost new]$ g++ -o fupper2.exe fupper2.cpp
[sandhya@localhost new]$ ./fupper2.exe < fupper2.cpp > fupper2.out
[sandhya@localhost new]$ cat fupper2.out
#INCLUDE <STDIO.H>
#INCLUDE <CTYPE.H>

INT MAIN()
{

INT C;

WHILE ( ( C = GETCHAR() ) != EOF )
PUTCHAR(TOUPPER(C));

RETURN 0;

}


[sandhya@localhost new]$

C/C++ Programming under Linux - Part 9

Or Else What K& R did not tell you !!!

There is a program which does file copying in the K&R book

//
// fcopy2.cpp
// g++ -o fcopy.exe fcopy2.cpp
// ./fcopy.exe < fcopy2.cpp > fcopy2.dup
//
#include <stdio.h>
#include <ctype.h>

int main()
{

int c;

while ( ( c = getchar() ) != EOF )
putchar(c);

return 0;

}


// compile using g++

g++ -o fcopy.exe fcopy2.cpp


What the book does not tell you is , we have to give I/O redirection commands to give input as well as output file name

./fcopy.exe < fcopy2.cpp > fcopy2.dup

Here is the command prompt log

[sandhya@localhost new]$ g++ -o fcopy.exe fcopy2.cpp
[sandhya@localhost new]$ ./fcopy.exe < fcopy2.cpp > fcopy2.dup
[sandhya@localhost new]$ cat fcopy2.dup
#include <stdio.h>
#include <ctype.h>

int main()
{

int c;

while ( ( c = getchar() ) != EOF )
putchar(c);

return 0;

}


[sandhya@localhost new]$

To fully comprehend this , try to learn more about I/O redirection operators like
< , > , | .

Monday, March 15, 2010

A nice book on Linux Internals ( other than Kernel )

After listening to an entertaining presentation on Buffer Overflows by Amal Krishnan @ Owasp meetup in Kochi , i stumbled upon a book which deals with the issues and techniques discussed by him and more.

The title of the book is "Programming Linux Hacker Tools uncovered" and the author is Ivan Sklyarov. The cost of the book is Rs. 360 and is available from BPB publications or some local dealer.

One nice thing about the book is good coverage of Standard Unix/Linux tools like Ping , TraceRoute , Port Scanners and other goodies. He gives source code of custom versions of these utilities. The book will be a good introduction to Advanced Socket Programming , Posix Threads and how these fit together to write Exploits.

There is a full chapter which deals with Stack and Heap Buffer Overruns. The book contains lot of Shell Code for experimentation and if you are familiar with intel x86 instruction encoding , the book will help you write your own. ( This code is specific to IA-32 architecture. For other architectures , the idea will be same but encoding will be different )

I have just skimmed through the pages . IMHO , is a book worth it's money.

Sunday, March 14, 2010

"Better People" Syndrome

All these days , i understood that i was suffering from a "malady" called "Better People" Syndrome , which is rampant among techies.

Most techies from my era , are "pissed" off when they see guys with half their knowledge make a cry by naming themselves as gurus.

In olden days , there was a belief in tech circles that four good people can be better than 10 average people while composing a team. There were some confirming events to this belief as there was no medium like internet , google search and only initiated people were of use.

The times changed , and with easy access to knowledge and ability to network with techies who can be geographically anywhere , the momentum tilted in favor of bigger teams. As Napolean remarked once , "The God smiles at bigger armies" ( The Quality can be beaten by Brute force quantity )


When Netherlands and Korea Play soccer it is expected that Dutch will win every time. Give One extra player to Korean team. The Korean team will win
more times. ( As the differential between players at international level is small)

Do not underestimate the advantage of numbers. Redefine intelligence ( I have re-defined it for myself )

The Success of Wipro , Infosys , CTS and others in india shows there is advantage in numbers. ( An average free-lancer will beat the best in Wipro/Infy in most cases ... but, the wisdom and sweat of crowds help getting modern projects into completion )

To manage this madness ( forced upon them ) , these companies appoint Project Managers , Delivery managers , all sorts of hierarchies. ( Try to put a structure on the chaos )

The Next time , you meet a guy from these companies , do not frown upon them. They are supposed to perform in an environment which believes in number game. Those managers has to come from the ranks of Civil Servants , Retd. Army officers , Bank Managers or guys with heavy engineering background etc...!!!


The moral of the story is , from a bottom line perspective , more technical knowlege is a classic case of Diminishing Returns.

OWASP meet up at Kochi

Yesterday (14th march 2010 ), I went for OWASP meeting which was held at Cyber Prism , Kacheri Pady , Kochi.

As usual after a chit with other attendees , we went to the venue hall. The session was taken by Amal Krishnan , from Cognizant Technologies , Kochi.

He gave a good primer on Linux user management , Executable internals , x86 architecture specifics , gdb and shell code before getting on to his demonstration programs.

It is one of the best sessions i have attended in recent years. The slides were really good in it's content and presentation.

The Key take aways for me were

a) Linux user rights management
b) directory bit/owner/group/others bit layout internals
c) setuid/setgid stuff
d) Gdb commands ( i took notes ! )
e) Shell code specifics
f) Modus operandi of injecting a Shell code into an executable
g) Nostalgia of my younger days ( hacking MSDOS !!! )


You can contact Amal Krishnan for getting slides @ his email id (amalkrishnan@acm.org)

Those of you who could not turn up will benifit from the slides and
one can join OWASP mailing list by following instructions at this page

Thursday, March 11, 2010

C/C++ Programming under Linux - Part 8

This time we will learn how to find the dependencies of a library created by you.

ldd executable will give the dependencies

the syntax is

ldd

[sandhya@localhost shared]$ ldd libAdd.so
linux-gate.so.1 => (0x00110000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x0015e000)
libm.so.6 => /lib/libm.so.6 (0x00250000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00113000)
libc.so.6 => /lib/libc.so.6 (0x00279000)
/lib/ld-linux.so.2 (0x0013b000)
[sandhya@localhost shared]$

C/C++ Programming under Linux - Part 7

In Part 5 , when we linked a shared library created by us , we were forced to give
./ prefix before the library name (./libAdd.so )

By exporting LD_LIBRARY_PATH with the current directory we can eliminate that

export LD_LIBRARY_PATH=.;$LD_LIBRARY_PATH

After this it is easy to link the shared library


[sandhya@localhost shared]$ g++ -fpic Add.cpp -shared -o libAdd.so
[sandhya@localhost shared]$ g++ -o test.exe mn.cpp libAdd.so
[sandhya@localhost shared]$ ./test.exe
7
[sandhya@localhost shared]$

if we give the current directory in the path through PATH environment variable , there is no need to give ./ while executing our executable

export PATH=.:$PATH

[sandhya@localhost shared]$ export PATH=.:$PATH
[sandhya@localhost shared]$ test.exe
7
[sandhya@localhost shared]$

C/C++ Programming under Linux - Part 6

In the part 5 , we learned about Dynamic Linking ( or shared library ). The Shared library was linked by the LD linker with the main executable.

This time , we will learn how the application can load a DLL manually and call a method through Function Pointers.

In other words , it is a classic example of Dynamic Linking of a Shared Library ( Library is linked at the run time )

a) Key in The Shared library code ( Add.cpp )

// Add.cpp
#include <stdio.h>

extern "C" int Add( int x , int y ) {
return x + y;
}

// Eof Add.cpp




b) Compile the file Add.cpp into a shared library

g++ -fpic Add.cpp -shared -o libAdd.so

c) Key in the main file

// main.cpp
#include <stdio.h>
#include <dlfcn.h> // necessary for dynamic loading of shared libraries


typedef int (*BinaryFunction)( int x , int y );



int main()
{

void * lib = dlopen("./libAdd.so", RTLD_LAZY);

if ( lib == 0 ) {
printf("Failed to load the shared library \n");
return -1;
}

BinaryFunction bf = (BinaryFunction)dlsym(lib,"Add");

if ( bf == 0 ) {
printf("Failed to retrieve function pointer \n");
return -1;
}

int r = (*bf)(3,4);
dlclose(lib);

printf("%d\n",r);

return 0;
}

// Eof main.cpp



d) Compile the main program with libdl

g++ -o test.exe main.cpp -ldl


Here is the log of my shell


[sandhya@localhost shared]$ g++ -fpic Add.cpp -shared -o libAdd.so
[sandhya@localhost shared]$ g++ -o test.exe main.cpp -ldl
[sandhya@localhost shared]$ ./test.exe
7
[sandhya@localhost shared]$