Solved

need cgi in c on unix

Posted on 1998-02-16
3
168 Views
Last Modified: 2013-12-25
I need a cgi program to be written in C and run on AIX.
It will take in variables from a form thru' POST method.
The CGI program will simply read inputs from the form and ouput the values to an output text file.
Any idea how I can do this?  
0
Comment
Question by:slok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 150 total points
ID: 1831996
Here is some code borrowed from my personal library:

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

#define output_file "/path_to_an/output.file"

#define MAX_KEY 32    /* max length of a key string */
#define MAX_VALUE 128 /* max length of a value string */

/* the key-value pairs structure */
typedef struct {
  char key[MAX_KEY];
  char value[MAX_VALUE];
} key_value;


/* local functions */
static char *get_query(void);
static int parse_query(char *, key_value **);
static void process_query(int, key_value *, FILE *);

/* to store error messages */
static char err_str[128];

void err()
{
  fputs("<HTML><BODY>\n", stdout);
  fprintf(stdout, "<H3>%s</H3>\n", str);
  fputs("</BODY></HTML>\n", stdout);

  exit(EXIT_SUCCESS);
}

int main(void)
{
  FILE *fp;
  char *query_str; /* query string */
  key_value *pkv;  /* key_value pairs */
  int kv_no;       /* key_value pairs count */

  fputs("Content-Type: text/html\n\n", stdout);
  fflush(stdout);

  if(!(fp = fopen(output_file, "a"))) {
    strcpy(err_str, "Internal error: please contact administrator");
    out_err();
  }

  if(!(query_str = get_query())) out_err();

  if(!(kv_no = parse_query(query_str, &pkv))) out_err();
  }

  /* print out results */
  process_query(kv_no, pkv, fp);

  fclose(fp);

  return EXIT_SUCCESS;
}


static char *
get_query(void)
{
  char *temp, *str;
  size_t size;

  if(!(temp = getenv("REQUEST_METHOD"))) {
    strcpy(err_str, "Cannot get REQUEST_METHOD");
    return NULL;
  }

  if(!strcmp(temp, "POST")) {                   /* method is POST */
    if(!(temp = getenv("CONTENT_LENGTH"))) {
      strcpy(err_str, "Cannot get CONTENT_LENGTH");
      return NULL;
    }
    size = atoi(temp)+1;
    if(!(str = malloc(size+1))) { /* an extra char for faster parsing */
      sprintf(err_str, "Cannot allocate query string: %s", strerror(errno));
      return NULL;
    }
    fgets(str, size, stdin);
    return str;
  }

  /* unknown method */
  sprintf(err_str, "Unsupported REQUEST_METHOD: %s", temp);
  return NULL;
}


#define PURGE(s) do {                                          \
  register char *curs, *base;                                    \
                                                      \
  for(curs=base=(s); *curs; curs++, base++)                        \
    switch(*curs) {                                          \
      case '+':                                                \
        *base = ' ';                                          \
        break;                                                \
      case '%':                                                \
        curs++;                                                \
        *curs = *curs < 65 ? *curs - 48 : (*curs & 0xdf) - 55;            \
        curs++;                                                \
        *curs = *curs < 65 ? *curs - 48 : (*curs & 0xdf) - 55;            \
        *base = (*(curs-1) << 4) | *curs;                        \
        break;                                                \
      default:                                                \
        *base = *curs;                                          \
    }                                                      \
  *base = 0;                                                \
} while(0)

static int
parse_query(char *query_str, key_value **ppkv)
{
  register char *qstr;
  register int i, which;
  int kv_no;
  key_value *pkv;

  if(!*query_str) {
    strcpy(err_str, "Empty query");
    return 0;
  }
  strcat(query_str, "&"); /* here is the extra char */

  /* first pass to get number of key-value pairs */
  for(kv_no=0, qstr=query_str; *qstr; qstr++)
    if(*qstr == '&') kv_no++;
  if(!(*ppkv = malloc(kv_no * sizeof(key_value)))) {
    sprintf(err_str, "Cannot allocate key-value pairs: %s", strerror(errno));
    return 0;
  }

  for(i=0, which=1, pkv=*ppkv, qstr=query_str; *qstr;)
    switch(*qstr) {
      case '=':
        pkv->key[i] = 0;
        i = 0;
        which = 0;
        qstr++;
        break;
      case '&':
        if(which) {
          pkv->key[i] = 0;
          PURGE(pkv->key);
        }
        else {
          pkv->value[i] = 0;
          PURGE(pkv->value);
        }
        i = 0;
        which = 1;
        pkv++;
        qstr++;
        break;
      default:
        if(which) {
          pkv->key[i++] = *qstr;
          if(i == MAX_KEY-1) for(; *qstr!='=' && *qstr!='&'; qstr++);
          else qstr++;
        }
        else {
          pkv->value[i++] = *qstr;
          if(i == MAX_VALUE-1) for(; *qstr!='&'; qstr++);
          else qstr++;
        }
    }

  return kv_no;
}


static void
process_query(int kv_no, key_value *pkv, FILE *fp)
{
  register int i;
  register key_value *_pkv;

  fputs("<HTML><BODY>\n", stdout);
  fputs("<H3>Here's the form you submitted:<H3>\n", stdout);
  fputs("<TABLE BORDER>\n", stdout);
  fputs("<STRONG><TR><TD>Keys</TD><TD>Values</TD></TR></STRONG>\n", stdout);

  for(_pkv=pkv, i=0; i<kv_no; i++, _pkv++) {
    fprintf(fp, "%s = %s\n", _pkv->key, _pkv->value);
    fprintf(stdout, "<TR><TD>%s</TD><TD>%s</TD></TR>\n", _pkv->key, _pkv->value);
  }

  fputs("</TABLE></BODY></HTML>\n", stdout);

  fputs("----\n", fp);
}
--//--

Please, check the includes on your OS.
And be sure, if you cut and paste, to eliminate trailing blanks from the PURGE(s) macro lines (otherwise you'll get a mess of error messages).

HTH, julio

BTW, would you mind increasing a bit the points?
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1831997
Sorry, where is:

void err()

should be:

void out_err(void)
0
 
LVL 3

Author Comment

by:slok
ID: 1831998
thanks... I have up the points for it.
I will try it out and get back to you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question