Source needed for ENCTYPE receiving program

Posted on 1997-07-03
Last Modified: 2008-03-06
I'm in desperate need of some C source for a CGI program that will receive a file via the ENCTYPE tag that Netscape supports and then write it into onto the disk. The source needs to be for Windows (I use the Micro$oft compiler).

Yes I know that this sort of thing is asking for security problems but I have a specific, Intranet application for it.

Question by:mravell
  • 2

Author Comment

ID: 1251981
Adjusted points to 150

Accepted Solution

ernst earned 150 total points
ID: 1251982
#include <string.h>
here's some c-code that I use. I'll include the html that goes
with it so you can substitute variables.  It's not for
windows, but the algorithm should be portable. It's really thrown
together, but you'll get the idea....

#include <stdio.h>
#include <unistd.h>
#ifndef NO_STDLIB_H
#include <stdlib.h>
#include <fcntl.h>
char *getenv();

typedef struct {
    char *name;
    char *val;
} entry;

#define MAX_ENTRIES 10

main(int argc, char *argv[]) {
   entry entries[MAX_ENTRIES];
        register int x,m=0;
        char header[50], openfile[128], endline[128];
        int cl, readlen, ctr, filenum;
        char *breakline, *wline, *ptr;
        FILE *out;

        sprintf(header,"Content-type: text/html%c%c",10,10);
        write(STDOUT_FILENO, header, strlen(header));
        printf("<BODY BGCOLOR=#404040 TEXT=#FFFFFF>\n");
        if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
            printf("This script should be referenced with a METHOD of POST.\n");

        cl = atoi(getenv("CONTENT_LENGTH"));
        total = malloc(cl);
        ctr = 0;
        while(cl > 0){
           read(STDIN_FILENO, &total[ctr++], 1);
        total[ctr] = 0;
        ptr = strchr(total, 13);
        breakline = total;
        *ptr++ = 0;
        total = ++ptr;
        sprintf(endline, "\n%s", breakline);
        do {
           ptr = strchr(total, 13);
           *ptr = 0;
           for (ctr = 0;ctr < 2;ctr++)
           total = ptr;
           ptr = strchr(total, 13);
           for (ctr = 0;ctr < 2;ctr++)
           total = ptr;
           ptr = strstr(wline,"name=");
           for (ctr = 0;ctr < 6;ctr++)
           entries[m].name = ptr;
           ptr = strchr(entries[m].name,'"');
           *ptr = 0;
           if (strcmp(entries[m].name, "fileitself") == 0) {
           ptr = strstr(entries[m].val, breakline);
           x = strlen(breakline) + 4;
           for (ctr = 0;ctr < x;ctr++)
           total = ptr;
        } while (strstr(total, "Content") != NULL);
   for (x = 0; x < m; x++) {
      if (strcmp(entries[x].name,"filenam") == 0)
         filenam = entries[x].val;
      else if (strcmp(entries[x].name,"userid") == 0)
         userid = entries[x].val;
      else if (strcmp(entries[x].name,"password") == 0)
         password = entries[x].val;
      else if (strcmp(entries[x].name,"fileitself") == 0)
         file = entries[x].val;
  if (strcmp(userid,"userid") != 0 || strcmp(password,"passwd") != 0) {
      printf("<H1>BAD USERID OR PASSWORD</H1>\n");
   if ((out = fopen(openfile, "w")) == NULL) {
      printf("file open error\n");
      printf("%s\n", openfile);
   while (strstr(ptr,breakline) != ptr) {
      fprintf(out, "%c", *ptr);
   printf("<H1>File uploaded</H1>\n");

########################Begin Html###########
<TITLE>Upload a file</TITLE>
<H3>upload a file to your directory</H3>
<FORM ENCTYPE="multipart/form-data" ACTION=/cgi-bin/upload METHOD=POST>
the name you want the file to be called on the server:
<INPUT TYPE=text NAME=filenam><BR>
your userid: <INPUT TYPE=text NAME=userid><BR>
your password: <INPUT TYPE=password NAME=password><BR>
file: <INPUT TYPE=file NAME=fileitself><BR>
<INPUT TYPE=submit value="go for it!">


Author Comment

ID: 1251983
Thanks Ernst,

This has given me enough to get started. I am porting it to Micro$oft C for use on NT.


Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Is string constant address ? 10 189
How to set environment variables in C 2 69
C dll call freezes 5 88
Passing command line arguments in C 10 81
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now