accessing dos from c program

J

JakeP

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
 
M

Malcolm

JakeP said:
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.
 
S

Severian

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
 
M

Martin Ambuhl

JakeP said:
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
 
X

Xenos

JakeP said:
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.edu/user/ralf/pub/WWW/files.html


DrX
 

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

No members online now.

Forum statistics

Threads
474,122
Messages
2,570,717
Members
47,283
Latest member
VonnieEwan

Latest Threads

Top