Novell Home

CLIB's opendir & readdir api sample

From Developer Community

Demonstrates how to do Opendir/readdir with CLIB.

Sample Code

#include <stdio.h>   
#include <stdlib.h>  
#include <errno.h>   
#include <dirent.h>     
int main(void)   
{      
   char  path[255];      
   int   cnt = 0;      
   DIR   *dirStructP;      
   DIR   *direntp;        
   printf("Enter the path to do readdir() on: ");      
   scanf("%s", path);          
   if ((dirStructP = opendir(path)) != NULL)
   {
      while ((direntp = readdir(dirStructP)) != NULL)
      {
         printf("name = %s\r\n", direntp->d_name);
         cnt++;            /* possibly dump out file info here...*/
      }         
      printf("%d entries scanned\n", cnt);         
      closedir(dirStructP);
   } else
   {
      printf("opendir returned errno: %#x\n", errno);        exit(1);
   }       
   return(0);
}

--Benjamin Fjeldsted

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.