DOS using C

E

enjoywithneha

Can anyone here please tell me how the system function is used to
execute DOS commands?
 
R

Richard Heathfield

(e-mail address removed) said:
Can anyone here please tell me how the system function is used to
execute DOS commands?

See comp.os.msdos.programmer for DOS-specific information, but the system()
part of your question is topical here.

Let us assume you have some system-specific command, FOO, which you wish to
execute with arguments bar and baz. Here is one simple way to do it:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int chomp(char *s)
{
char *p = NULL;
if(s != NULL)
{
p = strchr(s, '\n');
if(p != NULL)
{
*p = '\0';
}
}
return p != NULL;
}

int main(void)
{
char command[128] = "FOO ";
char argument[32] = {0};
if(system(NULL) == 0)
{
fputs("No command processor found!\n", stderr);
}
else
{
fputs("You are about to execute the FOO command,\n", stderr);
fputs("which takes two arguments. Please enter\n", stderr);
fputs("the first argument.\n", stderr);
if(fgets(argument, sizeof argument, stdin) != NULL)
{
if(chomp(argument))
{
strcat(command, argument);
strcat(command, " ");
fputs("Please enter the second argument.\n", stderr);
if(fgets(argument, sizeof argument, stdin) != NULL)
{
if(chomp(argument))
{
strcat(command, argument);
system(command);
}
else
{
fputs("Argument too long.\n", stderr);
}
}
}
else
{
fputs("Argument too long.\n", stderr);
}
}
else
{
fputs("Nothing to do.\n", stderr);
}
}

return 0;
}
 
S

santosh

Can anyone here please tell me how the system function is used to
execute DOS commands?

The system() function passes it's argument, which a const qualified
pointer to type char, to the host environment's command line processor,
if it's not a null pointer. It then returns the status value returned
by the command processor.

What type of strings are legitimate arguments to the system's command
processor is outside the scope of standard C and varies from operating
system to operating system and command processor to command processor.

As an example if you want to execute the DOS command DIR you could
write:

int rv;
rv = system("dir");

Or if you want to execute another program, say TEST.EXE, within the
subfolder PROG in the C:\ drive, you could say:

int rv;
rv = system("c:\\prog\\test.exe");
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address removed) said:
Can anyone here please tell me how the system function is used to
execute DOS commands?

See comp.os.msdos.programmer for DOS-specific information, but the system()
part of your question is topical here.

Let us assume you have some system-specific command, FOO, which you wish to
execute with arguments bar and baz. Here is one simple way to do it:
[...]
char command[128] = "FOO "; [...]
strcat(command, argument);
strcat(command, " "); [...]
strcat(command, argument);
system(command);
[...]

Strictly speaking, there's no guarantee that

system("FOO arg1 arg2");

will execute the command "FOO" with arguments "arg1" and "arg2". The
interpretation of the string passed to system() is entirely
implementation-defined. (It happens to work that way on every system
I'm familiar with, though.)
 
W

Walter Roberson

Strictly speaking, there's no guarantee that

system("FOO arg1 arg2");

will execute the command "FOO" with arguments "arg1" and "arg2". The
interpretation of the string passed to system() is entirely
implementation-defined. (It happens to work that way on every system
I'm familiar with, though.)

Leaving aside things like VMS's /OUTPUT= modifiers,
it is easy to find modern examples: take any Unix system
and let arg1 or arg2 contain redirections or other shell meta-characters.

Thus, in order to execute the command FOO with the expected
arguments, one may have to undertake system-specific encoding
(e.g., quoting) of the arguments before submiting the system()
command.
 

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

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top