Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Igor_Kavinski
Minister of Gerbil Affairs
Topic Author
Posts: 2077
Joined: Fri Dec 22, 2006 2:34 am

Help with C, CGI and IIS 5

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?
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Help with C, CGI and IIS 5

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.
 
Igor_Kavinski
Minister of Gerbil Affairs
Topic Author
Posts: 2077
Joined: Fri Dec 22, 2006 2:34 am

Re: Help with C, CGI and IIS 5

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?
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Help with C, CGI and IIS 5

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 :(
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Help with C, CGI and IIS 5

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?
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Help with C, CGI and IIS 5

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 :(
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Help with C, CGI and IIS 5

Wed Aug 18, 2010 7:40 pm

OK, understand it now.
Thanks!
 
Igor_Kavinski
Minister of Gerbil Affairs
Topic Author
Posts: 2077
Joined: Fri Dec 22, 2006 2:34 am

Re: Help with C, CGI and IIS 5

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.
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Help with C, CGI and IIS 5

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 :(

Who is online

Users browsing this forum: No registered users and 14 guests
GZIP: On