469,270 Members | 1,626 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Use of System function

Hi Guys,
I am little new to C. I wish to know the way to use the 'system'
function. I mean I know that the function is used to run an external
DOS command but every time I use it it returns -1 which meansthe
command could not be executed.
Kindly help me............

Mar 28 '06 #1
16 1863
BHARAT MEHTA wrote:
Hi Guys,
I am little new to C. I wish to know the way to use the 'system'
function. I mean I know that the function is used to run an external
DOS command but every time I use it it returns -1 which meansthe
command could not be executed.


system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
on Unix) to execute the specified command. If you just give a program
name such as `unzip', then `unzip' must be in a directory specfied in
the PATH environment variable. To be sure, use an absolute path to
the executable, eg.

system("/usr/games/fortune");
system("C:\Program Files\Winzip\wzip32.exe");

Some systems may have no notion of a shell. For those, system()
is a no-op. You can check for the presence of a shell by calling
system() with an argument of NULL, and checking the return value.
If non-zero, a shell is available.

HTH,
Ralph

--
Ralph Moritz

Laugh at your problems; everybody else does.

Mar 28 '06 #2
"Ralph A. Moritz" writes:
BHARAT MEHTA wrote:
Hi Guys,
I am little new to C. I wish to know the way to use the 'system'
function. I mean I know that the function is used to run an external
DOS command but every time I use it it returns -1 which meansthe
command could not be executed.


system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
on Unix) to execute the specified command. If you just give a program
name such as `unzip', then `unzip' must be in a directory specfied in
the PATH environment variable. To be sure, use an absolute path to
the executable, eg.

system("/usr/games/fortune");
system("C:\Program Files\Winzip\wzip32.exe");


If that second one doesn't work check up on escape sequences WRT the '\'
character..
Mar 28 '06 #3

"osmium" <r1********@comcast.net> wrote in message
news:48************@individual.net...
"Ralph A. Moritz" writes:
BHARAT MEHTA wrote:
Hi Guys,
I am little new to C. I wish to know the way to use the 'system'
function. I mean I know that the function is used to run an external
DOS command but every time I use it it returns -1 which meansthe
command could not be executed.


system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
on Unix) to execute the specified command. If you just give a program
name such as `unzip', then `unzip' must be in a directory specfied in
the PATH environment variable. To be sure, use an absolute path to
the executable, eg.

system("/usr/games/fortune");
system("C:\Program Files\Winzip\wzip32.exe");


If that second one doesn't work check up on escape sequences WRT the '\'
character..


The second one does work and is correct for MS-DOS. The string in system()
passed directly to the OS's command processor _AS_IS_. For the MS-DOS
command.com command line, one does not need to escape the backslash '\'
character as you would for printf().
Mar 29 '06 #4
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:

"osmium" <r1********@comcast.net> wrote in message
news:48************@individual.net...
"Ralph A. Moritz" writes:
> BHARAT MEHTA wrote:
>> Hi Guys,
>> I am little new to C. I wish to know the way to use the 'system'
>> function. I mean I know that the function is used to run an external
>> DOS command but every time I use it it returns -1 which meansthe
>> command could not be executed.
>
> system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
> on Unix) to execute the specified command. If you just give a program
> name such as `unzip', then `unzip' must be in a directory specfied in
> the PATH environment variable. To be sure, use an absolute path to
> the executable, eg.
>
> system("/usr/games/fortune");
> system("C:\Program Files\Winzip\wzip32.exe");


If that second one doesn't work check up on escape sequences WRT the '\'
character..


The second one does work and is correct for MS-DOS. The string in system()
passed directly to the OS's command processor _AS_IS_. For the MS-DOS
command.com command line, one does not need to escape the backslash '\'
character as you would for printf().


Eh?

You _always_ need to escape the backslash when it appears in a string
literal. It's not printf that interprets it, it's the compiler. It's not
like a % sequence.
Mar 29 '06 #5
"Rod Pemberton" <do*********@sorry.bitbuck.cmm> writes:
"osmium" <r1********@comcast.net> wrote in message
news:48************@individual.net...
"Ralph A. Moritz" writes:
> BHARAT MEHTA wrote:
>> Hi Guys,
>> I am little new to C. I wish to know the way to use the 'system'
>> function. I mean I know that the function is used to run an external
>> DOS command but every time I use it it returns -1 which meansthe
>> command could not be executed.
>
> system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
> on Unix) to execute the specified command. If you just give a program
> name such as `unzip', then `unzip' must be in a directory specfied in
> the PATH environment variable. To be sure, use an absolute path to
> the executable, eg.
>
> system("/usr/games/fortune");
> system("C:\Program Files\Winzip\wzip32.exe");


If that second one doesn't work check up on escape sequences WRT the '\'
character..


The second one does work and is correct for MS-DOS. The string in system()
passed directly to the OS's command processor _AS_IS_. For the MS-DOS
command.com command line, one does not need to escape the backslash '\'
character as you would for printf().


Wrong. (Did you actually try it?)

The argument to system() is a string literal, and is interpreted
according to C's rules for string literals. The fact that it's an
argument to system() is irrelevant.

Since the language doesn't define the escape sequences \P, \W, or \w,
strictly speaking the argument isn't even a valid token, and a
diagnostic is required. After the diagnostic is issued, the compiler
can do anything it likes, including rejecting the program. It could
conceivably treat "\P" as equivalent to "\\P", but that would be a bad
idea, since it's not allowed to mess with \a, \b, \f, \n, \r, \t, or \v.

(The rules are more lax for #include directives, since in
#include "foobar.h"
the "foobar.h" isn't actually a string literal.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #6

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@sorry.bitbuck.cmm> writes:
"osmium" <r1********@comcast.net> wrote in message
news:48************@individual.net...
"Ralph A. Moritz" writes:
> BHARAT MEHTA wrote:
>> Hi Guys,
>> I am little new to C. I wish to know the way to use the 'system'
>> function. I mean I know that the function is used to run an external
>> DOS command but every time I use it it returns -1 which meansthe
>> command could not be executed.
>
> system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
> on Unix) to execute the specified command. If you just give a program
> name such as `unzip', then `unzip' must be in a directory specfied in
> the PATH environment variable. To be sure, use an absolute path to
> the executable, eg.
>
> system("/usr/games/fortune");
> system("C:\Program Files\Winzip\wzip32.exe");

If that second one doesn't work check up on escape sequences WRT the '\' character..
The second one does work and is correct for MS-DOS. The string in system() passed directly to the OS's command processor _AS_IS_. For the MS-DOS
command.com command line, one does not need to escape the backslash '\'
character as you would for printf().


Wrong. (Did you actually try it?)


I have existing code which does the same thing (which I which consulted
prior to my post). No escapes are needed and it works with multiple DOS
based compilers.
The argument to system() is a string literal, and is interpreted
according to C's rules for string literals. The fact that it's an
argument to system() is irrelevant.


Apparently not. I haven't checked the ISO spec., but Harbison and Steele
agrees with me that the string from system() is passed as is to the OS in an
implementation defined manner.

Mar 29 '06 #7

"Jordan Abel" <ra*******@gmail.com> wrote in message
news:sl***********************@random.yi.org...
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:

"osmium" <r1********@comcast.net> wrote in message
news:48************@individual.net...
"Ralph A. Moritz" writes:

> BHARAT MEHTA wrote:
>> Hi Guys,
>> I am little new to C. I wish to know the way to use the 'system'
>> function. I mean I know that the function is used to run an external
>> DOS command but every time I use it it returns -1 which meansthe
>> command could not be executed.
>
> system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
> on Unix) to execute the specified command. If you just give a program
> name such as `unzip', then `unzip' must be in a directory specfied in
> the PATH environment variable. To be sure, use an absolute path to
> the executable, eg.
>
> system("/usr/games/fortune");
> system("C:\Program Files\Winzip\wzip32.exe");

If that second one doesn't work check up on escape sequences WRT the '\' character..


The second one does work and is correct for MS-DOS. The string in system() passed directly to the OS's command processor _AS_IS_. For the MS-DOS
command.com command line, one does not need to escape the backslash '\'
character as you would for printf().


Eh?

You _always_ need to escape the backslash when it appears in a string
literal. It's not printf that interprets it, it's the compiler. It's not
like a % sequence.


See my reply to Keith.
Mar 29 '06 #8
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:
"osmium" <r1********@comcast.net> wrote in message
news:48************@individual.net...
"Ralph A. Moritz" writes: [...] > system("/usr/games/fortune");
> system("C:\Program Files\Winzip\wzip32.exe");

If that second one doesn't work check up on escape sequences WRT the '\'
character..


The second one does work and is correct for MS-DOS. The string in system()
passed directly to the OS's command processor _AS_IS_. For the MS-DOS
command.com command line, one does not need to escape the backslash '\'
character as you would for printf().


Eh?

You _always_ need to escape the backslash when it appears in a string
literal. It's not printf that interprets it, it's the compiler. It's not
like a % sequence.


You're right, printf doesn't interpret the argument to system(). 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #9
"Rod Pemberton" <do*********@sorry.bitbuck.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@sorry.bitbuck.cmm> writes:
> "osmium" <r1********@comcast.net> wrote in message
> news:48************@individual.net...
>> "Ralph A. Moritz" writes: [...] >> > system("/usr/games/fortune");
>> > system("C:\Program Files\Winzip\wzip32.exe");
>>
>> If that second one doesn't work check up on escape sequences WRT
>> the '\' character..
>
> The second one does work and is correct for MS-DOS. The string
> in system() passed directly to the OS's command processor
> _AS_IS_. For the MS-DOS command.com command line, one does not
> need to escape the backslash '\' character as you would for
> printf().
Wrong. (Did you actually try it?)


I have existing code which does the same thing (which I which consulted
prior to my post). No escapes are needed and it works with multiple DOS
based compilers.


If so, the compilers in question may be providing an extension. If
they don't produce a diagnostic, they're non-conforming.

Show us a complete and self-contained C program that supports your
claim. For example:

#include <stdlib.h>
int main(void)
{
system("...");
return 0;
}

where "..." is replaced with whatever you like. Make sure it actually
works with some MS-DOS compiler (and tell us which one).

If you're able to do that, see what happens if the argument contains
one of the standard escape sequences: \a \b \f \n \r \t \v. If any of
those is interpreted as something other than an alert, backspace, form
feed, new line, carriage return, horizontal tab, or vertical tab
charater, respectively, then your compiler is broken.
The argument to system() is a string literal, and is interpreted
according to C's rules for string literals. The fact that it's an
argument to system() is irrelevant.


Apparently not. I haven't checked the ISO spec.,


I suggest you do so.
but Harbison and
Steele agrees with me that the string from system() is passed as is
to the OS in an implementation defined manner.


You've misinterpreted Harbison and Steele. The string argument is
passed to the operating system's command processor for execution in
some implementation-defined way, but any implementation-defined
behavior occurs only after the string is passed to system(). The
evaluation of the argument expression is not affected by the fact that
it's in a call to system().

This:

"C:\Program Files\Winzip\wzip32.exe"

is not a valid C token (except possibly in a #include directive).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #10
On 2006-03-29, Keith Thompson <ks***@mib.org> wrote:
"Rod Pemberton" <do*********@sorry.bitbuck.cmm> writes:
"osmium" <r1********@comcast.net> wrote in message
news:48************@individual.net...
"Ralph A. Moritz" writes:
> BHARAT MEHTA wrote:
>> Hi Guys,
>> I am little new to C. I wish to know the way to use the 'system'
>> function. I mean I know that the function is used to run an external
>> DOS command but every time I use it it returns -1 which meansthe
>> command could not be executed.
>
> system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
> on Unix) to execute the specified command. If you just give a program
> name such as `unzip', then `unzip' must be in a directory specfied in
> the PATH environment variable. To be sure, use an absolute path to
> the executable, eg.
>
> system("/usr/games/fortune");
> system("C:\Program Files\Winzip\wzip32.exe");

If that second one doesn't work check up on escape sequences WRT the '\'
character..


The second one does work and is correct for MS-DOS. The string in system()
passed directly to the OS's command processor _AS_IS_. For the MS-DOS
command.com command line, one does not need to escape the backslash '\'
character as you would for printf().


Wrong. (Did you actually try it?)

The argument to system() is a string literal, and is interpreted
according to C's rules for string literals. The fact that it's an
argument to system() is irrelevant.

Since the language doesn't define the escape sequences \P, \W, or \w,
strictly speaking the argument isn't even a valid token, and a
diagnostic is required. After the diagnostic is issued, the compiler
can do anything it likes, including rejecting the program. It could
conceivably treat "\P" as equivalent to "\\P", but that would be a bad
idea, since it's not allowed to mess with \a, \b, \f, \n, \r, \t, or
\v.


Even in a string literal that also contains \P? suppose "\P" is defined
by the implementation as "expand to \P and cause all other escapes in
the string literal not to be interpreted"
Mar 29 '06 #11
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:
I have existing code which does the same thing (which I which
consulted prior to my post). No escapes are needed and it works with
multiple DOS based compilers.


So they choose to define the escape \P as expanding to \\P. they have
that right.
The argument to system() is a string literal, and is interpreted
according to C's rules for string literals. The fact that it's an
argument to system() is irrelevant.


Apparently not. I haven't checked the ISO spec., but Harbison and Steele
agrees with me that the string from system() is passed as is to the OS in an
implementation defined manner.


You misunderstand what "as is" really means.
Mar 29 '06 #12
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:

"Jordan Abel" <ra*******@gmail.com> wrote in message
news:sl***********************@random.yi.org...
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:
>
> "osmium" <r1********@comcast.net> wrote in message
> news:48************@individual.net...
>> "Ralph A. Moritz" writes:
>>
>> > BHARAT MEHTA wrote:
>> >> Hi Guys,
>> >> I am little new to C. I wish to know the way to use the 'system'
>> >> function. I mean I know that the function is used to run an external
>> >> DOS command but every time I use it it returns -1 which meansthe
>> >> command could not be executed.
>> >
>> > system() uses the shell (COMMAND.COM on DOS/Windows, /bin/sh
>> > on Unix) to execute the specified command. If you just give a program
>> > name such as `unzip', then `unzip' must be in a directory specfied in
>> > the PATH environment variable. To be sure, use an absolute path to
>> > the executable, eg.
>> >
>> > system("/usr/games/fortune");
>> > system("C:\Program Files\Winzip\wzip32.exe");
>>
>> If that second one doesn't work check up on escape sequences WRT the '\' >> character..
>
> The second one does work and is correct for MS-DOS. The string in system() > passed directly to the OS's command processor _AS_IS_. For the MS-DOS
> command.com command line, one does not need to escape the backslash '\'
> character as you would for printf().


Eh?

You _always_ need to escape the backslash when it appears in a string
literal. It's not printf that interprets it, it's the compiler. It's not
like a % sequence.


See my reply to Keith.


You misunderstand what "the string" means in the statement you cited.
"the string" means the sequence of characters AFTER the string literal
token has had escapes substituted for control characters.
Mar 29 '06 #13
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-29, Keith Thompson <ks***@mib.org> wrote:

[...]
The argument to system() is a string literal, and is interpreted
according to C's rules for string literals. The fact that it's an
argument to system() is irrelevant.

Since the language doesn't define the escape sequences \P, \W, or \w,
strictly speaking the argument isn't even a valid token, and a
diagnostic is required. After the diagnostic is issued, the compiler
can do anything it likes, including rejecting the program. It could
conceivably treat "\P" as equivalent to "\\P", but that would be a bad
idea, since it's not allowed to mess with \a, \b, \f, \n, \r, \t, or
\v.


Even in a string literal that also contains \P? suppose "\P" is defined
by the implementation as "expand to \P and cause all other escapes in
the string literal not to be interpreted"


A diagnostic is required for a token containing \P that otherwise
looks like a string literal. Once the diagnostic is issued, the
compiler is free to accept the program; the behavior is then undefined
(even if the implementation documents the behavior, it's still
"undefined behavior" as the C standard defines the term).

So yes, an implementation could behave as you describe. I don't
believe that's what's happening here. The \P appears merely because
the program refers to a file under the <C:\Program Files> directory
(folder, whatever).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #14
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:
I have existing code which does the same thing (which I which
consulted prior to my post). No escapes are needed and it works with
multiple DOS based compilers.


So they choose to define the escape \P as expanding to \\P. they have
that right.


What do you mean by "right"? There is no "right" expansion for \P in
a string literal.

If the compiler is behaving as you suggest, what would you expect it
to do for:

system("C:\foo\bar\foobar.exe");

? If it converts "\f" to anything other than a form feed, or "\b" to
anything other than a backspace, it's broken. (Conceivably the
system() function could be doing silly things like translating a form
feed character to a backslash-f sequence; presumably fopen() and a
host of other library functions would have to do the same thing.) And
how would you pass an actual horizontal tab character to system()

See question 19.17 in the FAQ, <http://www.c-faq.com/osdep/dospath.html>.
It also mentions (somewhat off-topically) that MS-DOS also accepts '/'
characters as directory separators; perhaps that's the source of the
confusion?

I suggest waiting for Rod Pemberton to demonstrate the actual behavior
he's talking about -- or, better yet, for someone else to do so.
Speculating about extensions that some DOS compiler *might* provide
doesn't seem useful.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #15
On 2006-03-29, Keith Thompson <ks***@mib.org> wrote:
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:
I have existing code which does the same thing (which I which
consulted prior to my post). No escapes are needed and it works with
multiple DOS based compilers.


So they choose to define the escape \P as expanding to \\P. they have
that right.


What do you mean by "right"? There is no "right" expansion for \P in
a string literal.


I mean the right they have to do it, which they are exercising.
Mar 29 '06 #16
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-29, Keith Thompson <ks***@mib.org> wrote:
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-29, Rod Pemberton <do*********@sorry.bitbuck.cmm> wrote:
I have existing code which does the same thing (which I which
consulted prior to my post). No escapes are needed and it works with
multiple DOS based compilers.

So they choose to define the escape \P as expanding to \\P. they have
that right.


What do you mean by "right"? There is no "right" expansion for \P in
a string literal.


I mean the right they have to do it, which they are exercising.


Ok, I misunderstood; I thought you meant "they have that correct".

(But I've seen no real evidence so far that they really are exercising
that right.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by AussieRules | last post: by
22 posts views Thread by schneider | last post: by
7 posts views Thread by =?Utf-8?B?SmVycnkgQw==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.