473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

DOS using C

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

May 29 '06 #1
4 1776
en***********@yahoo.com 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;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 29 '06 #2
en***********@yahoo.com wrote:
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");

May 29 '06 #3
Richard Heathfield <in*****@invalid.invalid> writes:
en***********@yahoo.com 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.)

--
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.
May 29 '06 #4
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
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.
--
All is vanity. -- Ecclesiastes
May 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
3
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
8
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
0
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.