Re: accessing dos from c program

From: Martin Ambuhl (mambuhl_at_earthlink.net)
Date: 12/13/03


Date: Sat, 13 Dec 2003 20:58:42 GMT

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


Relevant Pages

  • Re: Status of DJGPP
    ... i did quite a few klines of c with djgpp, and i might help with a starting point. ... disk, dos screen font manipulation, a tui used also by freedos, but mine ... all under linux with free dos. ... There are alot of freedos command options, ...
    (comp.os.msdos.djgpp)
  • Re: Vista-compatible?
    ... What do you recommend for DJGPP? ... DOSBox will even run on non-x86 cpus. ... slow for normal DOS stuff. ... compatibility layer there. ...
    (comp.os.msdos.djgpp)
  • Re: The system cannot execute the specified program (again)
    ... What you're asking for is for DJGPP to become something different than ... But to take the dependence on DOS (and thus limitations ... If you want GNU tools for Windows, ... IT'S A DOS COMPILER. ...
    (comp.os.msdos.djgpp)
  • Re: M3PC.96 (m3bin.exe, m3src.exe) ??
    ... complete DB system in Modula-3. ... or port from NT386 cg-burs interface available in pm3, ... The only main advantage to '96 is that it uses DJGPP v2 instead of ... machine (raw DOS), but the "extender" didn't work, no ...
    (comp.lang.modula3)
  • Re: ZILE 2.3.7 (very hard to build, needs weird hacks just to compile)
    ... Also "M-x shell ... Vista (DJGPP build), won't even halfway try on DOS. ... Author intends to add PDcurses support. ...
    (comp.os.msdos.djgpp)