[comp.lang.c] Where oh where is my directory

perrone@loligo.uucp (Perrone Ford) (03/25/89)

Help , Althogh I wa originally developing this program
for TurboC I am interested if there is an ANSI way to
list the files in the current or another directory.  
What I am trying to accompish is to read the directory 
entries into a buffer and then print the buffer out in the
middle of a graphics routine

	In TurboC this would amount to being able to do soemthing
	like: 


#include <stdio.h>
#include <grphics.h>  /*  Maybe curses  */
main () 
{
char *listing;
listing = system("dir");
initgraph = DETECT;
initgraph(&graphdriver, &graphmode);
outtext(listing);
}



however whenever i make the system call, the screen gets trashed
whether or not i change the active or visual pages...
anyone got any good ideas?


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Perrone T. Ford                   | Bitnet : PFord@FSU.Bitnet    %
% arpa: Perrone@loligo.com.fsu.edu  |       ford@systems.fsu.edu   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

henry@utzoo.uucp (Henry Spencer) (03/25/89)

In article <7712@pyr.gatech.EDU> perrone@loligo.UUCP (Perrone Ford) writes:
>Help , Althogh I wa originally developing this program
>for TurboC I am interested if there is an ANSI way to
>list the files in the current or another directory.  

There isn't even an ANSI notion of a directory, never mind an ANSI way
to list the files in it.
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

gandalf@csli.STANFORD.EDU (Juergen Wagner) (03/25/89)

In article <7712@pyr.gatech.EDU> perrone@loligo.UUCP (Perrone Ford) writes:
>What I am trying to accompish is to read the directory 
>entries into a buffer and then print the buffer out in the
>middle of a graphics routine

Basically, there are two ways: call a program like "ls" (UNIX), or do it
yourself. I reckon your problem is not an ANSI/non-ANSI problem, it's more
an operating system problem.

>#include <stdio.h>
>#include <grphics.h>  /*  Maybe curses  */
>main () 
>{
>char *listing;
>listing = system("dir");
>initgraph = DETECT;
>initgraph(&graphdriver, &graphmode);
>outtext(listing);
>}

Sure your screen gets trashed. The problem is that your call to "dir" doesn't
return a list of file names neatly packed into a string, it returns an
integer (probably :-). I have no idea what exactly is possible on your system
but try to look for one of the following:

	(a) Something like opendir(), readdir(), closedir() to
	    access directory entries, and collect them into a
	    string. Even better, print them as you read them.

	(b) Something forking a subprocess which allows you to
	    collect the output of the subprocess in some way
	    (popen would be nice).

	(c) In the worst case, call a program doing what you want,
	    have the program write its output to a file, and read
	    that file. I can promise you that it'll be sloooow but
	    it will work.

Good luck,
-- 
Juergen Wagner		   			gandalf@csli.stanford.edu
						 wagner@arisia.xerox.com

jdc@naucse.UUCP (John Campbell) (03/25/89)

From article <1989Mar25.002620.20439@utzoo.uucp>, by henry@utzoo.uucp (Henry Spencer):
> In article <7712@pyr.gatech.EDU> perrone@loligo.UUCP (Perrone Ford) writes:
>>Help , Althogh I wa originally developing this program
>>for TurboC I am interested if there is an ANSI way to
>>list the files in the current or another directory.  
> 
> There isn't even an ANSI notion of a directory, never mind an ANSI way
> to list the files in it.
> -- 
> Welcome to Mars!  Your       |     Henry Spencer at U of Toronto Zoology
> passport and visa, comrade?  | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

Yes, one wouldn't expect Henry Spencer to be wrong, and he's not, of course,
but...

As a practical matter, shouldn't someone mention opendir(), readdir() and
the dirent.h stuff that is commonly used to get the most portability out
of this thorny problem.   I seem to remember that there are SysV and
MSDOS versions of these routines implemented and I would guess that most
systems which have heirarchical directories can be made to emulate them.
(Any Amiga or MacIntosh owners disagree?)

I, for one, would like it if some of the good PC programmers wrote their 
stuff so it was more easy to move to my unix machine  :-).  
-- 
	John Campbell               ...!arizona!naucse!jdc
                                    CAMPBELL@NAUVAX.bitnet
	unix?  Sure send me a dozen, all different colors.

bobmon@iuvax.cs.indiana.edu (RAMontante) (03/26/89)

perrone@loligo.UUCP (Perrone Ford) <7712@pyr.gatech.EDU> :
-What I am trying to accompish is to read the directory 
-entries into a buffer and then print the buffer out in the
-middle of a graphics routine
-
-#include <stdio.h>
-#include <grphics.h>  /*  Maybe curses  */
-main () 
-{
-char *listing;
-listing = system("dir");
-initgraph = DETECT;
-initgraph(&graphdriver, &graphmode);
-outtext(listing);
-}

If I understand your intent correctly, you're waaay off base.  You seem to
want 'listing' to point to a character string containing the result of the
"dir" command, which you will then display within a graphics image.

However:  the 'system("dir")' command does not result in 'listing' pointing
to any such string.  Instead, "dir" tries hard to display its output
directly on the *screen* regardless of the screen-display mode (text,
graphics, CGA/VGA/whathaveyou).  Then 'system()' returns an *integer*
indicating whether or not the "dir" command successfully ran or not -- since
"dir" is builtin to the MSDOS command interpreter, the only way MSDOS will
claim that "dir" failed is if COMMAND.COM is missing;  a "dir" that crashes
looks successful to MSDOS.  (If that sounds absurdly irrational, then you're
beginning to understand.)

Since integers and char pointers are often similar-sized, this bogus
assignment to 'listing' will sort-of work (unless you've enabled ALL the
TurboC warnings, in which case it'll tell you of the type mismatch).
Once you go into graphics mode, you give 'outtext()' a value which it thinks
is a memory address but which in fact is either 0 or 1.  It is unlikely
that any character-string-like object starts at either of these "addresses".

Lastly, of course, the whole graphics business is particular to Turbo C (or
an equivalent package) which "knows" that it's accessing a particular
bit-mapped display.  I'm pretty sure that pANSI avoids any considerations at
all like those embodied by <graphics.h>.  (Is <curses.h> sanctioned by
pANSI?  I don't think so.)  Pure C shouldn't know whether it's driving a
bit-mapped display or an electric typewriter.

mnarayan@hcr.UUCP (Michael Narayan) (04/08/89)

perrone@loligo.UUCP (Perrone Ford) <7712@pyr.gatech.EDU> :
>What I am trying to accompish is to read the directory 
>entries into a buffer and then print the buffer out in the
>middle of a graphics routine
>
> [stuff deleted]

Since you now know that system("dir") will not work, here is how you
can do what you want. TurboC (and Microsoft C I believe) provide two
routines called findfirst() and findnext() which allow you to retrieve
the entries in a directory given a pattern (* and ? wildcards allowed).
You can use these two functions to retrieve the directory entries you
want into a buffer, which you can later print out.

---------------------------------------------------------------
Michael J. Narayan
HCR Corp.
{utcsri!utzoo}!hcr!mnarayan

Nobody else could have opinions like these.
I'm just the co-op (slave labour)
---------------------------------------------------------------
HCR Corp.