www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/06/03:54:57

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: DIR/System question - speed
Date: Mon, 05 May 1997 21:42:50 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 67
Message-ID: <336E545A.4F8C@cs.com>
References: <336DF41B DOT 1120 AT ece DOT uc DOT edu>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp108.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Paul Campisi wrote:
> 
> Hello,
>         In a program I am writing, I want to get the current directory and
> display it on the screen in the *same* format as the DIR command.
>         The problem is if I do a SYSTEM call, it is WAY to slow, and of course
> SPAWN or EXEC won't work because dir isn't an executable but a command
> shell call.

Please specify what version of DJGPP you are using.  In v1.x, using
system involved swapping out the entire program to disk before running
the command; obviously this was an extremely slow task.  In v2.x, the
speed of system() was vastly improved, and in v2.01, system() attempts
to emulate as much of the process of calling other programs as
possible.  So a 2 second delay is way too much for a v2.01 program,
unless there is something else wrong.  Please post the code fragment you
use and someone here will try to test it for you.

>         My questions is, is there any way to get a display like DIR without
> having
> the hang with system or any way around using the system call to get the
> directory?
> (of course, without having to include all the sorting and parsing of
> using findnext?)
>         I was thinking, maybe to try and redirect the output of the system call
> to a
> pointer, and then display that, but I am not sure how to redirect the
> output of system
> or if that would be any faster???

Well, there's always the popen() command, which allows you to spawn an
external program and redirect its standard input or standard output to a
file.  To capture the output of 'dir', you'd do something like this:

#include <stdio.h>

int main( void )
{
    char line[256];
    FILE *fp;

    /* open pipe for reading */
    if ( ( fp = popen( "dir", "r" ) ) == NULL )
    {
        fprintf( stderr, "Couldn't open pipe.\n" );
        exit( 1 );
    }

    while ( !feof( fp ) )
    {
        fgets( line, 256, fp );
        printf( line );
    }
    pclose( fp );   /* close pipe */

    return 0;
}

Strangely, I just tested this and it froze when the pipe command was
invoked.  Maybe this is a bug?

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I |     mailto:fighteer AT cs DOT com      |
| Descent 2: The Infinite Abyss - The greatest Internet game of all |
| time just got better!  This time, you're going all the way down...|
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019