LINUX TIPS AND TRICKS --- March 16, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________________________

Listing Directory Files
By Danny Kalev

This week, I will show how to programmatically access a directory's 
files.

The POSIX standard defines abstract functions and data types for 
accessing a directory's files in a portable manner, regardless of the 
actual file system used. These functions and data types are declared in 
the header file <dirent.h>.

The two main data structures are DIR and dirent. You use a pointer to 
DIR to iterate through a directory's files. The second data structure, 
dirent, represents a directory file. Most of the fields in the 
structure dirent are not portable; they depend on the actual file 
system you're using. However, the field d_name, which stores the 
current directory file's name, is portable and that's the only thing we 
care about.

Before you can iterate the a directory's files, you have to open it 
explicitly using the opendir() function:

    #include <dirent.h>
    DIR *pdir=opendir (const char *path);

Similarly, you use the closedir() function to close a directory once 
you're done:

    int closedir(DIR * pdir);

Remember, you can open directories only for reading. Therefore, opendir
() takes a single parameter, namely the directory's pathname, which 
must be an existing directory's name (you can't create a new directory 
using opendir(); instead use mkdir()). Once you've opened a directory 
successfully, you access the entries therein sequentially until you've 
reached the directory's end. The function readdir() returns a dirent 
pointer associated with the next directory file. Here is the prototype 
of this function:

    dirent* pfile readdir(DIR * pdir);

readdir() returns a NULL pointer when the directory contains no more 
entries, or when an error has occurred. If you must distinguish between 
these two conditions, then you should reset errno before calling readdir
() and check its value afterwards.

The following program opens the current directory, retrieves its files 
in a loop, and writes their names to the standard output. You can 
easily turn it into a generic directory-listing program by passing the 
directory pathname as a command line argument.

    #include <dirent.h>
    #include <errno.h>
    #include <stdio.h>
    int main()
    {
     DIR *pdir;
     struct dirent *pfile;
     if (!(pdir=opendir ("."); /*open current directory*/
     {
      perror("opendir");
      return -1;
     }
     errno=0; /*reset before calling readdir*/
     while(pfile=readdir(pdir))
     {
      printf("%s\n",pfile->d_name);
     }
     if (errno==0)
      closedir(pdir);
     return 0;
    }
   
To re-read the current directory from the beginning, use the rewinddir
() function. After calling rewinddir(), readdir() will return the first 
file in the directory:


    int rewinddir(DIR *pdir);


About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com.
________________________________________________________________________________

ADDITIONAL RESOURCES

Communicating the POSIX way 
Implementing POSIX IPC interfaces into the Solaris kernel

http://www.unixinsider.com/jsw/lintps_nl/
	swol-12-1999/swol-12-insidesolaris.html

Linux-Windows file access 
Discover the current options and future solutions 
http://www.linuxworld.com/jlw/lintps_nl/
	lw-2001-01/lw-01-legacy.html

Fiddling around with files, part one
http://www.unixinsider.com/jsw/lintps_nl/swol-02-1998/
	swol-02-insidesolaris.html

Fiddling around with files, part two
http://www.unixinsider.com/jsw/lintps_nl/swol-03-1998/
	swol-03-insidesolaris.html
________________________________________________________________________________

COMMUNITY DISCUSSIONS

Linux Software Development
Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Moderated by Danny Kalev.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
	webx?14@@.ee6b652/294!skip=231

Ask the Geek
Got a question about Linux setup, tuning, security, or maintenance? 
Hackers, newbies, and all the geeks in between knock heads and devise 
solutions in this discussion moderated by LinuxWorld.com's resident 
geek.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
	webx?14@@.ee6c981/388!skip=349
________________________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
  Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account Executive at:
  dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz, Eastern
  Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager at: paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
________________________________________________________________________________

PRIVACY POLICY

http://www2.itworld.com/CDA/ITW_Privacy_Policy

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
