By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

accessing dos from c program

P: n/a
how should you access the dos os from a c program whuch as djgpp?

need to write an app that would for example get a list of all *.h
files and store them in a linked list so the names need to be
parsable......

seems straight forward on unix, but need to do it from a dos window on
a win98 platform.

cheers
JohnO


Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"JakeP" <sp*********@hotmail.com> wrote in message

need to write an app that would for example get a list of all *.h
files and store them in a linked list so the names need to be
parsable......

The standard library has no directory functions, so you need to use an
extension.
Nov 14 '05 #2

P: n/a
On Sat, 13 Dec 2003 12:43:35 +0000, JakeP <sp*********@hotmail.com>
wrote:
how should you access the dos os from a c program whuch as djgpp?

need to write an app that would for example get a list of all *.h
files and store them in a linked list so the names need to be
parsable......

seems straight forward on unix, but need to do it from a dos window on
a win98 platform.


Guh. There is no C standard library call to traverse a directory.
Study your platform and its API. IIRC, in Win32, you need
FindFirstFile. _findfirst may also be available, depending on your
compiler.

- Sev

Nov 14 '05 #3

P: n/a
JakeP wrote:
how should you access the dos os from a c program whuch as djgpp?

need to write an app that would for example get a list of all *.h
files and store them in a linked list so the names need to be
parsable......

seems straight forward on unix, but need to do it from a dos window on
a win98 platform.


The basic answer is to use system(). Any other answer -- for unix or
windows or dos or any of many other choices -- takes us beyond standard C.
Below is an example of use.
Notes:
The string argument to system(), unless it is null, has no meaning in
C: it is up to your command processor to determine its meaning.
This example does not include the redirection to a file that would be
needed for your application.
The particulars of system() and its interaction with various possible
shells can be complicated for djgpp. Read your documentation for the (off
topic) details.
Your (off topic) <dir.h>, <sys/dir.h>, and <dirent.h> functions are
mostly supported under djgpp. Because these are not standard C functions,
you need to direct any questions -- after reading the documentation -- to
comp.os.msdos.djgpp, the djgpp mailing list, or one of the gnu.* newsgroups.

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

int main(void)
{
if (!system(0)) {
printf("No shell available for system()\n");
exit(EXIT_FAILURE);
}
else { /* DOS specific crap */
char *shell = getenv("SHELL");
char *comspec = getenv("COMSPEC");
printf("DOS specific crap:\n"
"SHELL: \"%s\"\n"
"COMSPEC: \"%s\"\n",
((shell) ? shell : "not found"),
((comspec) ? comspec : "not found"));
putchar('\n');
printf("system(\"dir *.h\") returned %d\n", system("dir *h"));
putchar('\n');
printf("system(\"../rundos \"dir *.h\"\") returned %d\n",
system("../rundos \"dir *h\""));
putchar('\n');
printf("system(\"ls -l *.h\") returned %d\n",
system("ls -l *h"));
return 0;
}
}
DOS specific crap:
SHELL: "C:\WINDOWS\SYSTEM32\COMMAND.COM"
COMSPEC: "C:\WINDOWS\SYSTEM32\COMMAND.COM"

crap.h
system("dir *.h") returned 0
Volume in drive C has no label
Volume Serial Number is 3466-C6BD
Directory of C:\MARTIN\C

CRAP H 750 12/13/03 3:38p
1 file(s) 750 bytes
1023932928 bytes free
system("../rundos "dir *.h"") returned 0

-rw-r--r-- 1 dosuser root 750 Dec 13 15:38 crap.h
system("ls -l *.h") returned 0
--
Martin Ambuhl

Nov 14 '05 #4

P: n/a

"JakeP" <sp*********@hotmail.com> wrote in message
news:0v********************************@4ax.com...
how should you access the dos os from a c program whuch as djgpp?

need to write an app that would for example get a list of all *.h
files and store them in a linked list so the names need to be
parsable......

seems straight forward on unix, but need to do it from a dos window on
a win98 platform.

cheers
JohnO

If you are using djgpp, use the dirent.h functions. If not, and your
compiler has no equilivent, use DOS's software interrupts. See Ralf Brown's
Interrupt list:
http://www-2.cs.cmu.edu/afs/cs.cmu.e...WWW/files.html
DrX
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.