writing cgi programs in c: the system() call.

J

jleslie48

I've written a cgi program in C using the borland 5.5 free compiler,
and it runs just fine on an Apache server.

My only issue is if I issue some system calls the cgi suspends until
the call finishes. for example if in my c program I have a line:

system("notepad");


I can see with the taskmanager that an instance of notepad.exe is
running, but its not on my desktop and the cgi program that made the
system call suspends until I kill the notepad.exe process.

I even tried imbedding the the notepad call in a .bat file as so:

system("mybatfile.bat");

mybatfile.bat:
REM call a routine
notepad t.txt


I get the same result.

If I re-write mybatfile.bat to some routine that completes
automatically, all goes well for example:

mybatfile.bat:
REM a simple canned routine
dir >t.txt

this works fine and sure enough, there is a new file named t.txt
created.

What is the right way to call notepad from a cgi, I imagine one of the
variations of spawn() or exec() will do the trick; I'm hoping some
here will know.

TIA,

Jon
 
F

Flash Gordon

jleslie48 wrote, On 22/09/08 21:55:
I've written a cgi program in C using the borland 5.5 free compiler,
and it runs just fine on an Apache server.

Well, CGI's are not topical here, nor is your main problem which is a
fundamental miss-understanding of things.
My only issue is if I issue some system calls the cgi suspends until
the call finishes.

The C standard allows it to work that way.
for example if in my c program I have a line:

system("notepad");

Now, why on earth would you want to call notepad from a CGI? Where do
you expect notepad will run? Where do you expect notepad to display its
Window? Remember that notepad is a Windows program that pre-dates
Windows having built-in support for networking.

I even tried imbedding the the notepad call in a .bat file as so:

system("mybatfile.bat");

mybatfile.bat:
REM call a routine
notepad t.txt

Why would you get anything different?

What is the right way to call notepad from a cgi,

There is not a correct way to do this. I don't know what you are trying
to achieve, but whatever it is you are trying to use the wrong method.
Ask in a web-development group (or web forum) about the real problem
that you are trying to solve.
I imagine one of the
variations of spawn() or exec() will do the trick;

Neither of those are standard C and I don't think they are standard
Windows API calls either.
I'm hoping some
here will know.

You are in luck that someone here can see that whatever you want to do
you are using completely the wrong approach. As I say above, ask about
your real problem (you no, the one you think can be solved by running
notepad) in a web development group.
 
R

Richard Tobin

jleslie48 said:
My only issue is if I issue some system calls the cgi suspends until
the call finishes. for example if in my c program I have a line:

system("notepad");


I can see with the taskmanager that an instance of notepad.exe is
running, but its not on my desktop and the cgi program that made the
system call suspends until I kill the notepad.exe process.

I take it this is Microsoft Windows, which I'm not really familiar
with, so this is just guesswork: in the other windowing operating
systems I've used it's non-trivial for a "daemon" process like a web
server to start up programs that display in the window system. For
one thing, which user's desktop would they appear on? There is
usually some inherited context (such as the DISPLAY environment
variable in X on unix) that indicates that a program is running under
the window system.

Presumably there is documentation on the Microsoft web site about how
to do this, but I can't help with finding it.

You also have to consider just what it is you want to happen: what
good does it do for a CGI script to open something on the desktop?
Are you assuming that it's the person sitting at the screen who
has caused the script to be run?

-- Richard
 
J

jleslie48

I've written a cgi program in C using the borland 5.5 free compiler,
and it runs just fine on an Apache server.

My only issue is if I issue some system calls the cgi suspends until
the call finishes.  for example if in my c program I have a line:

system("notepad");

I can see with the taskmanager that an instance of notepad.exe is
running, but its not on my desktop and the cgi program that made the
system call suspends until I kill the notepad.exe process.

I even tried imbedding the the notepad call in a .bat file as so:

system("mybatfile.bat");

mybatfile.bat:
REM call a routine
notepad t.txt

I get the same result.

If I re-write mybatfile.bat to some routine that completes
automatically, all goes well for example:

mybatfile.bat:
REM a simple canned routine
dir >t.txt

this works fine and sure enough, there is a new file named t.txt
created.

What is the right way to call notepad from a cgi, I imagine one of the
variations of spawn() or exec() will do the trick; I'm hoping some
here will know.

TIA,

Jon

Thanks to all for your input. for reference purposes, I'm using a
windoze xp pro OS, the borland 5.5 C++ compiler, an apache server, and
no damned visual studio bloatware or windoze api behemoth. Just plain
old c, putting out plain old ascii, that the apache server sends out
to a browser that understands it as HTML code, nice and simple, ports
to any linux, any windoze OS, or HP-UX machine. The fact that its a
cgi is rather minor, accept that for some reason that I expect has to
do with the parent-child inheritance rules, the system call doesn't
launch the notepad process on the screen or the task bar at the
bottom.

I fully understand that this is a non-standard use of cgi, and I was
only expecting to use this utility in a http://localhost environment,
viz, the apache server that is responding to the form request from the
browser are on the same machine. In other words, I want to open a
browser window on the machine that is running the apache server, and
have it open a notepad session on a file that I want the nighttime
operator to do some changes to.

I've looked around for better fit usergroups, but after an hour or so,
this one seemed to be the only one that at least talks about
programming in C. None of the others seem to do any real
programming.
 
K

Kenny McCormack

Your explanation makes everything clear. Ignore the retards who
continue to pretend either that they have no idea what you are talking
about or assume (despite all your statements to the contrary) that you
are running on Unix and/or a headless server box.

Anyway, I think what you are looking for is:

system("start notepad");

That is, incidentally, the equivalent of:

system("notepad &");

in the Unix world...
 
K

Keith Thompson

CGI runs on a *server*. A server need not even have a monitor.
Even if it does, it may be used only when the system is rebooted,
and shared with a dozen other systems via a big KVM switch.


What does
system("notepad &");
do? It might avoid the suspension until the call finishes (it would
on UNIX), or it might just result in notepad complaining about its
command-line arguments.

The OP is running on Windows XP. Appending "&" to the command will do
nothing useful. For details, as in a Windows newsgroup.

To the original poster: There is (or was) a newsgroup that discusses
CGI programming, comp.infosystems.www.authoring.cgi. Or you can try
comp.os.ms-windows.programmer.win32. I don't think you'll get any
directly useful information in comp.lang.c, just suggestions to ask
elsewhere (which is useful, but only indirectly) and bad guesses.

I've restored the attribution line that Gordon snipped. Permission to
quote this article without attribution is denied.
 
F

Flash Gordon

Kenny McCormack wrote, On 23/09/08 00:30:
Your explanation makes everything clear.

It makes it clear the OP is trying to do something that won't work.
Ignore the retards who
continue to pretend either that they have no idea what you are talking
about or assume (despite all your statements to the contrary) that you
are running on Unix and/or a headless server box.

I made none of those assumptions.
Anyway, I think what you are looking for is:

system("start notepad");

Which won't work for the OP for precicely the same reasons that the OP
is not getting notepad displayed currently.
That is, incidentally, the equivalent of:

system("notepad &");

in the Unix world...

Something I at least was fully aware of. I did not suggest it because it
does not solve the OP's problem, and if Apache is even vaguely sensibly
designed it will make it as hard as possible to launch a usable instance
of notepad and Windows will actually cooperate in making it hard. This
is why the OP needs to discuss the real problem somewhere it is topical,
a completely different solution will be required.
 
K

Kenny McCormack

Kenny McCormack wrote, On 23/09/08 00:30:
It makes it clear the OP is trying to do something that won't work.

Maybe, maybe not. But the OP's specific problem - the reason he posted
(go back and look at the original text if you like) - was that the
system() function was "hanging" (a word that probably doesn't occur in
your precious "standard", but I think you know what I mean). He could
tell from "task manager" (or whatever system processes monitoring
software he is using) that notepad was running, but the problem was that
his batch file was "hanging". Using "start" will fix that. So, I have
answered the question as posted.

Now, as for whether or not that will cause a window to pop up on the
operator's screen - that's another question entirely. But that was *not*
the question posted. If that is an issue for the OP (and we have no way
of knowing whether it is - but I see that you have made your usual set
of assumptions), then that fact will come out eventually, and we can
deal with it when it does.
 
F

Flash Gordon

Kenny McCormack wrote, On 23/09/08 21:58:
Kenny McCormack wrote, On 23/09/08 00:30:

Maybe, maybe not.

Well, if you think the OP was trying to generate a hung process you
could have suggested easier methods.
But the OP's specific problem - the reason he posted
(go back and look at the original text if you like) - was that the
system() function was "hanging" (a word that probably doesn't occur in
your precious "standard", but I think you know what I mean). He could

I know exactly what he meant, and from the fact that he comments, "but
its not on my desktop" it is obvious to anyone who knows about Windows
and notepad that this is a problem.

Now, as for whether or not that will cause a window to pop up on the
operator's screen - that's another question entirely.

Now you are doing exactly what you complain about the "regs", including
me, doing and deliberately not dealing with the problem the OP obviously
has.
But that was *not*
the question posted.

It *was* included in the original post.
If that is an issue for the OP (and we have no way
of knowing whether it is - but I see that you have made your usual set
of assumptions),

You mean assuming the OP wants a solution that will work?
then that fact will come out eventually, and we can
deal with it when it does.

The only facts required to know the OP is barking up a tree in
completely the wrong forest were in the original post. If you don't know
enough about Windows to know that then don't complain at people who *do*
know enough for telling someone they need a completely different approach.
 
J

jleslie48

I've written a cgi program in C using the borland 5.5 free compiler,
and it runs just fine on an Apache server.

My only issue is if I issue some system calls the cgi suspends until
the call finishes.  for example if in my c program I have a line:

system("notepad");

I can see with the taskmanager that an instance of notepad.exe is
running, but its not on my desktop and the cgi program that made the
system call suspends until I kill the notepad.exe process.

I even tried imbedding the the notepad call in a .bat file as so:

system("mybatfile.bat");

mybatfile.bat:
REM call a routine
notepad t.txt

I get the same result.

If I re-write mybatfile.bat to some routine that completes
automatically, all goes well for example:

mybatfile.bat:
REM a simple canned routine
dir >t.txt

this works fine and sure enough, there is a new file named t.txt
created.

What is the right way to call notepad from a cgi, I imagine one of the
variations of spawn() or exec() will do the trick; I'm hoping some
here will know.

TIA,

Jon

thanks again for all answers. I will try the "start notepad" on
wednesday, I was away from my desk today. If my cgi.exe program
completes, that at least solves half my problem and gets me closer to
a solution. From the borland help files I can see that exec() and
spawn() have a whole host of variations and flavors, changing the
rules of dependency, parent-child status etc. I guess I just have to
use trial and terror to see. I was hoping someone would have tried
something like this. Maybe fork() would also work, however, that is
the one that I would of expected to fail though, as that WOULD work
with exactly the same envrionment as the apache server, aka, as a
background task without a user interface.

I highly doubt I'm running into any security issue, as there is
nothing from preventing me from executing a bat file that "del/q/f/s c:
\windows" as that command requires no user input. You'd think the boys
as Microsoft would of plugged that one before silly old notepad.

quite frankly I don't think this is a big stretch for this group.
This is pretty straightforward C programming, and not a cgi issue.
Most cgi programmers would be lost in the C world, and all their GUI
"write software for me" packages just make a mess out of understanding
things, so those folks are mostly useless. I think if you write any
background task for windoze xp and have it launch notepad I would have
the same issue, but I don't even know how I could write that. I'm
just a simple old command line programmer.

The html/cgi/forms environment is a very easy interface to work with,
gives me full access to all the installed files and software on my
computer, and I'm really just using the apache server as a means to a
nice interface so that the interns who run the place at night can be
trained much faster and their jobs are made as simple as possible.
Using this technique I've managed to get a whole host of things done
that I never could of without a staff of nighttime operators that were
much more computer literate. Now I want them to be dropped into a
notepad session for whatever reason I have. That reason is
unimportant. I'm quite sure it can be done. Heck the dos prompt does
it.
 
K

Keith Thompson

jleslie48 said:
quite frankly I don't think this is a big stretch for this group.
This is pretty straightforward C programming, and not a cgi issue.
Most cgi programmers would be lost in the C world, and all their GUI
"write software for me" packages just make a mess out of understanding
things, so those folks are mostly useless. I think if you write any
background task for windoze xp and have it launch notepad I would have
the same issue, but I don't even know how I could write that. I'm
just a simple old command line programmer.
[...]

Standard C has no concept of "background tasks", which is exactly why
this isn't the right place for your question.

Have you tried posting to comp.os.ms-windows.programmer.win32?
 
F

Flash Gordon

jleslie48 wrote, On 24/09/08 01:39:

thanks again for all answers. I will try the "start notepad" on
wednesday, I was away from my desk today. If my cgi.exe program

Well, it won't work. Feel free to waste your time though.
completes, that at least solves half my problem and gets me closer to
a solution. From the borland help files I can see that exec() and
spawn() have a whole host of variations and flavors, changing the

They are non-standard and only Boreland programmers will know about
them. However, they definitely should *not* work.

with exactly the same envrionment as the apache server, aka, as a
background task without a user interface.

Here you are getting closer to the problem, but there is more to it than
that.
I highly doubt I'm running into any security issue, as there is
nothing from preventing me from executing a bat file that "del/q/f/s c:
\windows" as that command requires no user input. You'd think the boys
as Microsoft would of plugged that one before silly old notepad.

It isn't notepad as such that is the problem. However, this is not the
correct place to discus it.
quite frankly I don't think this is a big stretch for this group.
This is pretty straightforward C programming, and not a cgi issue.

No, it's not a cgi problem, it's a Windows problem you would have with
all sorts of other software as well.
Most cgi programmers would be lost in the C world, and all their GUI
"write software for me" packages just make a mess out of understanding
things, so those folks are mostly useless.

Shows how little you know. I say that as someone who has written CGI's
in Unix and work with people who write web based applications.

The html/cgi/forms environment is a very easy interface to work with,
gives me full access to all the installed files and software on my
computer, and I'm really just using the apache server as a means to a
nice interface so that the interns who run the place at night can be
trained much faster and their jobs are made as simple as possible.

Now you are finding out why it is the wrong tool for the job.
Using this technique I've managed to get a whole host of things done
that I never could of without a staff of nighttime operators that were
much more computer literate. Now I want them to be dropped into a
notepad session for whatever reason I have. That reason is
unimportant. I'm quite sure it can be done.

So ask somewhere they might know about the internals of Windows.
Heck the dos prompt does
it.

The DOS prompt is running in a *very* different context. If you don't
understand that then you are a long way from being able to understand
what the problem is, and until you understand the problem you are in no
position to udge whether it might be possible.
 
J

James Kuyper

Flash said:
jleslie48 wrote, On 24/09/08 01:39: ....

They are non-standard and only Boreland programmers will know about
them. However, they definitely should *not* work.

I agree that they are non-standard, and therefore off-topic. However,
they are not Borland-specific; I've seen the same (or at least similar)
functions available on a wide variety of compilers.
 
K

Kenny McCormack

I agree that they are non-standard, and therefore off-topic. However,
they are not Borland-specific; I've seen the same (or at least similar)
functions available on a wide variety of compilers.

But that is the terminology of this newsgroup. If something is
implemented in 99% of the platforms that we care about (note that I am
not making that specific claim about the functions in question here),
they call it "implementation specific". They are not using the word
"specific" in the sense that ordinary English speakers do.

Note also that they claim that something is not C (and is no more C
than, say, Heathfield's ass) if it really is 98% C.

Total lack of common sense.
 
J

James Kuyper

Kenny said:
But that is the terminology of this newsgroup. If something is
implemented in 99% of the platforms that we care about (note that I
am not making that specific claim about the functions in question
here), they call it "implementation specific". ...

I would not have objected if he had called them "implementation
specific". That is exactly the correct term. He said that "only Boreland
programmers will know about them", and that is NOT correct.
... They are not using the word "specific" in the sense that
ordinary English speakers do.

Definition 4 a) at

<http://www.ask.com/web?q=dictionary:+specific&content=ahdict|5141&o=0&l=dir>

is the one that is most closely related to the concept of
"implementation specific". The example given by that definition is
"age-specific voting patterns". That term means "voting patterns which
are different for different age groups". An "implementation-specific
value" means a "value which is different for different implementations".

It doesn't matter if 99% of the age groups all vote identically; if the
remaining 1% of the age groups vote in a significantly different
pattern, the voting pattern is age-specific. Similarly, it doesn't
matter if a function is provided by 99% of the implementations. If the
remaining 1% of the implementations do not provide it, then support for
that function is implementation-specific.

....
Total lack of common sense.

Common sense isn't what's it's cracked up to be. It's neither common,
nor sensical, nor is it a good guide to follow when discussions get
technical.

Common sense used to say that the world was, overall, flat. Only those
scholars in their Ivory towers with their heads in the clouds would
natter on about how the evidence (such as lunar eclipses) showed that
Earth had to be spherical. I would not recommend navigating a vessel on
any transoceanic voyages using such "common sense". Stick to
coastal-hopping, it's safer.
 
F

Flash Gordon

James Kuyper wrote, On 24/09/08 11:41:
I agree that they are non-standard, and therefore off-topic. However,
they are not Borland-specific; I've seen the same (or at least similar)
functions available on a wide variety of compilers.

I'm not aware of them being available on a wide variety of Windows
compilers and would not guarantee they behave the same as the Posix
functions with similar names or the MS functions with slightly different
names. However, I do accept that they are not necessarily specific to
Borland.
 
J

jleslie48

]
Standard C has no concept of "background tasks", which is exactly why
this isn't the right place for your question.

Standard C is completely based on background tasks. the "system",
"exec", "fork", family of library functions are some of the most
fundamental of the language. K&R's first edition introduced them, the
entire UNIX operating system is built with them, and the current K&R
book continues with all of the constructs of the Parent-Child
paradigm.

The >system("start notepad");< command does free up my cgi to
respond, but alas as predicted, I still don't have a desktop
addressable notepad session.
 
J

jleslie48

Standard C has no concept of "background tasks", which is exactly why
this isn't the right place for your question.

I guess I'm running into trouble with the Windoze vs UNIX
implementations.
K&R and Pike detail very early on the relationship of C and the UNIX
OS with the "system", "exec", "fork", family of library functions as
some of the most
fundamental of the language.

The >system("start notepad");< command does free up my cgi to
respond, but alas as predicted, I still don't have a desktop
addressable notepad session.
 
C

CBFalconer

jleslie48 said:
.... snip ...

Standard C is completely based on background tasks. the "system",
"exec", "fork", family of library functions are some of the most
fundamental of the language. K&R's first edition introduced them,
the entire UNIX operating system is built with them, and the
current K&R book continues with all of the constructs of the
Parent-Child paradigm.

You are very wrong. Standard C doesn't include exec or fork in any
form. Unix is not built on Standard C, if that means "limited to".

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
<http://www.dinkumware.com/c99.aspx> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top