Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 5th, 2005, 8:14 AM   #1
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
I need a little help to get started on CGI scripting

I'm a terrible novice at this, I only started getting interesting yesterday. So even if my question sounds stupid, try to answer it if you can.

Now, I have a background in static HTML and the C language.

I understand CGI scripts are pieces of code written in Perl or actual executables to be run from the server. Now, if I compile a C program and I place it in the cgi-bin folder of the server root, why does the server not "execute" it?

I tried something very simple, a file named "database.txt" and each time the script was supposed to be lannched it would modify the file somehow (i.e. add a character to it).

I'm running the Abyss Web Server to test the future site, but it doesn't want to run the executable. (I attempted to install Apache yesterday, but I gave up after 2 hours of tinkering with it - why doesn't it come in Windows friendly?). Well Abyss thing runs static javascript et. all, but I can't get it to run cgi although it should according to the specifications.
Aphex_Twin is offline   Reply With Quote
Old Mar 5th, 2005, 10:18 AM   #2
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
I actually managed to compile a script to do something, yet it can't open files. Any help would be most welcome

Here is the source of the test program I compiled:
#include <stdio.h>
#include <stdlib.h>

main()
{
FILE *f;
f=fopen("http:\\\\127.0.0.1\\cgi-bin\\database.txt","a");
if (f==NULL)
{
printf("Content-type: text/html\n\n");
printf ("<p>ERROR!</p>");
}

fprintf (f, "%c ", 'X');
fclose(f);
}


This outputs "ERROR!" all the time and the file is not modified (although it exists at that location)
Aphex_Twin is offline   Reply With Quote
Old Mar 5th, 2005, 10:44 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You want forward-slashes, mate.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 5th, 2005, 11:32 AM   #4
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
Nope, still doesn't work. I removed the slashes altogether and placed the file in the same folder as the cgi. So how does file management work (preferably with C)?
Aphex_Twin is offline   Reply With Quote
Old Mar 24th, 2005, 9:15 AM   #5
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
f=fopen("http:\\\\127.0.0.1\\cgi-bin\\database.txt","a");

There's your problem. I know the backslash/forwardslash thing has been sorted out, but there's more trouble here than that. fopen()'s first argument is supposed to be a filename, not a URL; to open a URL takes a lot more work (with sockets) and isn't what you want to do here anyway.

If the file is in the same directory as the CGI program, open it with:

f = fopen("database.txt", "a");

If you can't open for writing, check to make sure the file (database.txt) isn't set read-only. It might also be worth getting your CGI program to check if its present working directory actually is the program it's in - this isn't always true for CGi scripts, which sometimes run with root (/) as their PWD, in which case you will have to give the full path.

I haven't tested this snippet, but it should work:

#!/usr/bin/perl -wT

print "Content-type: text/plain\015\012\015\012";
print "Document Root: ".$ENV{'DOCUMENT_ROOT'}."\n";

If you put this in your cgi-bin and make sure it's set executable (and possibly that it's saved with a .pl or .cgi extension), it'll tell you your document root. On my development machine, this gives me /var/www/htdocs (it's a Slackware box with Apache).

What I really think you need, though, is a good book on CGI. Did you know that the arguments are sent as environment variables to the script? That POSTed form data can be read from the CGI script's standard input?

You're also best off with a good CGI library. I've never actually done any extensive CGI programming in C so I don't know of one for C. If you're using Perl, the CGI.pm Perl module is great. These libraries help make it easier to access your arguments, receive multipart uploads, and they'll even often help generate HTML code so you don't have to include it in your program, which can be a bit laborious.

Finally, CGI programming is Unix programming. Unix is the operating system of the internet. I don't care what anyone may say about market share; there's a reason why the slashes in URLs face that way. So you might find Eric S. Raymond's "The Art of UNIX Programming" a nice read.

Hope this helps!
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC