[comp.lang.c] List a directory in a C program on Unix machines

cys@caen.engin.umich.edu (08/10/90)

Hello everyone,
  I am currently writing a C program on Unix machines. What I want to
do is to store the file names in a directory into a character array.
So I can do something to those files. I want to use Unix system calls
to handle it. Could anyone help me out with that? 
  Thanks in advance.

--Jim
  hev7@ub.cc.umich.edu

ping@cubmol.bio.columbia.edu (Shiping Zhang) (08/11/90)

In article <1990Aug10.154506.846@caen.engin.umich.edu> cys@caen.engin.umich.edu writes:
>  I am currently writing a C program on Unix machines. What I want to
>do is to store the file names in a directory into a character array.
>So I can do something to those files. I want to use Unix system calls
>to handle it. Could anyone help me out with that? 


I think opendir(), readdir() etc. can handle this problems. Check
your man pages.

-ping

ciemo@bananaPC.wpd.sgi.com (Dave Ciemiewicz) (08/11/90)

In article <1990Aug10.154506.846@caen.engin.umich.edu>,
cys@caen.engin.umich.edu writes:
> 
> Hello everyone,
>   I am currently writing a C program on Unix machines. What I want to
> do is to store the file names in a directory into a character array.
> So I can do something to those files. I want to use Unix system calls
> to handle it. Could anyone help me out with that? 
>   Thanks in advance.
> 
> --Jim
>   hev7@ub.cc.umich.edu

Try readdir().  See the readdir(3) manual page.

						--- Ciemo

vjs@rhyolite.wpd.sgi.com (Vernon Schryver) (08/11/90)

Which machines understand terminfo, termcap, or curses, but do not also
understand an environment varible like TERMINFO, TERMCAP or TERMCAPS for
individual second guessing of administrators?

I ask only extend my list of broken, compeating machines.  Since at least
4.2BSD and maybe 4.1c, that family has had TERMCAP (or was it TERMCAPS?),
so the solar and Boston crowd are ok.  SVR3 knows about TERMINFO, so most
PC's and many SV machines are ok.  Did SVR0 have TERMCAP?


Vernon Schryver   vjs@sgi.com

rh@smds.UUCP (Richard Harter) (08/12/90)

In article <1990Aug10.213953.25619@cubmol.bio.columbia.edu>, ping@cubmol.bio.columbia.edu (Shiping Zhang) writes:
> In article <1990Aug10.154506.846@caen.engin.umich.edu> cys@caen.engin.umich.edu writes:
> >  I am currently writing a C program on Unix machines. What I want to
> >do is to store the file names in a directory into a character array.
> >So I can do something to those files. I want to use Unix system calls
> >to handle it. Could anyone help me out with that? 

> I think opendir(), readdir() etc. can handle this problems. Check
> your man pages.

But be warned.  There are some portability pitfalls so the routine to
fetch the file names should be encapsulated.  The major variants that
I can think of offhand are:

(a)	BSD	Uses <sys/dir.h> 
(b)	SYS V	Uses <dirent.h>
(c)	SYS V	Older versions of SYS V may not have these routines.
		You can write your own.  THe directory is a file 
		containing an array of structures which can be read.
		The format is in the manual.
(d)	VMS	Write your own.  There are C callable VMS library
		routines for extracting the needed information.
(e)	Primos	Has a routine called lsdir which returns just what
		you want.


-- 
Richard Harter, Software Maintenance and Development Systems, Inc.
Net address: jjmhome!smds!rh Phone: 508-369-7398 
US Mail: SMDS Inc., PO Box 555, Concord MA 01742
This sentence no verb.  This sentence short.  This signature done.

cys@caen.engin.umich.edu (CAO YUSONG ) (08/12/90)

In article <159@smds.UUCP> rh@smds.UUCP (Richard Harter) writes:
>In article <1990Aug10.213953.25619@cubmol.bio.columbia.edu>, ping@cubmol.bio.columbia.edu (Shiping Zhang) writes:
>> In article <1990Aug10.154506.846@caen.engin.umich.edu> cys@caen.engin.umich.edu writes:
>> >  I am currently writing a C program on Unix machines. What I want to
>> >do is to store the file names in a directory into a character array.
>> >So I can do something to those files. I want to use Unix system calls
>> >to handle it. Could anyone help me out with that? 
>
>> I think opendir(), readdir() etc. can handle this problems. Check
>> your man pages.
>
>But be warned.  There are some portability pitfalls so the routine to
>fetch the file names should be encapsulated.  The major variants that
>I can think of offhand are:
>
>(a)	BSD	Uses <sys/dir.h> 
>(b)	SYS V	Uses <dirent.h>
>(c)	SYS V	Older versions of SYS V may not have these routines.
>		You can write your own.  THe directory is a file 
>		containing an array of structures which can be read.
>		The format is in the manual.
>(d)	VMS	Write your own.  There are C callable VMS library
>		routines for extracting the needed information.
>(e)	Primos	Has a routine called lsdir which returns just what
>		you want.
>
>
>-- 
>Richard Harter, Software Maintenance and Development Systems, Inc.
>Net address: jjmhome!smds!rh Phone: 508-369-7398 
>US Mail: SMDS Inc., PO Box 555, Concord MA 01742
>This sentence no verb.  This sentence short.  This signature done.

Thanks everyone who replied my message. You guys are really fantastic!

chris@mimsy.umd.edu (Chris Torek) (08/13/90)

(Given the subject, why is this in comp.lang.c?  Why the usa
distribution?  I am moving it to comp.unix.questions, although I
anticipate no further comments.)

In article <159@smds.UUCP> rh@smds.UUCP (Richard Harter) writes:
[re opendir/readdir/closedir]
>But be warned.  There are some portability pitfalls so the routine to
>fetch the file names should be encapsulated.  The major variants that
>I can think of offhand are:
>
>(a)	BSD	Uses <sys/dir.h> 
>(b)	SYS V	Uses <dirent.h>

POSIX says <dirent.h>; 4.3BSD-Reno and later systems thus use <dirent.h>.

>(c)	SYS V	Older versions of SYS V may not have these routines.
>		You can write your own. ...

Or use Doug Gwyn's version of Kirk McKusick's version of these routines.

[other systems deleted]
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris
	(New campus phone system, active sometime soon: +1 301 405 2750)