web services in c with cgi

I was trying to setup a simply webservice to reply to POST requests. Earlier this was being done in tomcat which seem a little overkill to me since I already had a webserver up and running. So, a quick c program to respond to request is all that I needed. And here is the result.

/*
 * =====================================================================================
 *
 *       Filename:  Login.cpp
 *
 *    Description:
 *
 *        Version:  1.0

 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Amit Agarwal (),
 *   Organization:
*
* == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == =
    */
#include 
#include 
#include 


#define MAXLEN 1024

#ifdef DBG
#define DEBUG(a,b) fprintf(stderr, a, b);
#else
#define DEBUG(a,b) 
#endif

#define HEAD "n"
#define TAIL ""

int main(void)
{

    char input[MAXLEN];
    char * data;
    char*lenstr;
    long len;


    printf("%s%c%cnn",
           "Content-Type:text/xml;charset=iso-8859-1", 13, 10);

    // len = getenv("QUERY_STRING"); // This is for GET
    lenstr = getenv("CONTENT_LENGTH");
    DEBUG( "Length string is %sn", lenstr);

    if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > MAXLEN)
        printf("Error in invocation - wrong FORM probably.");
DEBUG( "Length is %ldn", len);
{
int count=0;
while ( count < len )
input[count++] = getchar();
input[count]='?';
DEBUG ( "Read characters = %dn", count)
}

//fprintf(stderr, "VAlue is %sn", input);
///unencode(input + 5 , input + len, data);

data = input;
DEBUG("Data value is %sn", data);

printf(HEAD);
printf("Everything else goes heren");
printf(TAIL);

fprintf(stderr, "Sent resp: tid[%s], eaid[%s], tcode[%s]n", tid, eaid, tcode);
}

Additionally, the makefile:

all:
gcc -o Login Login.c -Wall

debug:
gcc -o Login Login.c -Wall -DDBG=1

And finally, compile this with make command and put in the cgi-bin folder and that is all that you need ūüôā

C program to get the ascii string from a string

Example diagram of the printf function in the ...
Example diagram of the printf function in the C programming language (Photo credit: Wikipedia)

Lot of times, you would like to get the complete string in hex or ascii format and if you are one of them then this is something that will be helpful for you ūüôā

Example output:

lp-amita[d=~/bin]> ./ascii aamit
String – aamit, Length – 5

String : aamit
Hex    : 0x61616d6974
Dec    :   9797109105116

And the source code:

 

/*n* =====================================================================================
*
*       Filename:  ascii.c
*
*    Description:  ascii to dec
*
*        Version:  1.0
*        Created:  03/02/2012 12:08:49 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Amit Agarwal (aka)
*  Last modified: Thu Mar 08, 2012  08:47PM
*
* =====================================================================================
*/
void main (int argc, char **argv){
    int i=0;
    char a[200]="",b[200]="";
    printf("String - %s, Length - %dn",argv[1],strlen(argv[1]));

    while (i < strlen(argv[1])){
        sprintf(a,"%s%x",a,argv[1][i]);
        sprintf(b,"%s%d",b,argv[1][i]);
        i++;
    }
    printf ("n");
    printf ("String : %sn",argv[1]);
    printf ("Hex    : 0x%sn", a);
    printf ("Dec    : %sn", b);
}
Enhanced by Zemanta

colorgcc – Color your compiler output on Fedora

Lets start with installing colorgcc :

sudo yum install colorgcc

Now once that is done, you will need to ensure that the call to g++, gcc and others that you want to use, you will need to create a link in the “~/bin” directory, like so:

for i in g++ gcc c++ cc 
do
    ln -s $(which colorgcc) ~/bin/$i
done

The one liner above will create the links for colorgcc as your favourite compiler in your homedir. Don’t forget to add the bin diretory to your PATH variable:

export PATH=~/bin:$PATH

And now finally the ~/.colorgccrc file taken from Ubuntu for your reference:

#
# colorgcc configuration file
#
# $Id: colorgccrc,v 1.1.1.1 1999/04/25 15:24:03 jamoyers Exp $
#
# This file should be named $HOME/.colorgccrc
#
#
# The following groups of attributes may be combined for a given color:
#
# clear         black      on_black
# reset            red        on_red
# bold            green      on_green
# underline     yellow     on_yellow
# underscore    blue       on_blue
# blink            magenta    on_magenta
# reverse       cyan       on_cyan
# concealed     white      on_white
#
# For example, srcColor: bold cyan on_yellow
#

# gccVersion: if defined, append "-" to the compiler paths
# defined hereunder. Otherwise, those paths remains as is set
# gccVersion: 4.1.1

# Define the paths to the actual location of the various compilers.
# (Currently, colorgcc only understands these: g++ gcc c++ cc g77 f77 gcj)
g++: /usr/bin/g++
gcc: /usr/bin/gcc
c++: /usr/bin/g++
cc:  /usr/bin/gcc
#g77: /usr/bin/g77
#f77: /usr/bin/g77
#gcj: /usr/bin/gcj

# Don't do color if our terminal type ($TERM) is one of these.
# (List all terminal types on one line, seperated by whitespace.)
nocolor: dumb emacs

# Text between ` and ' is usually source code.
srcColor: bold cyan

# Text other than a warning or error.
introColor: reset

# Warnings and errors both have similar formats:
#    filename:999:Message
# Each field may be assigned a different color.

# Warnings
warningFileNameColor: reset
warningNumberColor:   blue
warningMessageColor:  yellow

# Errors
errorFileNameColor: reset
errorNumberColor:   blue
errorMessageColor:  bold red

# Possible translations, if you use localized compiler.
# (List all translations on one line, seperated by whitespace.)
warningTranslations:
errorTranslations:
Enhanced by Zemanta