Tuesday, December 9, 2008

Interesting C Program -24

Hi all,
Here is the alpha version of the shell that I recently developed. This program runs fine in Linux. This shell will accept max 3 arguments.

Here's the program:

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/wait.h"
char InputData[100];
char Command[100];
char Parameter1[100];
char Parameter2[100];
unsigned int CommandLength = 0;
unsigned int Parameter1Length = 0;
unsigned int Parameter2Length = 0;
unsigned int i=0;

void read_command(void);

int main()
{
int status;
printf("[Balaji]>");
while(1)
{
read_command();
if(fork()!=0)
{
//waitpid(-1,&status,0);
wait(&status);
}
else
{
if((Parameter1[0] == '\0')&&(Parameter2[0]=='\0'))
{
execlp(Command,Command,'\0');
}
else if((Parameter1[0] != '\0')&&(Parameter2[0]=='\0'))
{
execlp(Command,Command,Parameter1,'\0');
}
else
{
execlp(Command,Command,Parameter1,Parameter2,'\0');
}
}
printf("[Balaji]>");
for(i=0;i<CommandLength;i++)
{
Command[i] = '\0';
}
for(i=0;i<Parameter1Length;i++)
{
Parameter1[i] = '\0';
}
for(i=0;i {
Parameter2[i] = '\0';
}
}
}
void read_command(void)
{
unsigned char ParameterStart = 0;
gets(InputData);
for(i=0;InputData[i]!='\0';i++)
{
if((InputData[i]!=' ')&&(ParameterStart==0))
{
Command[i]=InputData[i];
CommandLength = i;
}
else if((InputData[i]==' ')&&(ParameterStart ==0))
{
Command[i]='\0';
CommandLength=i;
ParameterStart = 1;
}
else if((InputData[i]!=' ')&&(ParameterStart==1))
{
Parameter1[i-CommandLength-1] = InputData[i];
Parameter1Length = i-CommandLength-1;
}
else if((InputData[i]==' ')&&(ParameterStart==1))
{
Parameter1[i-CommandLength] = '\0';
Parameter1Length = (i-CommandLength);
ParameterStart = 2;
}
else if((InputData[i]!=' ')&&(ParameterStart==2))
{
Parameter2[i-CommandLength-Parameter1Length-1] = InputData[i];
Parameter2Length = i-CommandLength-Parameter1Length-1;
}
else if((InputData[i]==' ')&&(ParameterStart==1))
{
Parameter2[i-CommandLength-Parameter1Length] = '\0';
Parameter2Length = (i-CommandLength-Parameter1Length);
ParameterStart = 3;
}

}
if((InputData[i]=='\0')&&(ParameterStart==0))
{
Parameter1[0] = '\0';
Parameter2[0] = '\0';
Parameter1Length = 1;
Parameter2Length = 1;
}
}


.
gcc -o balaji shell.c
./balaji

Saturday, December 6, 2008

Interesting C Program -23

Hi all,
Its long time since I updated my blog. This time, a real weird puzzle is here for you to solve.
Observe the following program...


#include "stdio.h"
#include "stdlib.h"
int main()
{
char data[20];
printf("Type Something Here with spaces: ");
scanf("%s",&data);
printf("%s",data);
system("pause");
return 0;
}


when I run the code, I asks us to type something with spaces.
I typed "abcd efgh".
Try to guess the output.



The output is just "abcd".

Alas what happened to "efgh"!!!

The answer is in scanf's implementation.

It has been mentioned in "http://www.opengroup.org/onlinepubs/009695399/functions/scanf.html", that,
s
Matches a sequence of bytes that are not white-space characters. The application shall ensure that the corresponding argument is a pointer to the initial byte of an array of char, signed char, or unsigned char large enough to accept the sequence and a terminating null character code, which shall be added automatically.



Isn't it weird?

Wednesday, January 16, 2008

Interesting C Program -22

Hi all,
This month's LFY had an interesting C Program that caught my attention. Lets discuss that in detail.

I have the following C Program.

#include "stdio.h"
int main()
{
struct value
{
int val:1;
};
struct value x;
x.val = 1;
printf("%d",x.val);
return 0;
}




What could be the output of the following Program?

I too thought that the output would be 1. But its not 1, but -1. Why?


The explanation goes like this.

The structure member has been declared "int", which is "signed int"

We all know that the MSB of a signed int will be the sign bit. If it is 1 then the number is negative and if it is zero the number is positive.

Right, now my question is, if the val takes the MSB ie. in binary format 1000 0000 0000 0000, which should be a -0. If the val takes LSB ie. in binary format 0000 0000 0000 0000, which should return 1. Why is it printing -1?


The explanation goes like this.

Any signed integer will be represented in its two's complement form.

Try out the following program.

#include "stdio.h"
#include "math.h"
void func(int x);
int main()
{
int x=-10;
func(x);
return 0;
}
void func(int x)
{
unsigned char x1;
signed int i;
for(i=15;i>=0;i--)
{
x1 = ((x/(unsigned int)pow(2,i))%2)+0x30;
printf("%c",x1);
}
}


This will print -10 in binary format. If you look at it, you will get "1111111111110110", which is two's complement of 10.


So the output, 1000 0000, will be printed in two's complement form, which is 1000 0001. This is signed integer. So outputs -1 instead of 1.

Search Google

Books that I refer to...

  • The Complete Reference C, Fourth Edition
  • The C Programming Language