Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie with spawnl commamd??

0 views
Skip to first unread message

Steve Maughan

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
I'm newish to c++ programming. On my current task, the program is to run
another program depending on what number's selected from a menu. To do
this, I figured I'd need to use the spawn command to run the .exe file.
Problem is that I've been at it for a couple of hours and I can't get
the spawn command to work.

I don't suppose anyone coulf post the code for the spawn command to get
it to run a .exe file from the root (c:) directory on my hard drive?

Thanks

--
Steve Maughan

Don't run away from your problems.
Riding is much faster.


Sunil Rao

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
Steve Maughan wrote:
>
> I'm newish to c++ programming. On my current task, the program is to run
> another program depending on what number's selected from a menu. To do
> this, I figured I'd need to use the spawn command to run the .exe file.
> Problem is that I've been at it for a couple of hours and I can't get
> the spawn command to work.
>
> I don't suppose anyone coulf post the code for the spawn command to get
> it to run a .exe file from the root (c:) directory on my hard drive?

spawn() isn't a function specified by the ANSI standard for C++ - it
does however specify system() which you'll find declared in <stdlib.h>
or in the new style header <cstdlib>. If you really feel you have to use
spawn() you'll have to ask one of the experts in a group related to DOS
or to your compiler.

--
{ Sunil Rao }
"A boiled egg, you see, is not merely an egg that has been boiled.
It is a self-contained package, an irresistible alliance, of
violence and nursery rhymes. To get into it, you must destroy it."
- Terry Durack in "The Independent on Sunday", as quoted in
"Pseuds Corner" of "Private Eye" magazine!

Martin Ambuhl

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
Steve Maughan wrote:
>
> I'm newish to c++ programming. On my current task, the program is to run
> another program depending on what number's selected from a menu. To do
> this, I figured I'd need to use the spawn command to run the .exe file.
> Problem is that I've been at it for a couple of hours and I can't get
> the spawn command to work.
>
> I don't suppose anyone coulf post the code for the spawn command to get
> it to run a .exe file from the root (c:) directory on my hard drive?

The only way to run another program in standard C or C++ is to use
system(). If you must use spawn(), then a newsgroup for your
implementation is the right place to ask. Your implementation need not
define spawn in any particular way, since it is not a standard function
in either C or C++.

Just to try to be helpful, here is the blatently stolen info page for
spawn in the djgpp implementation of gcc. It includes the requested
example. Remember that your implementation need not be anything like
this:

spawn*
======

Syntax
------

#include <process.h>

int spawnl(int mode, const char *path, const char *argv0, ...,
NULL);
int spawnle(int mode, const char *path, const char *argv0, ...,
NULL /*, const char **envp */);
int spawnlp(int mode, const char *path, const char *argv0, ...,
NULL);
int spawnlpe(int mode, const char *path, const char *argv0, ...,
NULL /*, const char **envp */);

int spawnv(int mode, const char *path, const char **argv);
int spawnve(int mode, const char *path, const char **argv, const
char **envp);
int spawnvp(int mode, const char *path, const char **argv);
int spawnvpe(int mode, const char *path, const char **argv, const
char **envp);

Description
-----------

These functions run other programs. The PATH points to the program to
run, and may optionally include its extension. These functions will
look for a file PATH with the extensions `.com', `.exe', `.bat',
`.btm', `.sh', `.ksh', `.pl' and `.sed'; if none is found, neither in
the current directly nor along the `PATH', they will look for PATH
itself.

`.com' programs are invoked via the usual DOS calls; DJGPP `.exe'
programs are invoked in a way that allows long command lines to be
passed; other `.exe' programs are invoked via DOS; `.bat' and `.btm'
programs are invoked via the command processor given by the `COMSPEC'
environment variable; `.sh', `.ksh' programs and programs with any
other extensions that have `#!' as their first two characters are
assumed to be Unix-style scripts and are invoked by calling a program
whose pathname immediately follows the first two characters. (If the
name of that program is a Unix-style pathname, without a drive letter
and without an extension, like `/bin/sh', the `spawn' functions will
additionally look them up on the `PATH'; this allows to run Unix
scripts without editing, if you have a shell installed somewhere along
your `PATH'.) Any non-recognizable files will be also invoked via DOS
calls.

*WARNING!* DOS is rather stupid in invoking programs: if the file
doesn't have the telltale "MZ" signature of the `.exe' style programs,
DOS assumes it is a `.com' style image and tries to execute it
directly. If the file is not really an executable program, your
application will almost certainly crash. Applications that need to be
robust in such situations should test whether the program file is
indeed an executable, e.g. with calls to `stat' (*note stat::.) or
`_is_executable' (*note _is_executable::.) library functions.

Note that built-in commands of the shells can *not* be invoked via
these functions; use `system' instead, or invoke the appropriate shell
with the built-in command as its argument.

The programs are invoked with the arguments given. The zeroth argument
is normally not used, since MS-DOS cannot pass it separately, but for
compatibility it should be the name of the program. There are two ways
of passing arguments. The `l' functions (like `spawnl') take a list of
arguments, with a `NULL' at the end of the list. This is useful when
you know how many argument there will be ahead of time. The `v'
functions (like `spawnv') take a pointer to a list of arguments, which
also must be `NULL'-terminated. This is useful when you need to
compute the number of arguments at runtime.

In either case, you may also specify `e' to indicate that you will be
giving an explicit environment, else the current environment is used.
You may also specify `p' to indicate that you would like `spawn*' to
search the `PATH' (in either the environment you pass or the current
environment) for the executable, else it will only check the explicit
path given.

Note that these function understand about other DJGPP programs, and will
call them directly, so that you can pass command lines longer than 126
characters to them without any special code. DJGPP programs called by
these functions will *not* glob the arguments passed to them; other
programs also won't glob the arguments if they suppress expansion when
given quoted filenames.

*Note exec*::.

Return Value
------------

If successful and `mode' is `P_WAIT', these functions return the exit
code of the child process in the lower 8 bits of the return value.
Note that if the program is run by a command processor (e.g., if it's a
batch file), the exit code of that command processor will be returned.
`COMMAND.COM' is notorious for returning 0 even if it couldn't run the
command.

If successful and MODE is `P_OVERLAY', these functions will not return.

If there is an error (e.g., the program specified as `argv[0]' cannot
be run, or the command line is too long), these functions return -1 and
set `errno' to indicate the error. If the child program was
interrupted by <Ctrl-C> or a Critical Device error, `errno' is set to
`EINTR' (even if the child's exit code is 0), and bits 8-17 of the
return value are set to `SIGINT' or `SIGABRT', accordingly. Note that
you must set the signal handler for `SIGINT' to `SIG_IGN', or arrange
for the handler to return, or else your program will be aborted before
it will get chance to set the value of the return code.

Portability
-----------

not ANSI, not POSIX

Example
-------


char *environ[] = {
"PATH=c:\\dos;c:\\djgpp;c:\\usr\\local\\bin",
"DJGPP=c:/djgpp",
0
};

char *args[] = {
"gcc",
"-v",
"hello.c",
0
};

spawnvpe(P_WAIT, "gcc", args, environ);


--
Martin Ambuhl (mam...@earthlink.net)
Note: mam...@tiac.net will soon be inactive


Paul Lutus

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
<< I don't suppose anyone coulf post the code for the spawn command to get
it to run a .exe file from the root (c:) directory on my hard drive? >>

You supposed correctly. Please post your code and ask for help.
--

Paul Lutus
www.arachnoid.com

Steve Maughan wrote in message <36D31179...@bigfoot.com>...


>I'm newish to c++ programming. On my current task, the program is to run
>another program depending on what number's selected from a menu.

<snip>


0 new messages