home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / forut062.zip / ForUtil-0.62 / lib / filelist.c < prev    next >
C/C++ Source or Header  |  1996-08-28  |  4KB  |  145 lines

  1. #ifndef lint
  2. static char rcsId[]="$Source: /usr/local/rcs/ForUtil/lib/RCS/filelist.c,v $";
  3. #endif
  4. /*****
  5. * filelist.c : routine for creating a filelist of a directory
  6. *
  7. * This file Version    $Revision: 1.5 $
  8. *
  9. * Creation date:    Mon Feb  4 03:59:03 GMT+0100 1980
  10. * Last modification:     $Date: 1996/08/27 19:20:11 $
  11. * By:            $Author: koen $
  12. * Current State:    $State: Exp $
  13. *
  14. * Author:        koen
  15. * (C)Copyright 1995 Ripley Software Development
  16. * All Rights Reserved
  17. *****/
  18. /*****
  19. * ChangeLog 
  20. * $Log: filelist.c,v $
  21. * Revision 1.5  1996/08/27 19:20:11  koen
  22. * msdos related changes
  23. *
  24. * Revision 1.4  1996/08/02 14:53:41  koen
  25. * Changed the extension scanning stuff. It now really scans for the 
  26. * extensions only. 
  27. * Removed the verbose stuff. It made the libForUtil not really a lib since 
  28. * it dependent on an extern variable.
  29. *
  30. * Revision 1.3  1996/07/16 09:20:09  koen
  31. * corrected a bug for num_extensions == 0
  32. *
  33. * Revision 1.2  1996/05/06 00:36:48  koen
  34. * Adapted for MSDOS
  35. *
  36. * Revision 1.1  1996/04/25 02:30:15  koen
  37. * Initial Revision
  38. *
  39. *****/ 
  40. /* needed to prevent multiple variable decls under MSDOS in sysdeps.h */
  41. #define LIBRARY_OBJECT
  42.  
  43. /* include this before anything else */
  44. #ifdef HAVE_CONFIG_H
  45. #include "autoconf.h"
  46. #endif
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #ifdef HAVE_DIRENT_H
  54. # include <dirent.h>
  55. # define direct dirent
  56. # else
  57. # ifdef HAVE_SYS_DIR_H
  58. # include <sys/dir.h>
  59. # endif
  60. #endif
  61.  
  62. #include "forutil.h"
  63. #include "memmacros.h"
  64.  
  65. /*****
  66. * Build a filelist of the directory path. The files found are stored in
  67. * file_list, the number of files in the list in num_files.
  68. * ext_table is used to check the filenames read agains extensions specified
  69. * in ext_table. This check is only performed if num_extensions is non-zero
  70. *****/
  71. void
  72. build_file_list(char **file_list[], int *num_files, char *path, 
  73.     char *ext_table[], int num_extensions)
  74. {
  75.     DIR *dirp;
  76.     struct direct *dp;
  77.     struct stat buf;
  78.     char s_path[MAXPATHLEN];
  79.     int i, sub_files= *num_files, count=0;
  80.  
  81.     sprintf(s_path, "%s%s.", path, CHAR_SLASH);
  82. /* try to open the given dir. If we fail, don't bother */
  83.     dirp = opendir(s_path);
  84.     if (dirp == NULL)
  85.         perror(path);
  86.     else 
  87.     { 
  88. /* walk each entry in this directory */
  89.         for(dp=readdir(dirp); dp != NULL;)
  90.         {
  91. /* skip self and parent */
  92.             if (strcmp(dp->d_name,".")!=0 && strcmp(dp->d_name,"..")!= 0)
  93.             {
  94.                 sprintf(s_path, "%s%s%s", path, CHAR_SLASH, dp->d_name);
  95. #ifdef VERBOSE
  96.                 printf("build_file_list, testing file %s\n", s_path);
  97. #endif 
  98. /* try if we can read this file */
  99.                 if((stat(s_path, &buf)) != 0)
  100.                 {
  101.                     perror(s_path);
  102.                 }
  103. /*****
  104. * if we can read it, see if it is a directory. If it is, skip it, else
  105. * store it. 
  106. *****/
  107.                 else if (!(buf.st_mode & S_IFDIR))
  108.                 {
  109. /* check against given extensions (if any). */
  110.                     for(i = 0 ; i < num_extensions; i++)
  111.                     { 
  112.                         if(ext_table[i][0] == '-' && 
  113.                             !(file_has_ext(dp->d_name)))
  114.                             break;
  115.                         if((strend(dp->d_name, ext_table[i]))) 
  116.                             break;
  117.                     }
  118.                     if( num_extensions == 0 || i != num_extensions)
  119.                     {
  120.                         checked_realloc(*file_list, sub_files+1, char*);
  121. /* +2 for the / and a terminating \0 */
  122.                         checked_malloc(*(*file_list+sub_files), 
  123.                             (strlen(path)+2+strlen(dp->d_name)), char);
  124.  
  125.                         sprintf(*(*file_list+sub_files), "%s%s%s", path, 
  126.                             CHAR_SLASH, dp->d_name); 
  127.                         (*num_files)++;
  128.                         sub_files++;
  129.                         count++;
  130.                     } 
  131.                 }
  132.             }
  133. /* get the next entry */
  134.             dp = readdir(dirp);
  135.         }
  136. #ifdef CLOSEDIR_VOID
  137.         closedir(dirp);
  138. #else 
  139.         if((closedir(dirp)) != 0)
  140.             perror(path);
  141. #endif
  142.     }
  143.     return;
  144.