Sunday, December 17, 2006

Your own uname!

Adding another simple code to find some basic system information on Linux/Unix machines.
There are some API's, you will need to do some input parsing, which you can add to make this act as your own "uname" command!!

I have not added those to keep the code clean and simple....KISS - "Keep It Simple Stupid"



#include "sys/utsname.h"
#include "iostream"
#include "stdlib.h"

int main()
{
struct utsname name; // Structure to hold the system info.

if(uname(&name)==-1){
perror("System error:: ");
return -1;
}
cout << "sysname == " << name.sysname <<
" release == " << name.release <<
" version == " << name.version <<
" machine == " << name.machine <<
endl;
return 0;
}

NOTE : The above code compiles under Linux/Unix for sure. Have not tested on other platforms...


Here are the functions I wrote for parsing the input string....You can use them if you want
CALLING PI is : parse_argv(argc, argv, name); // You will have to add this in main after the if{}

template < class T>
void
printErr(T str_ch)
{
cout << "Invalid option..." << str_ch <<
"\tUsage: uname [-m][-r][-v][-s]" << endl;
}

void
parse_argv(int argc, char** argv, const utsname& sysinfo)
{
while(--argc) { // Check arguments supplied or not as argc == 1 imp no args
char* str = *(argv+ argc);
if('-' == *str ) { // - imp option specifed or not
while( *++str ) {
switch( *str )
{
case 's': cout << " System == " << sysinfo.sysname << endl; break;
case 'm': cout << " Machine == " << sysinfo.machine << endl; break;
case 'v': cout << " Version == " << sysinfo.version << endl; break;
case 'r': cout << " Release == " << sysinfo.release << endl; break;
default : printErr(*str);
}
}
} else {
printErr(*(argv + argc));
}
}
}

Tuesday, December 12, 2006

Fun with programming

Well another interesting piece of code I came across on some website....sorry I forgot the source. But I claim no responsibility or originality of the code....

It is called a quine....
Well quine is a piece of code that outputs itself exaclty.
Search on the net and you will find many more of thses examples. Search for "quine"

Here is the one i liked!
main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}

Just compile and run the code....and enjoy the beauty of "C programming"

****************************************************************************

Here is another of my favourite fun code in C....
Program without the omni present main()....

// Code to print hello without main()
// compile gcc -nostartfiles filename.c

#include "stdlib.h"
#include "stdio.h"
_start()
{
printf("Hello World\n");
exit(0); // If no exit seg fault "Cannot find bounds of current function"
}

That is all folks...
I might update the page if i find some new interesting things...till then
Happy Coding :)

Monday, December 11, 2006

Remove those spaces!!

Its been a long time since I have posted something new and useful...well have been really busy with life!!

Here is a code I wrote for fun...it removes all the multiple tabs and blank spaces in a string.

Feel free to use it, but do give credit to the Author :)
And if you find any issues...or feel like giving some feed back...you are most welcome!!



/*
* Date: 10th December 2006
* Simple Utility code written by hundredrabh
* It simply removes more than 4 spaces and tab stops
* from the input string and replaces them with space.
* You can download and play with the code
* But please do give due credit to the author for his efforts!!
* Happy Coding
*/

#include "stdio.h"
#include "stdlib.h"

/* Function declaration and defination */

/* char* enterString(char*)
* Accepts a char pointer, to return the input string
* returns a pointer to the input string, and this pointer needs to be freed by the user!
* Error checking needs to be added for return values and realloc
// TODO - Enhance it for buffered reading/writing
// As realloc is slow....
*/
char*
enterString(char* str)
{
char ch;
int len = 0;
while(10 != (ch = fgetc(stdin)))
{
if(0 == (len % 512)) {
str = (char*)realloc(str, len + 513); // TODO - Need Error Checking here...
}
*(str + len) = ch;
*(str + len + 1) = '\0'; // adding the null termination as realloc does not return 0ed memory..so EOS determination is difficult.
++len;
}
return str; // This needs to be freed by the user
}

/* End of enterString(char*) */

/* DETAB
* void detab(const char*)
* returns a void, accepts a char*
* remove all the tab spaces or spaces which are in multiples of 4 from the string
* Spaces before and after a tab will be considered as continuous!!
*
*/
void
detab(char* str)
{
char* dummyRetStr = str;
int spaces = 0;
while(*str)
{
switch(*str)
{
case '\t': // Remove the tab
*str = ' ';
++spaces;
break;
// check for 4 spaces together
case ' ' :
++spaces;
break;
default :
spaces = 0;
break;
}
*dummyRetStr++ = *str++;
if(spaces == 4) dummyRetStr -=3, spaces =1; // Remove 4 spaces occuring together!
}
*dummyRetStr = '\0'; // null terminate the modified string
}


int
main()
{
char *inp = 0;
printf("Enter the string \n");

inp = enterString(inp); // Free inp, it has been malloced
detab(inp);

printf("You entered .... %s\n", inp);
realloc(inp, 0);
return 0;
}