Help with C, CGI and IIS 5

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Help with C, CGI and IIS 5

Postposted on Wed Aug 18, 2010 6:26 am

I am experimenting with CGI programming using C. Using IIS 5 as a webserver, everything seems to work fine in the browser. But I want to access resources outside the browser, like create files and directories and do a fair amount of folder management involving moving files etc between those folders. I thought I would simply use the system() function to execute console commands like copy, move etc. But it's not working. When I try to find the current drive, it tells me it's drive C. But when I try to get the current directory, it simply returns null. Using _mkdir() creates directories in the root of C drive. But I can't copy any files to that directory using the copy command. Creating another directory within the newly created directory is possible again with _mkdir. Are there any C functions that mimic the functions of the copy, move console commands?
Igor_Kavinski
Gerbil XP
 
Posts: 307
Joined: Fri Dec 22, 2006 2:34 am

Re: Help with C, CGI and IIS 5

Postposted on Wed Aug 18, 2010 7:47 am

I'm not a windows developer, but in POSIX land you would use the rename() call to do a mv whilst for a copy simply open the new file for write, the old file for read and loop through it. I wonder if you are running in to permissions issues more than anything else. Check the permissions set up in your webserver and make sure that the directories you are trying to access allow access by that user.
notfred
Grand Gerbil Poohbah
 
Posts: 3704
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Help with C, CGI and IIS 5

Postposted on Wed Aug 18, 2010 11:11 am

I'm pretty sure it's not permissions related because I can create as many directories as I want. But the console based commands all fail. Only library functions seem to work. Does that mean I have to call the system() function using some sort of privilege elevating function? How do I even go about doing that?
Igor_Kavinski
Gerbil XP
 
Posts: 307
Joined: Fri Dec 22, 2006 2:34 am

Re: Help with C, CGI and IIS 5

Postposted on Wed Aug 18, 2010 12:10 pm

Three things:

a) Your permissions are those of the webserver user. So you should set permissions on your folders accordingly: KB entry.
b) Messing with folders outside your web document directory is usually a Bad Idea.
c) If you already know some C, just use PHP (or Perl) and save yourself the hassle of compiling stuff, and the slowness of running it under CGI. Oh, that, and those languages are a much better fit for scripting than C is.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9932
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Help with C, CGI and IIS 5

Postposted on Wed Aug 18, 2010 1:37 pm

morphine wrote:c) If you already know some C, just use PHP (or Perl) and save yourself the hassle of compiling stuff, and the slowness of running it under CGI.
I'm confused by the "slowness of running it under CGI" part. Can you explain a bit more?
notfred
Grand Gerbil Poohbah
 
Posts: 3704
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Help with C, CGI and IIS 5

Postposted on Wed Aug 18, 2010 2:51 pm

notfred wrote:
morphine wrote:c) If you already know some C, just use PHP (or Perl) and save yourself the hassle of compiling stuff, and the slowness of running it under CGI.
I'm confused by the "slowness of running it under CGI" part. Can you explain a bit more?

I should have written "as CGI" instead of "under CGI", which doesn't make much sense.

When a scripting language is run via CGI, a new Apache/IIS process has to be created. When they're run via the respective webserver modules this overhead can be avoided. AFAIK FastCGI is a lot better, but a module is still preferable.

Even if performance isn't something to worry about in this specific case, there's little reason for the OP to have to run the rigmarole of writing / compiling / deploying a C program when a much more convenient script equivalent would do.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9932
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Help with C, CGI and IIS 5

Postposted on Wed Aug 18, 2010 7:40 pm

OK, understand it now.
Thanks!
notfred
Grand Gerbil Poohbah
 
Posts: 3704
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Help with C, CGI and IIS 5

Postposted on Thu Aug 19, 2010 7:21 am

I was finally able to get the file and folder moving functionality working by using the win32 CopyFile() and DeleteFile() functions. MoveFile() doesn't seem to work in Visual Studio 6. Hmmm...

There are some really fascinating C/C++ web programming frameworks out there like the Wt web toolkit and TNT webserver. Unfortunately, I find learning to be a very painful and time-consuming process. So for the time being, I will have to keep hacking at C code using CGI. Sigh.

The speed issues of CGI won't matter much to me since only a handful of people will be using my web app in an office LAN environment.
Igor_Kavinski
Gerbil XP
 
Posts: 307
Joined: Fri Dec 22, 2006 2:34 am

Re: Help with C, CGI and IIS 5

Postposted on Thu Aug 19, 2010 9:02 am

<?php

$src= 'c:/something';
$target= 'c:/otherthing';

rename( $src, $target );

unlink( $src.'/some_file' );
?>

It doesn't get much easier than this :)
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9932
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 3 guests