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?

Search Google

Books that I refer to...

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